up
This commit is contained in:
@@ -2,6 +2,8 @@ package board
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
@@ -13,16 +15,7 @@ import (
|
||||
"golang.org/x/oauth2"
|
||||
)
|
||||
|
||||
var (
|
||||
oauthConf = &oauth2.Config{
|
||||
ClientID: os.Getenv("GITHUB_CLIENT_ID"),
|
||||
ClientSecret: os.Getenv("GITHUB_CLIENT_SECRET"),
|
||||
// Scopes: []string{"read:user", "user:email"},
|
||||
Endpoint: oauth2.Endpoint{
|
||||
AuthURL: "https://github.com/login/oauth/authorize",
|
||||
TokenURL: "https://github.com/login/oauth/access_token",
|
||||
}}
|
||||
)
|
||||
var ()
|
||||
|
||||
type SignIn struct {
|
||||
Username string `json:"username"`
|
||||
@@ -40,9 +33,55 @@ type SignUp struct {
|
||||
Password string `json:"password"`
|
||||
}
|
||||
|
||||
func generateState() string {
|
||||
return generateRandomString(20)
|
||||
}
|
||||
|
||||
func generateCode() string {
|
||||
return generateRandomString(20)
|
||||
}
|
||||
|
||||
func generateRandomString(length int) string {
|
||||
byteArr := make([]byte, length)
|
||||
_, err := rand.Read(byteArr)
|
||||
if err != nil {
|
||||
log.Fatal("Error generating random string:", err)
|
||||
}
|
||||
|
||||
return base64.URLEncoding.EncodeToString(byteArr)
|
||||
}
|
||||
func IdentityHandler(c *gin.Context) {
|
||||
state := generateState()
|
||||
|
||||
session := sessions.Default(c)
|
||||
session.Set("state", state)
|
||||
if err := session.Save(); err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
oauthConf := &oauth2.Config{
|
||||
ClientID: os.Getenv("GITHUB_CLIENT_ID"),
|
||||
ClientSecret: os.Getenv("GITHUB_CLIENT_SECRET"),
|
||||
RedirectURL: "http://localhost:5173/",
|
||||
// Scopes: []string{"read:user", "user:email"},
|
||||
Endpoint: oauth2.Endpoint{
|
||||
AuthURL: "https://github.com/login/oauth/authorize",
|
||||
TokenURL: "https://github.com/login/oauth/access_token",
|
||||
}}
|
||||
url := oauthConf.AuthCodeURL(state)
|
||||
log.Println(url)
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"state": state,
|
||||
"redirectUri": url,
|
||||
})
|
||||
}
|
||||
|
||||
func SSOHandler(c *gin.Context) {
|
||||
signin := SSOSignIn{}
|
||||
|
||||
if err := c.BindQuery(&signin); err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Unmashal request body."})
|
||||
return
|
||||
}
|
||||
session := sessions.Default(c)
|
||||
savedState := session.Get("state")
|
||||
if savedState == nil || savedState.(string) != signin.State {
|
||||
@@ -50,6 +89,14 @@ func SSOHandler(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
oauthConf := &oauth2.Config{
|
||||
ClientID: os.Getenv("GITHUB_CLIENT_ID"),
|
||||
ClientSecret: os.Getenv("GITHUB_CLIENT_SECRET"),
|
||||
// Scopes: []string{"read:user", "user:email"},
|
||||
Endpoint: oauth2.Endpoint{
|
||||
AuthURL: "https://github.com/login/oauth/authorize",
|
||||
TokenURL: "https://github.com/login/oauth/access_token",
|
||||
}}
|
||||
// 使用 code 换取 token
|
||||
token, err := oauthConf.Exchange(context.Background(), signin.Code)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user