Skip to content

Commit

Permalink
refactor: chores and consistency fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
porcellus committed Nov 19, 2024
1 parent 9d68b6e commit 8f7f339
Show file tree
Hide file tree
Showing 14 changed files with 40 additions and 36 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [unreleased]

## [21.1.0] - 2024-11-19

- Adds `getCookieNameForTokenType` config option to allow customizing the cookie name for a token type.
- Adds `getResponseHeaderNameForTokenType` config option to allow customizing the response header name for a token type.
- Please note, that using this will require further customizations on the frontend

## [21.0.0] - 2024-10-07

- Added OAuth2Provider recipe
Expand Down
6 changes: 3 additions & 3 deletions lib/build/recipe/session/cookieAndHeaders.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ function getCORSAllowedHeaders() {
exports.getCORSAllowedHeaders = getCORSAllowedHeaders;
function getToken(config, req, tokenType, transferMethod, userContext) {
if (transferMethod === "cookie") {
return req.getCookieValue(config.getCookieNameFromTokenType(req, tokenType, userContext));
return req.getCookieValue(config.getCookieNameForTokenType(req, tokenType, userContext));
} else if (transferMethod === "header") {
const value = req.getHeaderValue(constants_2.authorizationHeaderKey);
if (value === undefined || !value.startsWith("Bearer ")) {
Expand All @@ -101,7 +101,7 @@ function setToken(config, res, tokenType, value, expires, transferMethod, req, u
setCookie(
config,
res,
config.getCookieNameFromTokenType(req, tokenType, userContext),
config.getCookieNameForTokenType(req, tokenType, userContext),
value,
expires,
tokenType === "refresh" ? "refreshTokenPath" : "accessTokenPath",
Expand Down Expand Up @@ -220,7 +220,7 @@ function hasMultipleCookiesForTokenType(config, req, tokenType, userContext) {
return false;
}
const cookies = parseCookieStringFromRequestHeaderAllowingDuplicates(cookieString);
const cookieName = config.getCookieNameFromTokenType(req, tokenType, userContext);
const cookieName = config.getCookieNameForTokenType(req, tokenType, userContext);
return cookies[cookieName] !== undefined && cookies[cookieName].length > 1;
}
exports.hasMultipleCookiesForTokenType = hasMultipleCookiesForTokenType;
Expand Down
4 changes: 2 additions & 2 deletions lib/build/recipe/session/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export declare type TypeInput = {
forCreateNewSession: boolean;
userContext: UserContext;
}) => TokenTransferMethod | "any";
getCookieNameFromTokenType?: (req: BaseRequest, tokenType: TokenType, userContext: UserContext) => string;
getCookieNameForTokenType?: (req: BaseRequest, tokenType: TokenType, userContext: UserContext) => string;
getResponseHeaderNameForTokenType?: (req: BaseRequest, tokenType: TokenType, userContext: UserContext) => string;
errorHandlers?: ErrorHandlers;
antiCsrf?: "VIA_TOKEN" | "VIA_CUSTOM_HEADER" | "NONE";
Expand All @@ -76,7 +76,7 @@ export declare type TypeNormalisedInput = {
userContext: UserContext;
}) => "strict" | "lax" | "none";
cookieSecure: boolean;
getCookieNameFromTokenType: (req: BaseRequest, tokenType: TokenType, userContext: UserContext) => string;
getCookieNameForTokenType: (req: BaseRequest, tokenType: TokenType, userContext: UserContext) => string;
getResponseHeaderNameForTokenType: (req: BaseRequest, tokenType: TokenType, userContext: UserContext) => string;
sessionExpiredStatusCode: number;
errorHandlers: NormalisedErrorHandlers;
Expand Down
2 changes: 1 addition & 1 deletion lib/build/recipe/session/utils.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export declare function validateClaimsInPayload(
reason: import("../../types").JSONValue;
}[]
>;
export declare function getCookieNameFromTokenType(
export declare function getCookieNameForTokenType(
_req: BaseRequest,
tokenType: TokenType
): "sAccessToken" | "sRefreshToken";
Expand Down
12 changes: 6 additions & 6 deletions lib/build/recipe/session/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var __importDefault =
return mod && mod.__esModule ? mod : { default: mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getResponseHeaderNameForTokenType = exports.getCookieNameFromTokenType = exports.validateClaimsInPayload = exports.getRequiredClaimValidators = exports.setAccessTokenInResponse = exports.normaliseSameSiteOrThrowError = exports.validateAndNormaliseUserInput = exports.getURLProtocol = exports.normaliseSessionScopeOrThrowError = exports.sendTokenTheftDetectedResponse = exports.sendInvalidClaimResponse = exports.sendUnauthorisedResponse = exports.sendTryRefreshTokenResponse = void 0;
exports.getResponseHeaderNameForTokenType = exports.getCookieNameForTokenType = exports.validateClaimsInPayload = exports.getRequiredClaimValidators = exports.setAccessTokenInResponse = exports.normaliseSameSiteOrThrowError = exports.validateAndNormaliseUserInput = exports.getURLProtocol = exports.normaliseSessionScopeOrThrowError = exports.sendTokenTheftDetectedResponse = exports.sendInvalidClaimResponse = exports.sendUnauthorisedResponse = exports.sendTryRefreshTokenResponse = void 0;
const cookieAndHeaders_1 = require("./cookieAndHeaders");
const recipe_1 = __importDefault(require("./recipe"));
const constants_1 = require("./constants");
Expand Down Expand Up @@ -221,11 +221,11 @@ function validateAndNormaliseUserInput(recipeInstance, appInfo, config) {
(config === null || config === void 0 ? void 0 : config.getTokenTransferMethod) === undefined
? defaultGetTokenTransferMethod
: config.getTokenTransferMethod,
getCookieNameFromTokenType:
(_d = config === null || config === void 0 ? void 0 : config.getCookieNameFromTokenType) !== null &&
getCookieNameForTokenType:
(_d = config === null || config === void 0 ? void 0 : config.getCookieNameForTokenType) !== null &&
_d !== void 0
? _d
: getCookieNameFromTokenType,
: getCookieNameForTokenType,
getResponseHeaderNameForTokenType:
(_e = config === null || config === void 0 ? void 0 : config.getResponseHeaderNameForTokenType) !== null &&
_e !== void 0
Expand Down Expand Up @@ -341,7 +341,7 @@ function defaultGetTokenTransferMethod({ req, forCreateNewSession }) {
return "any";
}
}
function getCookieNameFromTokenType(_req, tokenType) {
function getCookieNameForTokenType(_req, tokenType) {
switch (tokenType) {
case "access":
return constants_1.accessTokenCookieKey;
Expand All @@ -351,7 +351,7 @@ function getCookieNameFromTokenType(_req, tokenType) {
throw new Error("Unknown token type, should never happen.");
}
}
exports.getCookieNameFromTokenType = getCookieNameFromTokenType;
exports.getCookieNameForTokenType = getCookieNameForTokenType;
function getResponseHeaderNameForTokenType(_req, tokenType) {
switch (tokenType) {
case "access":
Expand Down
2 changes: 1 addition & 1 deletion lib/build/version.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/build/version.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions lib/ts/recipe/session/cookieAndHeaders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export function getToken(
userContext: UserContext
) {
if (transferMethod === "cookie") {
return req.getCookieValue(config.getCookieNameFromTokenType(req, tokenType, userContext));
return req.getCookieValue(config.getCookieNameForTokenType(req, tokenType, userContext));
} else if (transferMethod === "header") {
const value = req.getHeaderValue(authorizationHeaderKey);
if (value === undefined || !value.startsWith("Bearer ")) {
Expand Down Expand Up @@ -126,7 +126,7 @@ export function setToken(
setCookie(
config,
res,
config.getCookieNameFromTokenType(req, tokenType, userContext),
config.getCookieNameForTokenType(req, tokenType, userContext),
value,
expires,
tokenType === "refresh" ? "refreshTokenPath" : "accessTokenPath",
Expand Down Expand Up @@ -274,7 +274,7 @@ export function hasMultipleCookiesForTokenType(
}

const cookies = parseCookieStringFromRequestHeaderAllowingDuplicates(cookieString);
const cookieName = config.getCookieNameFromTokenType(req, tokenType, userContext);
const cookieName = config.getCookieNameForTokenType(req, tokenType, userContext);
return cookies[cookieName] !== undefined && cookies[cookieName].length > 1;
}

Expand Down
4 changes: 2 additions & 2 deletions lib/ts/recipe/session/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export type TypeInput = {
forCreateNewSession: boolean;
userContext: UserContext;
}) => TokenTransferMethod | "any";
getCookieNameFromTokenType?: (req: BaseRequest, tokenType: TokenType, userContext: UserContext) => string;
getCookieNameForTokenType?: (req: BaseRequest, tokenType: TokenType, userContext: UserContext) => string;
getResponseHeaderNameForTokenType?: (req: BaseRequest, tokenType: TokenType, userContext: UserContext) => string;

errorHandlers?: ErrorHandlers;
Expand All @@ -101,7 +101,7 @@ export type TypeNormalisedInput = {
userContext: UserContext;
}) => "strict" | "lax" | "none";
cookieSecure: boolean;
getCookieNameFromTokenType: (req: BaseRequest, tokenType: TokenType, userContext: UserContext) => string;
getCookieNameForTokenType: (req: BaseRequest, tokenType: TokenType, userContext: UserContext) => string;
getResponseHeaderNameForTokenType: (req: BaseRequest, tokenType: TokenType, userContext: UserContext) => string;
sessionExpiredStatusCode: number;
errorHandlers: NormalisedErrorHandlers;
Expand Down
4 changes: 2 additions & 2 deletions lib/ts/recipe/session/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ export function validateAndNormaliseUserInput(
config?.getTokenTransferMethod === undefined
? defaultGetTokenTransferMethod
: config.getTokenTransferMethod,
getCookieNameFromTokenType: config?.getCookieNameFromTokenType ?? getCookieNameFromTokenType,
getCookieNameForTokenType: config?.getCookieNameForTokenType ?? getCookieNameForTokenType,
getResponseHeaderNameForTokenType:
config?.getResponseHeaderNameForTokenType ?? getResponseHeaderNameForTokenType,
cookieDomain,
Expand Down Expand Up @@ -435,7 +435,7 @@ function defaultGetTokenTransferMethod({
}
}

export function getCookieNameFromTokenType(_req: BaseRequest, tokenType: TokenType) {
export function getCookieNameForTokenType(_req: BaseRequest, tokenType: TokenType) {
switch (tokenType) {
case "access":
return accessTokenCookieKey;
Expand Down
2 changes: 1 addition & 1 deletion lib/ts/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* License for the specific language governing permissions and limitations
* under the License.
*/
export const version = "21.0.0";
export const version = "21.1.0";

export const cdiSupported = ["5.2"];

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "supertokens-node",
"version": "21.0.0",
"version": "21.1.0",
"description": "NodeJS driver for SuperTokens core",
"main": "index.js",
"scripts": {
Expand Down
20 changes: 9 additions & 11 deletions test/test-server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import Multitenancy from "../../../recipe/multitenancy";
import Passwordless from "../../../recipe/passwordless";
import Session from "../../../recipe/session";
import { verifySession } from "../../../recipe/session/framework/express";
import { getResponseHeaderNameForTokenType, getCookieNameFromTokenType } from "../../../lib/build/recipe/session/utils";
import { getResponseHeaderNameForTokenType, getCookieNameForTokenType } from "../../../lib/build/recipe/session/utils";
import ThirdParty from "../../../recipe/thirdparty";
import TOTP from "../../../recipe/totp";
import OAuth2Provider from "../../../recipe/oauth2provider";
Expand Down Expand Up @@ -148,15 +148,13 @@ function initST(config: any) {
recipeList.push(
Session.init({
...config,
getResponseHeaderNameForTokenType: callbackWithLog(
getResponseHeaderNameForTokenType: loggingOverrideFuncSync(
"Session.getResponseHeaderNameForTokenType",
config?.getResponseHeaderNameForTokenType,
getResponseHeaderNameForTokenType
),
getCookieNameFromTokenType: callbackWithLog(
"Session.getCookieNameFromTokenType",
config?.getCookieNameFromTokenType,
getCookieNameFromTokenType
getCookieNameForTokenType: loggingOverrideFuncSync(
"Session.getCookieNameForTokenType",
getCookieNameForTokenType
),
override: {
apis: overrideBuilderWithLogging("Session.override.apis", config?.override?.apis),
Expand Down Expand Up @@ -230,9 +228,9 @@ function initST(config: any) {
getEmailForRecipeUserId: callbackWithLog(
"EmailVerification.getEmailForRecipeUserId",
config?.getEmailForRecipeUserId,
() => {
status: "UNKNOWN_USER_ID_ERROR";
}
() => ({
status: "UNKNOWN_USER_ID_ERROR",
})
),
override: {
apis: overrideBuilderWithLogging("EmailVerification.override.apis", config?.override?.apis),
Expand Down Expand Up @@ -476,7 +474,7 @@ app.listen(API_PORT, "localhost", () => {
logDebugMessage(`node-test-server-server started on localhost:${API_PORT}`);
});

function loggingOverrideFuncSync<T>(name: string, originalImpl: (...args: any[]) => Promise<T>) {
function loggingOverrideFuncSync<T>(name: string, originalImpl: (...args: any[]) => T) {
return function (...args: any[]) {
logOverrideEvent(name, "CALL", args);
try {
Expand Down

0 comments on commit 8f7f339

Please sign in to comment.