-
Notifications
You must be signed in to change notification settings - Fork 212
Resolve siteId dynamically via x-site-id header for multisite #3700
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
Merged
unandyala
merged 17 commits into
feature/httponly-session-cookies
from
unandyala.handle-dynamic-site-id
Mar 5, 2026
Merged
Changes from 15 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
9ee940f
Add Bearer token and refresh token injection for SLAS private client …
unandyala f83a50f
Resolve siteId dynamically via x-site-id header for multisite support
unandyala dc16bff
Remove static siteId fallback — x-site-id header is the sole source
unandyala 33eb226
Add x-site-id header to _app-config generator templates
unandyala 2b2b485
Guard applyScapiAuthHeaders behind HttpOnly flag, remove SLAS auth ch…
unandyala a44554b
fix lint errorrs
unandyala 630d2c8
add warning
unandyala 430d0de
Add change log
unandyala 7f42066
clear cookies
unandyala ea7c0cf
clear cookies
unandyala 1db750a
Merge branch 'feature/httponly-session-cookies' into unandyala.handle…
unandyala 0706784
merge from parent branch
unandyala 36c1932
Rename slasEndpointsRequiringAccessToken to slasLogoutEndpoint and ex…
unandyala 8c2b42f
merge from feature branch
unandyala 4c4d968
add change log
unandyala 39f33ca
refactor
unandyala 86b1375
fix lint errors
unandyala 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
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
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
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
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 |
|---|---|---|
|
|
@@ -16,7 +16,7 @@ import { | |
| X_ENCODED_HEADERS, | ||
| CONTENT_SECURITY_POLICY, | ||
| SLAS_TOKEN_RESPONSE_ENDPOINTS, | ||
| SLAS_ENDPOINTS_REQUIRING_ACCESS_TOKEN | ||
| SLAS_LOGOUT_ENDPOINT | ||
| } from './constants' | ||
| import { | ||
| catchAndLog, | ||
|
|
@@ -94,6 +94,50 @@ export const isBinary = (headers) => { | |
| return isContentTypeBinary(headers) | ||
| } | ||
|
|
||
| /** | ||
| * Inject Bearer token and refresh token from HttpOnly cookies for the SLAS logout endpoint. | ||
| * Reads the access token and refresh token from cookies keyed by siteId (from the x-site-id header), | ||
| * sets the Authorization header, and appends refresh_token to the query string. | ||
| * @private | ||
| */ | ||
| export const injectLogoutTokens = (proxyRequest, incomingRequest) => { | ||
| const cookieHeader = incomingRequest.headers.cookie | ||
| if (!cookieHeader) return | ||
|
|
||
| const cookies = cookie.parse(cookieHeader) | ||
| const siteId = incomingRequest.headers['x-site-id'] | ||
| if (!siteId) { | ||
| logger.warn( | ||
| 'x-site-id header is missing on SLAS logout request. ' + | ||
| 'Token injection skipped. ' + | ||
| 'Ensure the x-site-id header is set in CommerceApiProvider headers.', | ||
| {namespace: 'injectLogoutTokens'} | ||
| ) | ||
| return | ||
| } | ||
|
|
||
| const site = siteId.trim() | ||
vcua-mobify marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // Inject Bearer token from access token cookie | ||
| const accessToken = cookies[`cc-at_${site}`] | ||
| if (accessToken) { | ||
| proxyRequest.setHeader('Authorization', `Bearer ${accessToken}`) | ||
| } | ||
|
|
||
| // Inject refresh_token into query string from HttpOnly cookie | ||
| const refreshToken = cookies[`cc-nx_${site}`] | ||
| if (refreshToken) { | ||
| const url = new URL(proxyRequest.path, 'http://localhost') | ||
vcua-mobify marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| url.searchParams.set('refresh_token', refreshToken) | ||
| proxyRequest.path = url.pathname + url.search | ||
| } else { | ||
| logger.warn( | ||
| `Refresh token cookie (cc-nx_${site}) not found for ${incomingRequest.path}. The logout request may fail.`, | ||
| {namespace: 'injectLogoutTokens'} | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Environment variables that must be set for the Express app to run remotely. | ||
| * | ||
|
|
@@ -170,11 +214,10 @@ export const RemoteServerFactory = { | |
| // cookies applied when that feature is enabled. Users can override this in ssr.js. | ||
| tokenResponseEndpoints: SLAS_TOKEN_RESPONSE_ENDPOINTS, | ||
|
|
||
| // A regex for identifying which SLAS auth endpoints (/shopper/auth/) require the | ||
| // shopper's access token in the Authorization header (Bearer token from HttpOnly cookie). | ||
| // Most SLAS auth endpoints use Basic Auth with client credentials, but some like logout | ||
| // require the shopper's Bearer token. Users can override this in ssr.js. | ||
| slasEndpointsRequiringAccessToken: SLAS_ENDPOINTS_REQUIRING_ACCESS_TOKEN, | ||
| // A regex for matching the SLAS logout endpoint. When HttpOnly session cookies are | ||
| // enabled, the proxy injects the Bearer token and refresh token from HttpOnly cookies | ||
| // for this endpoint. Users can override this in ssr.js. | ||
| slasLogoutEndpoint: SLAS_LOGOUT_ENDPOINT, | ||
vcua-mobify marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // Custom callback to modify the SLAS private client proxy request. This callback is invoked | ||
| // after the built-in proxy request handling. Users can provide additional | ||
|
|
@@ -233,16 +276,6 @@ export const RemoteServerFactory = { | |
| // Note: HttpOnly session cookies are controlled by the MRT_DISABLE_HTTPONLY_SESSION_COOKIES | ||
| // env var (set by MRT in production, pwa-kit-dev locally). Read directly where needed. | ||
|
|
||
| // Extract siteId from app configuration for SCAPI auth | ||
| // This will be used to read the correct access token cookie | ||
| try { | ||
| const config = getConfig({buildDirectory: options.buildDir}) | ||
| options.siteId = config?.app?.commerceAPI?.parameters?.siteId || null | ||
| } catch (e) { | ||
| // Config may not be available yet (e.g., during build), that's okay | ||
| options.siteId = null | ||
| } | ||
|
|
||
| return options | ||
| }, | ||
|
|
||
|
|
@@ -399,8 +432,7 @@ export const RemoteServerFactory = { | |
| * @private | ||
| */ | ||
| _configureProxyConfigs(options) { | ||
| const siteId = options.siteId || null | ||
| configureProxyConfigs(options.appHostname, options.protocol, siteId) | ||
| configureProxyConfigs(options.appHostname, options.protocol) | ||
| }, | ||
|
|
||
| /** | ||
|
|
@@ -979,39 +1011,9 @@ export const RemoteServerFactory = { | |
| proxyRequest.setHeader('Authorization', `Basic ${encodedSlasCredentials}`) | ||
| } else if ( | ||
| process.env.MRT_DISABLE_HTTPONLY_SESSION_COOKIES === 'false' && | ||
| incomingRequest.path?.match(options.slasEndpointsRequiringAccessToken) | ||
| incomingRequest.path?.match(options.slasLogoutEndpoint) | ||
| ) { | ||
| // Inject tokens from HttpOnly cookies for endpoints like /oauth2/logout | ||
| const cookieHeader = incomingRequest.headers.cookie | ||
| if (cookieHeader) { | ||
| const cookies = cookie.parse(cookieHeader) | ||
| const siteId = options.mobify?.app?.commerceAPI?.parameters?.siteId | ||
| if (siteId) { | ||
| const site = siteId.trim() | ||
|
|
||
| // Inject Bearer token from access token cookie | ||
| const accessToken = cookies[`cc-at_${site}`] | ||
| if (accessToken) { | ||
| proxyRequest.setHeader('Authorization', `Bearer ${accessToken}`) | ||
| } | ||
|
|
||
| // Inject refresh_token into query string from HttpOnly cookie | ||
| // refresh_token ishouls required for /oauth2/logout | ||
| const refreshToken = cookies[`cc-nx_${site}`] | ||
| if (refreshToken) { | ||
| const url = new URL(proxyRequest.path, 'http://localhost') | ||
| url.searchParams.set('refresh_token', refreshToken) | ||
| proxyRequest.path = url.pathname + url.search | ||
| } else { | ||
| logger.warn( | ||
| `Registered refresh token cookie (cc-nx_${site}) not found for ${incomingRequest.path}. The logout request may fail.`, | ||
| { | ||
| namespace: '_setupSlasPrivateClientProxy' | ||
| } | ||
| ) | ||
| } | ||
| } | ||
| } | ||
| injectLogoutTokens(proxyRequest, incomingRequest) | ||
|
||
| } | ||
|
|
||
| // Allow users to apply additional custom modifications to the proxy request | ||
|
|
@@ -1499,10 +1501,9 @@ export const RemoteServerFactory = { | |
| * @param {RegExp} [options.tokenResponseEndpoints] - A regex pattern to match SLAS endpoints | ||
| * that return tokens in the response body. Used to determine which responses should have HttpOnly | ||
| * session cookies applied. Defaults to /\/oauth2\/(token|passwordless\/token)$/. | ||
| * @param {RegExp} [options.slasEndpointsRequiringAccessToken] - A regex pattern to match SLAS auth | ||
| * endpoints (/shopper/auth/) that require the shopper's access token in the Authorization header (Bearer token). | ||
| * Most SLAS auth endpoints use Basic Auth with client credentials, but some like logout require the shopper's | ||
| * Bearer token. Defaults to /\/oauth2\/logout/. | ||
| * @param {RegExp} [options.slasLogoutEndpoint] - A regex pattern to match the SLAS logout endpoint. | ||
| * When HttpOnly session cookies are enabled, the proxy injects the Bearer token and refresh token | ||
| * from HttpOnly cookies for this endpoint. Defaults to /\/oauth2\/logout/. | ||
| * @param {function} [options.onSLASPrivateProxyReq] - Custom callback to modify SLAS private client | ||
| * proxy requests. Called after built-in request handling. Signature: (proxyRequest, incomingRequest, res) => void. | ||
| * Use this to add custom headers or modify the proxy request. | ||
|
|
||
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
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
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.
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.
minor refactor - moved to a separate function
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.
Should this function be moved into process-token-response?
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.
it is not really related to processing the response. Other option is to rename process-token-response to a more generic one -
httponly-cookie-utilsand then move this function. I will take another look and see if it make sense to renameThere 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.
I will leave it as is for now and consider rename/refactor as we make more changes