/
wechat.go
131 lines (112 loc) · 3.68 KB
/
wechat.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
package sso
import (
"github.com/authgear/authgear-server/pkg/lib/authn/stdattrs"
"github.com/authgear/authgear-server/pkg/lib/config"
)
const (
wechatAuthorizationURL string = "https://open.weixin.qq.com/connect/oauth2/authorize"
)
type WechatImpl struct {
ProviderConfig config.OAuthSSOProviderConfig
Credentials config.OAuthSSOProviderCredentialsItem
StandardAttributesNormalizer StandardAttributesNormalizer
HTTPClient OAuthHTTPClient
}
func (*WechatImpl) Type() config.OAuthSSOProviderType {
return config.OAuthSSOProviderTypeWechat
}
func (w *WechatImpl) Config() config.OAuthSSOProviderConfig {
return w.ProviderConfig
}
func (w *WechatImpl) GetAuthURL(param GetAuthURLParam) (string, error) {
return MakeAuthorizationURL(wechatAuthorizationURL, AuthorizationURLParams{
// ClientID is not used by wechat.
WechatAppID: w.ProviderConfig.ClientID,
RedirectURI: param.RedirectURI,
Scope: w.ProviderConfig.Type.Scope(),
ResponseType: ResponseTypeCode,
// ResponseMode is unset.
State: param.State,
Prompt: w.GetPrompt(param.Prompt),
// Nonce is unset.
}.Query()), nil
}
func (w *WechatImpl) GetAuthInfo(r OAuthAuthorizationResponse, param GetAuthInfoParam) (AuthInfo, error) {
return w.NonOpenIDConnectGetAuthInfo(r, param)
}
func (w *WechatImpl) NonOpenIDConnectGetAuthInfo(r OAuthAuthorizationResponse, _ GetAuthInfoParam) (authInfo AuthInfo, err error) {
accessTokenResp, err := wechatFetchAccessTokenResp(
w.HTTPClient,
r.Code,
w.ProviderConfig.ClientID,
w.Credentials.ClientSecret,
)
if err != nil {
return
}
rawProfile, err := wechatFetchUserProfile(w.HTTPClient, accessTokenResp)
if err != nil {
return
}
config := w.Config()
var userID string
if config.IsSandboxAccount {
if accessTokenResp.UnionID() != "" {
err = InvalidConfiguration.New("invalid is_sandbox_account config, WeChat sandbox account should not have union id")
return
}
userID = accessTokenResp.OpenID()
} else {
userID = accessTokenResp.UnionID()
}
if userID == "" {
// this may happen if developer misconfigure is_sandbox_account, e.g. sandbox account doesn't have union id
err = InvalidConfiguration.New("invalid is_sandbox_account config, missing user id in wechat token response")
return
}
// https://developers.weixin.qq.com/doc/offiaccount/User_Management/Get_users_basic_information_UnionID.html
// Here is an example of how the raw profile looks like.
// {
// "sex": 0,
// "city": "",
// "openid": "redacted",
// "country": "",
// "language": "zh_CN",
// "nickname": "John Doe",
// "province": "",
// "privilege": [],
// "headimgurl": ""
// }
var gender string
if sex, ok := rawProfile["sex"].(float64); ok {
if sex == 1 {
gender = "male"
} else if sex == 2 {
gender = "female"
}
}
name, _ := rawProfile["nickname"].(string)
locale, _ := rawProfile["language"].(string)
authInfo.ProviderRawProfile = rawProfile
authInfo.ProviderUserID = userID
// Claims.Email.Required is not respected because wechat does not return the email claim.
authInfo.StandardAttributes = stdattrs.T{
stdattrs.Name: name,
stdattrs.Locale: locale,
stdattrs.Gender: gender,
}.WithNameCopiedToGivenName()
err = w.StandardAttributesNormalizer.Normalize(authInfo.StandardAttributes)
if err != nil {
return
}
return
}
func (w *WechatImpl) GetPrompt(prompt []string) []string {
// wechat doesn't support prompt parameter
// ref: https://developers.weixin.qq.com/doc/oplatform/en/Third-party_Platforms/Official_Accounts/official_account_website_authorization.html
return []string{}
}
var (
_ OAuthProvider = &WechatImpl{}
_ NonOpenIDConnectProvider = &WechatImpl{}
)