/
provider_google.go
62 lines (52 loc) · 1.55 KB
/
provider_google.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
package oauth2providers
import (
"context"
"golang.org/x/oauth2/google"
)
type googleProvider struct {
ProviderConfig
}
func (p *googleProvider) authCodeURL(state string, opts ...AuthCodeOption) string {
return p.AuthCodeURL(state, opts...)
}
func (p *googleProvider) exchange(ctx context.Context, code string, opts ...AuthCodeOption) (*Token, error) {
return p.Exchange(ctx, code, opts...)
}
func (p *googleProvider) refreshToken(ctx context.Context, token *Token) (*Token, error) {
return p.TokenSource(ctx, token).Token()
}
func newGoogleProvider(config ProviderConfig) *googleProvider {
// google endpoint
config.setAuthURL(google.Endpoint.AuthURL)
config.setTokenURL(google.Endpoint.TokenURL)
config.addScopes("https://www.googleapis.com/auth/userinfo.email", "https://www.googleapis.com/auth/userinfo.profile")
return &googleProvider{
ProviderConfig: config,
}
}
func (p *googleProvider) getUserInfo(ctx context.Context, token *Token) (UserInfo, error) {
client := p.Client(ctx, token)
resp, err := client.Get("https://www.googleapis.com/oauth2/v3/userinfo")
if err != nil {
return nil, err
}
defer resp.Body.Close()
user, err := parseJSONFromReader(resp.Body)
if err != nil {
return nil, err
}
userInfo := NewUserInfo()
if id, ok := user["sub"]; ok {
userInfo.setID(id.(string))
}
if name, ok := user["name"]; ok {
userInfo.setName(name.(string))
}
if email, ok := user["email"]; ok {
userInfo.setEmail(email.(string))
}
if picture, ok := user["picture"]; ok {
userInfo.setPictureURL(picture.(string))
}
return userInfo, nil
}