-
-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathproviders.go
293 lines (272 loc) · 8.7 KB
/
providers.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// Package provider implements all oauth2, oauth1 as well as custom and direct providers
package provider
import (
"crypto/sha1" //nolint
"encoding/json"
"fmt"
"github.com/dghubble/oauth1"
"github.com/dghubble/oauth1/twitter"
"github.com/go-pkgz/auth/v2/token"
"golang.org/x/oauth2"
"golang.org/x/oauth2/facebook"
"golang.org/x/oauth2/github"
"golang.org/x/oauth2/google"
"golang.org/x/oauth2/microsoft"
"golang.org/x/oauth2/yandex"
)
// UserAttributes is the type that will be used to map user data from provider to token.User
type UserAttributes map[string]string
// NewGoogle makes google oauth2 provider
func NewGoogle(p Params) Oauth2Handler {
return initOauth2Handler(p, Oauth2Handler{
name: "google",
endpoint: google.Endpoint,
scopes: []string{"https://www.googleapis.com/auth/userinfo.profile"},
infoURL: "https://www.googleapis.com/oauth2/v3/userinfo",
mapUser: func(data UserData, _ []byte) token.User {
userInfo := token.User{
// encode email with provider name to avoid collision if same id returned by other provider
ID: "google_" + token.HashID(sha1.New(), data.Value("sub")),
Name: data.Value("name"),
Picture: data.Value("picture"),
}
if userInfo.Name == "" {
userInfo.Name = "noname_" + userInfo.ID[8:12]
}
for k, v := range p.UserAttributes {
userInfo.SetStrAttr(v, data.Value(k))
}
return userInfo
},
})
}
// NewGithub makes github oauth2 provider
func NewGithub(p Params) Oauth2Handler {
return initOauth2Handler(p, Oauth2Handler{
name: "github",
endpoint: github.Endpoint,
scopes: []string{},
infoURL: "https://api.github.com/user",
mapUser: func(data UserData, _ []byte) token.User {
userInfo := token.User{
ID: "github_" + token.HashID(sha1.New(), data.Value("login")),
Name: data.Value("name"),
Picture: data.Value("avatar_url"),
}
// github may have no user name, use login in this case
if userInfo.Name == "" {
userInfo.Name = data.Value("login")
}
for k, v := range p.UserAttributes {
userInfo.SetStrAttr(v, data.Value(k))
}
return userInfo
},
})
}
// NewFacebook makes facebook oauth2 provider
func NewFacebook(p Params) Oauth2Handler {
// response format for fb /me call
type uinfo struct {
ID string `json:"id"`
Name string `json:"name"`
Picture struct {
Data struct {
URL string `json:"url"`
} `json:"data"`
} `json:"picture"`
}
return initOauth2Handler(p, Oauth2Handler{
name: "facebook",
endpoint: facebook.Endpoint,
scopes: []string{"public_profile"},
infoURL: "https://graph.facebook.com/me?fields=id,name,picture",
mapUser: func(data UserData, bdata []byte) token.User {
userInfo := token.User{
ID: "facebook_" + token.HashID(sha1.New(), data.Value("id")),
Name: data.Value("name"),
}
if userInfo.Name == "" {
userInfo.Name = userInfo.ID[0:16]
}
uinfoJSON := uinfo{}
if err := json.Unmarshal(bdata, &uinfoJSON); err == nil {
userInfo.Picture = uinfoJSON.Picture.Data.URL
}
for k, v := range p.UserAttributes {
userInfo.SetStrAttr(v, data.Value(k))
}
return userInfo
},
})
}
// NewYandex makes yandex oauth2 provider
func NewYandex(p Params) Oauth2Handler {
return initOauth2Handler(p, Oauth2Handler{
name: "yandex",
endpoint: yandex.Endpoint,
scopes: []string{},
// See https://tech.yandex.com/passport/doc/dg/reference/response-docpage/
infoURL: "https://login.yandex.ru/info?format=json",
mapUser: func(data UserData, _ []byte) token.User {
userInfo := token.User{
ID: "yandex_" + token.HashID(sha1.New(), data.Value("id")),
Name: data.Value("display_name"), // using Display Name by default
}
if userInfo.Name == "" {
userInfo.Name = data.Value("real_name") // using Real Name (== full name) if Display Name is empty
}
if userInfo.Name == "" {
userInfo.Name = data.Value("login") // otherwise using login
}
if data.Value("default_avatar_id") != "" {
userInfo.Picture = fmt.Sprintf("https://avatars.yandex.net/get-yapic/%s/islands-200", data.Value("default_avatar_id"))
}
for k, v := range p.UserAttributes {
userInfo.SetStrAttr(v, data.Value(k))
}
return userInfo
},
})
}
// NewTwitter makes twitter oauth2 provider
func NewTwitter(p Params) Oauth1Handler {
return initOauth1Handler(p, Oauth1Handler{
name: "twitter",
conf: oauth1.Config{
Endpoint: twitter.AuthorizeEndpoint,
},
infoURL: "https://api.twitter.com/1.1/account/verify_credentials.json",
mapUser: func(data UserData, _ []byte) token.User {
userInfo := token.User{
ID: "twitter_" + token.HashID(sha1.New(), data.Value("id_str")),
Name: data.Value("screen_name"),
Picture: data.Value("profile_image_url_https"),
}
if userInfo.Name == "" {
userInfo.Name = data.Value("name")
}
for k, v := range p.UserAttributes {
userInfo.SetStrAttr(v, data.Value(k))
}
return userInfo
},
})
}
// NewBattlenet makes Battle.net oauth2 provider
func NewBattlenet(p Params) Oauth2Handler {
return initOauth2Handler(p, Oauth2Handler{
name: "battlenet",
endpoint: oauth2.Endpoint{
AuthURL: "https://eu.battle.net/oauth/authorize",
TokenURL: "https://eu.battle.net/oauth/token",
AuthStyle: oauth2.AuthStyleInParams,
},
scopes: []string{},
infoURL: "https://eu.battle.net/oauth/userinfo",
mapUser: func(data UserData, _ []byte) token.User {
userInfo := token.User{
ID: "battlenet_" + token.HashID(sha1.New(), data.Value("id")),
Name: data.Value("battletag"),
}
for k, v := range p.UserAttributes {
userInfo.SetStrAttr(v, data.Value(k))
}
return userInfo
},
})
}
// NewMicrosoft makes microsoft azure oauth2 provider
func NewMicrosoft(p Params) Oauth2Handler {
return initOauth2Handler(p, Oauth2Handler{
name: "microsoft",
endpoint: microsoft.AzureADEndpoint("common"),
scopes: []string{"User.Read"},
infoURL: "https://graph.microsoft.com/v1.0/me",
// non-beta doesn't provide photo for consumers yet
// see https://github.com/microsoftgraph/microsoft-graph-docs/issues/3990
mapUser: func(data UserData, _ []byte) token.User {
userInfo := token.User{
ID: "microsoft_" + token.HashID(sha1.New(), data.Value("id")),
Name: data.Value("displayName"),
Picture: "https://graph.microsoft.com/beta/me/photo/$value",
}
for k, v := range p.UserAttributes {
userInfo.SetStrAttr(v, data.Value(k))
}
return userInfo
},
})
}
// NewPatreon makes patreon oauth2 provider
func NewPatreon(p Params) Oauth2Handler {
type uinfo struct {
Data struct {
Attributes struct {
FullName string `json:"full_name"`
ImageURL string `json:"image_url"`
} `json:"attributes"`
ID string `json:"id"`
Relationships struct {
Pledges struct {
Data []struct {
ID string `json:"id"`
Type string `json:"type"`
} `json:"data"`
} `json:"pledges"`
} `json:"relationships"`
} `json:"data"`
}
return initOauth2Handler(p, Oauth2Handler{
name: "patreon",
// see https://docs.patreon.com/?shell#oauth
endpoint: oauth2.Endpoint{
AuthURL: "https://www.patreon.com/oauth2/authorize",
TokenURL: "https://api.patreon.com/oauth2/token",
AuthStyle: oauth2.AuthStyleInParams,
},
scopes: []string{},
infoURL: "https://www.patreon.com/api/oauth2/api/current_user",
mapUser: func(data UserData, bdata []byte) token.User {
userInfo := token.User{}
uinfoJSON := uinfo{}
if err := json.Unmarshal(bdata, &uinfoJSON); err == nil {
userInfo.ID = "patreon_" + token.HashID(sha1.New(), userInfo.ID)
userInfo.Name = uinfoJSON.Data.Attributes.FullName
userInfo.Picture = uinfoJSON.Data.Attributes.ImageURL
// check if the user is your subscriber
if len(uinfoJSON.Data.Relationships.Pledges.Data) > 0 {
userInfo.SetPaidSub(true)
}
}
for k, v := range p.UserAttributes {
userInfo.SetStrAttr(v, data.Value(k))
}
return userInfo
},
})
}
// NewDiscord makes discord oauth2 provider
func NewDiscord(p Params) Oauth2Handler {
return initOauth2Handler(p, Oauth2Handler{
name: "discord",
// see https://discord.com/developers/docs/topics/oauth2
endpoint: oauth2.Endpoint{
AuthURL: "https://discord.com/oauth2/authorize",
TokenURL: "https://discord.com/api/oauth2/token",
},
infoURL: "https://discord.com/api/v10/users/@me",
scopes: []string{"identify"},
mapUser: func(data UserData, _ []byte) token.User {
userInfo := token.User{
ID: "discord_" + token.HashID(sha1.New(), data.Value("id")),
Name: data.Value("username"),
Picture: fmt.Sprintf("https://cdn.discordapp.com/avatars/%s/%s.webp", data.Value("id"), data.Value("avatar")),
}
for k, v := range p.UserAttributes {
userInfo.SetStrAttr(v, data.Value(k))
}
return userInfo
},
})
}