callback.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. package api
  2. import (
  3. "fmt"
  4. "net/http"
  5. "os"
  6. "github.com/casdoor/casdoor-go-sdk/casdoorsdk"
  7. "github.com/gin-gonic/gin"
  8. )
  9. // https://auth.3dqueen.cloud/login/oauth/authorize?client_id=9f22d6616ae14fe59dc4&redirect_uri=https://www.3dqueen.cloud/box/v1/boxcost/callback&response_type=code&scope=openid&state=STATE
  10. // 需要在第三方提供商配置回调 https://auth.3dqueen.cloud/callback
  11. func callback(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  12. authConf := apictx.Svc.Conf.Auth
  13. pemByte, err := os.ReadFile(authConf.Certificate)
  14. if err != nil {
  15. return nil, err
  16. }
  17. casdoorsdk.InitConfig(authConf.Endpoint, authConf.ClientId, authConf.ClientSecret, string(pemByte), authConf.OrganizationName, authConf.ApplicationName)
  18. token, err := casdoorsdk.GetOAuthToken(c.Query("code"), c.Query("state"))
  19. if err != nil {
  20. fmt.Println(err)
  21. }
  22. fmt.Println(token.AccessToken)
  23. claims, err := casdoorsdk.ParseJwtToken(token.AccessToken)
  24. if err != nil {
  25. fmt.Println(err)
  26. }
  27. fmt.Println(claims)
  28. c.Redirect(http.StatusFound, "https://auth.3dqueen.cloud")
  29. // claims.AccessToken = token.AccessToken
  30. return nil, nil
  31. }