2022-02-13 23:39:27 +08:00
|
|
|
// Copyright 2021 The Casdoor Authors. All Rights Reserved.
|
2021-06-09 11:15:49 +08:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package idp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2022-10-28 22:14:05 +08:00
|
|
|
"log"
|
2021-06-09 11:15:49 +08:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2023-04-29 01:11:58 +08:00
|
|
|
"github.com/casdoor/casdoor/util"
|
2021-06-09 11:15:49 +08:00
|
|
|
"golang.org/x/oauth2"
|
|
|
|
)
|
|
|
|
|
|
|
|
type DingTalkIdProvider struct {
|
|
|
|
Client *http.Client
|
|
|
|
Config *oauth2.Config
|
|
|
|
}
|
|
|
|
|
2021-08-07 22:02:56 +08:00
|
|
|
// NewDingTalkIdProvider ...
|
2021-06-09 11:15:49 +08:00
|
|
|
func NewDingTalkIdProvider(clientId string, clientSecret string, redirectUrl string) *DingTalkIdProvider {
|
|
|
|
idp := &DingTalkIdProvider{}
|
|
|
|
|
|
|
|
config := idp.getConfig(clientId, clientSecret, redirectUrl)
|
|
|
|
idp.Config = config
|
|
|
|
|
|
|
|
return idp
|
|
|
|
}
|
|
|
|
|
2021-08-07 22:02:56 +08:00
|
|
|
// SetHttpClient ...
|
2021-06-09 11:15:49 +08:00
|
|
|
func (idp *DingTalkIdProvider) SetHttpClient(client *http.Client) {
|
|
|
|
idp.Client = client
|
|
|
|
}
|
|
|
|
|
|
|
|
// getConfig return a point of Config, which describes a typical 3-legged OAuth2 flow
|
|
|
|
func (idp *DingTalkIdProvider) getConfig(clientId string, clientSecret string, redirectUrl string) *oauth2.Config {
|
2022-08-07 12:26:14 +08:00
|
|
|
endpoint := oauth2.Endpoint{
|
2022-02-10 17:14:18 +08:00
|
|
|
AuthURL: "https://api.dingtalk.com/v1.0/contact/users/me",
|
|
|
|
TokenURL: "https://api.dingtalk.com/v1.0/oauth2/userAccessToken",
|
2021-06-09 11:15:49 +08:00
|
|
|
}
|
|
|
|
|
2022-08-07 12:26:14 +08:00
|
|
|
config := &oauth2.Config{
|
2021-06-09 11:15:49 +08:00
|
|
|
// DingTalk not allow to set scopes,here it is just a placeholder,
|
|
|
|
// convenient to use later
|
2021-06-10 16:55:31 +08:00
|
|
|
Scopes: []string{"", ""},
|
2021-06-09 11:15:49 +08:00
|
|
|
|
|
|
|
Endpoint: endpoint,
|
|
|
|
ClientID: clientId,
|
|
|
|
ClientSecret: clientSecret,
|
|
|
|
RedirectURL: redirectUrl,
|
|
|
|
}
|
|
|
|
|
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
|
|
|
type DingTalkAccessToken struct {
|
2022-02-10 17:14:18 +08:00
|
|
|
ErrCode int `json:"code"`
|
|
|
|
ErrMsg string `json:"message"`
|
|
|
|
AccessToken string `json:"accessToken"` // Interface call credentials
|
|
|
|
ExpiresIn int64 `json:"expireIn"` // access_token interface call credential timeout time, unit (seconds)
|
2021-06-09 11:15:49 +08:00
|
|
|
}
|
|
|
|
|
2022-02-10 17:14:18 +08:00
|
|
|
// GetToken use code get access_token (*operation of getting authCode ought to be done in front)
|
|
|
|
// get more detail via: https://open.dingtalk.com/document/orgapp-server/obtain-user-token
|
2021-06-09 11:15:49 +08:00
|
|
|
func (idp *DingTalkIdProvider) GetToken(code string) (*oauth2.Token, error) {
|
2022-02-10 17:14:18 +08:00
|
|
|
pTokenParams := &struct {
|
|
|
|
ClientId string `json:"clientId"`
|
|
|
|
ClientSecret string `json:"clientSecret"`
|
|
|
|
Code string `json:"code"`
|
|
|
|
GrantType string `json:"grantType"`
|
|
|
|
}{idp.Config.ClientID, idp.Config.ClientSecret, code, "authorization_code"}
|
|
|
|
|
|
|
|
data, err := idp.postWithBody(pTokenParams, idp.Config.Endpoint.TokenURL)
|
2021-06-09 11:15:49 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-02-10 17:14:18 +08:00
|
|
|
pToken := &DingTalkAccessToken{}
|
|
|
|
err = json.Unmarshal(data, pToken)
|
2021-06-09 11:15:49 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-02-10 17:14:18 +08:00
|
|
|
if pToken.ErrCode != 0 {
|
|
|
|
return nil, fmt.Errorf("pToken.Errcode = %d, pToken.Errmsg = %s", pToken.ErrCode, pToken.ErrMsg)
|
|
|
|
}
|
2021-06-09 11:15:49 +08:00
|
|
|
|
|
|
|
token := &oauth2.Token{
|
2022-02-10 17:14:18 +08:00
|
|
|
AccessToken: pToken.AccessToken,
|
2022-08-09 16:50:49 +08:00
|
|
|
Expiry: time.Unix(time.Now().Unix()+pToken.ExpiresIn, 0),
|
2021-06-09 11:15:49 +08:00
|
|
|
}
|
|
|
|
return token, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
{
|
2022-02-10 17:14:18 +08:00
|
|
|
{
|
|
|
|
"nick" : "zhangsan",
|
|
|
|
"avatarUrl" : "https://xxx",
|
|
|
|
"mobile" : "150xxxx9144",
|
|
|
|
"openId" : "123",
|
|
|
|
"unionId" : "z21HjQliSzpw0Yxxxx",
|
|
|
|
"email" : "zhangsan@alibaba-inc.com",
|
|
|
|
"stateCode" : "86"
|
2021-06-09 11:15:49 +08:00
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
type DingTalkUserResponse struct {
|
2022-02-10 17:14:18 +08:00
|
|
|
Nick string `json:"nick"`
|
|
|
|
OpenId string `json:"openId"`
|
2022-09-08 14:44:06 +08:00
|
|
|
UnionId string `json:"unionId"`
|
2022-02-10 17:14:18 +08:00
|
|
|
AvatarUrl string `json:"avatarUrl"`
|
|
|
|
Email string `json:"email"`
|
2023-04-29 01:11:58 +08:00
|
|
|
Mobile string `json:"mobile"`
|
|
|
|
StateCode string `json:"stateCode"`
|
2021-06-09 11:15:49 +08:00
|
|
|
}
|
|
|
|
|
2022-02-10 17:14:18 +08:00
|
|
|
// GetUserInfo Use access_token to get UserInfo
|
|
|
|
// get more detail via: https://open.dingtalk.com/document/orgapp-server/dingtalk-retrieve-user-information
|
2021-06-09 11:15:49 +08:00
|
|
|
func (idp *DingTalkIdProvider) GetUserInfo(token *oauth2.Token) (*UserInfo, error) {
|
2022-02-10 17:14:18 +08:00
|
|
|
dtUserInfo := &DingTalkUserResponse{}
|
2021-06-09 11:15:49 +08:00
|
|
|
accessToken := token.AccessToken
|
|
|
|
|
2022-02-10 17:14:18 +08:00
|
|
|
reqest, err := http.NewRequest("GET", idp.Config.Endpoint.AuthURL, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
reqest.Header.Add("x-acs-dingtalk-access-token", accessToken)
|
|
|
|
resp, err := idp.Client.Do(reqest)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-04-21 23:22:50 +08:00
|
|
|
defer resp.Body.Close()
|
2021-06-09 11:15:49 +08:00
|
|
|
|
2022-08-09 16:50:49 +08:00
|
|
|
data, err := io.ReadAll(resp.Body)
|
2021-06-09 11:15:49 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-02-10 17:14:18 +08:00
|
|
|
err = json.Unmarshal(data, dtUserInfo)
|
|
|
|
if err != nil {
|
2021-06-09 11:15:49 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-04-29 01:11:58 +08:00
|
|
|
countryCode, err := util.GetCountryCode(dtUserInfo.StateCode, dtUserInfo.Mobile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-02-10 17:14:18 +08:00
|
|
|
}
|
|
|
|
|
2021-06-09 11:15:49 +08:00
|
|
|
userInfo := UserInfo{
|
2022-02-10 17:14:18 +08:00
|
|
|
Id: dtUserInfo.OpenId,
|
|
|
|
Username: dtUserInfo.Nick,
|
|
|
|
DisplayName: dtUserInfo.Nick,
|
2022-09-08 14:44:06 +08:00
|
|
|
UnionId: dtUserInfo.UnionId,
|
2022-02-10 17:14:18 +08:00
|
|
|
Email: dtUserInfo.Email,
|
2023-04-29 01:11:58 +08:00
|
|
|
Phone: dtUserInfo.Mobile,
|
|
|
|
CountryCode: countryCode,
|
2022-02-10 17:14:18 +08:00
|
|
|
AvatarUrl: dtUserInfo.AvatarUrl,
|
2021-06-09 11:15:49 +08:00
|
|
|
}
|
2023-03-10 21:47:54 +08:00
|
|
|
|
|
|
|
corpAccessToken := idp.getInnerAppAccessToken()
|
|
|
|
userId, err := idp.getUserId(userInfo.UnionId, corpAccessToken)
|
|
|
|
if err != nil {
|
2022-10-28 22:14:05 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
2023-03-10 21:47:54 +08:00
|
|
|
|
2023-05-17 22:14:57 +08:00
|
|
|
corpMobile, corpEmail, jobNumber, err := idp.getUserCorpEmail(userId, corpAccessToken)
|
2023-04-29 21:28:55 +08:00
|
|
|
if err == nil {
|
2023-05-17 22:14:57 +08:00
|
|
|
if corpMobile != "" {
|
|
|
|
userInfo.Phone = corpMobile
|
|
|
|
}
|
|
|
|
|
2023-04-29 21:28:55 +08:00
|
|
|
if corpEmail != "" {
|
|
|
|
userInfo.Email = corpEmail
|
|
|
|
}
|
|
|
|
|
|
|
|
if jobNumber != "" {
|
|
|
|
userInfo.Username = jobNumber
|
|
|
|
}
|
2023-03-10 21:47:54 +08:00
|
|
|
}
|
|
|
|
|
2021-06-09 11:15:49 +08:00
|
|
|
return &userInfo, nil
|
|
|
|
}
|
|
|
|
|
2022-02-10 17:14:18 +08:00
|
|
|
func (idp *DingTalkIdProvider) postWithBody(body interface{}, url string) ([]byte, error) {
|
|
|
|
bs, err := json.Marshal(body)
|
2021-06-09 11:15:49 +08:00
|
|
|
if err != nil {
|
2022-02-10 17:14:18 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
r := strings.NewReader(string(bs))
|
|
|
|
resp, err := idp.Client.Post(url, "application/json;charset=UTF-8", r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-08-09 16:50:49 +08:00
|
|
|
data, err := io.ReadAll(resp.Body)
|
2022-02-10 17:14:18 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-06-09 11:15:49 +08:00
|
|
|
}
|
|
|
|
defer func(Body io.ReadCloser) {
|
|
|
|
err := Body.Close()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}(resp.Body)
|
|
|
|
|
2022-02-10 17:14:18 +08:00
|
|
|
return data, nil
|
2021-06-09 11:15:49 +08:00
|
|
|
}
|
2022-10-28 22:14:05 +08:00
|
|
|
|
|
|
|
func (idp *DingTalkIdProvider) getInnerAppAccessToken() string {
|
|
|
|
body := make(map[string]string)
|
2023-03-10 21:47:54 +08:00
|
|
|
body["appKey"] = idp.Config.ClientID
|
|
|
|
body["appSecret"] = idp.Config.ClientSecret
|
|
|
|
respBytes, err := idp.postWithBody(body, "https://api.dingtalk.com/v1.0/oauth2/accessToken")
|
2022-10-28 22:14:05 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err.Error())
|
|
|
|
}
|
2023-03-10 21:47:54 +08:00
|
|
|
|
2022-10-28 22:14:05 +08:00
|
|
|
var data struct {
|
|
|
|
ExpireIn int `json:"expireIn"`
|
|
|
|
AccessToken string `json:"accessToken"`
|
|
|
|
}
|
|
|
|
err = json.Unmarshal(respBytes, &data)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err.Error())
|
|
|
|
}
|
|
|
|
return data.AccessToken
|
|
|
|
}
|
|
|
|
|
2023-03-10 21:47:54 +08:00
|
|
|
func (idp *DingTalkIdProvider) getUserId(unionId string, accessToken string) (string, error) {
|
2022-10-28 22:14:05 +08:00
|
|
|
body := make(map[string]string)
|
|
|
|
body["unionid"] = unionId
|
2023-03-10 21:47:54 +08:00
|
|
|
respBytes, err := idp.postWithBody(body, "https://oapi.dingtalk.com/topapi/user/getbyunionid?access_token="+accessToken)
|
2022-10-28 22:14:05 +08:00
|
|
|
if err != nil {
|
2023-03-10 21:47:54 +08:00
|
|
|
return "", err
|
2022-10-28 22:14:05 +08:00
|
|
|
}
|
2023-03-10 21:47:54 +08:00
|
|
|
|
2022-10-28 22:14:05 +08:00
|
|
|
var data struct {
|
|
|
|
ErrCode int `json:"errcode"`
|
|
|
|
ErrMessage string `json:"errmsg"`
|
2023-03-10 21:47:54 +08:00
|
|
|
Result struct {
|
|
|
|
UserId string `json:"userid"`
|
|
|
|
} `json:"result"`
|
2022-10-28 22:14:05 +08:00
|
|
|
}
|
|
|
|
err = json.Unmarshal(respBytes, &data)
|
|
|
|
if err != nil {
|
2023-03-10 21:47:54 +08:00
|
|
|
return "", err
|
2022-10-28 22:14:05 +08:00
|
|
|
}
|
|
|
|
if data.ErrCode == 60121 {
|
2023-04-29 21:28:55 +08:00
|
|
|
return "", fmt.Errorf("该应用只允许本企业内部用户登录,您不属于该企业,无法登录")
|
2022-12-17 21:10:20 +08:00
|
|
|
} else if data.ErrCode != 0 {
|
2023-03-10 21:47:54 +08:00
|
|
|
return "", fmt.Errorf(data.ErrMessage)
|
|
|
|
}
|
|
|
|
return data.Result.UserId, nil
|
|
|
|
}
|
|
|
|
|
2023-05-17 22:14:57 +08:00
|
|
|
func (idp *DingTalkIdProvider) getUserCorpEmail(userId string, accessToken string) (string, string, string, error) {
|
|
|
|
// https://open.dingtalk.com/document/isvapp/query-user-details
|
2023-03-10 21:47:54 +08:00
|
|
|
body := make(map[string]string)
|
|
|
|
body["userid"] = userId
|
|
|
|
respBytes, err := idp.postWithBody(body, "https://oapi.dingtalk.com/topapi/v2/user/get?access_token="+accessToken)
|
|
|
|
if err != nil {
|
2023-05-17 22:14:57 +08:00
|
|
|
return "", "", "", err
|
2023-03-10 21:47:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var data struct {
|
|
|
|
ErrMessage string `json:"errmsg"`
|
|
|
|
Result struct {
|
2023-05-17 22:14:57 +08:00
|
|
|
Mobile string `json:"mobile"`
|
2023-04-29 21:28:55 +08:00
|
|
|
Email string `json:"email"`
|
|
|
|
JobNumber string `json:"job_number"`
|
2023-03-10 21:47:54 +08:00
|
|
|
} `json:"result"`
|
|
|
|
}
|
|
|
|
err = json.Unmarshal(respBytes, &data)
|
|
|
|
if err != nil {
|
2023-05-17 22:14:57 +08:00
|
|
|
return "", "", "", err
|
2023-03-10 21:47:54 +08:00
|
|
|
}
|
|
|
|
if data.ErrMessage != "ok" {
|
2023-05-17 22:14:57 +08:00
|
|
|
return "", "", "", fmt.Errorf(data.ErrMessage)
|
2022-10-28 22:14:05 +08:00
|
|
|
}
|
2023-05-17 22:14:57 +08:00
|
|
|
return data.Result.Mobile, data.Result.Email, data.Result.JobNumber, nil
|
2022-10-28 22:14:05 +08:00
|
|
|
}
|