Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions internal/api/oauthserver/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,13 @@ func (s *Server) UserRevokeOAuthGrant(w http.ResponseWriter, r *http.Request) er
return terr
}

// Drop unexchanged codes before sessions. An exchange that already
// locked one of these rows finishes and inserts its session first;
// the session delete below then removes that session too.
if terr := models.DeleteApprovedOAuthServerAuthorizations(tx, user.ID, clientID); terr != nil {
return terr
}

// Delete all sessions associated with this OAuth client for this user
// This will invalidate all refresh tokens for those sessions
if terr := models.RevokeOAuthSessions(tx, user.ID, clientID); terr != nil {
Expand Down
77 changes: 77 additions & 0 deletions internal/api/oauthserver/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import (
"net/http"
"net/http/httptest"
"testing"
"time"

"github.com/gofrs/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"github.com/supabase/auth/internal/api/apierrors"
"github.com/supabase/auth/internal/api/shared"
"github.com/supabase/auth/internal/conf"
"github.com/supabase/auth/internal/conf/confload"
Expand Down Expand Up @@ -711,6 +713,81 @@ func (ts *OAuthClientTestSuite) TestUserRevokeOAuthGrant() {
assert.Nil(ts.T(), deletedSession)
}

func (ts *OAuthClientTestSuite) exchangeAuthorizationCode(client *models.OAuthServerClient, code string) *httptest.ResponseRecorder {
req := httptest.NewRequest(http.MethodPost, "/oauth/token", nil)
ctx := shared.WithOAuthServerClient(req.Context(), client)
req = req.WithContext(ctx)
w := httptest.NewRecorder()
err := ts.Server.handleAuthorizationCodeGrant(ctx, w, req, &OAuthTokenParams{
GrantType: GrantTypeAuthorizationCode,
Code: code,
})
if err != nil {
ts.T().Fatalf("exchange: %v", err)
}
return w
}

func (ts *OAuthClientTestSuite) TestRevokeOAuthGrantRejectsUnexchangedCode() {
owner := ts.createTestUser("revoke-code-owner@example.com")
otherUser := ts.createTestUser("revoke-code-other@example.com")
client, _ := ts.createTestOAuthClient()
otherClient, _ := ts.createTestOAuthClient()

ts.createTestConsent(owner.ID.String(), client.ID.String(), []string{"profile"})
ts.createTestConsent(owner.ID.String(), otherClient.ID.String(), []string{"profile"})
ts.createTestConsent(otherUser.ID.String(), client.ID.String(), []string{"profile"})

// Same client, same user, exchanged before the revoke. Proves this grant can mint tokens.
issued := ts.exchangeAuthorizationCode(client, ts.mintApprovedCode(client.ID, owner.ID))
assert.Equal(ts.T(), http.StatusOK, issued.Code)
var issuedBody map[string]any
require.NoError(ts.T(), json.Unmarshal(issued.Body.Bytes(), &issuedBody))
assert.NotEmpty(ts.T(), issuedBody["access_token"])
assert.NotEmpty(ts.T(), issuedBody["refresh_token"])

unused := ts.mintApprovedCode(client.ID, owner.ID)
otherClientCode := ts.mintApprovedCode(otherClient.ID, owner.ID)
otherUserCode := ts.mintApprovedCode(client.ID, otherUser.ID)

pending := models.NewOAuthServerAuthorization(models.NewOAuthServerAuthorizationParams{
ClientID: client.ID,
RedirectURI: "https://example.com/callback",
Scope: "profile",
TTL: time.Hour,
})
require.NoError(ts.T(), models.CreateOAuthServerAuthorization(ts.DB, pending))
require.NoError(ts.T(), pending.SetUser(ts.DB, owner.ID))

req := httptest.NewRequest(http.MethodDelete, "/user/oauth/grants?client_id="+client.ID.String(), nil)
req = req.WithContext(shared.WithUser(req.Context(), owner))
w := httptest.NewRecorder()
require.NoError(ts.T(), ts.Server.UserRevokeOAuthGrant(w, req))
assert.Equal(ts.T(), http.StatusNoContent, w.Code)

exchangeReq := httptest.NewRequest(http.MethodPost, "/oauth/token", nil)
exchangeCtx := shared.WithOAuthServerClient(exchangeReq.Context(), client)
exchangeReq = exchangeReq.WithContext(exchangeCtx)
exchangeW := httptest.NewRecorder()
err := ts.Server.handleAuthorizationCodeGrant(exchangeCtx, exchangeW, exchangeReq, &OAuthTokenParams{
GrantType: GrantTypeAuthorizationCode,
Code: unused,
})
var oauthErr *apierrors.OAuthError
require.ErrorAs(ts.T(), err, &oauthErr)
assert.Equal(ts.T(), "invalid_grant", oauthErr.Err)

stillOtherClient := ts.exchangeAuthorizationCode(otherClient, otherClientCode)
assert.Equal(ts.T(), http.StatusOK, stillOtherClient.Code)
stillOtherUser := ts.exchangeAuthorizationCode(client, otherUserCode)
assert.Equal(ts.T(), http.StatusOK, stillOtherUser.Code)

kept, err := models.FindOAuthServerAuthorizationByID(ts.DB, pending.AuthorizationID)
require.NoError(ts.T(), err)
assert.Equal(ts.T(), models.OAuthServerAuthorizationPending, kept.Status)
assert.Nil(ts.T(), kept.AuthorizationCode)
}

func (ts *OAuthClientTestSuite) TestUserRevokeOAuthGrantNotFound() {
// Create test user
user := ts.createTestUser("test4@example.com")
Expand Down
7 changes: 7 additions & 0 deletions internal/models/oauth_authorization.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,13 @@ func CleanupExpiredOAuthServerAuthorizations(tx *storage.Connection) error {
return tx.RawQuery(query, OAuthServerAuthorizationExpired, OAuthServerAuthorizationPending).Exec()
}

// DeleteApprovedOAuthServerAuthorizations removes unexchanged authorization
// codes issued to one user for one client.
func DeleteApprovedOAuthServerAuthorizations(tx *storage.Connection, userID, clientID uuid.UUID) error {
query := "DELETE FROM " + (&OAuthServerAuthorization{}).TableName() + " WHERE user_id = ? AND client_id = ? AND status = ?"
return tx.RawQuery(query, userID, clientID, OAuthServerAuthorizationApproved).Exec()
}

// Error types for OAuth authorization operations

type OAuthServerAuthorizationNotFoundError struct{}
Expand Down