diff --git a/login/service/community.gop b/login/service/community.gop new file mode 100644 index 00000000..73d1e847 --- /dev/null +++ b/login/service/community.gop @@ -0,0 +1,43 @@ +import ( + "github.com/casdoor/casdoor-go-sdk/casdoorsdk" +) + +type CasdoorConfig struct { + endPoint string + clientId string + clientSecret string + certificate string + organizationName string + applicationName string +} + +func init() { + conf := new(CasdoorConfig) + + endPoint := conf.endPoint + clientID := conf.clientId + clientSecret := conf.clientSecret + certificate := conf.certificate + organizationName := conf.organizationName + applicationName := conf.applicationName + //TODO Post insertion of environmental variables + if endPoint == "" { + endPoint = os.Getenv("GOP_CASDOOR_ENDPOINT") + } + if clientID == "" { + clientID = os.Getenv("GOP_CASDOOR_CLIENTID") + } + if clientSecret == "" { + clientSecret = os.Getenv("GOP_CASDOOR_CLIENTSECRET") + } + if certificate == "" { + certificate = os.Getenv("GOP_CASDOOR_CERTIFICATE") + } + if organizationName == "" { + organizationName = os.Getenv("GOP_CASDOOR_ORGANIZATIONNAME") + } + if applicationName == "" { + applicationName = os.Getenv("GOP_CASDOOR_APPLICATONNAME") + } + casdoorsdk.InitConfig(endPoint, clientID, clientSecret, certificate, organizationName, applicationName) +} diff --git a/login/service/community_yap.gox b/login/service/community_yap.gox new file mode 100644 index 00000000..86a1bbdb --- /dev/null +++ b/login/service/community_yap.gox @@ -0,0 +1,74 @@ + +import ( + "fmt" + "log" + "net/http" + "github.com/casdoor/casdoor-go-sdk/casdoorsdk" + "os" +) + + +get "/p/:id", ctx => { + ctx.yap "article", { + "id": ctx.param("id"), + } +} +get "/", ctx => { + ctx.yap "home", {} +} + + + + +get "/login",ctx => { + casdoorURL := "https://casdoor-community.qiniu.io" + clientID := "49a8ac9729e314a05bf0" + redirectURI := "http://localhost:8080/callback" + ResponseType := "code" + Scope := "read" + State := "casdoor" + loginURL := fmt.Sprintf("%s/login/oauth/authorize?client_id=%s&response_type=%s&redirect_uri=%s&scope=%s&state=%s",casdoorURL,clientID,ResponseType,redirectURI,Scope,State) + http.Redirect(ctx.ResponseWriter,ctx.Request,loginURL, http.StatusFound) +} + +get "/callback", ctx => { + + code :=ctx.URL.Query().Get("code") + state :=ctx.URL.Query().Get("state") + fmt.Println("--code and state--") + fmt.Println("code:",code) + fmt.Println("state:",state) + token, err := casdoorsdk.GetOAuthToken(code, state) + if err != nil { + fmt.Println("err",err) + } + claim, err := casdoorsdk.ParseJwtToken(token.AccessToken) + if err != nil { + log.Fatal("err",err) + } + username := claim.User.Name + ctx.yap "callback", { + "username": username, + "usertoken":token.AccessToken, + } + + + +} + +get "/test", ctx => { + cookie, _ := ctx.Request.Cookie("token") + claim, err := casdoorsdk.ParseJwtToken(cookie.Value) + if err != nil { + log.Fatal("err",err)//跳转到登录界面 + }else { + fmt.Println("username:",claim.User.Name) + fmt.Println("userId:",claim.User.Id) + fmt.Println("password:",claim.User.Password) + } + ctx.yap "test", {} + +} + + +run ":8080" diff --git a/login/service/yap/callback_yap.html b/login/service/yap/callback_yap.html new file mode 100644 index 00000000..4962a7f1 --- /dev/null +++ b/login/service/yap/callback_yap.html @@ -0,0 +1,16 @@ + +
+ + + +