Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Casdoor login include github login #49

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions login/service/community.gop
Original file line number Diff line number Diff line change
@@ -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)
}
74 changes: 74 additions & 0 deletions login/service/community_yap.gox
Original file line number Diff line number Diff line change
@@ -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--")
Copy link
Contributor

@xhyqaq xhyqaq Jan 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delete comment

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"
16 changes: 16 additions & 0 deletions login/service/yap/callback_yap.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<html>
<head>
<meta charset="utf-8"/>
</head>
<body>
<h1>This is callback page </h1>
<h1> username :{{.username}} </h1>
<h2>Go+ Community test page <a href="/test">go!</a></h2>
</body>


<script>
var token = {{.usertoken}}
document.cookie = "token="+token
</script>
</html>
9 changes: 9 additions & 0 deletions login/service/yap/home_yap.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<html>
<head>
<meta charset="utf-8"/>
</head>
<body>
Go+ Community <a href="/p/123">written in Go+</a>
<a href="/login">login</a>
</body>
</html>
9 changes: 9 additions & 0 deletions login/service/yap/login_yap.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<html>
<head>
<meta charset="utf-8"/>

</head>
<body>
Go+ Community <a href="/p/123">written in Go+</a>
</body>
</html>
9 changes: 9 additions & 0 deletions login/service/yap/test_yap.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<html>
<head>
<meta charset="utf-8"/>

</head>
<body>
<h1>test page</h1>
</body>
</html>