auth.go 3.2 KB

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