auth.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package user
  2. import (
  3. "github.com/0xJacky/Nginx-UI/api"
  4. "github.com/0xJacky/Nginx-UI/model"
  5. "github.com/0xJacky/Nginx-UI/settings"
  6. "net/http"
  7. "github.com/casdoor/casdoor-go-sdk/casdoorsdk"
  8. "github.com/gin-gonic/gin"
  9. "github.com/pkg/errors"
  10. "golang.org/x/crypto/bcrypt"
  11. "gorm.io/gorm"
  12. )
  13. type LoginUser struct {
  14. Name string `json:"name" binding:"required,max=255"`
  15. Password string `json:"password" binding:"required,max=255"`
  16. }
  17. func Login(c *gin.Context) {
  18. var user LoginUser
  19. ok := api.BindAndValid(c, &user)
  20. if !ok {
  21. return
  22. }
  23. u, _ := model.GetUser(user.Name)
  24. if err := bcrypt.CompareHashAndPassword([]byte(u.Password), []byte(user.Password)); err != nil {
  25. c.JSON(http.StatusForbidden, gin.H{
  26. "message": "The username or password is incorrect",
  27. })
  28. return
  29. }
  30. token, err := model.GenerateJWT(u.Name)
  31. if err != nil {
  32. c.JSON(http.StatusInternalServerError, gin.H{
  33. "message": err.Error(),
  34. })
  35. return
  36. }
  37. c.JSON(http.StatusOK, gin.H{
  38. "message": "ok",
  39. "token": token,
  40. })
  41. }
  42. func Logout(c *gin.Context) {
  43. token := c.GetHeader("Authorization")
  44. if token != "" {
  45. err := model.DeleteToken(token)
  46. if err != nil {
  47. c.JSON(http.StatusInternalServerError, gin.H{
  48. "message": err.Error(),
  49. })
  50. return
  51. }
  52. }
  53. c.JSON(http.StatusNoContent, nil)
  54. }
  55. type CasdoorLoginUser struct {
  56. Code string `json:"code" binding:"required,max=255"`
  57. State string `json:"state" binding:"required,max=255"`
  58. }
  59. func CasdoorCallback(c *gin.Context) {
  60. var loginUser CasdoorLoginUser
  61. ok := api.BindAndValid(c, &loginUser)
  62. if !ok {
  63. return
  64. }
  65. endpoint := settings.CasdoorSettings.Endpoint
  66. clientId := settings.CasdoorSettings.ClientId
  67. clientSecret := settings.CasdoorSettings.ClientSecret
  68. certificate := settings.CasdoorSettings.Certificate
  69. organization := settings.CasdoorSettings.Organization
  70. application := settings.CasdoorSettings.Application
  71. if endpoint == "" || clientId == "" || clientSecret == "" || certificate == "" || organization == "" || application == "" {
  72. c.JSON(http.StatusInternalServerError, gin.H{
  73. "message": "Casdoor is not configured",
  74. })
  75. return
  76. }
  77. casdoorsdk.InitConfig(endpoint, clientId, clientSecret, certificate, organization, application)
  78. token, err := casdoorsdk.GetOAuthToken(loginUser.Code, loginUser.State)
  79. if err != nil {
  80. c.JSON(http.StatusInternalServerError, gin.H{
  81. "message": err.Error(),
  82. })
  83. return
  84. }
  85. claims, err := casdoorsdk.ParseJwtToken(token.AccessToken)
  86. if err != nil {
  87. c.JSON(http.StatusInternalServerError, gin.H{
  88. "message": err.Error(),
  89. })
  90. return
  91. }
  92. u, err := model.GetUser(claims.Name)
  93. if err != nil {
  94. if errors.Is(err, gorm.ErrRecordNotFound) {
  95. c.JSON(http.StatusForbidden, gin.H{
  96. "message": "User not exist",
  97. })
  98. } else {
  99. c.JSON(http.StatusInternalServerError, gin.H{
  100. "message": err.Error(),
  101. })
  102. }
  103. return
  104. }
  105. userToken, err := model.GenerateJWT(u.Name)
  106. if err != nil {
  107. c.JSON(http.StatusInternalServerError, gin.H{
  108. "message": err.Error(),
  109. })
  110. return
  111. }
  112. c.JSON(http.StatusOK, gin.H{
  113. "message": "ok",
  114. "token": userToken,
  115. })
  116. }