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 max. cache period to auth methods #208

Merged
Show file tree
Hide file tree
Changes from all 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
30 changes: 28 additions & 2 deletions packages/lit-auth-client/src/lib/providers/DiscordProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import {
import { ethers } from 'ethers';
import { sha256 } from 'ethers/lib/utils';

const MAX_EXPIRATION_LENGTH = 30;
const MAX_EXPIRATION_UNIT = 'minutes';

export default class DiscordProvider extends BaseProvider {
/**
* The redirect URI that Lit's login server should send the user back to
Expand Down Expand Up @@ -136,11 +139,34 @@ export default class DiscordProvider extends BaseProvider {
const storageUID = this.getAuthMethodStorageUID(accessToken);

if (this.storageProvider.isExpired(storageUID)) {
const expirationLength =
_options.expirationLength ?? MAX_EXPIRATION_LENGTH;
const expirationUnit = _options.expirationUnit ?? MAX_EXPIRATION_UNIT;

const userExpirationISOString = this.storageProvider.convertToISOString(
expirationLength,
expirationUnit
);

const maxExpirationISOString = this.storageProvider.convertToISOString(
MAX_EXPIRATION_LENGTH,
MAX_EXPIRATION_UNIT
);

const userExpirationDate = new Date(userExpirationISOString);
const maxExpirationDate = new Date(maxExpirationISOString); // Just convert the ISO string to a Date

if (userExpirationDate > maxExpirationDate) {
throw new Error(
`The expiration date for this auth method cannot be more than ${MAX_EXPIRATION_LENGTH} ${MAX_EXPIRATION_UNIT} from now. Please provide a valid expiration length and unit.}`
);
}

this.storageProvider.setExpirableItem(
storageUID,
JSON.stringify(authMethod),
_options.expirationLength ?? 24,
_options.expirationUnit ?? 'hours'
expirationLength,
expirationUnit
);
}
}
Expand Down
30 changes: 28 additions & 2 deletions packages/lit-auth-client/src/lib/providers/GoogleProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import { BaseProvider } from './BaseProvider';
import { ethers } from 'ethers';
import * as jose from 'jose';

const MAX_EXPIRATION_LENGTH = 30;
const MAX_EXPIRATION_UNIT = 'minutes';

// import {
// LitAbility,
// LitAccessControlConditionResource,
Expand Down Expand Up @@ -132,11 +135,34 @@ export default class GoogleProvider extends BaseProvider {
const storageUID = this.getAuthMethodStorageUID(idToken);

if (this.storageProvider.isExpired(storageUID)) {
const expirationLength =
_options.expirationLength ?? MAX_EXPIRATION_LENGTH;
const expirationUnit = _options.expirationUnit ?? MAX_EXPIRATION_UNIT;

const userExpirationISOString = this.storageProvider.convertToISOString(
expirationLength,
expirationUnit
);

const maxExpirationISOString = this.storageProvider.convertToISOString(
MAX_EXPIRATION_LENGTH,
MAX_EXPIRATION_UNIT
);

const userExpirationDate = new Date(userExpirationISOString);
const maxExpirationDate = new Date(maxExpirationISOString); // Just convert the ISO string to a Date

if (userExpirationDate > maxExpirationDate) {
throw new Error(
`The expiration date for this auth method cannot be more than ${MAX_EXPIRATION_LENGTH} ${MAX_EXPIRATION_UNIT} from now. Please provide a valid expiration length and unit.}`
);
}

this.storageProvider.setExpirableItem(
storageUID,
JSON.stringify(authMethod),
_options.expirationLength ?? 24,
_options.expirationUnit ?? 'hours'
expirationLength,
expirationUnit
);
}
}
Expand Down
32 changes: 30 additions & 2 deletions packages/lit-auth-client/src/lib/providers/OtpProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import { BaseProvider } from './BaseProvider';
import { OtpProviderOptions } from '@lit-protocol/types';
import { ethers } from 'ethers';

const MAX_EXPIRATION_LENGTH = 30;
const MAX_EXPIRATION_UNIT = 'minutes';

export class OtpProvider extends BaseProvider {
#accessToken: string | undefined;

Expand Down Expand Up @@ -76,11 +79,36 @@ export class OtpProvider extends BaseProvider {
const storageUID = this.getAuthMethodStorageUID(accessToken);

if (this.storageProvider.isExpired(storageUID)) {
const expirationLength =
_options.expirationLength ?? MAX_EXPIRATION_LENGTH;
const expirationUnit = _options.expirationUnit ?? MAX_EXPIRATION_UNIT;

const userExpirationISOString =
this.storageProvider.convertToISOString(
expirationLength,
expirationUnit
);

const maxExpirationISOString =
this.storageProvider.convertToISOString(
MAX_EXPIRATION_LENGTH,
MAX_EXPIRATION_UNIT
);

const userExpirationDate = new Date(userExpirationISOString);
const maxExpirationDate = new Date(maxExpirationISOString); // Just convert the ISO string to a Date

if (userExpirationDate > maxExpirationDate) {
throw new Error(
`The expiration date for this auth method cannot be more than ${MAX_EXPIRATION_LENGTH} ${MAX_EXPIRATION_UNIT} from now. Please provide a valid expiration length and unit.}`
);
}

this.storageProvider.setExpirableItem(
storageUID,
item,
_options.expirationLength ?? 24,
_options.expirationUnit ?? 'hours'
expirationLength,
expirationUnit
);
}
}
Expand Down
30 changes: 28 additions & 2 deletions packages/lit-auth-client/src/lib/providers/WebAuthnProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import { getRPIdFromOrigin, parseAuthenticatorData } from '../utils';
import { BaseProvider } from './BaseProvider';
import { RegistrationResponseJSON } from '@simplewebauthn/typescript-types';

const MAX_EXPIRATION_LENGTH = 3;
const MAX_EXPIRATION_UNIT = 'minutes';

export default class WebAuthnProvider extends BaseProvider {
/**
* Name of relying party. Defaults to "lit"
Expand Down Expand Up @@ -208,11 +211,34 @@ export default class WebAuthnProvider extends BaseProvider {
const storageUID = this.getAuthMethodStorageUID(authMethod.accessToken);

if (this.storageProvider.isExpired(storageUID)) {
const expirationLength =
_options.expirationLength ?? MAX_EXPIRATION_LENGTH;
const expirationUnit = _options.expirationUnit ?? MAX_EXPIRATION_UNIT;

const userExpirationISOString = this.storageProvider.convertToISOString(
expirationLength,
expirationUnit
);

const maxExpirationISOString = this.storageProvider.convertToISOString(
MAX_EXPIRATION_LENGTH,
MAX_EXPIRATION_UNIT
);

const userExpirationDate = new Date(userExpirationISOString);
const maxExpirationDate = new Date(maxExpirationISOString); // Just convert the ISO string to a Date

if (userExpirationDate > maxExpirationDate) {
throw new Error(
`The expiration date for this auth method cannot be more than ${MAX_EXPIRATION_LENGTH} ${MAX_EXPIRATION_UNIT} from now. Please provide a valid expiration length and unit.}`
);
}

this.storageProvider.setExpirableItem(
storageUID,
JSON.stringify(authMethod),
_options.expirationLength ?? 24,
_options.expirationUnit ?? 'hours'
expirationLength,
expirationUnit
);
}
}
Expand Down
Loading