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

fix: Throw NotAuthenticated on token errors #1357

Merged
merged 1 commit into from
May 15, 2019
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
14 changes: 10 additions & 4 deletions packages/authentication/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export class AuthenticationBase {
* @param optsOverride The options to extend the defaults (`configuration.jwtOptions`) with
* @param secretOverride Use a different secret instead
*/
createAccessToken (payload: string | Buffer | object, optsOverride?: SignOptions, secretOverride?: Secret) {
async createAccessToken (payload: string | Buffer | object, optsOverride?: SignOptions, secretOverride?: Secret) {
const { secret, jwtOptions } = this.configuration;
// Use configuration by default but allow overriding the secret
const jwtSecret = secretOverride || secret;
Expand All @@ -171,7 +171,7 @@ export class AuthenticationBase {
* @param optsOverride The options to extend the defaults (`configuration.jwtOptions`) with
* @param secretOverride Use a different secret instead
*/
verifyAccessToken (accessToken: string, optsOverride?: JwtVerifyOptions, secretOverride?: Secret) {
async verifyAccessToken (accessToken: string, optsOverride?: JwtVerifyOptions, secretOverride?: Secret) {
const { secret, jwtOptions } = this.configuration;
const jwtSecret = secretOverride || secret;
const options = merge({}, jwtOptions, optsOverride);
Expand All @@ -183,8 +183,14 @@ export class AuthenticationBase {
delete options.algorithm;
}

// @ts-ignore
return verifyJWT(accessToken, jwtSecret, options);
try {
// @ts-ignore
const isValid = await verifyJWT(accessToken, jwtSecret, options);

return isValid;
} catch (error) {
throw new NotAuthenticated(error.message, error);
}
}

/**
Expand Down
4 changes: 4 additions & 0 deletions packages/authentication/test/core.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ describe('authentication/core', () => {

assert.fail('Should never get here');
} catch (error) {
assert.strictEqual(error.name, 'NotAuthenticated');
assert.ok(/jwt issuer invalid/.test(error.message));
}
});
Expand All @@ -374,7 +375,10 @@ describe('authentication/core', () => {
await auth.verifyAccessToken(expiredToken);
assert.fail('Should never get here');
} catch (error) {
assert.strictEqual(error.name, 'NotAuthenticated');
assert.strictEqual(error.message, 'jwt expired');
assert.strictEqual(error.data.name, 'TokenExpiredError');
assert.ok(error.data.expiredAt);
}
});
});
Expand Down
2 changes: 1 addition & 1 deletion packages/transport-commons/test/socket/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ describe('@feathersjs/transport-commons', () => {
}
});
});

it('.get without params', done => {
const socket = new EventEmitter();

Expand Down