Skip to content

Commit

Permalink
fix: token TTL being a helper function is now accepted
Browse files Browse the repository at this point in the history
  • Loading branch information
panva committed Sep 23, 2019
1 parent 4e6c543 commit a930355
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 22 deletions.
25 changes: 16 additions & 9 deletions lib/helpers/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -389,15 +389,22 @@ class Configuration {

checkTTL() {
Object.entries(this.ttl).forEach(([key, value]) => {
if (
(
typeof value === 'function'
&& value.constructor.toString() !== 'function Function() { [native code] }'
) || (
!Number.isSafeInteger(value)
|| value <= 0
)
) {
let valid = false;
switch (typeof value) {
case 'function':
if (value.constructor.toString() === 'function Function() { [native code] }') {
valid = true;
}
break;
case 'number':
if (Number.isSafeInteger(value) && value > 0) {
valid = true;
}
break;
default:
}

if (!valid) {
throw new TypeError(`ttl.${key} must be a positive integer or a regular function returning one`);
}
});
Expand Down
20 changes: 20 additions & 0 deletions test/configuration/constructor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,26 @@ describe('Provider configuration', () => {
throws.forEach((fn) => {
expect(fn).to.throw('ttl.AccessToken must be a positive integer or a regular function returning one');
});

let okay = [
() => { new Provider('http://localhost:3000', { ttl: { default: 1 } }); },
() => { new Provider('http://localhost:3000', { ttl: { default: Number.MAX_SAFE_INTEGER } }); },
() => { new Provider('http://localhost:3000', { ttl: { default() { return 600; } } }); },
];

okay.forEach((fn) => {
expect(fn).not.to.throw();
});

okay = [
() => { new Provider('http://localhost:3000', { ttl: { AccessToken: 1 } }); },
() => { new Provider('http://localhost:3000', { ttl: { AccessToken: Number.MAX_SAFE_INTEGER } }); },
() => { new Provider('http://localhost:3000', { ttl: { AccessToken() { return 600; } } }); },
];

okay.forEach((fn) => {
expect(fn).not.to.throw();
});
});
});

Expand Down
22 changes: 9 additions & 13 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,7 @@ export type TokenFormat = 'opaque' | 'jwt' | 'jwt-ietf' | 'paseto';
export type AccessTokenFormatFunction = (ctx: KoaContextWithOIDC, token: AccessToken) => TokenFormat;
export type ClientCredentialsFormatFunction = (ctx: KoaContextWithOIDC, token: ClientCredentials) => TokenFormat;

export type AccessTokenTTLFunction = (ctx: KoaContextWithOIDC, accessToken: AccessToken, client: Client) => number;
export type AuthorizationCodeTTLFunction = (ctx: KoaContextWithOIDC, authorizationCode: AuthorizationCode, client: Client) => number;
export type ClientCredentialsTTLFunction = (ctx: KoaContextWithOIDC, clientCredentials: ClientCredentials, client: Client) => number;
export type DeviceCodeTTLFunction = (ctx: KoaContextWithOIDC, deviceCode: DeviceCode, client: Client) => number;
export type IdTokenTTLFunction = (ctx: KoaContextWithOIDC, idToken: IdToken, client: Client) => number;
export type RefreshTokenTTLFunction = (ctx: KoaContextWithOIDC, refreshToken: RefreshToken, client: Client) => number;
export type TTLFunction<T> = (ctx: KoaContextWithOIDC, token: T, client: Client) => number;

export interface AnyObject {
[key: string]: any;
Expand Down Expand Up @@ -976,13 +971,14 @@ export interface Configuration {
revocationEndpointAuthMethods?: ClientAuthMethod[];

ttl?: {
AccessToken?: AccessTokenTTLFunction | number;
AuthorizationCode?: AuthorizationCodeTTLFunction | number;
ClientCredentials?: ClientCredentialsTTLFunction | number;
DeviceCode?: DeviceCodeTTLFunction | number;
IdToken?: IdTokenTTLFunction | number;
RefreshToken?: RefreshTokenTTLFunction | number;
[key: string]: unknown;
AccessToken?: TTLFunction<AccessToken> | number;
AuthorizationCode?: TTLFunction<AuthorizationCode> | number;
ClientCredentials?: TTLFunction<ClientCredentials> | number;
DeviceCode?: TTLFunction<DeviceCode> | number;
IdToken?: TTLFunction<IdToken> | number;
RefreshToken?: TTLFunction<RefreshToken> | number;

[key: string]: any;
};

extraClientMetadata?: {
Expand Down

0 comments on commit a930355

Please sign in to comment.