Skip to content

Commit

Permalink
chore: Refactor login handlers to use CanLogin helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
waveyboym committed Aug 23, 2024
1 parent 80202d4 commit 568995b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 34 deletions.
40 changes: 6 additions & 34 deletions occupi-backend/pkg/handlers/auth_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,11 @@ func Login(ctx *gin.Context, appsession *models.AppSession, role string, cookies
return
}

if canLogin, err := cache.CanMakeLogin(appsession, requestUser.Email); !canLogin && (err == nil || err.Error() != "cache not found") {
if canLogin, err := CanLogin(ctx, appsession, requestUser.Email); !canLogin {
if err != nil {
captureError(ctx, err)
logrus.WithError(err).Error("Error checking if user can login")
}

ctx.JSON(http.StatusTooManyRequests, utils.ErrorResponse(
http.StatusTooManyRequests,
"Too many login attempts",
constants.TooManyRequestsCode,
"Too many login attempts, please try again later",
nil))
return
}

Expand Down Expand Up @@ -120,18 +113,11 @@ func BeginLoginAdmin(ctx *gin.Context, appsession *models.AppSession) {
return
}

if canLogin, err := cache.CanMakeLogin(appsession, requestEmail.Email); !canLogin && (err == nil || err.Error() != "cache not found") {
if canLogin, err := CanLogin(ctx, appsession, requestEmail.Email); !canLogin {
if err != nil {
captureError(ctx, err)
logrus.WithError(err).Error("Error checking if user can login")
}

ctx.JSON(http.StatusTooManyRequests, utils.ErrorResponse(
http.StatusTooManyRequests,
"Too many login attempts",
constants.TooManyRequestsCode,
"Too many login attempts, please try again later",
nil))
return
}

Expand Down Expand Up @@ -185,7 +171,7 @@ func BeginLoginAdmin(ctx *gin.Context, appsession *models.AppSession) {
}

// Save the session data - cache will expire in x defined minutes according to the config
if err := cache.SetSession(appsession, session, uuid); err != nil && err.Error() != "cache not found" {
if err := cache.SetSession(appsession, session, uuid); err != nil {
captureError(ctx, err)
ctx.JSON(http.StatusInternalServerError, utils.InternalServerError())
fmt.Printf("error saving WebAuthn session data: %v", err)
Expand Down Expand Up @@ -269,18 +255,11 @@ func BeginRegistrationAdmin(ctx *gin.Context, appsession *models.AppSession) {
return
}

if canLogin, err := cache.CanMakeLogin(appsession, requestEmail.Email); !canLogin && (err == nil || err.Error() != "cache not found") {
if canLogin, err := CanLogin(ctx, appsession, requestEmail.Email); !canLogin {
if err != nil {
captureError(ctx, err)
logrus.WithError(err).Error("Error checking if user can login")
}

ctx.JSON(http.StatusTooManyRequests, utils.ErrorResponse(
http.StatusTooManyRequests,
"Too many registration attempts",
constants.TooManyRequestsCode,
"Too many registration attempts, please try again later",
nil))
return
}

Expand Down Expand Up @@ -323,7 +302,7 @@ func BeginRegistrationAdmin(ctx *gin.Context, appsession *models.AppSession) {
}

// Save the session data - cache will expire in x defined minutes according to the config
if err := cache.SetSession(appsession, session, uuid); err != nil && err.Error() != "cache not found" {
if err := cache.SetSession(appsession, session, uuid); err != nil {
captureError(ctx, err)
logrus.WithError(err).Error("Error saving session data in cache")
ctx.JSON(http.StatusInternalServerError, utils.InternalServerError())
Expand Down Expand Up @@ -408,18 +387,11 @@ func Register(ctx *gin.Context, appsession *models.AppSession) {
return
}

if canLogin, err := cache.CanMakeLogin(appsession, requestUser.Email); !canLogin && (err == nil || err.Error() != "cache not found") {
if canLogin, err := CanLogin(ctx, appsession, requestUser.Email); !canLogin {
if err != nil {
captureError(ctx, err)
logrus.WithError(err).Error("Error checking if user can login")
}

ctx.JSON(http.StatusTooManyRequests, utils.ErrorResponse(
http.StatusTooManyRequests,
"Too many registration attempts",
constants.TooManyRequestsCode,
"Too many registration attempts, please try again later",
nil))
return
}

Expand Down
14 changes: 14 additions & 0 deletions occupi-backend/pkg/handlers/auth_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"time"

"github.com/COS301-SE-2024/occupi/occupi-backend/pkg/authenticator"
"github.com/COS301-SE-2024/occupi/occupi-backend/pkg/cache"
"github.com/COS301-SE-2024/occupi/occupi-backend/pkg/constants"
"github.com/COS301-SE-2024/occupi/occupi-backend/pkg/database"
"github.com/COS301-SE-2024/occupi/occupi-backend/pkg/mail"
Expand Down Expand Up @@ -554,3 +555,16 @@ func AttemptToSignNewEmail(ctx *gin.Context, appsession *models.AppSession, emai
}
return nil
}

func CanLogin(ctx *gin.Context, appsession *models.AppSession, email string) (bool, error) {
if canLogin, err := cache.CanMakeLogin(appsession, email); !canLogin && (err == nil || err.Error() != "cache not found") {
ctx.JSON(http.StatusTooManyRequests, utils.ErrorResponse(
http.StatusTooManyRequests,
"Too many login attempts",
constants.TooManyRequestsCode,
"Too many login attempts, please try again later",
nil))
return false, err
}
return true, nil
}

0 comments on commit 568995b

Please sign in to comment.