refactor: use tryParseRequest

This commit is contained in:
Derrick Hammer 2023-06-07 08:49:14 -04:00
parent 9d843bffdb
commit bfbf13a57d
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
2 changed files with 22 additions and 16 deletions

View File

@ -85,6 +85,24 @@ func (a *AccountController) PostRegister() {
return
}
if len(r.Pubkey) > 0 {
r.Pubkey = strings.ToLower(r.Pubkey)
var count int64
err := db.Get().Model(&model.Key{}).Where("pubkey = ?", r.Pubkey).Count(&count).Error
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
logger.Get().Error("error querying accounts", zap.Error(err), zap.String("pubkey", r.Pubkey))
a.Ctx.StopWithError(iris.StatusInternalServerError, err)
}
if count > 0 {
logger.Get().Debug("account with pubkey already exists", zap.Error(err), zap.String("pubkey", r.Pubkey))
// An account with the same pubkey already exists.
// Return an error response to the client.
a.Ctx.StopWithError(iris.StatusConflict, errors.New("an account with this pubkey already exists"))
return
}
}
// Create a new Account model with the provided email and hashed password.
account := model.Account{
Email: r.Email,

View File

@ -168,10 +168,7 @@ func generateAndSaveChallengeToken(accountID uint, maxAge time.Duration) (string
func (a *AuthController) PostLogin() {
var r LoginRequest
// Read the login request from the client.
if err := a.Ctx.ReadJSON(&r); err != nil {
logger.Get().Debug("failed to parse request", zap.Error(err))
a.Ctx.StopWithError(iris.StatusBadRequest, err)
if !tryParseRequest(r, a.Ctx) {
return
}
@ -218,10 +215,7 @@ func (a *AuthController) PostLogin() {
func (a *AuthController) PostPubkeyChallenge() {
var r ChallengeRequest
// Read the login request from the client.
if err := a.Ctx.ReadJSON(&r); err != nil {
logger.Get().Debug("failed to parse request", zap.Error(err))
a.Ctx.StopWithError(iris.StatusBadRequest, err)
if !tryParseRequest(r, a.Ctx) {
return
}
@ -252,10 +246,7 @@ func (a *AuthController) PostPubkeyChallenge() {
func (a *AuthController) PostPubkeyLogin() {
var r PubkeyLoginRequest
// Read the key login request from the client.
if err := a.Ctx.ReadJSON(&r); err != nil {
logger.Get().Debug("failed to parse request", zap.Error(err))
a.Ctx.StopWithError(iris.StatusBadRequest, err)
if !tryParseRequest(r, a.Ctx) {
return
}
@ -338,10 +329,7 @@ func (a *AuthController) PostPubkeyLogin() {
func (a *AuthController) PostLogout() {
var r LogoutRequest
// Read the logout request from the client.
if err := a.Ctx.ReadJSON(&r); err != nil {
logger.Get().Debug("failed to parse request", zap.Error(err))
a.Ctx.StopWithError(iris.StatusBadRequest, err)
if !tryParseRequest(r, a.Ctx) {
return
}