Skip to content

Commit

Permalink
钉钉第三方扫码登录
Browse files Browse the repository at this point in the history
  • Loading branch information
mengfengkong committed Jul 7, 2020
1 parent 75dc201 commit af94bd2
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
6 changes: 3 additions & 3 deletions sdk/api_authentication_caller.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ func (client *DingTalkClient) GetUserInfoFromAdmin(code string) (GetUserInfoFrom

//服务端通过临时授权码获取授权用户的个人信息
//https://open-doc.dingtalk.com/microapp/serverapi3/vmzkak
func (s *DingTalkSDK) GetUserInfoByCode(code string) (GetUserInfoByCodeResp, error) {
func (client *DingTalkOauthClient) GetUserInfoByCode(code string) (GetUserInfoByCodeResp, error) {
params := map[string]string{
"tmp_auth_code": code,
}
paramsJson, _ := json.ToJson(params)
body, err := ExcuteOapi("https://oapi.dingtalk.com/sns/getuserinfo_bycode", s.SuiteKey, s.SuiteSecret, "_", "", paramsJson)

body, err := ExcuteOapi("https://oapi.dingtalk.com/sns/getuserinfo_bycode", client.OauthAppId, client.OauthAppSecret, paramsJson)
resp := GetUserInfoByCodeResp{}
if err != nil {
return resp, err
Expand Down
6 changes: 6 additions & 0 deletions sdk/api_authentication_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,9 @@ func TestGetUserInfoFromAdmin(t *testing.T) {
t.Log(json.ToJson(resp))
t.Log(err)
}

func TestDingTalkSDK_GetUserInfoByCode(t *testing.T) {
resp, err := NewDingTalkOauthClient().GetUserInfoByCode("492b19afde343b559cc53f9b096adfe1")
t.Log(json.ToJson(resp))
t.Log(err)
}
29 changes: 17 additions & 12 deletions sdk/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ type DingTalkClient struct {
AgentId int64
}

type DingTalkOauthClient struct {
OauthAppId string
OauthAppSecret string
}

func NewDingTalkOauthClient() *DingTalkOauthClient {
return &DingTalkOauthClient{
OauthAppId: os.Getenv("OAUTH_APP_ID"),
OauthAppSecret: os.Getenv("OAUTH_APP_SECRET"),
}
}

func NewSDK() *DingTalkSDK {
appId, err := strconv.ParseInt(os.Getenv("APP_ID"), 10, 64)
if err != nil {
Expand Down Expand Up @@ -98,28 +110,21 @@ func (corp *Corp) CreateDingTalkClient() (*DingTalkClient, error) {
return NewDingTalkClient(tokenInfo.AccessToken, authInfo.AuthInfo.Agent[0].AgentId), nil
}

func ExcuteOapi(url string, accessKey string, accessSecret string, suiteTicket string, corpId string, body string) (string, error) {
func ExcuteOapi(url string, oauthAppId string, oauthAppSecret string, body string) (string, error) {
timestamp := time.Now().UnixNano() / 1e6
nativeSignature := strconv.FormatInt(timestamp, 10)
if suiteTicket != "" {
nativeSignature += "\n" + suiteTicket
}

afterHmacSHA256 := encrypt.SHA256(nativeSignature, accessSecret)
oauthAppId = "dingoayheivmu34mylzrzg"
oauthAppSecret = "PeQfl0uBtO3VLr-Feix5cIEWM9Oo81Mhec-SRiO2SPiEPU18fg0YZaB7fHXVkX3U"
afterHmacSHA256 := encrypt.SHA256(nativeSignature, oauthAppSecret)
afterBase64 := encrypt.BASE64(afterHmacSHA256)
afterUrlEncode := encrypt.URLEncode(afterBase64)

params := map[string]string{
"timestamp": strconv.FormatInt(timestamp, 10),
"accessKey": accessKey,
"accessKey": oauthAppId,
"signature": afterUrlEncode,
}
if suiteTicket != "" {
params["suiteTicket"] = suiteTicket
}
if corpId != "" {
params["corpId"] = corpId
}

return http.Post(url, params, body)
}

0 comments on commit af94bd2

Please sign in to comment.