Skip to content

Commit

Permalink
add preshared key validator
Browse files Browse the repository at this point in the history
  • Loading branch information
ferrine committed Feb 22, 2022
1 parent 24d8a40 commit 98edcd0
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions handler/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,15 +187,22 @@ func NewClient(db store.IStore) echo.HandlerFunc {
}
}

presharedKey, err := wgtypes.GenerateKey()
if err != nil {
log.Error("Cannot generated preshared key: ", err)
return c.JSON(http.StatusInternalServerError, jsonHTTPResponse{
false, "Cannot generate Wireguard preshared key",
})
if client.PresharedKey == "" {
presharedKey, err := wgtypes.GenerateKey()
if err != nil {
log.Error("Cannot generated preshared key: ", err)
return c.JSON(http.StatusInternalServerError, jsonHTTPResponse{
false, "Cannot generate Wireguard preshared key",
})
}
client.PresharedKey = presharedKey.String()
} else {
_, err := wgtypes.ParseKey(client.PresharedKey)
if err != nil {
log.Error("Cannot verify wireguard preshared key: ", err)
return c.JSON(http.StatusInternalServerError, jsonHTTPResponse{false, "Cannot verify Wireguard preshared key"})
}
}

client.PresharedKey = presharedKey.String()
client.CreatedAt = time.Now().UTC()
client.UpdatedAt = client.CreatedAt

Expand Down

0 comments on commit 98edcd0

Please sign in to comment.