-
Notifications
You must be signed in to change notification settings - Fork 0
/
oauth2.go
312 lines (285 loc) · 9.73 KB
/
oauth2.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package lockbox
import (
"bytes"
"context"
"errors"
"fmt"
"net/http"
"net/url"
"path"
"strings"
)
const (
oauth2ServiceDefaultBasePath = "/oauth2/v1/"
oauth2ServerError = "server_error"
oauth2InvalidRequestError = "invalid_request"
oauth2InvalidGrantError = "invalid_grant"
oauth2InvalidClientError = "invalid_client"
oauth2UnsupportedResponseTypeError = "unsupported_response_type"
)
var (
// ErrOAuth2RequestMissingEmail is returned when a request requires an
// email, but none was set.
ErrOAuth2RequestMissingEmail = errors.New("email must be set")
// ErrOAuth2RequestMissingCode is returned when a request requires a
// code, but none was set.
ErrOAuth2RequestMissingCode = errors.New("code must be set")
// ErrOAuth2RequestMissingToken is returned when a request requires a
// token, but none is set.
ErrOAuth2RequestMissingToken = errors.New("token must be set")
// ErrInvalidGrantError is returned when a grant that is invalid, for
// any reason, is used.
ErrInvalidGrantError = errors.New("invalid grant")
// ErrInvalidClientCredentialsError is returned when the client
// credentials that were presented were invalid, for any reason.
ErrInvalidClientCredentialsError = errors.New("invalid client credentials")
// ErrUnsupportedResponseTypeError is returned when the server doesn't
// recognize the response type that go-lockbox is requesting.
ErrUnsupportedResponseTypeError = errors.New("unsupported response type; this is either a server or go-lockbox error")
// ErrUnexpectedBody is returned if a response body is returned for a
// request that doesn't expect a response.
ErrUnexpectedBody = errors.New("unexpected body; this request shouldn't return a body, but it did, possibly an error we didn't expect or couldn't parse. This is either a server or go-lockbox error")
)
// OAuth2Service is the oauth2 service. Set the BasePath to modify where
// requests will be sent relative to the Client's base URL. OAuth2Service
// should only be instantiated by calling NewClient.
type OAuth2Service struct {
BasePath string
client *Client
}
func (o OAuth2Service) buildURL(p string) string {
return path.Join(o.BasePath, p)
}
// OAuth2Response is used to represent all the information returned in the body
// of the response to an OAuth2 request.
type OAuth2Response struct {
AccessToken string `json:"access_token,omitempty"`
TokenType string `json:"token_type,omitempty"`
ExpiresIn int `json:"expires_in,omitempty"`
RefreshToken string `json:"refresh_token,omitempty"`
Scope string `json:"scope,omitempty"`
Error string `json:"error,omitempty"`
}
// ExchangeRefreshToken uses the refresh token passed to obtain a new access
// token and refresh token from the oauth2 service.
func (o OAuth2Service) ExchangeRefreshToken(ctx context.Context, token string, scopes []string) (OAuth2Response, error) {
if token == "" {
return OAuth2Response{}, ErrOAuth2RequestMissingToken
}
vals := url.Values{}
vals.Set("grant_type", "refresh_token")
vals.Set("refresh_token", token)
if len(scopes) > 0 {
vals.Set("scope", strings.Join(scopes, " "))
}
buf := bytes.NewBuffer([]byte(vals.Encode()))
req, err := o.client.NewRequest(ctx, http.MethodPost, o.buildURL("/token"), buf)
if err != nil {
return OAuth2Response{}, fmt.Errorf("error constructing request: %w", err)
}
err = o.client.AddClientCredentials(req)
if err != nil {
return OAuth2Response{}, err
}
res, err := o.client.Do(req)
if err != nil {
return OAuth2Response{}, fmt.Errorf("error making request: %w", err)
}
resp, err := oauthResponse(res)
if err != nil {
return OAuth2Response{}, err
}
if res.StatusCode == http.StatusInternalServerError &&
resp.Error == oauth2ServerError {
return resp, ErrServerError
}
if res.StatusCode == http.StatusUnauthorized &&
resp.Error == oauth2InvalidClientError {
return resp, ErrInvalidClientCredentialsError
}
if res.StatusCode == http.StatusBadRequest &&
resp.Error == oauth2InvalidRequestError {
return resp, ErrInvalidRequestError
}
if res.StatusCode == http.StatusBadRequest &&
resp.Error == oauth2InvalidGrantError {
return resp, ErrInvalidGrantError
}
if res.StatusCode == http.StatusBadRequest &&
resp.Error == oauth2UnsupportedResponseTypeError {
return resp, ErrUnsupportedResponseTypeError
}
if resp.Error != "" {
return resp, ErrUnexpectedError
}
return resp, nil
}
// ExchangeGoogleIDToken uses the passed ID token from Google to obtain a new
// access token and refresh token from the oauth2 service.
func (o OAuth2Service) ExchangeGoogleIDToken(ctx context.Context, token string, scopes []string) (OAuth2Response, error) {
if token == "" {
return OAuth2Response{}, ErrOAuth2RequestMissingToken
}
vals := url.Values{}
vals.Set("grant_type", "google_id")
vals.Set("id_token", token)
if len(scopes) > 0 {
vals.Set("scope", strings.Join(scopes, " "))
}
buf := bytes.NewBuffer([]byte(vals.Encode()))
req, err := o.client.NewRequest(ctx, http.MethodPost, o.buildURL("/token"), buf)
if err != nil {
return OAuth2Response{}, fmt.Errorf("error constructing request: %w", err)
}
err = o.client.AddClientCredentials(req)
if err != nil {
return OAuth2Response{}, err
}
res, err := o.client.Do(req)
if err != nil {
return OAuth2Response{}, fmt.Errorf("error making request: %w", err)
}
resp, err := oauthResponse(res)
if err != nil {
return OAuth2Response{}, err
}
if res.StatusCode == http.StatusInternalServerError &&
resp.Error == oauth2ServerError {
return resp, ErrServerError
}
if res.StatusCode == http.StatusUnauthorized &&
resp.Error == oauth2InvalidClientError {
return resp, ErrInvalidClientCredentialsError
}
if res.StatusCode == http.StatusBadRequest &&
resp.Error == oauth2InvalidRequestError {
return resp, ErrInvalidRequestError
}
if res.StatusCode == http.StatusBadRequest &&
resp.Error == oauth2InvalidGrantError {
return resp, ErrInvalidGrantError
}
if res.StatusCode == http.StatusBadRequest &&
resp.Error == oauth2UnsupportedResponseTypeError {
return resp, ErrUnsupportedResponseTypeError
}
if resp.Error != "" {
return resp, ErrUnexpectedError
}
return resp, nil
}
// SendEmail requests an authentication email be sent to the specified email
// address to kick off the email login flow.
func (o OAuth2Service) SendEmail(ctx context.Context, email string, scopes []string) error {
if email == "" {
return ErrOAuth2RequestMissingEmail
}
vals := url.Values{}
vals.Set("response_type", "email")
vals.Set("email", email)
if len(scopes) > 0 {
vals.Set("scope", strings.Join(scopes, " "))
}
buf := bytes.NewBuffer([]byte(vals.Encode()))
req, err := o.client.NewRequest(ctx, http.MethodPost, o.buildURL("/authorize"), buf)
if err != nil {
return fmt.Errorf("error constructing request: %w", err)
}
err = o.client.AddClientCredentials(req)
if err != nil {
return err
}
res, err := o.client.Do(req)
if err != nil {
return fmt.Errorf("error making request: %w", err)
}
// if we get a StatusNoContent response, everything went fine
// we can bail now
if res.StatusCode == http.StatusNoContent {
return nil
}
// if we get any other response, we need to decode the response
// and figure out what went wrong so we can surface a useful error
resp, err := oauthResponse(res)
if err != nil {
return err
}
if res.StatusCode == http.StatusInternalServerError &&
resp.Error == oauth2ServerError {
return ErrServerError
}
if res.StatusCode == http.StatusUnauthorized &&
resp.Error == oauth2InvalidClientError {
return ErrInvalidClientCredentialsError
}
if res.StatusCode == http.StatusBadRequest &&
resp.Error == oauth2InvalidRequestError {
return ErrInvalidRequestError
}
if res.StatusCode == http.StatusBadRequest &&
resp.Error == oauth2InvalidGrantError {
return ErrInvalidGrantError
}
if res.StatusCode == http.StatusBadRequest &&
resp.Error == oauth2UnsupportedResponseTypeError {
return ErrUnsupportedResponseTypeError
}
if resp.Error != "" {
return ErrUnexpectedError
}
return ErrUnexpectedBody
}
// ExchangeEmailCode uses the passed code, obtained from an authentication
// email, to obtain a new access token and refresh token from the oauth2
// service.
func (o OAuth2Service) ExchangeEmailCode(ctx context.Context, code string) (OAuth2Response, error) {
if code == "" {
return OAuth2Response{}, ErrOAuth2RequestMissingCode
}
v := url.Values{}
v.Set("grant_type", "email")
v.Set("code", code)
buf := bytes.NewBuffer([]byte(v.Encode()))
req, err := o.client.NewRequest(ctx, http.MethodPost, o.buildURL("/token"), buf)
if err != nil {
return OAuth2Response{}, fmt.Errorf("error constructing request: %w", err)
}
err = o.client.AddClientCredentials(req)
if err != nil {
return OAuth2Response{}, err
}
res, err := o.client.Do(req)
if err != nil {
return OAuth2Response{}, fmt.Errorf("error making request: %w", err)
}
// if we get any other response, we need to decode the response
// and figure out what went wrong so we can surface a useful error
resp, err := oauthResponse(res)
if err != nil {
return OAuth2Response{}, err
}
if res.StatusCode == http.StatusInternalServerError &&
resp.Error == oauth2ServerError {
return resp, ErrServerError
}
if res.StatusCode == http.StatusUnauthorized &&
resp.Error == oauth2InvalidClientError {
return resp, ErrInvalidClientCredentialsError
}
if res.StatusCode == http.StatusBadRequest &&
resp.Error == oauth2InvalidRequestError {
return resp, ErrInvalidRequestError
}
if res.StatusCode == http.StatusBadRequest &&
resp.Error == oauth2InvalidGrantError {
return resp, ErrInvalidGrantError
}
if res.StatusCode == http.StatusBadRequest &&
resp.Error == oauth2UnsupportedResponseTypeError {
return resp, ErrUnsupportedResponseTypeError
}
if resp.Error != "" {
return resp, ErrUnexpectedError
}
return resp, nil
}