auth.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package user
  2. import (
  3. "math/rand/v2"
  4. "net/http"
  5. "sync"
  6. "time"
  7. "github.com/0xJacky/Nginx-UI/internal/user"
  8. "github.com/0xJacky/Nginx-UI/query"
  9. "github.com/0xJacky/Nginx-UI/settings"
  10. "github.com/gin-gonic/gin"
  11. "github.com/uozi-tech/cosy"
  12. "github.com/uozi-tech/cosy/logger"
  13. )
  14. var mutex = &sync.Mutex{}
  15. type LoginUser struct {
  16. Name string `json:"name" binding:"required,max=255"`
  17. Password string `json:"password" binding:"required,max=255"`
  18. OTP string `json:"otp"`
  19. RecoveryCode string `json:"recovery_code"`
  20. }
  21. const (
  22. ErrMaxAttempts = 4291
  23. Enabled2FA = 199
  24. Error2FACode = 4034
  25. LoginSuccess = 200
  26. )
  27. type LoginResponse struct {
  28. Message string `json:"message"`
  29. Error string `json:"error,omitempty"`
  30. Code int `json:"code"`
  31. *user.AccessTokenPayload
  32. SecureSessionID string `json:"secure_session_id,omitempty"`
  33. }
  34. func Login(c *gin.Context) {
  35. // make sure that only one request is processed at a time
  36. mutex.Lock()
  37. defer mutex.Unlock()
  38. // check if the ip is banned
  39. clientIP := c.ClientIP()
  40. b := query.BanIP
  41. banIP, _ := b.Where(b.IP.Eq(clientIP),
  42. b.ExpiredAt.Gte(time.Now().Unix()),
  43. b.Attempts.Gte(settings.AuthSettings.MaxAttempts),
  44. ).Count()
  45. if banIP > 0 {
  46. c.JSON(http.StatusTooManyRequests, LoginResponse{
  47. Message: "Max attempts",
  48. Code: ErrMaxAttempts,
  49. })
  50. return
  51. }
  52. var json LoginUser
  53. ok := cosy.BindAndValid(c, &json)
  54. if !ok {
  55. return
  56. }
  57. u, err := user.Login(json.Name, json.Password)
  58. if err != nil {
  59. user.BanIP(clientIP)
  60. random := time.Duration(rand.Int() % 10)
  61. time.Sleep(random * time.Second)
  62. cosy.ErrHandler(c, err)
  63. return
  64. }
  65. // Check if the user enables 2FA
  66. var secureSessionID string
  67. if u.EnabledOTP() {
  68. if json.OTP == "" && json.RecoveryCode == "" {
  69. c.JSON(http.StatusOK, LoginResponse{
  70. Message: "The user has enabled 2FA",
  71. Code: Enabled2FA,
  72. })
  73. user.BanIP(clientIP)
  74. return
  75. }
  76. if err = user.VerifyOTP(u, json.OTP, json.RecoveryCode); err != nil {
  77. cosy.ErrHandler(c, err)
  78. user.BanIP(clientIP)
  79. return
  80. }
  81. secureSessionID = user.SetSecureSessionID(u.ID)
  82. }
  83. // login success, clear banned record
  84. _, _ = b.Where(b.IP.Eq(clientIP)).Delete()
  85. logger.Info("[User Login]", u.Name)
  86. accessToken, err := user.GenerateJWT(u)
  87. if err != nil {
  88. cosy.ErrHandler(c, err)
  89. return
  90. }
  91. c.JSON(http.StatusOK, LoginResponse{
  92. Code: LoginSuccess,
  93. Message: "ok",
  94. AccessTokenPayload: accessToken,
  95. SecureSessionID: secureSessionID,
  96. })
  97. }
  98. func Logout(c *gin.Context) {
  99. token := c.GetHeader("Authorization")
  100. if token != "" {
  101. user.DeleteToken(token)
  102. }
  103. c.JSON(http.StatusNoContent, nil)
  104. }