Merge branch 'refs/heads/feat-login-website'
This commit is contained in:
commit
2b349829b3
|
|
@ -10,6 +10,7 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/google/uuid"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
@ -748,17 +749,17 @@ func LoginRedirect(c *gin.Context) {
|
||||||
|
|
||||||
func GetThirdPartyAuthUrl(c *gin.Context) {
|
func GetThirdPartyAuthUrl(c *gin.Context) {
|
||||||
platform := c.Query("platform")
|
platform := c.Query("platform")
|
||||||
uuid := c.Query("uuid")
|
uuid_ := c.Query("uuid")
|
||||||
hType := c.Query("type") //操作类型add,login
|
hType := c.Query("type") //操作类型add,login
|
||||||
var resp proto.GenerateResp
|
var resp proto.GenerateResp
|
||||||
if platform == "" || uuid == "" || hType == "" {
|
if platform == "" || uuid_ == "" || hType == "" {
|
||||||
resp.Code = proto.ParameterError
|
resp.Code = proto.ParameterError
|
||||||
resp.Message = "platform or uuid is empty"
|
resp.Message = "platform or uuid is empty"
|
||||||
c.JSON(http.StatusOK, resp)
|
c.JSON(http.StatusOK, resp)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var state proto.ThirdPartyLoginState
|
var state proto.ThirdPartyLoginState
|
||||||
state.UUID = uuid
|
state.UUID = uuid_
|
||||||
state.Type = hType
|
state.Type = hType
|
||||||
state.Platform = platform
|
state.Platform = platform
|
||||||
state.Project = "SAW"
|
state.Project = "SAW"
|
||||||
|
|
@ -782,14 +783,18 @@ func GetThirdPartyAuthUrl(c *gin.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
//需要将uuid绑定在该用户上
|
//需要将uuid绑定在该用户上
|
||||||
worker.SetRedisWithExpire("user_add_platform_"+uuid, strconv.Itoa(userID), time.Minute*9)
|
worker.SetRedisWithExpire("user_add_platform_"+uuid_, strconv.Itoa(userID), time.Minute*9)
|
||||||
state.UserID = userID
|
state.UserID = userID
|
||||||
}
|
}
|
||||||
|
|
||||||
stateStr, _ := json.Marshal(state)
|
stateStr, _ := json.Marshal(state)
|
||||||
|
stateID := uuid.NewString()
|
||||||
|
worker.SetRedisWithExpire("state_id_"+stateID, string(stateStr), time.Minute*9)
|
||||||
|
|
||||||
var respUrl string
|
var respUrl string
|
||||||
//base64编码
|
//base64编码
|
||||||
stateBase64Str := base64.StdEncoding.EncodeToString(stateStr)
|
stateBase64Str := base64.StdEncoding.EncodeToString(stateStr)
|
||||||
|
stateBase64Str = stateID
|
||||||
switch platform {
|
switch platform {
|
||||||
case "qq":
|
case "qq":
|
||||||
params := url.Values{}
|
params := url.Values{}
|
||||||
|
|
@ -802,7 +807,7 @@ func GetThirdPartyAuthUrl(c *gin.Context) {
|
||||||
case "github":
|
case "github":
|
||||||
params := url.Values{}
|
params := url.Values{}
|
||||||
params.Add("client_id", proto.Config.GITHUB_CLIENT_ID)
|
params.Add("client_id", proto.Config.GITHUB_CLIENT_ID)
|
||||||
params.Add("login", uuid)
|
params.Add("login", uuid_)
|
||||||
params.Add("state", stateBase64Str)
|
params.Add("state", stateBase64Str)
|
||||||
baseUri := proto.GitHuAuthorizeBaseUrl
|
baseUri := proto.GitHuAuthorizeBaseUrl
|
||||||
respUrl = fmt.Sprintf("%s?%s", baseUri, params.Encode())
|
respUrl = fmt.Sprintf("%s?%s", baseUri, params.Encode())
|
||||||
|
|
@ -859,23 +864,26 @@ type GetThirdPartyAddAuthUrlReq struct {
|
||||||
|
|
||||||
func handleThirdPartyCallback(c *gin.Context) {
|
func handleThirdPartyCallback(c *gin.Context) {
|
||||||
var resp proto.GenerateResp
|
var resp proto.GenerateResp
|
||||||
code := c.Query("code") //code
|
code := c.Query("code") //code
|
||||||
stateBase64Str := c.Query("state") //state
|
stateID := c.Query("state") //state
|
||||||
|
|
||||||
//解析base64
|
//解析base64
|
||||||
decodedBytes, err := base64.StdEncoding.DecodeString(stateBase64Str)
|
//decodedBytes, err := base64.StdEncoding.DecodeString(stateBase64Str)
|
||||||
if err != nil {
|
//
|
||||||
fmt.Println("Decoding error:", err)
|
stateStr := worker.GetRedis("state_id_" + stateID)
|
||||||
|
if stateStr == "" {
|
||||||
|
log.Println("state is empty,stateID=", stateID)
|
||||||
} else {
|
} else {
|
||||||
decodedStr := string(decodedBytes)
|
|
||||||
//json解析
|
//json解析
|
||||||
var state proto.ThirdPartyLoginState
|
var state proto.ThirdPartyLoginState
|
||||||
err = json.Unmarshal([]byte(decodedStr), &state)
|
err := json.Unmarshal([]byte(stateStr), &state)
|
||||||
log.Println("handle callback state:", decodedStr, "\tcode:", code)
|
log.Println("handle callback state:", stateStr, "\tcode:", code)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("json unmarshal error:", err)
|
log.Println("json unmarshal error:", err)
|
||||||
} else {
|
} else {
|
||||||
service.DoThirdPartyCallBack(&state, code)
|
service.DoThirdPartyCallBack(&state, code)
|
||||||
}
|
}
|
||||||
|
worker.DelRedis("state_id_" + stateID) //删除state
|
||||||
}
|
}
|
||||||
resp.Code = 0
|
resp.Code = 0
|
||||||
resp.Message = "success"
|
resp.Message = "success"
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue