Skip to content

Commit 018ebba

Browse files
authored
fix: default to st-auth-mode if getTokenTransferMethod returns any in createNewSession (#784)
1 parent 12f26f6 commit 018ebba

File tree

4 files changed

+72
-4
lines changed

4 files changed

+72
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [unreleased]
99

10+
- `createNewSession` now defaults to the value of the `st-auth-mode` header (if available) if the configured `getTokenTransferMethod` returns `any`.
11+
1012
## [16.7.1] - 2024-01-09
1113

1214
- Fixes type output of `resetPasswordUsingToken` in emailpassword and thirdpartyemailpassword recipe to not include statuses that happen based on email change.

lib/build/recipe/session/sessionRequestFunctions.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,13 @@ async function createNewSessionInRequest({
354354
logger_1.logDebugMessage("createNewSession: Access token payload built");
355355
let outputTransferMethod = config.getTokenTransferMethod({ req, forCreateNewSession: true, userContext });
356356
if (outputTransferMethod === "any") {
357-
outputTransferMethod = "header";
357+
const authModeHeader = cookieAndHeaders_1.getAuthModeFromHeader(req);
358+
// We default to header if we can't "parse" it or if it's undefined
359+
if (authModeHeader === "cookie") {
360+
outputTransferMethod = authModeHeader;
361+
} else {
362+
outputTransferMethod = "header";
363+
}
358364
}
359365
logger_1.logDebugMessage("createNewSession: using transfer method " + outputTransferMethod);
360366
if (

lib/ts/recipe/session/sessionRequestFunctions.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,13 @@ import { getRequiredClaimValidators } from "./utils";
1212
import { getRidFromHeader, isAnIpAddress, normaliseHttpMethod, setRequestInUserContextIfNotDefined } from "../../utils";
1313
import { logDebugMessage } from "../../logger";
1414
import { availableTokenTransferMethods, protectedProps } from "./constants";
15-
import { clearSession, getAntiCsrfTokenFromHeaders, getToken, setCookie } from "./cookieAndHeaders";
15+
import {
16+
clearSession,
17+
getAntiCsrfTokenFromHeaders,
18+
getAuthModeFromHeader,
19+
getToken,
20+
setCookie,
21+
} from "./cookieAndHeaders";
1622
import { ParsedJWTInfo, parseJWTWithoutSignatureVerification } from "./jwt";
1723
import { validateAccessTokenStructure } from "./accessToken";
1824
import { NormalisedAppinfo } from "../../types";
@@ -410,7 +416,13 @@ export async function createNewSessionInRequest({
410416

411417
let outputTransferMethod = config.getTokenTransferMethod({ req, forCreateNewSession: true, userContext });
412418
if (outputTransferMethod === "any") {
413-
outputTransferMethod = "header";
419+
const authModeHeader = getAuthModeFromHeader(req);
420+
// We default to header if we can't "parse" it or if it's undefined
421+
if (authModeHeader === "cookie") {
422+
outputTransferMethod = authModeHeader;
423+
} else {
424+
outputTransferMethod = "header";
425+
}
414426
}
415427
logDebugMessage("createNewSession: using transfer method " + outputTransferMethod);
416428

test/auth-modes.test.js

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,31 @@ describe(`auth-modes: ${printPath("[test/auth-modes.test.js]")}`, function () {
156156
});
157157

158158
describe("with user provided getTokenTransferMethod", () => {
159-
it("should use headers if getTokenTransferMethod returns any", async function () {
159+
it("should use headers if getTokenTransferMethod returns any and there is no st-auth-mode header", async function () {
160+
const connectionURI = await startST();
161+
SuperTokens.init({
162+
supertokens: {
163+
connectionURI,
164+
},
165+
appInfo: {
166+
apiDomain: "api.supertokens.io",
167+
appName: "SuperTokens",
168+
websiteDomain: "supertokens.io",
169+
},
170+
recipeList: [Session.init({ antiCsrf: "VIA_TOKEN", getTokenTransferMethod: () => "any" })],
171+
});
172+
173+
const app = getTestApp();
174+
175+
const resp = await createSession(app, undefined);
176+
assert.strictEqual(resp.accessToken, undefined);
177+
assert.strictEqual(resp.refreshToken, undefined);
178+
assert.strictEqual(resp.antiCsrf, undefined);
179+
assert.notStrictEqual(resp.accessTokenFromHeader, undefined);
180+
assert.notStrictEqual(resp.refreshTokenFromHeader, undefined);
181+
});
182+
183+
it("should use cookies if getTokenTransferMethod returns any and st-auth-mode is set to cookie", async function () {
160184
const connectionURI = await startST();
161185
SuperTokens.init({
162186
supertokens: {
@@ -173,6 +197,30 @@ describe(`auth-modes: ${printPath("[test/auth-modes.test.js]")}`, function () {
173197
const app = getTestApp();
174198

175199
const resp = await createSession(app, "cookie");
200+
assert.notStrictEqual(resp.accessToken, undefined);
201+
assert.notStrictEqual(resp.refreshToken, undefined);
202+
assert.notStrictEqual(resp.antiCsrf, undefined);
203+
assert.strictEqual(resp.accessTokenFromHeader, undefined);
204+
assert.strictEqual(resp.refreshTokenFromHeader, undefined);
205+
});
206+
207+
it("should use headers if getTokenTransferMethod returns any and st-auth-mode is set to header", async function () {
208+
const connectionURI = await startST();
209+
SuperTokens.init({
210+
supertokens: {
211+
connectionURI,
212+
},
213+
appInfo: {
214+
apiDomain: "api.supertokens.io",
215+
appName: "SuperTokens",
216+
websiteDomain: "supertokens.io",
217+
},
218+
recipeList: [Session.init({ antiCsrf: "VIA_TOKEN", getTokenTransferMethod: () => "any" })],
219+
});
220+
221+
const app = getTestApp();
222+
223+
const resp = await createSession(app, "header");
176224
assert.strictEqual(resp.accessToken, undefined);
177225
assert.strictEqual(resp.refreshToken, undefined);
178226
assert.strictEqual(resp.antiCsrf, undefined);

0 commit comments

Comments
 (0)