@workos-inc/authkit-nextjs4.6.35.2 kB
import { WORKOS_REDIRECT_URI, WORKOS_COOKIE_MAX_AGE, WORKOS_COOKIE_DOMAIN, WORKOS_COOKIE_SAMESITE, } from './env-variables.js';
const DEFAULT_COOKIE_MAX_AGE = 60 * 60 * 24 * 400; // 400 days, the maximum allowed by Chrome
const PKCE_COOKIE_MAX_AGE = 600; // 10 minutes
const JWT_COOKIE_MAX_AGE = 30; // seconds
const JWT_COOKIE_NAME = 'workos-access-token';
const EPOCH = new Date(0).toUTCString();
function assertValidSamSite(sameSite) {
if (!['lax', 'strict', 'none'].includes(sameSite.toLowerCase())) {
throw new Error(`Invalid SameSite value: ${sameSite}`);
}
}
/** Unparseable URLs count as HTTPS so a bad value fails closed to `Secure`. */
function isHttpsOrUnparseable(url) {
try {
return new URL(url).protocol === 'https:';
}
catch {
return true;
}
}
function isLocalhost(url) {
try {
const { hostname } = new URL(url);
return hostname === 'localhost' || hostname === '127.0.0.1';
}
catch {
return false;
}
}
/**
* The single policy for the `Secure` attribute on every AuthKit cookie.
*
* Behind a TLS-terminating proxy the incoming request URL is the internal `http://`
* origin, so it can't be trusted on its own. If any origin signal is HTTPS, the
* cookie is Secure. With no signals at all, it fails closed to Secure.
*/
function isSecureOrigin(urls, { productionFloor = false } = {}) {
const candidates = [...urls, WORKOS_REDIRECT_URI].filter((url) => Boolean(url));
if (candidates.length === 0 || candidates.some(isHttpsOrUnparseable)) {
return true;
}
return productionFloor && process.env.NODE_ENV === 'production' && !candidates.every(isLocalhost);
}
/**
* The browser-facing origin a TLS-terminating proxy reports with `x-forwarded-proto`
* (first value) and `x-forwarded-host`. Only an `https` protocol yields a URL, so the
* header can turn `Secure` on but never off.
*/
export function forwardedOriginUrl(headers) {
const proto = headers.get('x-forwarded-proto')?.split(',')[0].trim().toLowerCase();
if (proto !== 'https') {
return undefined;
}
const host = (headers.get('x-forwarded-host') || headers.get('host') || 'localhost').split(',')[0].trim();
return `https://${host}`;
}
/**
* Origin signals available in a server action / route handler: the request URL and
* redirect URI the AuthKit middleware recorded on the request headers.
*/
export function originUrlsFromHeaders(headers) {
return [headers.get('x-url'), headers.get('x-redirect-uri')];
}
export function getCookieOptions(urls, { expired = false } = {}) {
const sameSite = WORKOS_COOKIE_SAMESITE || 'lax';
assertValidSamSite(sameSite);
let maxAge = DEFAULT_COOKIE_MAX_AGE;
if (expired) {
maxAge = 0;
}
else if (WORKOS_COOKIE_MAX_AGE) {
const parsed = parseInt(WORKOS_COOKIE_MAX_AGE, 10);
maxAge = Number.isFinite(parsed) ? parsed : DEFAULT_COOKIE_MAX_AGE;
}
return {
path: '/',
httpOnly: true,
secure: sameSite.toLowerCase() === 'none' || isSecureOrigin(urls),
sameSite,
// It's fine to have a long cookie expiry date as the access/refresh tokens
// act as the actual time-limited aspects of the session.
maxAge,
domain: WORKOS_COOKIE_DOMAIN || '',
};
}
/**
* Cookie options for the PKCE verifier cookie.
* 'strict' blocks the cookie on the cross-site redirect back from WorkOS; downgrade to 'lax'.
* 'none' is more permissive and must be preserved for iframe/cross-origin embed flows.
* Max-age is always capped to 10 minutes — PKCE cookies are single-use and short-lived.
*/
export function getPKCECookieOptions(urls, { expired = false } = {}) {
const options = getCookieOptions(urls, { expired });
return {
...options,
sameSite: options.sameSite.toLowerCase() === 'strict' ? 'lax' : options.sameSite,
maxAge: expired ? 0 : PKCE_COOKIE_MAX_AGE,
};
}
/** Serialize a cookie for a raw `Set-Cookie` header (e.g. in middleware, where `cookies()` is unavailable). */
export function serializeCookie(name, value, options) {
const sameSite = options.sameSite.charAt(0).toUpperCase() + options.sameSite.slice(1).toLowerCase();
const parts = [
`${name}=${value}`,
`Path=${options.path}`,
'HttpOnly',
`SameSite=${sameSite}`,
`Max-Age=${options.maxAge}`,
];
if (options.maxAge === 0) {
parts.push(`Expires=${EPOCH}`);
}
if (options.domain) {
parts.push(`Domain=${options.domain}`);
}
if (options.secure) {
parts.push('Secure');
}
return parts.join('; ');
}
/**
* The short-lived, script-readable access token cookie used by `eagerAuth`.
* Uses the same `Secure` policy as every other AuthKit cookie, plus the production floor.
*/
export function getJwtCookie(body, urls, expired = false) {
const parts = [
`${JWT_COOKIE_NAME}=${expired ? '' : (body ?? '')}`,
'SameSite=Lax',
`Max-Age=${expired ? 0 : JWT_COOKIE_MAX_AGE}`,
];
if (isSecureOrigin(urls, { productionFloor: true })) {
parts.push('Secure');
}
if (expired) {
parts.push(`Expires=${EPOCH}`);
}
return parts.join('; ');
}
//# sourceMappingURL=cookie.js.map
Shown as plain text from the published tarball, after apm checked it against the version's checksum. Nothing in it runs on this page.