Skip to content

fix: Add redirect_uri parameter to /token endpoint for the Authorization Code Flow #354

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

Closed
wants to merge 1 commit into from
Closed
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
73 changes: 60 additions & 13 deletions src/server/auth/handlers/token.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('Token Handler', () => {
const validClient: OAuthClientInformationFull = {
client_id: 'valid-client',
client_secret: 'valid-secret',
redirect_uris: ['https://example.com/callback']
redirect_uris: ['https://valid.com/callback']
};

// Mock client store
Expand All @@ -44,7 +44,7 @@ describe('Token Handler', () => {
clientsStore: mockClientStore,

async authorize(client: OAuthClientInformationFull, params: AuthorizationParams, res: Response): Promise<void> {
res.redirect('https://example.com/callback?code=mock_auth_code');
res.redirect('https://valid.com/callback?code=mock_auth_code');
},

async challengeForAuthorizationCode(client: OAuthClientInformationFull, authorizationCode: string): Promise<string> {
Expand All @@ -56,8 +56,8 @@ describe('Token Handler', () => {
throw new InvalidGrantError('The authorization code is invalid');
},

async exchangeAuthorizationCode(client: OAuthClientInformationFull, authorizationCode: string): Promise<OAuthTokens> {
if (authorizationCode === 'valid_code') {
async exchangeAuthorizationCode(client: OAuthClientInformationFull, authorizationCode: string, redirectUri: string): Promise<OAuthTokens> {
if (authorizationCode === 'valid_code' && redirectUri === 'https://valid.com/callback') {
return {
access_token: 'mock_access_token',
token_type: 'bearer',
Expand Down Expand Up @@ -123,7 +123,10 @@ describe('Token Handler', () => {
.send({
client_id: 'valid-client',
client_secret: 'valid-secret',
grant_type: 'authorization_code'
grant_type: 'authorization_code',
code: 'valid_code',
code_verifier: 'valid_verifier',
redirect_uri: 'https://valid.com/callback'
});

expect(response.status).toBe(405);
Expand Down Expand Up @@ -171,7 +174,10 @@ describe('Token Handler', () => {
.send({
client_id: 'invalid-client',
client_secret: 'wrong-secret',
grant_type: 'authorization_code'
grant_type: 'authorization_code',
code: 'valid_code',
code_verifier: 'valid_verifier',
redirect_uri: 'https://valid.com/callback'
});

expect(response.status).toBe(400);
Expand All @@ -187,7 +193,8 @@ describe('Token Handler', () => {
client_secret: 'valid-secret',
grant_type: 'authorization_code',
code: 'valid_code',
code_verifier: 'valid_verifier'
code_verifier: 'valid_verifier',
redirect_uri: 'https://valid.com/callback'
});

expect(response.status).toBe(200);
Expand All @@ -204,7 +211,8 @@ describe('Token Handler', () => {
client_secret: 'valid-secret',
grant_type: 'authorization_code',
// Missing code
code_verifier: 'valid_verifier'
code_verifier: 'valid_verifier',
redirect_uri: 'https://valid.com/callback'
});

expect(response.status).toBe(400);
Expand All @@ -219,8 +227,26 @@ describe('Token Handler', () => {
client_id: 'valid-client',
client_secret: 'valid-secret',
grant_type: 'authorization_code',
code: 'valid_code'
code: 'valid_code',
// Missing code_verifier
redirect_uri: 'https://valid.com/callback'
});

expect(response.status).toBe(400);
expect(response.body.error).toBe('invalid_request');
});

it('requires redirect_uri parameter', async () => {
const response = await supertest(app)
.post('/token')
.type('form')
.send({
client_id: 'valid-client',
client_secret: 'valid-secret',
grant_type: 'authorization_code',
code: 'valid_code',
code_verifier: 'valid_verifier'
// Missing redirect_uri
});

expect(response.status).toBe(400);
Expand All @@ -239,7 +265,8 @@ describe('Token Handler', () => {
client_secret: 'valid-secret',
grant_type: 'authorization_code',
code: 'valid_code',
code_verifier: 'invalid_verifier'
code_verifier: 'invalid_verifier',
redirect_uri: 'https://valid.com/callback'
});

expect(response.status).toBe(400);
Expand All @@ -256,13 +283,31 @@ describe('Token Handler', () => {
client_secret: 'valid-secret',
grant_type: 'authorization_code',
code: 'expired_code',
code_verifier: 'valid_verifier'
code_verifier: 'valid_verifier',
redirect_uri: 'https://valid.com/callback'
});

expect(response.status).toBe(400);
expect(response.body.error).toBe('invalid_grant');
});

it('rejects unregistered redirect_uri', async () => {
const response = await supertest(app)
.post('/token')
.type('form')
.send({
client_id: 'valid-client',
client_secret: 'valid-secret',
grant_type: 'authorization_code',
code: 'valid_code',
code_verifier: 'valid_verifier',
redirect_uri: 'https://wrong.com/callback'
});

expect(response.status).toBe(400);
expect(response.body.error).toBe('invalid_request');
});

it('returns tokens for valid code exchange', async () => {
const response = await supertest(app)
.post('/token')
Expand All @@ -272,7 +317,8 @@ describe('Token Handler', () => {
client_secret: 'valid-secret',
grant_type: 'authorization_code',
code: 'valid_code',
code_verifier: 'valid_verifier'
code_verifier: 'valid_verifier',
redirect_uri: 'https://valid.com/callback'
});

expect(response.status).toBe(200);
Expand Down Expand Up @@ -322,7 +368,8 @@ describe('Token Handler', () => {
client_secret: 'valid-secret',
grant_type: 'authorization_code',
code: 'valid_code',
code_verifier: 'any_verifier'
code_verifier: 'any_verifier',
redirect_uri: 'https://valid.com/callback'
});

expect(response.status).toBe(200);
Expand Down
8 changes: 6 additions & 2 deletions src/server/auth/handlers/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const TokenRequestSchema = z.object({
const AuthorizationCodeGrantSchema = z.object({
code: z.string(),
code_verifier: z.string(),
redirect_uri: z.string(),
});

const RefreshTokenGrantSchema = z.object({
Expand Down Expand Up @@ -88,7 +89,10 @@ export function tokenHandler({ provider, rateLimit: rateLimitConfig }: TokenHand
throw new InvalidRequestError(parseResult.error.message);
}

const { code, code_verifier } = parseResult.data;
const { code, code_verifier, redirect_uri } = parseResult.data;
if (!client.redirect_uris.includes(redirect_uri)) {
throw new InvalidRequestError("Unregistered redirect_uri");
}

const skipLocalPkceValidation = provider.skipLocalPkceValidation;

Expand All @@ -102,7 +106,7 @@ export function tokenHandler({ provider, rateLimit: rateLimitConfig }: TokenHand
}

// Passes the code_verifier to the provider if PKCE validation didn't occur locally
const tokens = await provider.exchangeAuthorizationCode(client, code, skipLocalPkceValidation ? code_verifier : undefined);
const tokens = await provider.exchangeAuthorizationCode(client, code, redirect_uri, skipLocalPkceValidation ? code_verifier : undefined);
res.status(200).json(tokens);
break;
}
Expand Down
2 changes: 1 addition & 1 deletion src/server/auth/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export interface OAuthServerProvider {
/**
* Exchanges an authorization code for an access token.
*/
exchangeAuthorizationCode(client: OAuthClientInformationFull, authorizationCode: string, codeVerifier?: string): Promise<OAuthTokens>;
exchangeAuthorizationCode(client: OAuthClientInformationFull, authorizationCode: string, redirectUri: string, codeVerifier?: string): Promise<OAuthTokens>;

/**
* Exchanges a refresh token for an access token.
Expand Down
1 change: 1 addition & 0 deletions src/server/auth/providers/proxyProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ describe("Proxy OAuth Server Provider", () => {
const tokens = await provider.exchangeAuthorizationCode(
validClient,
"test-code",
"https://example.com/callback",
"test-verifier"
);

Expand Down
2 changes: 2 additions & 0 deletions src/server/auth/providers/proxyProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,14 @@ export class ProxyOAuthServerProvider implements OAuthServerProvider {
async exchangeAuthorizationCode(
client: OAuthClientInformationFull,
authorizationCode: string,
redirectUri: string,
codeVerifier?: string
): Promise<OAuthTokens> {
const params = new URLSearchParams({
grant_type: "authorization_code",
client_id: client.client_id,
code: authorizationCode,
redirect_uri: redirectUri,
});

if (client.client_secret) {
Expand Down