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

allow customisation of the JWT fetcher #88

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ For an explanation of the interactions between CloudFront, Cognito and Lambda@Ed
* `csrfProtection` *object* (Optional) Enables CSRF protection
* `nonceSigningSecret` *string* Secret used for signing nonce cookies
* `logLevel` *string* (Optional) Logging level. Default: `'silent'`. One of `'fatal'`, `'error'`, `'warn'`, `'info'`, `'debug'`, `'trace'` or `'silent'`.
* `jwtVerifierFetcherRequestOptions` *object* (Optional) Default: undefined. Use to override JwtVerifier fetcher request options, for example re-configuring the JWKS response timeout from the default 1500ms. The interface for request options adds one additional option to the Node.js standard RequestOptions, "responseTimeout", with which a timeout can be set within which the response must be received. (Note the "timeout" in the Node.js standard RequestOptions, concerns something else: the socket idle timeout). See https://github.com/awslabs/aws-jwt-verify#configuring-the-jwks-response-timeout-and-other-http-options-with-jsonfetcher for more information.

*This is the class constructor.*

Expand Down
15 changes: 13 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import pino from 'pino';
import { parse, stringify } from 'querystring';
import { CookieAttributes, CookieSettingsOverrides, CookieType, Cookies, SAME_SITE_VALUES, SameSite, getCookieDomain } from './util/cookie';
import { CSRFTokens, NONCE_COOKIE_NAME_SUFFIX, NONCE_HMAC_COOKIE_NAME_SUFFIX, PKCE_COOKIE_NAME_SUFFIX, generateCSRFTokens, signNonce, urlSafe } from './util/csrf';
import { SimpleJsonFetcher } from 'aws-jwt-verify/https';
import { SimpleJwksCache } from 'aws-jwt-verify/jwk';

export interface AuthenticatorParams {
region: string;
Expand All @@ -25,6 +27,7 @@ export interface AuthenticatorParams {
csrfProtection?: {
nonceSigningSecret: string;
},
jwtVerifierFetcherRequestOptions?: SimpleJsonFetcher['defaultRequestOptions']
}

interface LogoutConfiguration {
Expand Down Expand Up @@ -59,6 +62,7 @@ export class Authenticator {
_cookieSettingsOverrides?: CookieSettingsOverrides;
_logger;
_jwtVerifier;
_jwtVerifierFetcherRequestOptions?: AuthenticatorParams['jwtVerifierFetcherRequestOptions']

constructor(params: AuthenticatorParams) {
this._verifyParams(params);
Expand All @@ -83,7 +87,15 @@ export class Authenticator {
userPoolId: params.userPoolId,
clientId: params.userPoolAppId,
tokenUse: 'id',
});
},
{
jwksCache: new SimpleJwksCache({
fetcher: new SimpleJsonFetcher({
defaultRequestOptions: this._jwtVerifierFetcherRequestOptions,
}),
}),
}
);
this._csrfProtection = params.csrfProtection;
this._logoutConfiguration = params.logoutConfiguration;
this._parseAuthPath = (params.parseAuthPath || '').replace(/^\//, '');
Expand Down Expand Up @@ -792,4 +804,3 @@ export class Authenticator {
}
}
}