-
Notifications
You must be signed in to change notification settings - Fork 727
fix: return RFC 6749 errors from the /oauth/token refresh grant #2660
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
nishant-iyengar
wants to merge
1
commit into
supabase:master
Choose a base branch
from
nishant-iyengar:fix/oauth-token-endpoint-rfc6749-errors
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.
+170
−21
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| package oauthserver | ||
|
|
||
| import ( | ||
| "net/http" | ||
|
|
||
| "github.com/supabase/auth/internal/api/apierrors" | ||
| ) | ||
|
|
||
| // Token endpoint error codes per RFC 6749 Section 5.2, extending the set in authorize.go. | ||
| // https://datatracker.ietf.org/doc/html/rfc6749#section-5.2 | ||
| const ( | ||
| oAuth2ErrorInvalidClient = "invalid_client" | ||
| oAuth2ErrorInvalidGrant = "invalid_grant" | ||
| oAuth2ErrorUnsupportedGrantType = "unsupported_grant_type" | ||
| ) | ||
|
|
||
| // Only codes meaning the grant itself is dead may map to invalid_grant: that is the code telling a client to stop retrying and re-authorize the user. | ||
| var grantErrorCodes = map[string]string{ | ||
| apierrors.ErrorCodeRefreshTokenNotFound: oAuth2ErrorInvalidGrant, | ||
| apierrors.ErrorCodeRefreshTokenAlreadyUsed: oAuth2ErrorInvalidGrant, | ||
| apierrors.ErrorCodeSessionNotFound: oAuth2ErrorInvalidGrant, | ||
| apierrors.ErrorCodeSessionExpired: oAuth2ErrorInvalidGrant, | ||
| apierrors.ErrorCodeUserBanned: oAuth2ErrorInvalidGrant, | ||
|
|
||
| apierrors.ErrorCodeValidationFailed: oAuth2ErrorInvalidRequest, | ||
| } | ||
|
|
||
| // oauthTokenError translates an error from the shared token service into an error response the token endpoint is allowed to return, per RFC 6749 Section 5.2 (https://datatracker.ietf.org/doc/html/rfc6749#section-5.2). The service is shared with /auth/v1/token, whose clients parse the HTTPError shape, so the translation has to happen at this boundary. | ||
| func oauthTokenError(err error) error { | ||
| switch e := err.(type) { | ||
| case nil: | ||
| return nil | ||
|
|
||
| case *apierrors.OAuthError: | ||
| return e | ||
|
|
||
| case *apierrors.HTTPError: | ||
| // A 409, 429 or 5xx says nothing about the grant, and every value in the spec's set asserts something about this request that retrying will not change. | ||
| if e.HTTPStatus != http.StatusBadRequest { | ||
| return e | ||
| } | ||
|
|
||
| code, ok := grantErrorCodes[e.ErrorCode] | ||
| if !ok { | ||
| code = oAuth2ErrorInvalidRequest | ||
| } | ||
|
|
||
| return apierrors.NewOAuthError(code, e.Message).WithInternalError(e) | ||
|
|
||
| case interface{ Cause() error }: | ||
| // storage.CommitWithError, used where the transaction must still commit. | ||
| if cause := e.Cause(); cause != nil && cause != err { | ||
| return oauthTokenError(cause) | ||
| } | ||
| } | ||
|
|
||
| return err | ||
| } |
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,91 @@ | ||
| package oauthserver | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/supabase/auth/internal/api/apierrors" | ||
| "github.com/supabase/auth/internal/storage" | ||
| ) | ||
|
|
||
| func TestOAuthTokenError(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| err error | ||
| // the expected OAuth error code, empty when the error should pass through untranslated | ||
| expected string | ||
| // the expected error_description, when it needs asserting | ||
| expectedDescription string | ||
| // the status an untranslated error must keep | ||
| expectedStatus int | ||
| }{ | ||
| { | ||
| name: "refresh token not found should return invalid_grant", | ||
| err: apierrors.NewBadRequestError(apierrors.ErrorCodeRefreshTokenNotFound, "Invalid Refresh Token: Refresh Token Not Found"), | ||
| expected: "invalid_grant", | ||
| }, | ||
| { | ||
| name: "session expired should return invalid_grant", | ||
| err: apierrors.NewBadRequestError(apierrors.ErrorCodeSessionExpired, "Invalid Refresh Token: Session Expired"), | ||
| expected: "invalid_grant", | ||
| }, | ||
| { | ||
| name: "refresh token reuse wrapped in CommitWithError should return invalid_grant", | ||
| err: storage.NewCommitWithError(apierrors.NewBadRequestError(apierrors.ErrorCodeRefreshTokenAlreadyUsed, "Invalid Refresh Token: Already Used")), | ||
| expected: "invalid_grant", | ||
| }, | ||
| { | ||
| name: "the internal message should not reach the client", | ||
| err: apierrors.NewBadRequestError(apierrors.ErrorCodeRefreshTokenAlreadyUsed, "Invalid Refresh Token: Already Used").WithInternalMessage("Possible abuse attempt: %v", "6a3c1f0e"), | ||
| expected: "invalid_grant", | ||
| expectedDescription: "Invalid Refresh Token: Already Used", | ||
| }, | ||
| { | ||
| name: "validation failure should return invalid_request", | ||
| err: apierrors.NewBadRequestError(apierrors.ErrorCodeValidationFailed, "Invalid Refresh Token: Not Issued By This Server"), | ||
| expected: "invalid_request", | ||
| }, | ||
| { | ||
| name: "unrecognized error code should return invalid_request, not invalid_grant", | ||
| err: apierrors.NewBadRequestError("some_future_error_code", "Something new"), | ||
| expected: "invalid_request", | ||
| }, | ||
| { | ||
| name: "concurrent refresh should pass through as a 409", | ||
| err: apierrors.NewConflictError("Too many concurrent token refresh requests on the same session or refresh token"), | ||
| expectedStatus: 409, | ||
| }, | ||
| { | ||
| name: "internal failure should pass through as a 500", | ||
| err: apierrors.NewInternalServerError("error generating jwt token"), | ||
| expectedStatus: 500, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| result := oauthTokenError(tt.err) | ||
|
|
||
| if tt.expected == "" { | ||
| httpErr, ok := result.(*apierrors.HTTPError) | ||
| if !ok { | ||
| t.Fatalf("oauthTokenError() = %T, expected the error to pass through as *apierrors.HTTPError", result) | ||
| } | ||
| if httpErr.HTTPStatus != tt.expectedStatus { | ||
| t.Errorf("oauthTokenError() status = %v, expected %v", httpErr.HTTPStatus, tt.expectedStatus) | ||
| } | ||
| return | ||
| } | ||
|
|
||
| oauthErr, ok := result.(*apierrors.OAuthError) | ||
| if !ok { | ||
| t.Fatalf("oauthTokenError() = %T, expected *apierrors.OAuthError", result) | ||
| } | ||
| if oauthErr.Err != tt.expected { | ||
| t.Errorf("oauthTokenError() error = %v, expected %v", oauthErr.Err, tt.expected) | ||
| } | ||
| if tt.expectedDescription != "" && oauthErr.Description != tt.expectedDescription { | ||
| t.Errorf("oauthTokenError() error_description = %v, expected %v", oauthErr.Description, tt.expectedDescription) | ||
| } | ||
| }) | ||
| } | ||
| } |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
question: is there a test covering this to ensure the
errorkey is in the response body?