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

feat: Support params symbol to skip authenticate hook #1296

Merged
merged 1 commit into from
Apr 18, 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
8 changes: 7 additions & 1 deletion packages/authentication/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ const debug = Debug('@feathersjs/authentication/base');
const verifyJWT = promisify(jsonwebtoken.verify);
const createJWT = promisify(jsonwebtoken.sign);

// TypeScript hack because it does not allow symbols as indexes!?
export const AUTHENTICATE: string = Symbol('@feathersjs/authentication/internal') as any;

export interface AuthenticationResult {
[key: string]: any;
}
Expand Down Expand Up @@ -205,7 +208,10 @@ export class AuthenticationBase {
}

const { strategy } = authentication;
const authParams = Object.assign(params, { authentication: true });
const authParams = {
...params,
[AUTHENTICATE]: false
};

// Throw an error is a `strategy` is indicated but not in the allowed strategies
if (strategy && !allowed.includes(strategy)) {
Expand Down
7 changes: 6 additions & 1 deletion packages/authentication/src/hooks/authenticate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { HookContext } from '@feathersjs/feathers';
import { NotAuthenticated } from '@feathersjs/errors';
import Debug from 'debug';
import { AuthenticationService } from '../service';
import { AUTHENTICATE } from '../core';

const debug = Debug('@feathersjs/authentication/hooks/authenticate');

Expand Down Expand Up @@ -44,7 +45,11 @@ export default (originalSettings: string|AuthenticateHookSettings, ...originalSt
throw new NotAuthenticated('The authenticate hook does not need to be used on the authentication service');
}

if (authentication && authentication !== true) {
if (params[AUTHENTICATE] === false) {
return context;
}

if (authentication) {
const authParams = omit(params, 'provider', 'authentication');

debug('Authenticating with', authentication, strategies);
Expand Down
3 changes: 2 additions & 1 deletion packages/authentication/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ export {
AuthenticationBase,
AuthenticationRequest,
AuthenticationResult,
AuthenticationStrategy
AuthenticationStrategy,
AUTHENTICATE
} from './core';
export { AuthenticationBaseStrategy } from './strategy';
export { AuthenticationService } from './service';
Expand Down
8 changes: 4 additions & 4 deletions packages/authentication/test/core.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import assert from 'assert';
import feathers, { Application } from '@feathersjs/feathers';
import jwt from 'jsonwebtoken';

import { AuthenticationBase } from '../src/core';
import { AuthenticationBase, AUTHENTICATE } from '../src/core';
import { Strategy1, Strategy2, MockRequest } from './fixtures';
import { ServerResponse } from 'http';

Expand Down Expand Up @@ -141,7 +141,7 @@ describe('authentication/core', () => {

assert.deepStrictEqual(result, Object.assign({}, Strategy2.result, {
authentication,
params: { authentication: true }
params: { [AUTHENTICATE]: false }
}));
});

Expand All @@ -159,7 +159,7 @@ describe('authentication/core', () => {

assert.deepStrictEqual(result, Object.assign({
params: Object.assign(params, {
authentication: true
[AUTHENTICATE]: false
}),
authentication
}, Strategy2.result));
Expand Down Expand Up @@ -198,7 +198,7 @@ describe('authentication/core', () => {

assert.deepStrictEqual(result, Object.assign({
authentication,
params: { authentication: true }
params: { [AUTHENTICATE]: false }
}, Strategy2.result));
});

Expand Down
18 changes: 8 additions & 10 deletions packages/authentication/test/hooks/authenticate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import feathers, { Application, Params, Service } from '@feathersjs/feathers';

import { Strategy1, Strategy2 } from '../fixtures';
import { AuthenticationService, hooks } from '../../src';
import { AuthenticationResult } from '../../src/core';
import { AuthenticationResult, AUTHENTICATE } from '../../src/core';

const { authenticate } = hooks;

Expand Down Expand Up @@ -147,7 +147,7 @@ describe('authentication/hooks/authenticate', () => {

assert.deepStrictEqual(result, Object.assign({
authentication: params.authentication,
params: { authentication: true }
params: {}
}, Strategy2.result));
});

Expand Down Expand Up @@ -183,16 +183,14 @@ describe('authentication/hooks/authenticate', () => {
}
});

it('passes with authentication true but external call', async () => {
const result = await app.service('users').get(1, {
it('passes with [AUTHENTICATE]: false but external call', async () => {
const params = {
provider: 'rest',
authentication: true
});
[AUTHENTICATE]: false
};
const result = await app.service('users').get(1, params);

assert.deepStrictEqual(result, {
provider: 'rest',
authentication: true
});
assert.deepStrictEqual(result, params);
});

it('errors when used on the authentication service', async () => {
Expand Down