/
github.go
181 lines (153 loc) · 4.98 KB
/
github.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
172
173
174
175
176
177
178
179
180
181
package sso
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"
"github.com/authgear/authgear-server/pkg/api/apierrors"
"github.com/authgear/authgear-server/pkg/lib/authn/stdattrs"
"github.com/authgear/authgear-server/pkg/lib/config"
"github.com/authgear/authgear-server/pkg/util/errorutil"
)
const (
githubAuthorizationURL string = "https://github.com/login/oauth/authorize"
// nolint: gosec
githubTokenURL string = "https://github.com/login/oauth/access_token"
githubUserInfoURL string = "https://api.github.com/user"
)
type GithubImpl struct {
ProviderConfig config.OAuthSSOProviderConfig
Credentials config.OAuthSSOProviderCredentialsItem
StandardAttributesNormalizer StandardAttributesNormalizer
HTTPClient OAuthHTTPClient
}
func (*GithubImpl) Type() config.OAuthSSOProviderType {
return config.OAuthSSOProviderTypeGithub
}
func (g *GithubImpl) Config() config.OAuthSSOProviderConfig {
return g.ProviderConfig
}
func (g *GithubImpl) GetAuthURL(param GetAuthURLParam) (string, error) {
// https://docs.github.com/en/developers/apps/building-oauth-apps/authorizing-oauth-apps#1-request-a-users-github-identity
return MakeAuthorizationURL(githubAuthorizationURL, AuthorizationURLParams{
ClientID: g.ProviderConfig.ClientID,
RedirectURI: param.RedirectURI,
Scope: g.ProviderConfig.Type.Scope(),
// ResponseType is unset.
// ResponseMode is unset.
State: param.State,
// Prompt is unset.
// Nonce is unset.
}.Query()), nil
}
func (g *GithubImpl) GetAuthInfo(r OAuthAuthorizationResponse, param GetAuthInfoParam) (authInfo AuthInfo, err error) {
return g.NonOpenIDConnectGetAuthInfo(r, param)
}
func (g *GithubImpl) NonOpenIDConnectGetAuthInfo(r OAuthAuthorizationResponse, param GetAuthInfoParam) (authInfo AuthInfo, err error) {
accessTokenResp, err := g.exchangeCode(r, param)
if err != nil {
return
}
userProfile, err := g.fetchUserInfo(accessTokenResp)
if err != nil {
return
}
authInfo.ProviderRawProfile = userProfile
idJSONNumber, _ := userProfile["id"].(json.Number)
email, _ := userProfile["email"].(string)
login, _ := userProfile["login"].(string)
picture, _ := userProfile["avatar_url"].(string)
profile, _ := userProfile["html_url"].(string)
id := string(idJSONNumber)
authInfo.ProviderUserID = id
stdAttrs, err := stdattrs.Extract(map[string]interface{}{
stdattrs.Email: email,
stdattrs.Name: login,
stdattrs.GivenName: login,
stdattrs.Picture: picture,
stdattrs.Profile: profile,
}, stdattrs.ExtractOptions{
EmailRequired: *g.ProviderConfig.Claims.Email.Required,
})
if err != nil {
err = apierrors.AddDetails(err, errorutil.Details{
"ProviderType": apierrors.APIErrorDetail.Value(g.ProviderConfig.Type),
})
return
}
authInfo.StandardAttributes = stdAttrs
err = g.StandardAttributesNormalizer.Normalize(authInfo.StandardAttributes)
if err != nil {
return
}
return
}
func (g *GithubImpl) exchangeCode(r OAuthAuthorizationResponse, param GetAuthInfoParam) (accessTokenResp AccessTokenResp, err error) {
q := make(url.Values)
q.Set("client_id", g.ProviderConfig.ClientID)
q.Set("client_secret", g.Credentials.ClientSecret)
q.Set("code", r.Code)
q.Set("redirect_uri", param.RedirectURI)
body := strings.NewReader(q.Encode())
req, _ := http.NewRequest("POST", githubTokenURL, body)
// https://docs.github.com/en/developers/apps/building-oauth-apps/authorizing-oauth-apps#2-users-are-redirected-back-to-your-site-by-github
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := g.HTTPClient.Do(req)
if err != nil {
return
}
defer resp.Body.Close()
if resp.StatusCode == 200 {
err = json.NewDecoder(resp.Body).Decode(&accessTokenResp)
if err != nil {
return
}
} else {
var errResp oauthErrorResp
err = json.NewDecoder(resp.Body).Decode(&errResp)
if err != nil {
return
}
err = errResp.AsError()
}
return
}
func (g *GithubImpl) fetchUserInfo(accessTokenResp AccessTokenResp) (userProfile map[string]interface{}, err error) {
tokenType := accessTokenResp.TokenType()
accessTokenValue := accessTokenResp.AccessToken()
authorizationHeader := fmt.Sprintf("%s %s", tokenType, accessTokenValue)
req, err := http.NewRequest(http.MethodGet, githubUserInfoURL, nil)
if err != nil {
return
}
req.Header.Add("Authorization", authorizationHeader)
resp, err := g.HTTPClient.Do(req)
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return
}
if resp.StatusCode != 200 {
err = fmt.Errorf("failed to fetch user profile: unexpected status code: %d", resp.StatusCode)
return
}
decoder := json.NewDecoder(resp.Body)
// Deserialize "id" as json.Number.
decoder.UseNumber()
err = decoder.Decode(&userProfile)
if err != nil {
return
}
return
}
func (*GithubImpl) GetPrompt(prompt []string) []string {
// Github does not support prompt.
return []string{}
}
var (
_ OAuthProvider = &GithubImpl{}
_ NonOpenIDConnectProvider = &GithubImpl{}
)