-
Notifications
You must be signed in to change notification settings - Fork 0
/
github.go
181 lines (152 loc) 路 4.35 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 github
import (
"context"
"encoding/json"
"io"
"net/http"
"strconv"
"github.com/zeiss/fiber-goth/adapters"
"github.com/zeiss/fiber-goth/providers"
"golang.org/x/oauth2"
"golang.org/x/oauth2/endpoints"
)
var _ providers.Provider = (*githubProvider)(nil)
var (
AuthURL = "https://github.com/login/oauth/authorize"
TokenURL = "https://github.com/login/oauth/access_token"
UserURL = "https://api.github.com/user"
EmailURL = "https://api.github.com/user/emails"
)
// DefaultScopes holds the default scopes used for GitHub.
var DefaultScopes = []string{"user:email", "read:user"}
type githubProvider struct {
id string
name string
clientKey string
secret string
callbackURL string
userURL string
emailURL string
authURL string
providerType providers.ProviderType
client *http.Client
config *oauth2.Config
providers.UnimplementedProvider
}
// New creates a new GitHub provider.
func New(clientKey, secret, callbackURL string, scopes ...string) *githubProvider {
p := &githubProvider{
id: "github",
name: "GitHub",
clientKey: clientKey,
secret: secret,
callbackURL: callbackURL,
userURL: UserURL,
emailURL: EmailURL,
authURL: AuthURL,
providerType: providers.ProviderTypeOAuth2,
client: providers.DefaultClient,
}
p.config = newConfig(p, scopes...)
return p
}
// ID returns the provider's ID.
func (g *githubProvider) ID() string {
return g.id
}
// Name returns the provider's name.
func (g *githubProvider) Name() string {
return g.name
}
// Type returns the provider's type.
func (g *githubProvider) Type() providers.ProviderType {
return g.providerType
}
type authIntent struct {
authURL string
}
// GetAuthURL returns the URL for the authentication end-point.
func (a *authIntent) GetAuthURL() (string, error) {
if a.authURL == "" {
return "", providers.ErrNoAuthURL
}
return a.authURL, nil
}
// BeginAuth starts the authentication process.
func (g *githubProvider) BeginAuth(ctx context.Context, adapter adapters.Adapter, state string) (providers.AuthIntent, error) {
verifier := oauth2.GenerateVerifier()
url := g.config.AuthCodeURL(state, oauth2.S256ChallengeOption(verifier))
return &authIntent{
authURL: url,
}, nil
}
// CompleteAuth completes the authentication process.
func (g *githubProvider) CompleteAuth(ctx context.Context, adapter adapters.Adapter, params providers.AuthParams) (adapters.GothUser, error) {
u := struct {
ID int `json:"id"`
Email string `json:"email"`
Bio string `json:"bio"`
Name string `json:"name"`
Login string `json:"login"`
Picture string `json:"avatar_url"`
Location string `json:"location"`
}{}
code := params.Get("code")
if code == "" {
return adapters.GothUser{}, adapters.ErrUnimplemented
}
token, err := g.config.Exchange(ctx, code)
if err != nil {
return adapters.GothUser{}, err
}
req, err := http.NewRequestWithContext(ctx, "GET", g.userURL, nil)
if err != nil {
return adapters.GothUser{}, err
}
req.Header.Add("Authorization", "Bearer "+token.AccessToken)
resp, err := g.client.Do(req)
if err != nil {
return adapters.GothUser{}, err
}
defer io.Copy(io.Discard, resp.Body) // equivalent to `cp body /dev/null`
defer resp.Body.Close()
err = json.NewDecoder(resp.Body).Decode(&u)
if err != nil {
return adapters.GothUser{}, err
}
user := adapters.GothUser{
Name: u.Name,
Email: u.Email,
Image: &u.Picture,
Accounts: []adapters.GothAccount{
{
Type: adapters.AccountTypeOAuth2,
Provider: g.ID(),
ProviderAccountID: adapters.StringPtr(strconv.Itoa(u.ID)),
AccessToken: adapters.StringPtr(token.AccessToken),
RefreshToken: adapters.StringPtr(token.RefreshToken),
ExpiresAt: adapters.TimePtr(token.Expiry),
SessionState: token.Extra("state").(string),
},
},
}
user, err = adapter.CreateUser(ctx, user)
if err != nil {
return adapters.GothUser{}, err
}
user, err = adapter.GetUser(ctx, user.ID)
if err != nil {
return adapters.GothUser{}, err
}
return user, nil
}
func newConfig(p *githubProvider, scopes ...string) *oauth2.Config {
c := &oauth2.Config{
ClientID: p.clientKey,
ClientSecret: p.secret,
RedirectURL: p.callbackURL,
Endpoint: endpoints.GitHub,
Scopes: append(DefaultScopes, scopes...),
}
return c
}