/
provider_dingtalk.go
171 lines (141 loc) · 4.84 KB
/
provider_dingtalk.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// Copyright © 2023 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package oidc
import (
"context"
"encoding/json"
"net/url"
"strings"
"time"
"github.com/pkg/errors"
"golang.org/x/oauth2"
"github.com/ory/x/httpx"
"github.com/hashicorp/go-retryablehttp"
"github.com/ory/herodot"
)
type ProviderDingTalk struct {
config *Configuration
reg Dependencies
}
func NewProviderDingTalk(
config *Configuration,
reg Dependencies,
) Provider {
return &ProviderDingTalk{
config: config,
reg: reg,
}
}
func (g *ProviderDingTalk) Config() *Configuration {
return g.config
}
func (g *ProviderDingTalk) oauth2(ctx context.Context) *oauth2.Config {
var endpoint = oauth2.Endpoint{
AuthURL: "https://login.dingtalk.com/oauth2/auth",
TokenURL: "https://api.dingtalk.com/v1.0/oauth2/userAccessToken",
}
return &oauth2.Config{
ClientID: g.config.ClientID,
ClientSecret: g.config.ClientSecret,
Endpoint: endpoint,
// DingTalk only allow to set scopes: openid or openid corpid
Scopes: g.config.Scope,
RedirectURL: g.config.Redir(g.reg.Config().OIDCRedirectURIBase(ctx)),
}
}
func (g *ProviderDingTalk) AuthCodeURLOptions(r ider) []oauth2.AuthCodeOption {
return []oauth2.AuthCodeOption{
oauth2.SetAuthURLParam("prompt", "consent"),
}
}
func (g *ProviderDingTalk) OAuth2(ctx context.Context) (*oauth2.Config, error) {
return g.oauth2(ctx), nil
}
func (g *ProviderDingTalk) Exchange(ctx context.Context, code string, opts ...oauth2.AuthCodeOption) (*oauth2.Token, error) {
conf, err := g.OAuth2(ctx)
if err != nil {
return nil, errors.WithStack(herodot.ErrInternalServerError.WithReasonf("%s", err))
}
pTokenParams := &struct {
ClientId string `json:"clientId"`
ClientSecret string `json:"clientSecret"`
Code string `json:"code"`
GrantType string `json:"grantType"`
}{conf.ClientID, conf.ClientSecret, code, "authorization_code"}
bs, err := json.Marshal(pTokenParams)
if err != nil {
return nil, errors.WithStack(herodot.ErrInternalServerError.WithReasonf("%s", err))
}
r := strings.NewReader(string(bs))
client := g.reg.HTTPClient(ctx, httpx.ResilientClientDisallowInternalIPs())
req, err := retryablehttp.NewRequest("POST", conf.Endpoint.TokenURL, r)
if err != nil {
return nil, errors.WithStack(herodot.ErrInternalServerError.WithReasonf("%s", err))
}
req.Header.Add("Content-Type", "application/json;charset=UTF-8")
resp, err := client.Do(req)
if err != nil {
return nil, errors.WithStack(herodot.ErrInternalServerError.WithReasonf("%s", err))
}
defer resp.Body.Close()
if err := logUpstreamError(g.reg.Logger(), resp); err != nil {
return nil, err
}
var dToken struct {
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)
}
if err := json.NewDecoder(resp.Body).Decode(&dToken); err != nil {
return nil, errors.WithStack(herodot.ErrInternalServerError.WithReasonf("%s", err))
}
if dToken.ErrCode != 0 {
return nil, errors.WithStack(herodot.ErrInternalServerError.WithReasonf("dToken.ErrCode = %d, dToken.ErrMsg = %s", dToken.ErrCode, dToken.ErrMsg))
}
token := &oauth2.Token{
AccessToken: dToken.AccessToken,
Expiry: time.Unix(time.Now().Unix()+int64(dToken.ExpiresIn), 0),
}
return token, nil
}
func (g *ProviderDingTalk) Claims(ctx context.Context, exchange *oauth2.Token, _ url.Values) (*Claims, error) {
userInfoURL := "https://api.dingtalk.com/v1.0/contact/users/me"
accessToken := exchange.AccessToken
client := g.reg.HTTPClient(ctx, httpx.ResilientClientDisallowInternalIPs())
req, err := retryablehttp.NewRequest("GET", userInfoURL, nil)
if err != nil {
return nil, errors.WithStack(herodot.ErrInternalServerError.WithReasonf("%s", err))
}
req.Header.Add("x-acs-dingtalk-access-token", accessToken)
resp, err := client.Do(req)
if err != nil {
return nil, errors.WithStack(herodot.ErrInternalServerError.WithReasonf("%s", err))
}
defer resp.Body.Close()
if err := logUpstreamError(g.reg.Logger(), resp); err != nil {
return nil, err
}
var user struct {
Nick string `json:"nick"`
OpenId string `json:"openId"`
AvatarUrl string `json:"avatarUrl"`
Email string `json:"email"`
ErrMsg string `json:"message"`
ErrCode string `json:"code"`
}
if err := json.NewDecoder(resp.Body).Decode(&user); err != nil {
return nil, errors.WithStack(herodot.ErrInternalServerError.WithReasonf("%s", err))
}
if user.ErrMsg != "" {
return nil, errors.WithStack(herodot.ErrInternalServerError.WithReasonf("userResp.ErrCode = %s, userResp.ErrMsg = %s", user.ErrCode, user.ErrMsg))
}
return &Claims{
Issuer: userInfoURL,
Subject: user.OpenId,
Nickname: user.Nick,
Name: user.Nick,
Picture: user.AvatarUrl,
Email: user.Email,
}, nil
}