Skip to content
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

feat: add threshold in DateProvider #242

Merged
merged 7 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bundle/bundle.js

Large diffs are not rendered by default.

35 changes: 35 additions & 0 deletions lib/build/claims/primitiveArrayClaim.js

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

7 changes: 7 additions & 0 deletions lib/build/claims/primitiveClaim.js

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/fetch.js

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

8 changes: 4 additions & 4 deletions lib/build/recipeImplementation.js

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/types.d.ts

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

3 changes: 3 additions & 0 deletions lib/build/utils/dateProvider/defaultImplementation.d.ts

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

11 changes: 10 additions & 1 deletion lib/build/utils/dateProvider/defaultImplementation.js

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

15 changes: 6 additions & 9 deletions lib/build/utils/dateProvider/index.js

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

4 changes: 3 additions & 1 deletion lib/build/utils/dateProvider/types.d.ts

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

25 changes: 25 additions & 0 deletions lib/ts/claims/primitiveArrayClaim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ export class PrimitiveArrayClaim<ValueType> {
id?: string
): SessionClaimValidator => {
const DateProvider = DateProviderReference.getReferenceOrThrow().dateProvider;
if (maxAgeInSeconds !== undefined && maxAgeInSeconds < DateProvider.getThresholdInSeconds()) {
throw new Error(
`maxAgeInSeconds must be greater than the DateProvider threshold value -> ${DateProvider.getThresholdInSeconds()}`
);
}
return {
id: id !== undefined ? id : this.id,
refresh: ctx => this.refresh(ctx),
Expand Down Expand Up @@ -75,6 +80,11 @@ export class PrimitiveArrayClaim<ValueType> {
id?: string
): SessionClaimValidator => {
const DateProvider = DateProviderReference.getReferenceOrThrow().dateProvider;
if (maxAgeInSeconds !== undefined && maxAgeInSeconds < DateProvider.getThresholdInSeconds()) {
throw new Error(
`maxAgeInSeconds must be greater than the DateProvider threshold value -> ${DateProvider.getThresholdInSeconds()}`
);
}
return {
id: id !== undefined ? id : this.id,
refresh: ctx => this.refresh(ctx),
Expand Down Expand Up @@ -121,6 +131,11 @@ export class PrimitiveArrayClaim<ValueType> {
id?: string
): SessionClaimValidator => {
const DateProvider = DateProviderReference.getReferenceOrThrow().dateProvider;
if (maxAgeInSeconds !== undefined && maxAgeInSeconds < DateProvider.getThresholdInSeconds()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these checks should be in shouldRefresh

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

throw new Error(
`maxAgeInSeconds must be greater than the DateProvider threshold value -> ${DateProvider.getThresholdInSeconds()}`
);
}
return {
id: id !== undefined ? id : this.id,
refresh: ctx => this.refresh(ctx),
Expand Down Expand Up @@ -164,6 +179,11 @@ export class PrimitiveArrayClaim<ValueType> {
id?: string
): SessionClaimValidator => {
const DateProvider = DateProviderReference.getReferenceOrThrow().dateProvider;
if (maxAgeInSeconds !== undefined && maxAgeInSeconds < DateProvider.getThresholdInSeconds()) {
throw new Error(
`maxAgeInSeconds must be greater than the DateProvider threshold value -> ${DateProvider.getThresholdInSeconds()}`
);
}
return {
id: id !== undefined ? id : this.id,
refresh: ctx => this.refresh(ctx),
Expand Down Expand Up @@ -211,6 +231,11 @@ export class PrimitiveArrayClaim<ValueType> {
id?: string
): SessionClaimValidator => {
const DateProvider = DateProviderReference.getReferenceOrThrow().dateProvider;
if (maxAgeInSeconds !== undefined && maxAgeInSeconds < DateProvider.getThresholdInSeconds()) {
throw new Error(
`maxAgeInSeconds must be greater than the DateProvider threshold value -> ${DateProvider.getThresholdInSeconds()}`
);
}
return {
id: id !== undefined ? id : this.id,
refresh: ctx => this.refresh(ctx),
Expand Down
17 changes: 13 additions & 4 deletions lib/ts/claims/primitiveClaim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,19 @@ export class PrimitiveClaim<ValueType> {
return {
id: id !== undefined ? id : this.id,
refresh: ctx => this.refresh(ctx),
shouldRefresh: (payload, ctx) =>
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 < DateProvider.now() - maxAgeInSeconds * 1000),
shouldRefresh: (payload, ctx) => {
if (maxAgeInSeconds !== undefined && maxAgeInSeconds < DateProvider.getThresholdInSeconds()) {
throw new Error(
`maxAgeInSeconds must be greater than the DateProvider threshold value -> ${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 < DateProvider.now() - maxAgeInSeconds * 1000)
);
},
validate: (payload, ctx) => {
const claimVal = this.getValueFromPayload(payload, ctx);
if (claimVal === undefined) {
Expand Down
2 changes: 1 addition & 1 deletion lib/ts/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -951,7 +951,7 @@ export const updateClockSkewUsingFrontToken = ({
}

const frontTokenPayload = parseFrontToken(frontToken);
const clockSkewInMillis = AuthHttpRequest.recipeImpl.getClockSkewInMillis({
const clockSkewInMillis = AuthHttpRequest.recipeImpl.calculateClockSkewInMillis({
accessTokenPayload: frontTokenPayload.up,
responseHeaders
});
Expand Down
8 changes: 4 additions & 4 deletions lib/ts/recipeImplementation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,25 +311,25 @@ export default function RecipeImplementation(recipeImplInput: {
}
},

getClockSkewInMillis: function ({
calculateClockSkewInMillis: function ({
accessTokenPayload
}: {
accessTokenPayload: any;
responseHeaders: Headers;
}): number {
logDebugMessage("getClockSkewInMillis: called");
logDebugMessage("calculateClockSkewInMillis: called");

const tokenIssuedAt = accessTokenPayload?.iat;
if (tokenIssuedAt === undefined || typeof tokenIssuedAt !== "number") {
logDebugMessage(
`getClockSkewInMillis: payload iat is undefined or not a number. This may happen due to an unsupported backend sdk. Returning 0`
`calculateClockSkewInMillis: payload iat is undefined or not a number. This may happen due to an unsupported backend sdk. Returning 0`
);
return 0;
}

const estimatedServerTimeNow = tokenIssuedAt * 1000;
const clockSkewInMillis = estimatedServerTimeNow - Date.now();
logDebugMessage("getClockSkewInMillis: returning " + clockSkewInMillis);
logDebugMessage("calculateClockSkewInMillis: returning " + clockSkewInMillis);

return clockSkewInMillis;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/ts/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ export type RecipeInterface = {
sessionTokenBackendDomain: string | undefined
): boolean;

getClockSkewInMillis(params: { accessTokenPayload: any; responseHeaders: Headers }): number;
calculateClockSkewInMillis(params: { accessTokenPayload: any; responseHeaders: Headers }): number;
};

export type ClaimValidationResult = { isValid: true } | { isValid: false; reason?: any };
Expand Down
13 changes: 12 additions & 1 deletion lib/ts/utils/dateProvider/defaultImplementation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ export class DateProvider {
private static instance?: DateProvider;
public static readonly CLOCK_SKEW_KEY = "__st_clockSkewInMillis";
private clockSkewInMillis: number = 0;
// Ensure a meaningful clock skew value by setting a threshold. Omitting a threshold would invariably lead to some
// clock skew due to server-client latency, arising from the time it takes for the token to arrive after being issued.
private thresholdInSeconds = 7;

// The static init method is used to create a singleton instance of DateProvider,
// as we require access to localStorage for initializing clockSkewInMillis.
Expand Down Expand Up @@ -56,8 +59,16 @@ export class DateProvider {
return DateProvider.instance;
}

getThresholdInSeconds(): number {
return this.thresholdInSeconds;
}

setThresholdInSeconds(thresholdInSeconds: number): void {
this.thresholdInSeconds = thresholdInSeconds;
}

setClientClockSkewInMillis(clockSkewInMillis: number): void {
this.clockSkewInMillis = clockSkewInMillis;
this.clockSkewInMillis = Math.abs(clockSkewInMillis) > this.thresholdInSeconds * 1000 ? clockSkewInMillis : 0;
const localStorage = WindowHandlerReference.getReferenceOrThrow().windowHandler.localStorage;
localStorage.setItemSync(DateProvider.CLOCK_SKEW_KEY, String(clockSkewInMillis));
}
Expand Down
Loading
Loading