@@ -17,9 +17,11 @@ import (
17
17
"golang.org/x/oauth2"
18
18
"golang.org/x/xerrors"
19
19
20
+ "github.com/coder/coder/coderd/authzquery"
20
21
"github.com/coder/coder/coderd/database"
21
22
"github.com/coder/coder/coderd/httpapi"
22
23
"github.com/coder/coder/coderd/httpmw"
24
+ "github.com/coder/coder/coderd/rbac"
23
25
"github.com/coder/coder/codersdk"
24
26
)
25
27
@@ -425,8 +427,9 @@ func (e httpError) Error() string {
425
427
426
428
func (api * API ) oauthLogin (r * http.Request , params oauthLoginParams ) (* http.Cookie , error ) {
427
429
var (
428
- ctx = r .Context ()
429
- user database.User
430
+ ctx = r .Context ()
431
+ systemCtx = authzquery .WithAuthorizeSystemContext (ctx , rbac .RolesAdminSystem ())
432
+ user database.User
430
433
)
431
434
432
435
err := api .Database .InTx (func (tx database.Store ) error {
@@ -435,7 +438,7 @@ func (api *API) oauthLogin(r *http.Request, params oauthLoginParams) (*http.Cook
435
438
err error
436
439
)
437
440
438
- user , link , err = findLinkedUser (ctx , tx , params .LinkedID , params .Email )
441
+ user , link , err = findLinkedUser (systemCtx , tx , params .LinkedID , params .Email )
439
442
if err != nil {
440
443
return xerrors .Errorf ("find linked user: %w" , err )
441
444
}
@@ -461,15 +464,15 @@ func (api *API) oauthLogin(r *http.Request, params oauthLoginParams) (*http.Cook
461
464
// with OIDC for the first time.
462
465
if user .ID == uuid .Nil {
463
466
var organizationID uuid.UUID
464
- organizations , _ := tx .GetOrganizations (ctx )
467
+ organizations , _ := tx .GetOrganizations (systemCtx )
465
468
if len (organizations ) > 0 {
466
469
// Add the user to the first organization. Once multi-organization
467
470
// support is added, we should enable a configuration map of user
468
471
// email to organization.
469
472
organizationID = organizations [0 ].ID
470
473
}
471
474
472
- _ , err := tx .GetUserByEmailOrUsername (ctx , database.GetUserByEmailOrUsernameParams {
475
+ _ , err := tx .GetUserByEmailOrUsername (systemCtx , database.GetUserByEmailOrUsernameParams {
473
476
Username : params .Username ,
474
477
})
475
478
if err == nil {
@@ -482,7 +485,7 @@ func (api *API) oauthLogin(r *http.Request, params oauthLoginParams) (*http.Cook
482
485
483
486
params .Username = httpapi .UsernameFrom (alternate )
484
487
485
- _ , err := tx .GetUserByEmailOrUsername (ctx , database.GetUserByEmailOrUsernameParams {
488
+ _ , err := tx .GetUserByEmailOrUsername (systemCtx , database.GetUserByEmailOrUsernameParams {
486
489
Username : params .Username ,
487
490
})
488
491
if xerrors .Is (err , sql .ErrNoRows ) {
@@ -501,7 +504,7 @@ func (api *API) oauthLogin(r *http.Request, params oauthLoginParams) (*http.Cook
501
504
}
502
505
}
503
506
504
- user , _ , err = api .CreateUser (ctx , tx , CreateUserRequest {
507
+ user , _ , err = api .CreateUser (systemCtx , tx , CreateUserRequest {
505
508
CreateUserRequest : codersdk.CreateUserRequest {
506
509
Email : params .Email ,
507
510
Username : params .Username ,
@@ -515,7 +518,7 @@ func (api *API) oauthLogin(r *http.Request, params oauthLoginParams) (*http.Cook
515
518
}
516
519
517
520
if link .UserID == uuid .Nil {
518
- link , err = tx .InsertUserLink (ctx , database.InsertUserLinkParams {
521
+ link , err = tx .InsertUserLink (systemCtx , database.InsertUserLinkParams {
519
522
UserID : user .ID ,
520
523
LoginType : params .LoginType ,
521
524
LinkedID : params .LinkedID ,
@@ -534,7 +537,7 @@ func (api *API) oauthLogin(r *http.Request, params oauthLoginParams) (*http.Cook
534
537
// The migration that added the user_links table could not populate
535
538
// the 'linked_id' field since it requires fields off the access token.
536
539
if link .LinkedID == "" {
537
- link , err = tx .UpdateUserLinkedID (ctx , database.UpdateUserLinkedIDParams {
540
+ link , err = tx .UpdateUserLinkedID (systemCtx , database.UpdateUserLinkedIDParams {
538
541
UserID : user .ID ,
539
542
LoginType : params .LoginType ,
540
543
LinkedID : params .LinkedID ,
@@ -545,7 +548,7 @@ func (api *API) oauthLogin(r *http.Request, params oauthLoginParams) (*http.Cook
545
548
}
546
549
547
550
if link .UserID != uuid .Nil {
548
- link , err = tx .UpdateUserLink (ctx , database.UpdateUserLinkParams {
551
+ link , err = tx .UpdateUserLink (systemCtx , database.UpdateUserLinkParams {
549
552
UserID : user .ID ,
550
553
LoginType : params .LoginType ,
551
554
OAuthAccessToken : params .State .Token .AccessToken ,
@@ -584,7 +587,7 @@ func (api *API) oauthLogin(r *http.Request, params oauthLoginParams) (*http.Cook
584
587
// In such cases in the current implementation this user can now no
585
588
// longer sign in until an administrator finds the offending built-in
586
589
// user and changes their username.
587
- user , err = tx .UpdateUserProfile (ctx , database.UpdateUserProfileParams {
590
+ user , err = tx .UpdateUserProfile (systemCtx , database.UpdateUserProfileParams {
588
591
ID : user .ID ,
589
592
Email : user .Email ,
590
593
Username : user .Username ,
@@ -602,7 +605,7 @@ func (api *API) oauthLogin(r *http.Request, params oauthLoginParams) (*http.Cook
602
605
return nil , xerrors .Errorf ("in tx: %w" , err )
603
606
}
604
607
605
- cookie , err := api .createAPIKey (ctx , createAPIKeyParams {
608
+ cookie , err := api .createAPIKey (systemCtx , createAPIKeyParams {
606
609
UserID : user .ID ,
607
610
LoginType : params .LoginType ,
608
611
RemoteAddr : r .RemoteAddr ,
0 commit comments