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

Add optionalToken parameter to middleware #342

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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: 24 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ export type Params = {
*/
isRevoked?: IsRevoked,

/**
* If set to true, continue to the next middleware when the
* request doesn't include a token, or the token is invalid,
* or the token is expired, or the token is revoked, without failing.
*
* @default false
*/
optionalToken?: boolean,

/**
* If sets to true, continue to the next middleware when the
* request doesn't include a token without failing.
Expand Down Expand Up @@ -114,6 +123,7 @@ export const expressjwt = (options: Params) => {
async () => options.secret as jwt.Secret;

const credentialsRequired = typeof options.credentialsRequired === 'undefined' ? true : options.credentialsRequired;
const optionalToken = typeof options.optionalToken === 'undefined' ? false : options.optionalToken;
const requestProperty = typeof options.requestProperty === 'string' ? options.requestProperty : 'auth';

const middleware = async function (req: express.Request, res: express.Response, next: express.NextFunction) {
Expand Down Expand Up @@ -141,19 +151,26 @@ export const expressjwt = (options: Params) => {
if (/^Bearer$/i.test(scheme)) {
token = credentials;
} else {
if (credentialsRequired) {
if (optionalToken) {
return next();
} else if (credentialsRequired) {
throw new UnauthorizedError('credentials_bad_scheme', { message: 'Format is Authorization: Bearer [token]' });
} else {
return next();
}
}
} else {
if (optionalToken) {
return next();
}
throw new UnauthorizedError('credentials_bad_format', { message: 'Format is Authorization: Bearer [token]' });
}
}

if (!token) {
if (credentialsRequired) {
if (optionalToken) {
return next();
} else if (credentialsRequired) {
throw new UnauthorizedError('credentials_required', { message: 'No authorization token was found' });
} else {
return next();
Expand All @@ -165,6 +182,9 @@ export const expressjwt = (options: Params) => {
try {
decodedToken = jwt.decode(token, { complete: true });
} catch (err) {
if (optionalToken) {
return next();
}
throw new UnauthorizedError('invalid_token', err);
}

Expand All @@ -176,13 +196,13 @@ export const expressjwt = (options: Params) => {
const wrappedErr = new UnauthorizedError('invalid_token', err);
if (err instanceof jwt.TokenExpiredError && typeof options.onExpired === 'function') {
await options.onExpired(req, wrappedErr);
} else {
} else if (!optionalToken) {
throw wrappedErr;
}
}

const isRevoked = options.isRevoked && await options.isRevoked(req, decodedToken) || false;
if (isRevoked) {
if (isRevoked && !optionalToken) {
throw new UnauthorizedError('revoked_token', { message: 'The token has been revoked.' });
}

Expand All @@ -198,5 +218,3 @@ export const expressjwt = (options: Params) => {

return middleware;
}


Loading