Skip to content

BillionMail gives you open-source MailServer, NewsLetter, Email Marketing — fully self-hosted, dev-friendly, and free from monthly fees. Join the discord: https://discord.gg/asfXzBUhZr

improve user login security: - added code verification when login failure - block client ip when too many retries.

Jack7c6cdb18b64e
diff --git a/core/api/rbac/v1/auth.go b/core/api/rbac/v1/auth.go
index 87f469da..225aa674 100644
--- a/core/api/rbac/v1/auth.go
+++ b/core/api/rbac/v1/auth.go
@@ -7,9 +7,11 @@ import (
// LoginReq defines the request for user login
type LoginReq struct {
- g.Meta `path:"/login" method:"post" tags:"Authentication" summary:"User login" sm:"User login" in:"body"`
- Username string `p:"username" v:"required#Username cannot be empty" dc:"Username"`
- Password string `p:"password" v:"required#Password cannot be empty" dc:"Password"`
+ g.Meta `path:"/login" method:"post" tags:"Authentication" summary:"User login" sm:"User login" in:"body"`
+ Username string `p:"username" v:"required#Username cannot be empty" dc:"Username"`
+ Password string `p:"password" v:"required#Password cannot be empty" dc:"Password"`
+ ValidateCodeId string `p:"validate_code_id" v:"min-length:1" dc:"Validation code ID"`
+ ValidateCode string `p:"validate_code" v:"min-length:1" dc:"Validation code"`
}
// LoginRes defines the response for user login
@@ -65,7 +67,7 @@ type GetValidateCodeReq struct {
type GetValidateCodeRes struct {
api_v1.StandardRes
Data struct {
- MustValidateCode string `json:"mustValidateCode" dc:"Must Validate Code"`
+ MustValidateCode bool `json:"mustValidateCode" dc:"Must Validate Code"`
LoginRetries int `json:"loginRetries" dc:"Login retries"`
MaxLoginRetries int `json:"maxLoginRetries" dc:"Max login retries"`
ValidateCodeBase64 string `json:"validateCodeBase64" dc:"Validation code in base64 format"`
diff --git a/core/internal/controller/rbac/rbac_v1_auth.go b/core/internal/controller/rbac/rbac_v1_auth.go
index d5cb06da..a99acaeb 100644
--- a/core/internal/controller/rbac/rbac_v1_auth.go
+++ b/core/internal/controller/rbac/rbac_v1_auth.go
@@ -2,17 +2,81 @@ package rbac
import (
"billionmail-core/api/rbac/v1"
+ "billionmail-core/internal/service/public"
service "billionmail-core/internal/service/rbac"
"context"
"fmt"
"github.com/gogf/gf/util/gconv"
"github.com/gogf/gf/v2/errors/gerror"
+ "github.com/gogf/gf/v2/frame/g"
+ "time"
)
// Login handles user login
func (c *ControllerV1) Login(ctx context.Context, req *v1.LoginReq) (res *v1.LoginRes, err error) {
res = &v1.LoginRes{}
+ maxRetries := 5
+ blockTime := 300
+
+ // Default validate status
+ validateSuccess := true
+
+ // Login success flag in current request
+ loginSuccessFlag := false
+
+ clientIp := g.RequestFromCtx(ctx).GetClientIp()
+
+ cacheKey := fmt.Sprintf("USER_LOGIN_RETRIES:%s", clientIp)
+
+ loginRetries, mustValidateCode := public.GetCache(cacheKey).(int)
+
+ // End of request, check if login was successful
+ defer func() {
+ // Validate failure
+ if !validateSuccess {
+ return
+ }
+
+ // Login success
+ if loginSuccessFlag {
+ public.RemoveCache(cacheKey)
+ return
+ }
+
+ // Increment login retries
+ public.SetCache(cacheKey, loginRetries+1, 300)
+ }()
+
+ if loginRetries >= maxRetries {
+ k := "USER_LOGIN_RETRIES_RELEASE_TIME:" + clientIp
+ releaseTime, blocked := public.GetCache(k).(int64)
+ if !blocked {
+ releaseTime = time.Now().Unix() + int64(blockTime)
+ public.SetCache(k, releaseTime, blockTime)
+ }
+
+ err = fmt.Errorf("Login failed too many times, please try again after %d seconds", releaseTime-time.Now().Unix())
+ return
+ }
+
+ // Check if validation code is required
+ if mustValidateCode {
+ validateSuccess = false
+
+ if req.ValidateCodeId == "" || req.ValidateCode == "" {
+ err = fmt.Errorf("Validation code ID and code cannot be empty")
+ return
+ }
+
+ if !service.VerifyCaptcha(req.ValidateCodeId, req.ValidateCode) {
+ err = fmt.Errorf("Invalid validation code")
+ return
+ }
+
+ validateSuccess = true
+ }
+
// Verify username and password
account, err := service.Account().Login(ctx, req.Username, req.Password)
if err != nil {
diff --git a/core/internal/controller/rbac/rbac_v1_get_validate_code.go b/core/internal/controller/rbac/rbac_v1_get_validate_code.go
index e6fe7961..9c4ac32e 100644
--- a/core/internal/controller/rbac/rbac_v1_get_validate_code.go
+++ b/core/internal/controller/rbac/rbac_v1_get_validate_code.go
@@ -1,14 +1,32 @@
package rbac
import (
+ "billionmail-core/internal/service/public"
+ "billionmail-core/internal/service/rbac"
"context"
-
- "github.com/gogf/gf/v2/errors/gcode"
- "github.com/gogf/gf/v2/errors/gerror"
+ "fmt"
+ "github.com/gogf/gf/v2/frame/g"
"billionmail-core/api/rbac/v1"
)
func (c *ControllerV1) GetValidateCode(ctx context.Context, req *v1.GetValidateCodeReq) (res *v1.GetValidateCodeRes, err error) {
- return nil, gerror.NewCode(gcode.CodeNotImplemented)
+ res = &v1.GetValidateCodeRes{}
+
+ clientIp := g.RequestFromCtx(ctx).GetClientIp()
+
+ cacheKey := fmt.Sprintf("USER_LOGIN_RETRIES:%s", clientIp)
+
+ res.Data.LoginRetries, res.Data.MustValidateCode = public.GetCache(cacheKey).(int)
+ res.Data.MaxLoginRetries = 5
+ res.Data.ValidateCodeId, res.Data.ValidateCodeBase64, err = rbac.GetCaptcha()
+
+ if err != nil {
+ err = fmt.Errorf("failed to get validate code: %w", err)
+ return
+ }
+
+ res.SetSuccess("Success")
+
+ return
}