1
0

casdoor.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package user
  2. import (
  3. "errors"
  4. "fmt"
  5. "net/http"
  6. "net/url"
  7. "os"
  8. "github.com/0xJacky/Nginx-UI/internal/user"
  9. "github.com/0xJacky/Nginx-UI/settings"
  10. "github.com/casdoor/casdoor-go-sdk/casdoorsdk"
  11. "github.com/gin-gonic/gin"
  12. "github.com/uozi-tech/cosy"
  13. "gorm.io/gorm"
  14. )
  15. type CasdoorLoginUser struct {
  16. Code string `json:"code" binding:"required,max=255"`
  17. State string `json:"state" binding:"required,max=255"`
  18. }
  19. func CasdoorCallback(c *gin.Context) {
  20. var loginUser CasdoorLoginUser
  21. ok := cosy.BindAndValid(c, &loginUser)
  22. if !ok {
  23. return
  24. }
  25. endpoint := settings.CasdoorSettings.Endpoint
  26. clientId := settings.CasdoorSettings.ClientId
  27. clientSecret := settings.CasdoorSettings.ClientSecret
  28. certificatePath := settings.CasdoorSettings.CertificatePath
  29. organization := settings.CasdoorSettings.Organization
  30. application := settings.CasdoorSettings.Application
  31. if endpoint == "" || clientId == "" || clientSecret == "" || certificatePath == "" ||
  32. organization == "" || application == "" {
  33. c.JSON(http.StatusInternalServerError, gin.H{
  34. "message": "Casdoor is not configured",
  35. })
  36. return
  37. }
  38. certBytes, err := os.ReadFile(certificatePath)
  39. if err != nil {
  40. cosy.ErrHandler(c, err)
  41. return
  42. }
  43. casdoorsdk.InitConfig(endpoint, clientId, clientSecret, string(certBytes), organization, application)
  44. token, err := casdoorsdk.GetOAuthToken(loginUser.Code, loginUser.State)
  45. if err != nil {
  46. cosy.ErrHandler(c, err)
  47. return
  48. }
  49. claims, err := casdoorsdk.ParseJwtToken(token.AccessToken)
  50. if err != nil {
  51. cosy.ErrHandler(c, err)
  52. return
  53. }
  54. u, err := user.GetUser(claims.Name)
  55. if err != nil {
  56. if errors.Is(err, gorm.ErrRecordNotFound) {
  57. c.JSON(http.StatusForbidden, gin.H{
  58. "message": "User not exist",
  59. })
  60. } else {
  61. cosy.ErrHandler(c, err)
  62. }
  63. return
  64. }
  65. userToken, err := user.GenerateJWT(u)
  66. if err != nil {
  67. cosy.ErrHandler(c, err)
  68. return
  69. }
  70. c.JSON(http.StatusOK, LoginResponse{
  71. Message: "ok",
  72. Token: userToken,
  73. })
  74. }
  75. func GetCasdoorUri(c *gin.Context) {
  76. clientId := settings.CasdoorSettings.ClientId
  77. redirectUri := settings.CasdoorSettings.RedirectUri
  78. state := settings.CasdoorSettings.Application
  79. endpoint := settings.CasdoorSettings.Endpoint
  80. // feature request #603
  81. if settings.CasdoorSettings.ExternalUrl != "" {
  82. endpoint = settings.CasdoorSettings.ExternalUrl
  83. }
  84. if endpoint == "" || clientId == "" || redirectUri == "" || state == "" {
  85. c.JSON(http.StatusOK, gin.H{
  86. "uri": "",
  87. })
  88. return
  89. }
  90. encodedRedirectUri := url.QueryEscape(redirectUri)
  91. c.JSON(http.StatusOK, gin.H{
  92. "uri": fmt.Sprintf(
  93. "%s/login/oauth/authorize?client_id=%s&response_type=code&redirect_uri=%s&state=%s&scope=read",
  94. endpoint, clientId, encodedRedirectUri, state),
  95. })
  96. }