Skip to content

Commit

Permalink
fix: Improve error message when authentication strategy is not allowed (
Browse files Browse the repository at this point in the history
  • Loading branch information
daffl authored Oct 3, 2019
1 parent 9b0ed6c commit 317a312
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
8 changes: 6 additions & 2 deletions packages/authentication/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,16 @@ export class AuthenticationBase {
async authenticate (authentication: AuthenticationRequest, params: Params, ...allowed: string[]) {
const { strategy } = authentication || ({} as AuthenticationRequest);
const [ authStrategy ] = this.getStrategies(strategy);
const strategyAllowed = allowed.includes(strategy);

debug('Running authenticate for strategy', strategy, allowed);

if (!authentication || !authStrategy || !allowed.includes(strategy)) {
if (!authentication || !authStrategy || !strategyAllowed) {
const additionalInfo = (!strategy && ' (no `strategy` set)') ||
(!strategyAllowed && ' (strategy not allowed in authStrategies)') || '';

// If there are no valid strategies or `authentication` is not an object
throw new NotAuthenticated(`Invalid authentication information` + (!strategy ? ' (no `strategy` set)' : ''));
throw new NotAuthenticated('Invalid authentication information' + additionalInfo);
}

return authStrategy.authenticate(authentication, {
Expand Down
2 changes: 1 addition & 1 deletion packages/authentication/test/core.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ describe('authentication/core', () => {
assert.fail('Should never get here');
} catch (error) {
assert.strictEqual(error.name, 'NotAuthenticated');
assert.strictEqual(error.message, 'Invalid authentication information');
assert.strictEqual(error.message, 'Invalid authentication information (strategy not allowed in authStrategies)');
}
});

Expand Down

0 comments on commit 317a312

Please sign in to comment.