1
0

auth.go 3.3 KB

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