Skip to content
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

FR-17841- [NextJS] /refresh endpoint fails with 400 after switching tenant #367

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
8 changes: 8 additions & 0 deletions packages/nextjs/src/middleware/ProxyRequestCallback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import sdkVersion from '../sdkVersion';
import config from '../config';
import CookieManager from '../utils/cookies';
import fronteggLogger from '../utils/fronteggLogger';
import { isRefreshTokenRequest } from '../utils/refreshAccessTokenIfNeeded/helpers';

const logger = fronteggLogger.child({ tag: 'FronteggApiMiddleware.ProxyRequestCallback' });
/**
Expand Down Expand Up @@ -48,6 +49,11 @@ const ProxyRequestCallback: ProxyReqCallback<ClientRequest, NextApiRequest> = (p
proxyReq.setHeader('cf-connecting-ip', cfConnectionIp);
}

if (isRefreshTokenRequest(req.url!)) {
logger.debug(`${req.url} | removing Authorization header`);
proxyReq.removeHeader('authorization');
}

[
'x-invoke-path',
'x-invoke-query',
Expand All @@ -57,6 +63,8 @@ const ProxyRequestCallback: ProxyReqCallback<ClientRequest, NextApiRequest> = (p
'cache-control',
].map((header) => proxyReq.removeHeader(header));

logger.debug(`${req.url} | headers to be sent:`, proxyReq.getHeaders());

logger.debug(`${req.url} | check if request has body`);
if (req.method !== 'GET' && req.body) {
logger.debug(`${req.url} | writing request body to proxyReq`);
Expand Down
11 changes: 11 additions & 0 deletions packages/nextjs/src/utils/refreshAccessTokenIfNeeded/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import api from '../../api';
import { getTokensFromCookie } from '../../common';
import { IncomingMessage } from 'http';
import config from '../../config';
import { ApiUrls } from '../../api/urls';

export function hasRefreshTokenCookie(cookies: Record<string, any>): boolean {
const logger = fronteggLogger.child({ tag: 'refreshToken.hasRefreshTokenCookie' });
Expand Down Expand Up @@ -95,3 +96,13 @@ export function isSamlCallback(url: string): boolean {
export function isSSOPostRequest(url: string): boolean {
return url === '/frontegg/auth/saml/callback' || url === '/frontegg/auth/oidc/callback';
}

/**
* Checks if the request URL is a refresh token request.
* This is used to determine if the current request is targeting
* one of the predefined refresh token URLs (embedded or hosted modes).
*/
export function isRefreshTokenRequest(url: string): boolean {
const refreshTokenUrls = [ApiUrls.refreshToken.embedded, ApiUrls.refreshToken.hosted];
return refreshTokenUrls.includes(url);
}
Loading