Skip to content

Commit 317a312

Browse files
authored
fix: Improve error message when authentication strategy is not allowed (#1600)
1 parent 9b0ed6c commit 317a312

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

packages/authentication/src/core.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,12 +211,16 @@ export class AuthenticationBase {
211211
async authenticate (authentication: AuthenticationRequest, params: Params, ...allowed: string[]) {
212212
const { strategy } = authentication || ({} as AuthenticationRequest);
213213
const [ authStrategy ] = this.getStrategies(strategy);
214+
const strategyAllowed = allowed.includes(strategy);
214215

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

217-
if (!authentication || !authStrategy || !allowed.includes(strategy)) {
218+
if (!authentication || !authStrategy || !strategyAllowed) {
219+
const additionalInfo = (!strategy && ' (no `strategy` set)') ||
220+
(!strategyAllowed && ' (strategy not allowed in authStrategies)') || '';
221+
218222
// If there are no valid strategies or `authentication` is not an object
219-
throw new NotAuthenticated(`Invalid authentication information` + (!strategy ? ' (no `strategy` set)' : ''));
223+
throw new NotAuthenticated('Invalid authentication information' + additionalInfo);
220224
}
221225

222226
return authStrategy.authenticate(authentication, {

packages/authentication/test/core.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ describe('authentication/core', () => {
190190
assert.fail('Should never get here');
191191
} catch (error) {
192192
assert.strictEqual(error.name, 'NotAuthenticated');
193-
assert.strictEqual(error.message, 'Invalid authentication information');
193+
assert.strictEqual(error.message, 'Invalid authentication information (strategy not allowed in authStrategies)');
194194
}
195195
});
196196

0 commit comments

Comments
 (0)