-
Notifications
You must be signed in to change notification settings - Fork 692
feat(token): add access_token grant for signing in with a Facebook access token #2609
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
spydon
wants to merge
1
commit into
master
Choose a base branch
from
feat/facebook-access-token-grant
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| package api | ||
|
|
||
| import ( | ||
| "context" | ||
| "net/http" | ||
|
|
||
| "golang.org/x/oauth2" | ||
|
|
||
| "github.com/supabase/auth/internal/api/apierrors" | ||
| "github.com/supabase/auth/internal/api/provider" | ||
| "github.com/supabase/auth/internal/metering" | ||
| "github.com/supabase/auth/internal/models" | ||
| "github.com/supabase/auth/internal/storage" | ||
| ) | ||
|
|
||
| // AccessTokenGrantParams are the parameters the AccessTokenGrant method accepts | ||
| type AccessTokenGrantParams struct { | ||
| Provider string `json:"provider"` | ||
| AccessToken string `json:"access_token"` | ||
| } | ||
|
|
||
| // AccessTokenGrant implements the access_token grant type flow, which allows | ||
| // signing in with a provider issued OAuth access token instead of an OIDC id | ||
| // token. | ||
| // | ||
| // It exists mainly for native Facebook logins on Android: the Facebook SDK | ||
| // reliably returns a classic Graph access token on every login, but only mints | ||
| // an OIDC id token (AuthenticationToken) on the first authorization, which | ||
| // makes the id_token grant unusable for repeat logins without falling back to | ||
| // the browser flow. | ||
| func (a *API) AccessTokenGrant(ctx context.Context, w http.ResponseWriter, r *http.Request) error { | ||
| db := a.db.WithContext(ctx) | ||
|
|
||
| params := &AccessTokenGrantParams{} | ||
| if err := retrieveRequestParams(r, params); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if params.AccessToken == "" { | ||
| return apierrors.NewOAuthError("invalid request", "access_token required") | ||
| } | ||
|
|
||
| if params.Provider == "" { | ||
| return apierrors.NewOAuthError("invalid request", "provider required") | ||
| } | ||
|
|
||
| oauthProvider, pConfig, err := a.OAuthProvider(ctx, params.Provider) | ||
| if err != nil { | ||
| return apierrors.NewBadRequestError(apierrors.ErrorCodeOAuthProviderNotSupported, "Unsupported provider: %q", params.Provider).WithInternalError(err) | ||
| } | ||
|
|
||
| if !pConfig.Enabled { | ||
| return apierrors.NewBadRequestError(apierrors.ErrorCodeProviderDisabled, "Provider (%q) is not enabled", params.Provider) | ||
| } | ||
|
|
||
| // Verifying that the access token was issued for this app is provider | ||
| // specific, so the grant is only available to providers that opt in. | ||
| verifier, ok := oauthProvider.(provider.AccessTokenVerifier) | ||
| if !ok { | ||
| return apierrors.NewBadRequestError(apierrors.ErrorCodeValidationFailed, "access_token grant is not supported for the %q provider", params.Provider) | ||
| } | ||
|
|
||
| if err := verifier.VerifyAccessToken(ctx, params.AccessToken); err != nil { | ||
| return apierrors.NewOAuthError("invalid request", "Invalid access token").WithInternalError(err) | ||
| } | ||
|
|
||
| userData, err := oauthProvider.GetUserData(ctx, &oauth2.Token{AccessToken: params.AccessToken}) | ||
| if err != nil { | ||
| return apierrors.NewOAuthError("invalid request", "Unable to fetch user data with the provided access token").WithInternalError(err) | ||
| } | ||
|
|
||
| userData.Metadata.EmailVerified = false | ||
| for _, email := range userData.Emails { | ||
| if email.Primary { | ||
| userData.Metadata.Email = email.Email | ||
| userData.Metadata.EmailVerified = email.Verified | ||
| break | ||
| } else { | ||
| userData.Metadata.Email = email.Email | ||
| userData.Metadata.EmailVerified = email.Verified | ||
| } | ||
| } | ||
|
|
||
| var grantParams models.GrantParams | ||
| grantParams.FillGrantParams(r) | ||
|
|
||
| if err := a.triggerBeforeUserCreatedExternal(r, db, userData, params.Provider); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| var createdUser bool | ||
| var token *AccessTokenResponse | ||
| var user *models.User | ||
| if err := db.Transaction(func(tx *storage.Connection) error { | ||
| var terr error | ||
|
|
||
| var decision models.AccountLinkingDecision | ||
| decision, user, terr = a.createAccountFromExternalIdentity(tx, r, userData, params.Provider, pConfig.EmailOptional) | ||
| if terr != nil { | ||
| return terr | ||
| } | ||
| createdUser = decision == models.CreateAccount | ||
|
|
||
| token, terr = a.issueRefreshToken(r, w.Header(), tx, user, models.OAuth, grantParams) | ||
| if terr != nil { | ||
| return terr | ||
| } | ||
|
|
||
| return nil | ||
| }); err != nil { | ||
| switch err.(type) { | ||
| case *storage.CommitWithError: | ||
| return err | ||
| case *HTTPError: | ||
| return err | ||
| default: | ||
| return apierrors.NewOAuthError("server_error", "Internal Server Error").WithInternalError(err) | ||
| } | ||
| } | ||
| if createdUser { | ||
| if err := a.triggerAfterUserCreated(r, db, user); err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| metering.RecordLogin(metering.LoginTypeOAuth, token.User.ID, &metering.LoginData{ | ||
| Provider: params.Provider, | ||
| }) | ||
|
|
||
| return sendJSON(w, http.StatusOK, token) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.