auth.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package api
  2. import (
  3. "fmt"
  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 := 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. fmt.Println("CasdoorCallback called")
  62. ok := BindAndValid(c, &loginUser)
  63. if !ok {
  64. return
  65. }
  66. endpoint := settings.ServerSettings.CasdoorEndpoint
  67. clientId := settings.ServerSettings.CasdoorClientId
  68. clientSecret := settings.ServerSettings.CasdoorClientSecret
  69. certificate := settings.ServerSettings.CasdoorCertificate
  70. organization := settings.ServerSettings.CasdoorOrganization
  71. application := settings.ServerSettings.CasdoorApplication
  72. if endpoint == "" || clientId == "" || clientSecret == "" || certificate == "" || organization == "" || application == "" {
  73. c.JSON(http.StatusInternalServerError, gin.H{
  74. "message": "Casdoor is not configured",
  75. })
  76. return
  77. }
  78. casdoorsdk.InitConfig(endpoint, clientId, clientSecret, certificate, organization, application)
  79. token, err := casdoorsdk.GetOAuthToken(loginUser.Code, loginUser.State)
  80. if err != nil {
  81. c.JSON(http.StatusInternalServerError, gin.H{
  82. "message": err.Error(),
  83. })
  84. return
  85. }
  86. claims, err := casdoorsdk.ParseJwtToken(token.AccessToken)
  87. if err != nil {
  88. c.JSON(http.StatusInternalServerError, gin.H{
  89. "message": err.Error(),
  90. })
  91. return
  92. }
  93. u, err := model.GetUser(claims.Name)
  94. if err != nil {
  95. if errors.Is(err, gorm.ErrRecordNotFound) {
  96. c.JSON(http.StatusForbidden, gin.H{
  97. "message": "User not exist",
  98. })
  99. } else {
  100. c.JSON(http.StatusInternalServerError, gin.H{
  101. "message": err.Error(),
  102. })
  103. }
  104. return
  105. }
  106. userToken, err := model.GenerateJWT(u.Name)
  107. if err != nil {
  108. c.JSON(http.StatusInternalServerError, gin.H{
  109. "message": err.Error(),
  110. })
  111. return
  112. }
  113. c.JSON(http.StatusOK, gin.H{
  114. "message": "ok",
  115. "token": userToken,
  116. })
  117. }