auth.go 2.9 KB

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