-
Notifications
You must be signed in to change notification settings - Fork 32
/
github.go
155 lines (127 loc) · 5.15 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
/* Copyright (c) 2021, VRAI Labs and/or its affiliates. All rights reserved.
*
* This software is licensed under the Apache License, Version 2.0 (the
* "License") as published by the Apache Software Foundation.
*
* 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 providers
import (
"encoding/base64"
"errors"
"fmt"
"github.com/supertokens/supertokens-golang/recipe/thirdparty/tpmodels"
"github.com/supertokens/supertokens-golang/supertokens"
)
func Github(input tpmodels.ProviderInput) *tpmodels.TypeProvider {
if input.Config.Name == "" {
input.Config.Name = "Github"
}
if input.Config.AuthorizationEndpoint == "" {
input.Config.AuthorizationEndpoint = "https://github.com/login/oauth/authorize"
}
if input.Config.TokenEndpoint == "" {
input.Config.TokenEndpoint = "https://github.com/login/oauth/access_token"
}
if input.Config.ValidateAccessToken == nil {
input.Config.ValidateAccessToken = func(accessToken string, clientConfig tpmodels.ProviderConfigForClientType, userContext supertokens.UserContext) error {
basicAuthToken := base64.StdEncoding.EncodeToString([]byte(clientConfig.ClientID + ":" + clientConfig.ClientSecret))
wrongClientIdError := errors.New("Access token does not belong to your application")
resp, status, err := doPostRequest("https://api.github.com/applications/"+clientConfig.ClientID+"/token", map[string]interface{}{
"access_token": accessToken,
}, map[string]interface{}{
"Authorization": "Basic " + basicAuthToken,
"Content-Type": "application/json",
})
if err != nil || status != 200 {
return errors.New("Invalid access token")
}
app, appOk := resp["app"]
if !appOk {
return wrongClientIdError
}
clientId, clientIdOk := app.(map[string]interface{})["client_id"]
if !clientIdOk {
return wrongClientIdError
}
if clientId != clientConfig.ClientID {
return wrongClientIdError
}
return nil
}
}
oOverride := input.Override
input.Override = func(originalImplementation *tpmodels.TypeProvider) *tpmodels.TypeProvider {
oGetConfig := originalImplementation.GetConfigForClientType
originalImplementation.GetConfigForClientType = func(clientType *string, userContext supertokens.UserContext) (tpmodels.ProviderConfigForClientType, error) {
config, err := oGetConfig(clientType, userContext)
if err != nil {
return tpmodels.ProviderConfigForClientType{}, err
}
if len(config.Scope) == 0 {
config.Scope = []string{"read:user", "user:email"}
}
return config, nil
}
originalImplementation.GetUserInfo = func(oAuthTokens tpmodels.TypeOAuthTokens, userContext supertokens.UserContext) (tpmodels.TypeUserInfo, error) {
headers := map[string]string{
"Authorization": fmt.Sprintf("Bearer %s", oAuthTokens["access_token"]),
"Accept": "application/vnd.github.v3+json",
}
rawResponse := map[string]interface{}{}
emailInfo, err := doGetRequest("https://api.github.com/user/emails", nil, headers)
if err != nil {
return tpmodels.TypeUserInfo{}, err
}
rawResponse["emails"] = emailInfo
userInfo, err := doGetRequest("https://api.github.com/user", nil, headers)
if err != nil {
return tpmodels.TypeUserInfo{}, err
}
rawResponse["user"] = userInfo
rawUserInfoResponseFromProvider := tpmodels.TypeRawUserInfoFromProvider{FromUserInfoAPI: rawResponse}
userInfoResult, err := getSupertokensUserInfoFromRawUserInfoResponseForGithub(rawUserInfoResponseFromProvider)
if err != nil {
return tpmodels.TypeUserInfo{}, err
}
return tpmodels.TypeUserInfo{
ThirdPartyUserId: userInfoResult.ThirdPartyUserId,
Email: userInfoResult.Email,
RawUserInfoFromProvider: rawUserInfoResponseFromProvider,
}, nil
}
if oOverride != nil {
originalImplementation = oOverride(originalImplementation)
}
return originalImplementation
}
return NewProvider(input)
}
func getSupertokensUserInfoFromRawUserInfoResponseForGithub(rawUserInfoResponse tpmodels.TypeRawUserInfoFromProvider) (tpmodels.TypeUserInfo, error) {
if rawUserInfoResponse.FromUserInfoAPI == nil {
return tpmodels.TypeUserInfo{}, errors.New("rawUserInfoResponse.FromUserInfoAPI is not available")
}
result := tpmodels.TypeUserInfo{
ThirdPartyUserId: fmt.Sprint(int64(rawUserInfoResponse.FromUserInfoAPI["user"].(map[string]interface{})["id"].(float64))),
}
emailsInfo := rawUserInfoResponse.FromUserInfoAPI["emails"].([]interface{})
for _, info := range emailsInfo {
emailInfoMap := info.(map[string]interface{})
if emailInfoMap["primary"].(bool) {
verified, verifiedOk := emailInfoMap["verified"].(bool)
result.Email = &tpmodels.EmailStruct{
ID: emailInfoMap["email"].(string),
IsVerified: verified && verifiedOk,
}
break
}
}
return result, nil
}