Skip to content

Commit

Permalink
fix: Throw NotAuthenticated on token verification errors (#1357)
Browse files Browse the repository at this point in the history
  • Loading branch information
daffl authored May 15, 2019
1 parent f439a9e commit e0120df
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
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

0 comments on commit e0120df

Please sign in to comment.