add preshared key validator

This commit is contained in:
Maxim Kochurov 2022-02-22 17:14:48 +00:00
parent 24d8a4044b
commit 98edcd0d04

View file

@ -187,6 +187,7 @@ func NewClient(db store.IStore) echo.HandlerFunc {
}
}
if client.PresharedKey == "" {
presharedKey, err := wgtypes.GenerateKey()
if err != nil {
log.Error("Cannot generated preshared key: ", err)
@ -194,8 +195,14 @@ func NewClient(db store.IStore) echo.HandlerFunc {
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.CreatedAt = time.Now().UTC()
client.UpdatedAt = client.CreatedAt