This repository has been archived by the owner on Mar 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
745 lines (671 loc) · 23.9 KB
/
handlers.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
package oauth2
import (
"context"
"encoding/json"
"errors"
"fmt"
"mime"
"net/http"
"net/url"
"strconv"
"strings"
"time"
"github.com/coreos/go-oidc/v3/oidc"
yall "yall.in"
"lockbox.dev/accounts"
"lockbox.dev/clients"
"lockbox.dev/grants"
"lockbox.dev/scopes"
"lockbox.dev/sessions"
"lockbox.dev/tokens"
)
type responseMethod int
const (
rmRedirect responseMethod = iota
rmOOB
)
var (
serverError = APIError{Error: "server_error", Code: http.StatusInternalServerError}
invalidGrantError = APIError{Error: "invalid_grant", Code: http.StatusBadRequest}
invalidRequestError = APIError{Error: "invalid_request", Code: http.StatusBadRequest}
errInvalidClientURI = errors.New("invalid URI")
)
// Service contains all the dependencies needed to complete the authentication
// and authorization flow.
type Service struct {
GoogleIDVerifier *oidc.IDTokenVerifier
GoogleClients []string
TokenExpiresIn int
Accounts accounts.Dependencies
Clients clients.Storer
Grants grants.Dependencies
Refresh tokens.Dependencies
Scopes scopes.Dependencies
Sessions sessions.Dependencies
Log *yall.Logger
Emailer emailer
}
// APIError is an error response that should be returned to the end user. It
// generally isn't meant for programmatic inspection.
type APIError struct {
Error string `json:"error"`
Code int `json:"-"`
}
// IsZero returns true if the APIError should be considered empty or the zero
// value.
func (a APIError) IsZero() bool {
return a.Error == ""
}
// granter is a generalization of all the ways a user can grant access
// to their account. It needs to be validatable, needs to create a grant,
// can optionally run a function when the grant is used, and must return
// whether it's a redirect flow or not.
type granter interface {
// if the grant isn't valid, return an error.
Validate(ctx context.Context) APIError
// return the ID of the profile the Grant is for.
ProfileID(ctx context.Context) string
// return the ID of the account the Grant is for.
AccountID(ctx context.Context) string
// populate a grant with the specified scopes
Grant(ctx context.Context, requestedScopes []string) grants.Grant
// optionally perform some action when the grant is used
Granted(ctx context.Context) error
// whether the grant type is a redirect flow or should return
// results as JSON
Redirects() bool
// whether the grant type creates and uses a grant immediately
// within the same request, or if a grant will be passed in
// as part of the request
CreatesGrantsInline() bool
}
type grantCreator interface {
GetAccount(ctx context.Context) (accounts.Account, APIError)
FillGrant(ctx context.Context, account accounts.Account, requestedScopes []string) (grants.Grant, APIError)
ResponseMethod() responseMethod
HandleOOBGrant(ctx context.Context, grant grants.Grant) error
}
// populate a url.Values with an APIError, so we can use it
// to generate a query string with the error included.
func errAsQueryParams(apiErr APIError) url.Values {
return url.Values{
"error": []string{apiErr.Error},
}
}
// populate a url.Values with the values from a Token, so we
// can use it to generate a query string with the token included.
func tokenAsQueryParams(token Token) url.Values {
return url.Values{
"access_token": []string{token.AccessToken},
"token_type": []string{token.TokenType},
"expires_in": []string{strconv.Itoa(token.ExpiresIn)},
"scope": []string{token.Scope},
}
}
// populate a url.Values with the values from a Grant, so we
// can use it to generate a query string with the grant included.
func grantAsQueryParams(grant grants.Grant) url.Values {
return url.Values{
"code": []string{grant.SourceID},
"code_type": []string{grant.SourceType},
}
}
// merge multiple url.Values into a single url.Values, overwriting
// values from lower-indexed url.Values if multiple url.Values have
// the same key set.
func mergeQueryParams(paramSets ...url.Values) url.Values {
query := url.Values{}
for _, params := range paramSets {
for k, v := range params {
query[k] = append(query[k], v...)
}
}
return query
}
// check that we're using a supported content type
func isContentType(ctx context.Context, r *http.Request, contentTypes ...string) bool {
contentTypeHeader := r.Header.Get("Content-type")
if contentTypeHeader == "" {
contentTypeHeader = "application/octet-stream"
yall.FromContext(ctx).Debug("no content-type header set, assuming application/octet-stream")
}
for _, v := range strings.Split(contentTypeHeader, ",") {
contentType, _, err := mime.ParseMediaType(v)
if err != nil {
yall.FromContext(ctx).WithError(err).WithField("content_type", v).WithField("content_type_header", contentTypeHeader).Debug("error parsing media type")
break
}
for _, candidate := range contentTypes {
if contentType == candidate {
yall.FromContext(ctx).WithField("content_type", candidate).Debug("suitable content-type header found")
return true
}
}
}
return false
}
//nolint: contextcheck // this is a false positive, we're actually chaining contexts correctly
func (s Service) redirectError(w http.ResponseWriter, r *http.Request, apiErr APIError, redirBase string) {
if redirBase == "" {
yall.FromContext(r.Context()).Error("redirecting but no redirectURL set")
s.renderError(w, r, apiErr)
return
}
redirectURL, err := url.Parse(redirBase)
if err != nil {
yall.FromContext(r.Context()).WithError(err).WithField("url", redirBase).Error("Error parsing redirect URL")
s.renderError(w, r, apiErr)
return
}
// add our new query params to any the URL may have had
q := mergeQueryParams(redirectURL.Query(), errAsQueryParams(apiErr))
// build the query back up as part of the URL
redirectURL.RawQuery = q.Encode()
http.Redirect(w, r, redirectURL.String(), http.StatusFound)
}
//nolint: contextcheck // this is a false positive, we're actually chaining contexts correctly
func (Service) renderError(w http.ResponseWriter, r *http.Request, apiErr APIError) {
w.Header().Set("content-type", "application/json")
w.WriteHeader(apiErr.Code)
enc := json.NewEncoder(w)
if err := enc.Encode(apiErr); err != nil {
yall.FromContext(r.Context()).WithError(err).Error("Error writing response")
}
}
func (s Service) redirectToken(w http.ResponseWriter, r *http.Request, token Token, redirBase string) {
redirectURL, err := url.Parse(redirBase)
if err != nil {
yall.FromContext(r.Context()).WithError(err).WithField("url", redirBase).Error("Error parsing redirect URL")
s.renderError(w, r, invalidRequestError)
return
}
// add our new query params to any the URL may have had
q := mergeQueryParams(redirectURL.Query(), tokenAsQueryParams(token))
// build the query back up as part of the URL
redirectURL.RawQuery = q.Encode()
http.Redirect(w, r, redirectURL.String(), http.StatusFound)
}
func (Service) renderToken(w http.ResponseWriter, r *http.Request, token Token) {
w.Header().Set("content-type", "application/json")
w.WriteHeader(http.StatusOK)
enc := json.NewEncoder(w)
if err := enc.Encode(token); err != nil {
yall.FromContext(r.Context()).WithError(err).Error("Error writing response")
}
}
func (s Service) redirectGrant(w http.ResponseWriter, r *http.Request, grant grants.Grant, redirBase string) {
redirectURL, err := url.Parse(redirBase)
if err != nil {
yall.FromContext(r.Context()).WithError(err).WithField("url", redirBase).Error("Error parsing redirect URL")
s.renderError(w, r, invalidRequestError)
return
}
// add our new query params to any the URL may have had
q := mergeQueryParams(redirectURL.Query(), grantAsQueryParams(grant))
// build the query back up as part of the URL
redirectURL.RawQuery = q.Encode()
http.Redirect(w, r, redirectURL.String(), http.StatusFound)
}
func (Service) returnGrantOOB(w http.ResponseWriter) {
w.WriteHeader(http.StatusNoContent)
}
// pull the client credentials out of the request.
func getClientCredentials(r *http.Request) (id, secret, redirect string) {
id = r.URL.Query().Get("client_id")
redirect = r.URL.Query().Get("redirect_uri")
if id != "" {
return id, secret, redirect
}
redirect = ""
var ok bool
id, secret, ok = r.BasicAuth()
if ok {
return id, secret, redirect
}
id = r.PostFormValue("client_id")
secret = r.PostFormValue("client_secret")
return id, secret, redirect
}
// check that the client credentials are valid
func (s Service) validateClientCredentials(ctx context.Context, clientID, clientSecret, redirectURI string) APIError {
log := yall.FromContext(ctx)
if clientID == "" {
log.Debug("no client ID specified")
return APIError{Code: http.StatusUnauthorized, Error: "invalid_client"}
}
if clientSecret != "" && redirectURI != "" {
log.Debug("client secret and redirect URI both set")
return APIError{Code: http.StatusUnauthorized, Error: "invalid_client"}
}
client, err := s.Clients.Get(ctx, clientID)
if err != nil {
if errors.Is(err, clients.ErrClientNotFound) {
log.Debug("client not found")
return APIError{Code: http.StatusUnauthorized, Error: "invalid_client"}
}
log.WithError(err).Error("error retrieving client")
return serverError
}
if redirectURI == "" {
err = client.CheckSecret(clientSecret)
if err != nil {
if errors.Is(err, clients.ErrIncorrectSecret) {
log.Debug("incorrect client secret")
return APIError{Code: http.StatusUnauthorized, Error: "invalid_client"}
}
log.WithError(err).Error("error checking client secret")
return serverError
}
}
return APIError{}
}
// find which scopes should be used for a client
// if none are passed in, a default set for the client is used
// if one or more are passed in, scopes the client and account can't use are stripped
func (s Service) checkScopes(ctx context.Context, clientID, profileID, accountID string, ids []string) ([]string, APIError) { //nolint:revive // this needs refactored, I know
var permittedScopes []scopes.Scope
var err error
if len(ids) < 1 {
permittedScopes, err = s.Scopes.Storer.ListDefault(ctx)
if err != nil {
return nil, serverError
}
} else {
resp, err := s.Scopes.Storer.GetMulti(ctx, ids)
if err != nil {
return nil, serverError
}
if len(resp) != len(ids) {
yall.FromContext(ctx).WithField("scope_ids", ids).WithField("scope_results", resp).
Warn("fewer scopes than requested returned, one likely doesn't exist")
}
for _, v := range resp {
permittedScopes = append(permittedScopes, v)
}
}
permittedScopes = scopes.FilterByClientID(ctx, permittedScopes, clientID)
permittedScopes = scopes.FilterByUserID(ctx, permittedScopes, accountID)
// TODO: filter scopes by profile ID
_ = profileID // gets rid of the revive error for not using the argument
results := make([]string, 0, len(permittedScopes))
for _, scope := range permittedScopes {
results = append(results, scope.ID)
}
return results, APIError{}
}
// determine what redirect URI to use for the client
func (s Service) getClientRedirectURI(ctx context.Context, clientID, passed string) (string, error) {
uris, err := s.Clients.ListRedirectURIs(ctx, clientID)
if err != nil {
return "", err
}
if passed == "" {
switch {
case len(uris) < 1:
return "", nil
case len(uris) > 1:
return "", fmt.Errorf("%w: client has multiple redirect URIs, none passed", errInvalidClientURI)
case len(uris) == 1:
return uris[0].URI, nil
}
}
switch {
case len(uris) >= 1:
for _, uri := range uris {
if passed == uri.URI {
return uri.URI, nil
}
}
return "", fmt.Errorf("%w: passed URI not a client URI", errInvalidClientURI)
case len(uris) < 1:
return "", fmt.Errorf("%w: client has no URIs, but one was passed", errInvalidClientURI)
}
return "", fmt.Errorf("%w: impossible error", errInvalidClientURI)
}
// create a grant in the Storer
func (s Service) createGrant(ctx context.Context, grant grants.Grant) (grants.Grant, APIError) {
grant, err := grants.FillGrantDefaults(grant)
if err != nil {
yall.FromContext(ctx).WithError(err).Error("Error filling grant defaults")
return grant, serverError
}
err = s.Grants.Storer.CreateGrant(ctx, grant)
if err != nil {
if errors.Is(err, grants.ErrGrantSourceAlreadyUsed) {
yall.FromContext(ctx).Debug("grant source already used")
return grant, invalidGrantError
}
yall.FromContext(ctx).WithError(err).WithField("grant", grant).Error("Error creating grant")
return grant, serverError
}
return grant, APIError{}
}
// determine which type of grant is being used based on the query params
func (s Service) getGranter(values url.Values, clientID string) granter { //nolint:ireturn // the point of the method is to pick an interface implementation
switch values.Get("grant_type") {
case "refresh_token":
return &refreshTokenGranter{
tokenVal: values.Get("refresh_token"),
client: clientID,
}
case "google_id":
return &googleIDGranter{
tokenVal: values.Get("id_token"),
client: clientID,
gClients: s.GoogleClients,
oidcVerifier: s.GoogleIDVerifier,
accounts: s.Accounts.Storer,
}
case "email":
return &emailGranter{
code: values.Get("code"),
clientID: clientID,
grants: s.Grants.Storer,
}
}
return nil
}
func (s Service) getGrantCreator(values url.Values, clientID string) grantCreator { //nolint:ireturn // the point of the method is to pick an interface implementation
if values.Get("response_type") == "email" {
return &emailGrantCreator{
email: values.Get("email"),
client: clientID,
accounts: s.Accounts.Storer,
emailer: s.Emailer,
}
}
return nil
}
// handle the access token request endpoint
// this endpoint is used when the user is ready to trade an existing grant for an
// access token.
func (s Service) handleAccessTokenRequest(w http.ResponseWriter, r *http.Request) {
log := yall.FromContext(r.Context())
// check our content-type. r.ParseForm() only works if the header is
// set to application/x-www-form-urlencoded
if !isContentType(r.Context(), r, "application/x-www-form-urlencoded") {
log.WithField("content_type", r.Header.Get("Content-Type")).Debug("invalid content type")
s.renderError(w, r, APIError{
Error: "unsupported_content_type",
Code: http.StatusUnsupportedMediaType,
})
return
}
// explicitly parse the form, so we can handle the error
err := r.ParseForm()
if err != nil {
log.WithError(err).Error("Error parsing form")
s.renderError(w, r, invalidRequestError)
return
}
// figure out which client we're dealing with
clientID, clientSecret, redirectURI := getClientCredentials(r)
log = log.WithField("oauth2_client_id", clientID).WithField("redirect_uri_given", redirectURI)
// make sure the client is who they say they are
clientErr := s.validateClientCredentials(yall.InContext(r.Context(), log), clientID, clientSecret, redirectURI)
if !clientErr.IsZero() {
log.WithField("api_error", clientErr).Debug("Error validating client")
s.renderError(w, r, clientErr)
return
}
log.WithField("grant_type", r.PostForm.Get("grant_type"))
// figure out what type of grant we're dealing with
granter := s.getGranter(r.PostForm, clientID)
if granter == nil {
// if g is nil, that means it's not a match for our supported types
log.Debug("Unsupported grant type")
s.renderError(w, r, APIError{
Error: "unsupported_grant_type",
Code: http.StatusBadRequest,
})
return
}
// figure out which redirect URI to use for our client
redirectURI, err = s.getClientRedirectURI(yall.InContext(r.Context(), log), clientID, redirectURI)
if err != nil {
log.WithError(err).Error("Error determining redirect URI.")
s.renderError(w, r, serverError)
return
}
log = log.WithField("redirect_uri", redirectURI)
// validate the grant
apiErr := granter.Validate(yall.InContext(r.Context(), log))
if !apiErr.IsZero() {
log.WithField("error_code", apiErr.Code).WithField("error_type", apiErr.Error).Debug("Error validating grant")
if granter.Redirects() {
s.redirectError(w, r, apiErr, redirectURI)
return
}
s.renderError(w, r, apiErr)
return
}
// figure out what scopes we should be using
requestedScopes := strings.Split(r.FormValue("scope"), " ")
log = log.WithField("scopes_requested", requestedScopes)
logCtx := yall.InContext(r.Context(), log)
checkedScopes, apiErr := s.checkScopes(logCtx, clientID, granter.ProfileID(logCtx), granter.AccountID(logCtx), requestedScopes)
if !apiErr.IsZero() {
log.WithField("error_code", apiErr.Code).WithField("error_type", apiErr.Error).Debug("Error checking scopes")
if granter.Redirects() {
s.redirectError(w, r, apiErr, redirectURI)
return
}
s.renderError(w, r, apiErr)
return
}
log = log.WithField("scopes_filtered", checkedScopes)
// retrieve or fill out our grant fields
grant := granter.Grant(yall.InContext(r.Context(), log), checkedScopes)
// create and store our grant for granters
// that don't create their own
if granter.CreatesGrantsInline() {
// build our grant
grant.CreateIP = getIP(r)
// store the grant
grant, apiErr = s.createGrant(yall.InContext(r.Context(), log), grant)
if !apiErr.IsZero() {
log.WithField("error_code", apiErr.Code).WithField("error_type", apiErr.Error).Debug("Error creating grant")
if granter.Redirects() {
s.redirectError(w, r, apiErr, redirectURI)
return
}
s.renderError(w, r, apiErr)
return
}
log = log.WithField("grant", grant.ID)
log.Debug("created inline grant")
} else {
log = log.WithField("grant", grant.ID)
}
grant, err = s.Grants.Storer.ExchangeGrant(yall.InContext(r.Context(), log), grants.GrantUse{
Grant: grant.ID,
IP: getIP(r),
Time: time.Now(),
})
if errors.Is(err, grants.ErrGrantAlreadyUsed) {
log.Debug("grant reuse attempted")
if granter.Redirects() {
s.redirectError(w, r, APIError{Code: http.StatusBadRequest, Error: "invalid_grant"}, redirectURI)
return
}
s.renderError(w, r, APIError{Code: http.StatusBadRequest, Error: "invalid_grant"})
return
}
// our granter.Validate call *should* catch this, but to be safe...
if errors.Is(err, grants.ErrGrantNotFound) {
log.Debug("unknown grant presented")
if granter.Redirects() {
s.redirectError(w, r, APIError{Code: http.StatusBadRequest, Error: "invalid_grant"}, redirectURI)
return
}
s.renderError(w, r, APIError{Code: http.StatusBadRequest, Error: "invalid_grant"})
return
}
if err != nil {
log.WithError(err).Error("error exchanging grant")
if granter.Redirects() {
s.redirectError(w, r, serverError, redirectURI)
return
}
s.renderError(w, r, serverError)
return
}
log.Debug("exchanged grant")
// issue tokens using the grant
token, apiErr := s.issueTokens(yall.InContext(r.Context(), log), grant)
if !apiErr.IsZero() {
log.WithField("error_code", apiErr.Code).WithField("error_type", apiErr.Error).Debug("Error issuing tokens")
if granter.Redirects() {
s.redirectError(w, r, apiErr, redirectURI)
return
}
s.renderError(w, r, apiErr)
return
}
log.Debug("issued tokens")
// call any functionality that we need to to mark the grant as used
err = granter.Granted(yall.InContext(r.Context(), log))
if err != nil {
log.WithError(err).Error("Error marking grant as used")
if granter.Redirects() {
s.redirectError(w, r, serverError, redirectURI)
return
}
s.renderError(w, r, serverError)
return
}
log.Debug("marked grant as used")
// return our tokens
if granter.Redirects() {
s.redirectToken(w, r, token, redirectURI)
return
}
s.renderToken(w, r, token)
}
// handle the grant request endpoint
// this endpoint is used when the user wants to to start the authorization
// process, to generate a Grant that can be redeemed for a token.
func (s Service) handleGrantRequest(w http.ResponseWriter, r *http.Request) {
log := yall.FromContext(r.Context())
// check our content-type. r.ParseForm() only works if the header is
// set to application/x-www-form-urlencoded
if !isContentType(r.Context(), r, "application/x-www-form-urlencoded") {
log.WithField("content_type", r.Header.Get("Content-Type")).Debug("invalid content type")
s.renderError(w, r, APIError{
Error: "unsupported_content_type",
Code: http.StatusUnsupportedMediaType,
})
return
}
// explicitly parse the form, so we can handle the error
err := r.ParseForm()
if err != nil {
log.WithError(err).Error("Error parsing form")
s.renderError(w, r, invalidRequestError)
return
}
// figure out which client we're dealing with
clientID, clientSecret, redirectURI := getClientCredentials(r)
log = log.WithField("oauth2_client_id", clientID).WithField("redirect_uri_given", redirectURI)
// make sure the client is who they say they are
clientErr := s.validateClientCredentials(yall.InContext(r.Context(), log), clientID, clientSecret, redirectURI)
if !clientErr.IsZero() {
log.WithField("api_error", clientErr).Debug("Error validating client")
s.renderError(w, r, clientErr)
return
}
log = log.WithField("response_type", r.PostForm.Get("response_type"))
// figure out what type of grant the user is requesting
grantCreator := s.getGrantCreator(r.PostForm, clientID)
if grantCreator == nil {
// if g is nil, that means it's not a match for our supported types
log.Debug("Unsupported response type")
s.renderError(w, r, APIError{
Error: "unsupported_response_type",
Code: http.StatusBadRequest,
})
return
}
// figure out which redirect URI to use for our client
redirectURI, err = s.getClientRedirectURI(yall.InContext(r.Context(), log), clientID, redirectURI)
if err != nil {
log.WithError(err).Error("Error determining redirect URI.")
s.renderError(w, r, serverError)
return
}
log = log.WithField("redirect_uri", redirectURI)
// get the account we're creating a grant for
account, apiErr := grantCreator.GetAccount(yall.InContext(r.Context(), log))
if !apiErr.IsZero() {
log.WithField("error_code", apiErr.Code).WithField("error_type", apiErr.Error).Debug("error getting account")
if grantCreator.ResponseMethod() == rmRedirect {
s.redirectError(w, r, apiErr, redirectURI)
return
}
s.renderError(w, r, apiErr)
return
}
// figure out what scopes we should be using
var requestedScopes []string
for _, scope := range strings.Split(r.FormValue("scope"), " ") {
if strings.TrimSpace(scope) != "" {
requestedScopes = append(requestedScopes, strings.TrimSpace(scope))
}
}
log = log.WithField("scopes_requested", requestedScopes)
checkedScopes, apiErr := s.checkScopes(yall.InContext(r.Context(), log), clientID, account.ProfileID, account.ID, requestedScopes)
if !apiErr.IsZero() {
log.WithField("error_code", apiErr.Code).WithField("error_type", apiErr.Error).Debug("Error checking scopes")
if grantCreator.ResponseMethod() == rmRedirect {
s.redirectError(w, r, apiErr, redirectURI)
return
}
s.renderError(w, r, apiErr)
return
}
log = log.WithField("scopes_filtered", checkedScopes)
// fill out our grant fields
grant, apiErr := grantCreator.FillGrant(yall.InContext(r.Context(), log), account, checkedScopes)
if !apiErr.IsZero() {
if grantCreator.ResponseMethod() == rmRedirect {
s.redirectError(w, r, apiErr, redirectURI)
return
}
s.renderError(w, r, apiErr)
return
}
grant.CreateIP = getIP(r)
// store the grant
grant, apiErr = s.createGrant(yall.InContext(r.Context(), log), grant)
if !apiErr.IsZero() {
log.WithField("error_code", apiErr.Code).WithField("error_type", apiErr.Error).Debug("Error creating grant")
if grantCreator.ResponseMethod() == rmRedirect {
s.redirectError(w, r, apiErr, redirectURI)
return
}
s.renderError(w, r, apiErr)
return
}
log = log.WithField("grant", grant.ID)
log.Debug("created grant")
if grantCreator.ResponseMethod() == rmOOB {
err = grantCreator.HandleOOBGrant(yall.InContext(r.Context(), log), grant)
if err != nil {
log.WithError(err).Error("Error handling OOB grant")
s.renderError(w, r, serverError)
return
}
// fall through, s.returnGrant will write the correct status
// code for us and handle this situation correctly.
}
// return the grant
switch grantCreator.ResponseMethod() {
case rmOOB:
s.returnGrantOOB(w)
case rmRedirect:
s.redirectGrant(w, r, grant, redirectURI)
default:
log.WithField("response_method", grantCreator.ResponseMethod()).Error("unrecognized response_method")
s.renderError(w, r, serverError)
}
}