auth.go 3.2 KB

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