supertokens-website
Advanced tools
Comparing version 17.0.5 to 18.0.0
@@ -10,2 +10,15 @@ # Changelog | ||
## [18.0.0] - 2024-01-18 | ||
## Breaking Changes | ||
- The default `DateProvider` implementation relies on `localStorage`. If your environment lacks support for `localStorage`, you must provide custom implementations for either the `DateProvider` or `localStorage`. | ||
### Added | ||
- Added a `DateProvider`, that both built-in and custom validators can use instead of `Date.now` to get an estimate of the server clock. | ||
- Added the `dateProvider` prop to the configuration that can be used to customize the built-in `DateProvider`. | ||
- Added `getClockSkewInMillis` as an overrideable function that estimates the time difference between the backend and the client. | ||
- Added a test to check that relative URLs get intercepted correctly | ||
## [17.0.5] - 2024-01-03 | ||
@@ -12,0 +25,0 @@ |
@@ -801,3 +801,3 @@ "use strict"; | ||
return __awaiter(this, void 0, void 0, function () { | ||
var refreshToken, accessToken, frontToken, antiCsrfToken, tok; | ||
var refreshToken, accessToken, frontToken, responseHeaders_1, antiCsrfToken, tok; | ||
return __generator(this, function (_b) { | ||
@@ -829,2 +829,14 @@ switch (_b.label) { | ||
_b.sent(); | ||
responseHeaders_1 = new Headers(); | ||
Object.entries(response.headers).forEach(function (_b) { | ||
var key = _b[0], | ||
value = _b[1]; | ||
Array.isArray(value) | ||
? value.forEach(function (item) { | ||
return responseHeaders_1.append(key, item); | ||
}) | ||
: responseHeaders_1.append(key, value); | ||
}); | ||
(0, | ||
fetch_1.updateClockSkewUsingFrontToken)({ frontToken: frontToken, responseHeaders: responseHeaders_1 }); | ||
_b.label = 6; | ||
@@ -831,0 +843,0 @@ case 6: |
@@ -135,2 +135,3 @@ "use strict"; | ||
exports.PrimitiveArrayClaim = void 0; | ||
var dateProvider_1 = require("../utils/dateProvider"); | ||
var PrimitiveArrayClaim = /** @class */ (function () { | ||
@@ -144,2 +145,3 @@ function PrimitiveArrayClaim(config) { | ||
} | ||
var DateProvider = dateProvider_1.default.getReferenceOrThrow().dateProvider; | ||
return { | ||
@@ -151,6 +153,14 @@ id: id !== undefined ? id : _this.id, | ||
shouldRefresh: function (payload, ctx) { | ||
if (maxAgeInSeconds !== undefined && maxAgeInSeconds < DateProvider.getThresholdInSeconds()) { | ||
throw new Error( | ||
"maxAgeInSeconds must be greater than or equal to the DateProvider threshold value -> ".concat( | ||
DateProvider.getThresholdInSeconds() | ||
) | ||
); | ||
} | ||
return ( | ||
_this.getValueFromPayload(payload, ctx) === undefined || | ||
// We know payload[this.id] is defined since the value is not undefined in this branch | ||
(maxAgeInSeconds !== undefined && payload[_this.id].t < Date.now() - maxAgeInSeconds * 1000) | ||
(maxAgeInSeconds !== undefined && | ||
payload[_this.id].t < DateProvider.now() - maxAgeInSeconds * 1000) | ||
); | ||
@@ -176,3 +186,3 @@ }, | ||
} | ||
ageInSeconds = (Date.now() - this.getLastFetchedTime(payload, ctx)) / 1000; | ||
ageInSeconds = (DateProvider.now() - this.getLastFetchedTime(payload, ctx)) / 1000; | ||
if (maxAgeInSeconds !== undefined && ageInSeconds > maxAgeInSeconds) { | ||
@@ -214,2 +224,3 @@ return [ | ||
} | ||
var DateProvider = dateProvider_1.default.getReferenceOrThrow().dateProvider; | ||
return { | ||
@@ -221,6 +232,14 @@ id: id !== undefined ? id : _this.id, | ||
shouldRefresh: function (payload, ctx) { | ||
if (maxAgeInSeconds !== undefined && maxAgeInSeconds < DateProvider.getThresholdInSeconds()) { | ||
throw new Error( | ||
"maxAgeInSeconds must be greater than or equal to the DateProvider threshold value -> ".concat( | ||
DateProvider.getThresholdInSeconds() | ||
) | ||
); | ||
} | ||
return ( | ||
_this.getValueFromPayload(payload, ctx) === undefined || | ||
// We know payload[this.id] is defined since the value is not undefined in this branch | ||
(maxAgeInSeconds !== undefined && payload[_this.id].t < Date.now() - maxAgeInSeconds * 1000) | ||
(maxAgeInSeconds !== undefined && | ||
payload[_this.id].t < DateProvider.now() - maxAgeInSeconds * 1000) | ||
); | ||
@@ -246,3 +265,3 @@ }, | ||
} | ||
ageInSeconds = (Date.now() - this.getLastFetchedTime(payload, ctx)) / 1000; | ||
ageInSeconds = (DateProvider.now() - this.getLastFetchedTime(payload, ctx)) / 1000; | ||
if (maxAgeInSeconds !== undefined && ageInSeconds > maxAgeInSeconds) { | ||
@@ -284,2 +303,3 @@ return [ | ||
} | ||
var DateProvider = dateProvider_1.default.getReferenceOrThrow().dateProvider; | ||
return { | ||
@@ -291,6 +311,14 @@ id: id !== undefined ? id : _this.id, | ||
shouldRefresh: function (payload, ctx) { | ||
if (maxAgeInSeconds !== undefined && maxAgeInSeconds < DateProvider.getThresholdInSeconds()) { | ||
throw new Error( | ||
"maxAgeInSeconds must be greater than or equal to the DateProvider threshold value -> ".concat( | ||
DateProvider.getThresholdInSeconds() | ||
) | ||
); | ||
} | ||
return ( | ||
_this.getValueFromPayload(payload, ctx) === undefined || | ||
// We know payload[this.id] is defined since the value is not undefined in this branch | ||
(maxAgeInSeconds !== undefined && payload[_this.id].t < Date.now() - maxAgeInSeconds * 1000) | ||
(maxAgeInSeconds !== undefined && | ||
payload[_this.id].t < DateProvider.now() - maxAgeInSeconds * 1000) | ||
); | ||
@@ -316,3 +344,3 @@ }, | ||
} | ||
ageInSeconds = (Date.now() - this.getLastFetchedTime(payload, ctx)) / 1000; | ||
ageInSeconds = (DateProvider.now() - this.getLastFetchedTime(payload, ctx)) / 1000; | ||
if (maxAgeInSeconds !== undefined && ageInSeconds > maxAgeInSeconds) { | ||
@@ -357,2 +385,3 @@ return [ | ||
} | ||
var DateProvider = dateProvider_1.default.getReferenceOrThrow().dateProvider; | ||
return { | ||
@@ -364,6 +393,14 @@ id: id !== undefined ? id : _this.id, | ||
shouldRefresh: function (payload, ctx) { | ||
if (maxAgeInSeconds !== undefined && maxAgeInSeconds < DateProvider.getThresholdInSeconds()) { | ||
throw new Error( | ||
"maxAgeInSeconds must be greater than or equal to the DateProvider threshold value -> ".concat( | ||
DateProvider.getThresholdInSeconds() | ||
) | ||
); | ||
} | ||
return ( | ||
_this.getValueFromPayload(payload, ctx) === undefined || | ||
// We know payload[this.id] is defined since the value is not undefined in this branch | ||
(maxAgeInSeconds !== undefined && payload[_this.id].t < Date.now() - maxAgeInSeconds * 1000) | ||
(maxAgeInSeconds !== undefined && | ||
payload[_this.id].t < DateProvider.now() - maxAgeInSeconds * 1000) | ||
); | ||
@@ -389,3 +426,3 @@ }, | ||
} | ||
ageInSeconds = (Date.now() - this.getLastFetchedTime(payload, ctx)) / 1000; | ||
ageInSeconds = (DateProvider.now() - this.getLastFetchedTime(payload, ctx)) / 1000; | ||
if (maxAgeInSeconds !== undefined && ageInSeconds > maxAgeInSeconds) { | ||
@@ -430,2 +467,3 @@ return [ | ||
} | ||
var DateProvider = dateProvider_1.default.getReferenceOrThrow().dateProvider; | ||
return { | ||
@@ -437,6 +475,14 @@ id: id !== undefined ? id : _this.id, | ||
shouldRefresh: function (payload, ctx) { | ||
if (maxAgeInSeconds !== undefined && maxAgeInSeconds < DateProvider.getThresholdInSeconds()) { | ||
throw new Error( | ||
"maxAgeInSeconds must be greater than or equal to the DateProvider threshold value -> ".concat( | ||
DateProvider.getThresholdInSeconds() | ||
) | ||
); | ||
} | ||
return ( | ||
_this.getValueFromPayload(payload, ctx) === undefined || | ||
// We know payload[this.id] is defined since the value is not undefined in this branch | ||
(maxAgeInSeconds !== undefined && payload[_this.id].t < Date.now() - maxAgeInSeconds * 1000) | ||
(maxAgeInSeconds !== undefined && | ||
payload[_this.id].t < DateProvider.now() - maxAgeInSeconds * 1000) | ||
); | ||
@@ -462,3 +508,3 @@ }, | ||
} | ||
ageInSeconds = (Date.now() - this.getLastFetchedTime(payload, ctx)) / 1000; | ||
ageInSeconds = (DateProvider.now() - this.getLastFetchedTime(payload, ctx)) / 1000; | ||
if (maxAgeInSeconds !== undefined && ageInSeconds > maxAgeInSeconds) { | ||
@@ -465,0 +511,0 @@ return [ |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.PrimitiveClaim = void 0; | ||
var dateProvider_1 = require("../utils/dateProvider"); | ||
var PrimitiveClaim = /** @class */ (function () { | ||
@@ -12,2 +13,3 @@ function PrimitiveClaim(config) { | ||
} | ||
var DateProvider = dateProvider_1.default.getReferenceOrThrow().dateProvider; | ||
return { | ||
@@ -19,6 +21,14 @@ id: id !== undefined ? id : _this.id, | ||
shouldRefresh: function (payload, ctx) { | ||
if (maxAgeInSeconds !== undefined && maxAgeInSeconds < DateProvider.getThresholdInSeconds()) { | ||
throw new Error( | ||
"maxAgeInSeconds must be greater than or equal to the DateProvider threshold value -> ".concat( | ||
DateProvider.getThresholdInSeconds() | ||
) | ||
); | ||
} | ||
return ( | ||
_this.getValueFromPayload(payload, ctx) === undefined || | ||
// We know payload[this.id] is defined since the value is not undefined in this branch | ||
(maxAgeInSeconds !== undefined && payload[_this.id].t < Date.now() - maxAgeInSeconds * 1000) | ||
(maxAgeInSeconds !== undefined && | ||
payload[_this.id].t < DateProvider.now() - maxAgeInSeconds * 1000) | ||
); | ||
@@ -34,3 +44,3 @@ }, | ||
} | ||
var ageInSeconds = (Date.now() - _this.getLastFetchedTime(payload, ctx)) / 1000; | ||
var ageInSeconds = (DateProvider.now() - _this.getLastFetchedTime(payload, ctx)) / 1000; | ||
if (maxAgeInSeconds !== undefined && ageInSeconds > maxAgeInSeconds) { | ||
@@ -37,0 +47,0 @@ return { |
@@ -67,1 +67,8 @@ import { RecipeInterface, NormalisedInputType, ResponseWithBody, TokenType } from "./types"; | ||
export declare function fireSessionUpdateEventsIfNecessary(wasLoggedIn: boolean, status: number, frontTokenHeaderFromResponse: string | null | undefined): void; | ||
/** | ||
* Updates the clock skew based on the provided frontToken and responseHeaders. | ||
*/ | ||
export declare const updateClockSkewUsingFrontToken: ({ frontToken, responseHeaders }: { | ||
frontToken: string | undefined | null; | ||
responseHeaders: Headers; | ||
}) => void; |
@@ -172,2 +172,3 @@ "use strict"; | ||
var logger_1 = require("./logger"); | ||
var dateProvider_1 = require("./utils/dateProvider"); | ||
var AuthHttpRequest = /** @class */ (function () { | ||
@@ -178,2 +179,3 @@ function AuthHttpRequest() {} | ||
windowHandler_1.default.init(options.windowHandler); | ||
dateProvider_1.default.init(options.dateProvider); | ||
lockFactory_1.default.init( | ||
@@ -180,0 +182,0 @@ options.lockFactory, |
@@ -155,2 +155,3 @@ "use strict"; | ||
var utils_1 = require("./utils"); | ||
var dateProvider_1 = require("./utils/dateProvider"); | ||
function RecipeImplementation(recipeImplInput) { | ||
@@ -263,3 +264,4 @@ return { | ||
} | ||
if (!(tokenInfo.ate < Date.now())) return [3 /*break*/, 5]; | ||
if (!(tokenInfo.ate < dateProvider_1.default.getReferenceOrThrow().dateProvider.now())) | ||
return [3 /*break*/, 5]; | ||
(0, | ||
@@ -304,3 +306,4 @@ logger_1.logDebugMessage)("getAccessTokenPayloadSecurely: access token expired. Refreshing session"); | ||
} | ||
if (!(tokenInfo.ate < Date.now())) return [3 /*break*/, 4]; | ||
if (!(tokenInfo.ate < dateProvider_1.default.getReferenceOrThrow().dateProvider.now())) | ||
return [3 /*break*/, 4]; | ||
(0, logger_1.logDebugMessage)("doesSessionExist: access token expired. Refreshing session"); | ||
@@ -541,2 +544,18 @@ return [4 /*yield*/, (0, fetch_1.getLocalSessionState)(false)]; | ||
} | ||
}, | ||
calculateClockSkewInMillis: function (_a) { | ||
var accessTokenPayload = _a.accessTokenPayload; | ||
(0, logger_1.logDebugMessage)("calculateClockSkewInMillis: called"); | ||
var tokenIssuedAt = | ||
accessTokenPayload === null || accessTokenPayload === void 0 ? void 0 : accessTokenPayload.iat; | ||
if (tokenIssuedAt === undefined || typeof tokenIssuedAt !== "number") { | ||
(0, logger_1.logDebugMessage)( | ||
"calculateClockSkewInMillis: payload iat is undefined or not a number. This may happen due to an unsupported backend sdk. Returning 0" | ||
); | ||
return 0; | ||
} | ||
var estimatedServerTimeNow = tokenIssuedAt * 1000; | ||
var clockSkewInMillis = estimatedServerTimeNow - Date.now(); | ||
(0, logger_1.logDebugMessage)("calculateClockSkewInMillis: returning " + clockSkewInMillis); | ||
return clockSkewInMillis; | ||
} | ||
@@ -543,0 +562,0 @@ }; |
@@ -5,2 +5,3 @@ import OverrideableBuilder from "supertokens-js-override"; | ||
import { LockFactory } from "./utils/lockFactory/types"; | ||
import { DateProviderInput } from "./utils/dateProvider/types"; | ||
export declare type Event = { | ||
@@ -39,2 +40,3 @@ action: "SIGN_OUT" | "REFRESH_SESSION" | "SESSION_CREATED" | "ACCESS_TOKEN_PAYLOAD_UPDATED"; | ||
windowHandler?: WindowHandlerInput; | ||
dateProvider?: DateProviderInput; | ||
preAPIHook?: RecipePreAPIHookFunction; | ||
@@ -128,2 +130,6 @@ postAPIHook?: RecipePostAPIHookFunction; | ||
shouldDoInterceptionBasedOnUrl(toCheckUrl: string, apiDomain: string, sessionTokenBackendDomain: string | undefined): boolean; | ||
calculateClockSkewInMillis(params: { | ||
accessTokenPayload: any; | ||
responseHeaders: Headers; | ||
}): number; | ||
}; | ||
@@ -130,0 +136,0 @@ export declare type ClaimValidationResult = { |
@@ -1,2 +0,2 @@ | ||
export declare const package_version = "17.0.5"; | ||
export declare const package_version = "18.0.0"; | ||
export declare const supported_fdi: string[]; |
@@ -18,3 +18,3 @@ "use strict"; | ||
*/ | ||
exports.package_version = "17.0.5"; | ||
exports.package_version = "18.0.0"; | ||
exports.supported_fdi = ["1.16", "1.17", "1.18"]; |
@@ -827,2 +827,3 @@ "use strict"; | ||
_a.sent(); | ||
(0, fetch_1.updateClockSkewUsingFrontToken)({ frontToken: frontToken, responseHeaders: headers }); | ||
_a.label = 6; | ||
@@ -829,0 +830,0 @@ case 6: |
{ | ||
"name": "supertokens-website", | ||
"version": "17.0.5", | ||
"version": "18.0.0", | ||
"description": "frontend sdk for website to be used for auth solution.", | ||
@@ -71,3 +71,3 @@ "main": "index.js", | ||
"path": "lib/build/bundleEntry.js", | ||
"limit": "20kb" | ||
"limit": "21kb" | ||
} | ||
@@ -74,0 +74,0 @@ ], |
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
399781
92
7824