Skip to content

Commit

Permalink
feat: Change and *JWT methods to *accessToken (#1304)
Browse files Browse the repository at this point in the history
  • Loading branch information
daffl authored Apr 23, 2019
1 parent 98fe8f8 commit 5ac826b
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 28 deletions.
4 changes: 2 additions & 2 deletions packages/authentication-local/test/strategy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ describe('@feathersjs/authentication-local/strategy', () => {
assert.ok(accessToken);
assert.strictEqual(authResult.user.email, email);

const decoded = await authService.verifyJWT(accessToken);
const decoded = await authService.verifyAccessToken(accessToken);

assert.strictEqual(decoded.sub, `${user.id}`);
});
Expand All @@ -129,7 +129,7 @@ describe('@feathersjs/authentication-local/strategy', () => {
assert.strictEqual(authResult.user.email, email);
assert.strictEqual(authResult.user.passsword, undefined);

const decoded = await authService.verifyJWT(accessToken);
const decoded = await authService.verifyAccessToken(accessToken);

assert.strictEqual(decoded.sub, `${user.id}`);
});
Expand Down
2 changes: 1 addition & 1 deletion packages/authentication-oauth/test/strategy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ describe('@feathersjs/authentication-oauth/strategy', () => {
const user = await app.service('users').create({
name: 'David'
});
const jwt = await authService.createJWT({}, {
const jwt = await authService.createAccessToken({}, {
subject: `${user.id}`
});

Expand Down
3 changes: 2 additions & 1 deletion packages/authentication/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"scripts": {
"prepublish": "npm run compile",
"compile": "shx rm -rf lib/ && tsc",
"test": "mocha --opts ../../mocha.ts.opts --recursive test/**.test.ts test/**/*.test.ts"
"test": "npm run compile && npm run mocha",
"mocha": "mocha --opts ../../mocha.ts.opts --recursive test/**.test.ts test/**/*.test.ts"
},
"directories": {
"lib": "lib"
Expand Down
8 changes: 4 additions & 4 deletions packages/authentication/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,12 @@ export class AuthenticationBase {
}

/**
* Create a new JWT with payload and options.
* Create a new access token with payload and options.
* @param payload The JWT payload
* @param optsOverride The options to extend the defaults (`configuration.jwtOptions`) with
* @param secretOverride Use a different secret instead
*/
createJWT (payload: string | Buffer | object, optsOverride?: SignOptions, secretOverride?: Secret) {
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 @@ -169,12 +169,12 @@ export class AuthenticationBase {
}

/**
* Verifies a JWT.
* Verifies an access token.
* @param accessToken The token to verify
* @param optsOverride The options to extend the defaults (`configuration.jwtOptions`) with
* @param secretOverride Use a different secret instead
*/
verifyJWT (accessToken: string, optsOverride?: JwtVerifyOptions, secretOverride?: Secret) {
verifyAccessToken (accessToken: string, optsOverride?: JwtVerifyOptions, secretOverride?: Secret) {
const { secret, jwtOptions } = this.configuration;
const jwtSecret = secretOverride || secret;
const options = merge({}, jwtOptions, optsOverride);
Expand Down
2 changes: 1 addition & 1 deletion packages/authentication/src/jwt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export class JWTStrategy extends AuthenticationBaseStrategy {
throw new NotAuthenticated('Not authenticated');
}

const payload = await this.authentication.verifyJWT(accessToken, params.jwt);
const payload = await this.authentication.verifyAccessToken(accessToken, params.jwt);
const entityId = payload.sub;
const result = {
accessToken,
Expand Down
6 changes: 3 additions & 3 deletions packages/authentication/src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class AuthenticationService extends AuthenticationBase implements Service
* @param authResult The authentication result
* @param params Service call parameters
*/
async getJwtOptions (authResult: AuthenticationResult, params: Params) {
async getTokenOptions (authResult: AuthenticationResult, params: Params) {
const { service, entity, entityId } = this.configuration;
const jwtOptions = merge({}, params.jwtOptions, params.jwt);
const hasEntity = service && entity && authResult[entity];
Expand Down Expand Up @@ -66,7 +66,7 @@ export class AuthenticationService extends AuthenticationBase implements Service

const [ payload, jwtOptions ] = await Promise.all([
this.getPayload(authResult, params),
this.getJwtOptions(authResult, params)
this.getTokenOptions(authResult, params)
]);

if (authResult.accessToken) {
Expand All @@ -75,7 +75,7 @@ export class AuthenticationService extends AuthenticationBase implements Service

debug('Creating JWT with', payload, jwtOptions);

const accessToken = await this.createJWT(payload, jwtOptions, params.secret);
const accessToken = await this.createAccessToken(payload, jwtOptions, params.secret);

return Object.assign({}, { accessToken }, authResult);
}
Expand Down
28 changes: 14 additions & 14 deletions packages/authentication/test/core.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,11 +272,11 @@ describe('authentication/core', () => {
describe('jwt', () => {
const message = 'Some payload';

describe('createJWT', () => {
describe('createAccessToken', () => {
// it('errors with no payload', () => {
// try {
// // @ts-ignore
// await auth.createJWT();
// await auth.createAccessToken();
// assert.fail('Should never get here');
// } catch (error) {
// assert.strictEqual(error.message, 'payload is required');
Expand All @@ -286,7 +286,7 @@ describe('authentication/core', () => {
it('with default options', async () => {
const msg = 'Some payload';

const accessToken = await auth.createJWT({ message: msg });
const accessToken = await auth.createAccessToken({ message: msg });
const decoded = jwt.decode(accessToken);
const settings = auth.configuration.jwtOptions;

Expand All @@ -308,7 +308,7 @@ describe('authentication/core', () => {
jwtid: 'something'
};

const accessToken = await auth.createJWT({ message }, overrides);
const accessToken = await auth.createAccessToken({ message }, overrides);

assert.ok(typeof accessToken === 'string');

Expand All @@ -330,34 +330,34 @@ describe('authentication/core', () => {
};

try {
await auth.createJWT({}, overrides);
await auth.createAccessToken({}, overrides);
assert.fail('Should never get here');
} catch (error) {
assert.strictEqual(error.message, '"algorithm" must be a valid string enum value');
}
});
});

describe('verifyJWT', () => {
describe('verifyAccessToken', () => {
let validToken: string;
let expiredToken: string;

beforeEach(async () => {
validToken = await auth.createJWT({ message });
expiredToken = await auth.createJWT({}, {
validToken = await auth.createAccessToken({ message });
expiredToken = await auth.createAccessToken({}, {
expiresIn: '1ms'
});
});

it('returns payload when token is valid', async () => {
const payload = await auth.verifyJWT(validToken);
const payload = await auth.verifyAccessToken(validToken);

assert.strictEqual(payload.message, message);
});

it('errors when custom algorithm property does not match', async () => {
try {
await auth.verifyJWT(validToken, {
await auth.verifyAccessToken(validToken, {
algorithm: [ 'HS512' ]
});
assert.fail('Should never get here');
Expand All @@ -368,7 +368,7 @@ describe('authentication/core', () => {

it('errors when algorithms property does not match', async () => {
try {
await auth.verifyJWT(validToken, {
await auth.verifyAccessToken(validToken, {
algorithms: [ 'HS512' ]
});
assert.fail('Should never get here');
Expand All @@ -379,7 +379,7 @@ describe('authentication/core', () => {

it('errors when secret is different', async () => {
try {
await auth.verifyJWT(validToken, {}, 'fdjskl');
await auth.verifyAccessToken(validToken, {}, 'fdjskl');

assert.fail('Should never get here');
} catch (error) {
Expand All @@ -389,7 +389,7 @@ describe('authentication/core', () => {

it('errors when other custom options do not match', async () => {
try {
await auth.verifyJWT(validToken, { issuer: 'someonelse' });
await auth.verifyAccessToken(validToken, { issuer: 'someonelse' });

assert.fail('Should never get here');
} catch (error) {
Expand All @@ -399,7 +399,7 @@ describe('authentication/core', () => {

it('errors when token is expired', async () => {
try {
await auth.verifyJWT(expiredToken);
await auth.verifyAccessToken(expiredToken);
assert.fail('Should never get here');
} catch (error) {
assert.strictEqual(error.message, 'jwt expired');
Expand Down
4 changes: 2 additions & 2 deletions packages/authentication/test/jwt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ describe('authentication/jwt', () => {
name: 'David'
});

accessToken = await service.createJWT({}, {
accessToken = await service.createAccessToken({}, {
subject: `${user.id}`
});

payload = await service.verifyJWT(accessToken);
payload = await service.verifyAccessToken(accessToken);
});

describe('with authenticate hook', () => {
Expand Down

0 comments on commit 5ac826b

Please sign in to comment.