Skip to content

Commit e126ccc

Browse files
committed
Propagates redirect_uri for token exchange
Uses the frontend-provided redirect_uri in the backend token exchange to match the authorization request and avoid invalid_grant errors. Removes server-side redirect URI derivation and sends redirect_uri from the client. Adds logging of redirect URI, client ID, and a truncated code for easier debugging. Updates validation messaging accordingly.
1 parent 6789ad9 commit e126ccc

File tree

3 files changed

+12
-7
lines changed

3 files changed

+12
-7
lines changed

server/src/index.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,13 @@ app.get("/api/health", (_req: Request, res: Response) => {
6161
// OAuth token exchange endpoint - proxies token requests to hide client secrets from browser
6262
app.post("/api/auth/exchange-token", express.json(), async (req: Request, res: Response) => {
6363
try {
64-
const { code, codeVerifier, environment } = req.body;
64+
const { code, codeVerifier, environment, redirectUri } = req.body;
6565

6666
// Validate required parameters
6767
if (!code || !codeVerifier || !environment) {
6868
return res.status(400).json({
6969
error: 'missing_parameters',
70-
message: 'code, codeVerifier, and environment are required'
70+
message: 'code, codeVerifier, environment, and redirectUri are required'
7171
});
7272
}
7373

@@ -83,7 +83,6 @@ app.post("/api/auth/exchange-token", express.json(), async (req: Request, res: R
8383
let loginBaseUrl: string;
8484
let clientId: string;
8585
let clientSecret: string;
86-
let redirectUri: string;
8786

8887
if (environment === 'dev') {
8988
loginBaseUrl = process.env.VITE_DEV_LOGIN_BASE_URL || '';
@@ -95,8 +94,8 @@ app.post("/api/auth/exchange-token", express.json(), async (req: Request, res: R
9594
clientSecret = process.env.VITE_PROD_OAUTH_WEB_CLIENT_SECRET || '';
9695
}
9796

98-
redirectUri = process.env.VITE_OAUTH_REDIRECT_URI ||
99-
`${req.protocol}://${req.get('host')}/account/callback`;
97+
// Use redirectUri from request body (sent by frontend)
98+
// This ensures the redirect_uri matches what was used in the authorization request
10099

101100
// Validate configuration
102101
if (!loginBaseUrl || !clientId || !clientSecret) {
@@ -109,6 +108,9 @@ app.post("/api/auth/exchange-token", express.json(), async (req: Request, res: R
109108

110109
console.log(`🔑 [token-exchange] Exchanging token for environment: ${environment}`);
111110
console.log(`🔑 [token-exchange] OAuth server: ${loginBaseUrl}`);
111+
console.log(`🔑 [token-exchange] Redirect URI: ${redirectUri}`);
112+
console.log(`🔑 [token-exchange] Client ID: ${clientId}`);
113+
console.log(`🔑 [token-exchange] Code (first 10 chars): ${code.substring(0, 10)}...`);
112114

113115
// Build token exchange request
114116
const tokenUrl = `${loginBaseUrl}/connect/token`;

src/lib/auth-service.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,8 @@ class AuthService {
249249
const envConfig = getCurrentEnvironmentConfig();
250250
console.log('🔐 [auth-service] Using environment config for login:', {
251251
loginBaseUrl: envConfig.loginBaseUrl,
252-
clientId: envConfig.oauthClientId
252+
clientId: envConfig.oauthClientId,
253+
redirectUri: OAUTH_CONFIG.redirectUri
253254
});
254255

255256
// Build authorization URL

src/lib/oauth2-utils.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ export async function exchangeCodeForToken(
124124
const backendUrl = `${window.location.origin}/api/auth/exchange-token`;
125125

126126
console.log(`🔑 [oauth2-utils] Calling backend token exchange for environment: ${environment}`);
127+
console.log(`🔑 [oauth2-utils] Redirect URI: ${config.redirectUri}`);
127128

128129
let response: Response;
129130

@@ -136,7 +137,8 @@ export async function exchangeCodeForToken(
136137
body: JSON.stringify({
137138
code,
138139
codeVerifier,
139-
environment
140+
environment,
141+
redirectUri: config.redirectUri // Send the redirect_uri that was used in authorization request
140142
})
141143
});
142144
} catch (error) {

0 commit comments

Comments
 (0)