@loopback/authentication 7.1.0 → 7.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -3,6 +3,17 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ # [7.2.0](https://github.com/strongloop/loopback-next/compare/@loopback/authentication@7.1.0...@loopback/authentication@7.2.0) (2021-04-06)
7
+
8
+
9
+ ### Features
10
+
11
+ * allow one strategy to fail the authentication process ([adbbf24](https://github.com/strongloop/loopback-next/commit/adbbf2439ffff42e5e6e3078d479e9c2031c196b))
12
+
13
+
14
+
15
+
16
+
6
17
  # [7.1.0](https://github.com/strongloop/loopback-next/compare/@loopback/authentication@7.0.7...@loopback/authentication@7.1.0) (2021-03-18)
7
18
 
8
19
 
@@ -2,17 +2,21 @@
2
2
  import { Getter, Provider, Setter } from '@loopback/core';
3
3
  import { Middleware, Request } from '@loopback/rest';
4
4
  import { UserProfile } from '@loopback/security';
5
- import { AuthenticateFn, AuthenticationStrategy } from '../types';
5
+ import { AuthenticateFn, AuthenticationOptions, AuthenticationStrategy } from '../types';
6
6
  /**
7
7
  * Provides the authentication action for a sequence
8
- * @example `context.bind('authentication.actions.authenticate').toProvider(AuthenticateActionProvider)`
8
+ * @example
9
+ * ```ts
10
+ * context.bind('authentication.actions.authenticate').toProvider(AuthenticateActionProvider)
11
+ * ```
9
12
  */
10
13
  export declare class AuthenticateActionProvider implements Provider<AuthenticateFn> {
11
14
  readonly getStrategies: Getter<AuthenticationStrategy | AuthenticationStrategy[] | undefined>;
12
15
  readonly setCurrentUser: Setter<UserProfile>;
13
16
  readonly setRedirectUrl: Setter<string>;
14
17
  readonly setRedirectStatus: Setter<number>;
15
- constructor(getStrategies: Getter<AuthenticationStrategy | AuthenticationStrategy[] | undefined>, setCurrentUser: Setter<UserProfile>, setRedirectUrl: Setter<string>, setRedirectStatus: Setter<number>);
18
+ private readonly options;
19
+ constructor(getStrategies: Getter<AuthenticationStrategy | AuthenticationStrategy[] | undefined>, setCurrentUser: Setter<UserProfile>, setRedirectUrl: Setter<string>, setRedirectStatus: Setter<number>, options?: AuthenticationOptions);
16
20
  /**
17
21
  * @returns authenticateFn
18
22
  */
@@ -13,7 +13,10 @@ const keys_1 = require("../keys");
13
13
  const types_1 = require("../types");
14
14
  /**
15
15
  * Provides the authentication action for a sequence
16
- * @example `context.bind('authentication.actions.authenticate').toProvider(AuthenticateActionProvider)`
16
+ * @example
17
+ * ```ts
18
+ * context.bind('authentication.actions.authenticate').toProvider(AuthenticateActionProvider)
19
+ * ```
17
20
  */
18
21
  let AuthenticateActionProvider = class AuthenticateActionProvider {
19
22
  constructor(
@@ -24,11 +27,12 @@ let AuthenticateActionProvider = class AuthenticateActionProvider {
24
27
  // To solve this, we are injecting a getter function that will
25
28
  // defer resolution of the strategy until authenticate() action
26
29
  // is executed.
27
- getStrategies, setCurrentUser, setRedirectUrl, setRedirectStatus) {
30
+ getStrategies, setCurrentUser, setRedirectUrl, setRedirectStatus, options = {}) {
28
31
  this.getStrategies = getStrategies;
29
32
  this.setCurrentUser = setCurrentUser;
30
33
  this.setRedirectUrl = setRedirectUrl;
31
34
  this.setRedirectStatus = setRedirectStatus;
35
+ this.options = options;
32
36
  }
33
37
  /**
34
38
  * @returns authenticateFn
@@ -42,56 +46,51 @@ let AuthenticateActionProvider = class AuthenticateActionProvider {
42
46
  */
43
47
  async action(request) {
44
48
  let strategies = await this.getStrategies();
45
- if (!strategies) {
49
+ if (strategies == null) {
46
50
  // The invoked operation does not require authentication.
47
51
  return undefined;
48
52
  }
49
53
  // convert to array if required
50
54
  strategies = Array.isArray(strategies) ? strategies : [strategies];
51
- let authenticated = false;
52
- let redirected = false;
53
- let authError;
54
- let authResponse;
55
- let userProfile;
55
+ const authErrors = [];
56
56
  for (const strategy of strategies) {
57
- // the first strategy to succeed or redirect will halt the execution chain
58
- if (authenticated || redirected)
59
- break;
57
+ let authResponse = undefined;
60
58
  try {
61
59
  authResponse = await strategy.authenticate(request);
62
- // response from `strategy.authenticate()` could return an object of type UserProfile or RedirectRoute
63
- if (rest_1.RedirectRoute.isRedirectRoute(authResponse)) {
64
- redirected = true;
65
- const redirectOptions = authResponse;
66
- // bind redirection url and status to the context
67
- // controller should handle actual redirection
68
- this.setRedirectUrl(redirectOptions.targetLocation);
69
- this.setRedirectStatus(redirectOptions.statusCode);
70
- }
71
- else if (authResponse) {
72
- authenticated = true;
73
- // if `strategy.authenticate()` returns an object of type UserProfile, set it as current user
74
- userProfile = authResponse;
75
- }
76
- else if (!authResponse) {
77
- // important to throw a non-protocol-specific error here
78
- const error = new Error(`User profile not returned from strategy's authenticate function`);
79
- Object.assign(error, {
80
- code: types_1.USER_PROFILE_NOT_FOUND,
81
- });
82
- throw error;
60
+ }
61
+ catch (err) {
62
+ if (this.options.failOnError) {
63
+ throw err;
83
64
  }
65
+ authErrors.push(err);
84
66
  }
85
- catch (error) {
86
- authError = authError !== null && authError !== void 0 ? authError : error;
67
+ // response from `strategy.authenticate()` could return an object of
68
+ // type UserProfile or RedirectRoute
69
+ if (rest_1.RedirectRoute.isRedirectRoute(authResponse)) {
70
+ const redirectOptions = authResponse;
71
+ // bind redirection url and status to the context
72
+ // controller should handle actual redirection
73
+ this.setRedirectUrl(redirectOptions.targetLocation);
74
+ this.setRedirectStatus(redirectOptions.statusCode);
75
+ return;
76
+ }
77
+ else if (authResponse != null) {
78
+ // if `strategy.authenticate()` returns an object of type UserProfile,
79
+ // set it as current user
80
+ const userProfile = authResponse;
81
+ this.setCurrentUser(userProfile);
82
+ return userProfile;
87
83
  }
88
84
  }
89
- if (!authenticated && !redirected)
90
- throw authError;
91
- if (userProfile) {
92
- this.setCurrentUser(userProfile);
93
- return userProfile;
85
+ if (authErrors.length) {
86
+ throw authErrors[0];
94
87
  }
88
+ // important to throw a non-protocol-specific error here
89
+ const error = new Error(`User profile not returned from strategy's authenticate function`);
90
+ Object.assign(error, {
91
+ code: types_1.USER_PROFILE_NOT_FOUND,
92
+ });
93
+ throw error;
95
94
  }
96
95
  };
97
96
  AuthenticateActionProvider = tslib_1.__decorate([
@@ -99,7 +98,8 @@ AuthenticateActionProvider = tslib_1.__decorate([
99
98
  tslib_1.__param(1, core_1.inject.setter(security_1.SecurityBindings.USER)),
100
99
  tslib_1.__param(2, core_1.inject.setter(keys_1.AuthenticationBindings.AUTHENTICATION_REDIRECT_URL)),
101
100
  tslib_1.__param(3, core_1.inject.setter(keys_1.AuthenticationBindings.AUTHENTICATION_REDIRECT_STATUS)),
102
- tslib_1.__metadata("design:paramtypes", [Function, Function, Function, Function])
101
+ tslib_1.__param(4, core_1.config({ fromBinding: keys_1.AuthenticationBindings.COMPONENT })),
102
+ tslib_1.__metadata("design:paramtypes", [Function, Function, Function, Function, Object])
103
103
  ], AuthenticateActionProvider);
104
104
  exports.AuthenticateActionProvider = AuthenticateActionProvider;
105
105
  let AuthenticationMiddlewareProvider = class AuthenticationMiddlewareProvider {
@@ -1 +1 @@
1
- {"version":3,"file":"auth-action.provider.js","sourceRoot":"","sources":["../../src/providers/auth-action.provider.ts"],"names":[],"mappings":";AAAA,sDAAsD;AACtD,wCAAwC;AACxC,+CAA+C;AAC/C,gEAAgE;;;;AAEhE,yCAA4E;AAC5E,yCAMwB;AACxB,iDAAiE;AACjE,kCAA+C;AAC/C,oCAKkB;AAClB;;;GAGG;AACH,IAAa,0BAA0B,GAAvC,MAAa,0BAA0B;IACrC;IACE,yDAAyD;IACzD,4DAA4D;IAC5D,qDAAqD;IACrD,qCAAqC;IACrC,8DAA8D;IAC9D,+DAA+D;IAC/D,eAAe;IAEN,aAER,EAEQ,cAAmC,EAEnC,cAA8B,EAE9B,iBAAiC;QARjC,kBAAa,GAAb,aAAa,CAErB;QAEQ,mBAAc,GAAd,cAAc,CAAqB;QAEnC,mBAAc,GAAd,cAAc,CAAgB;QAE9B,sBAAiB,GAAjB,iBAAiB,CAAgB;IAC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AC,UAAU,GAAG,GAAG,CAAC;iBACxB;gBACD,MAAM,KAAK,CAAC;aACb;YACD,OAAO,IAAI,EAAE,CAAC;QAChB,CAAC,CAAC;IACJ,CAAC;CACF,CAAA;AAtBY,gCAAgC;IAN5C,iBAAU,CACT,mBAAY,CAAC;QACX,KAAK,EAAE,2BAAoB,CAAC,cAAc;QAC1C,cAAc,EAAE,CAAC,2BAAoB,CAAC,UAAU,CAAC;KAClD,CAAC,CACH;IAGI,mBAAA,aAAM,CAAC,6BAAsB,CAAC,WAAW,CAAC,CAAA;;GAFlC,gCAAgC,CAsB5C;AAtBY,4EAAgC"}
1
+ {"version":3,"file":"auth-action.provider.js","sourceRoot":"","sources":["../../src/providers/auth-action.provider.ts"],"names":[],"mappings":";AAAA,sDAAsD;AACtD,wCAAwC;AACxC,+CAA+C;AAC/C,gEAAgE;;;;AAEhE,yCAOwB;AACxB,yCAMwB;AACxB,iDAAiE;AACjE,kCAA+C;AAC/C,oCAMkB;AAElB;;;;;;GAMG;AACH,IAAa,0BAA0B,GAAvC,MAAa,0BAA0B;IACrC;IACE,yDAAyD;IACzD,4DAA4D;IAC5D,qDAAqD;IACrD,qCAAqC;IACrC,8DAA8D;IAC9D,+DAA+D;IAC/D,eAAe;IAEN,aAER,EAEQ,cAAmC,EAEnC,cAA8B,EAE9B,iBAAiC,EAEzB,UAAiC,EAAE;QAV3C,kBAAa,GAAb,aAAa,CAErB;QAEQ,mBAAc,GAAd,cAAc,CAAqB;QAEnC,mBAAc,GAAd,cAAc,CAAgB;QAE9B,sBAAiB,GAAjB,iBAAiB,CAAgB;QAEzB,YAAO,GAAP,OAAO,CAA4B;IACnD,CAAC;IAEJ;;OAEG;IACH,KAAK;QACH,OAAO,OAAO,CAAC,EAAE,CAAC,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;IACzC,CAAC;IAED;;;OAGG;IACH,KAAK,CAAC,MAAM,CAAC,OAAgB;QAC3B,IAAI,UAAU,GAAG,MAAM,IAAI,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U,CAAC;KAClD,CAAC,CACH;IAGI,mBAAA,aAAM,CAAC,6BAAsB,CAAC,WAAW,CAAC,CAAA;;GAFlC,gCAAgC,CAsB5C;AAtBY,4EAAgC"}
package/dist/types.d.ts CHANGED
@@ -45,6 +45,14 @@ export interface AuthenticationOptions {
45
45
  * those methods without authentication metadata.
46
46
  */
47
47
  defaultMetadata?: AuthenticationMetadata[];
48
+ /**
49
+ * This flag allows an authentication strategy to abort the authentication by
50
+ * throwing an error if `failOnError` is set to `true`. By default, the
51
+ * authentication process continues to the next one even when a strategy
52
+ * throws an error. If one of other strategies succeed, the error will be
53
+ * discarded.
54
+ */
55
+ failOnError?: boolean;
48
56
  }
49
57
  /**
50
58
  * An interface that describes the common authentication strategy.
package/dist/types.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":";AAAA,sDAAsD;AACtD,wCAAwC;AACxC,+CAA+C;AAC/C,gEAAgE;;;AAEhE,yCAOwB;AAGxB,iCAA8C;AAgFjC,QAAA,iCAAiC,GAC5C,mCAAmC,CAAC;AAEzB,QAAA,sBAAsB,GAAG,wBAAwB,CAAC;AAE/D;;;;;;;GAOG;AACH,SAAgB,8BAA8B,CAC5C,OAAgB,EAChB,aAAkD;IAElD,OAAO,mBAAY,CACjB,OAAO,EACP,6BAAsB,CAAC,4CAA4C,EACnE,aAAa,EACb;QACE,SAAS,EACP,6BAAsB,CAAC,4CAA4C;KACtE,CACF,CAAC;AACJ,CAAC;AAbD,wEAaC;AAED;;GAEG;AACI,MAAM,cAAc,GAAoB,OAAO,CAAC,EAAE;IACvD,mBAAY,CACV,6BAAsB,CAAC,4CAA4C,CACpE,CAAC,OAAO,CAAC,CAAC;IACX,OAAO,CAAC,GAAG,CAAC;QACV,SAAS,EACP,6BAAsB,CAAC,4CAA4C;KACtE,CAAC,CAAC;AACL,CAAC,CAAC;AARW,QAAA,cAAc,kBAQzB;AAEF;;;;;GAKG;AACH,SAAgB,oCAAoC,CAClD,QAAkC,EAClC,YAAoB;IAEpB,OAAO,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,IAAI,CAAC,QAAQ,KAAK,YAAY,CAAC,CAAC;AAC/D,CAAC;AALD,oFAKC"}
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":";AAAA,sDAAsD;AACtD,wCAAwC;AACxC,+CAA+C;AAC/C,gEAAgE;;;AAEhE,yCAOwB;AAGxB,iCAA8C;AAyFjC,QAAA,iCAAiC,GAC5C,mCAAmC,CAAC;AAEzB,QAAA,sBAAsB,GAAG,wBAAwB,CAAC;AAE/D;;;;;;;GAOG;AACH,SAAgB,8BAA8B,CAC5C,OAAgB,EAChB,aAAkD;IAElD,OAAO,mBAAY,CACjB,OAAO,EACP,6BAAsB,CAAC,4CAA4C,EACnE,aAAa,EACb;QACE,SAAS,EACP,6BAAsB,CAAC,4CAA4C;KACtE,CACF,CAAC;AACJ,CAAC;AAbD,wEAaC;AAED;;GAEG;AACI,MAAM,cAAc,GAAoB,OAAO,CAAC,EAAE;IACvD,mBAAY,CACV,6BAAsB,CAAC,4CAA4C,CACpE,CAAC,OAAO,CAAC,CAAC;IACX,OAAO,CAAC,GAAG,CAAC;QACV,SAAS,EACP,6BAAsB,CAAC,4CAA4C;KACtE,CAAC,CAAC;AACL,CAAC,CAAC;AARW,QAAA,cAAc,kBAQzB;AAEF;;;;;GAKG;AACH,SAAgB,oCAAoC,CAClD,QAAkC,EAClC,YAAoB;IAEpB,OAAO,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,IAAI,CAAC,QAAQ,KAAK,YAAY,CAAC,CAAC;AAC/D,CAAC;AALD,oFAKC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@loopback/authentication",
3
- "version": "7.1.0",
3
+ "version": "7.2.0",
4
4
  "description": "A LoopBack component for authentication support.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -24,24 +24,24 @@
24
24
  "access": "public"
25
25
  },
26
26
  "peerDependencies": {
27
- "@loopback/core": "^2.15.0",
28
- "@loopback/rest": "^9.2.0"
27
+ "@loopback/core": "^2.15.1",
28
+ "@loopback/rest": "^9.2.1"
29
29
  },
30
30
  "dependencies": {
31
- "@loopback/security": "^0.4.0",
31
+ "@loopback/security": "^0.4.1",
32
32
  "@types/express": "^4.17.11",
33
33
  "@types/lodash": "^4.14.168",
34
34
  "lodash": "^4.17.21",
35
35
  "tslib": "^2.1.0"
36
36
  },
37
37
  "devDependencies": {
38
- "@loopback/build": "^6.3.0",
39
- "@loopback/core": "^2.15.0",
40
- "@loopback/eslint-config": "^10.1.0",
41
- "@loopback/openapi-spec-builder": "^3.1.0",
42
- "@loopback/rest": "^9.2.0",
43
- "@loopback/testlab": "^3.3.0",
44
- "@types/node": "^10.17.55",
38
+ "@loopback/build": "^6.3.1",
39
+ "@loopback/core": "^2.15.1",
40
+ "@loopback/eslint-config": "^10.1.1",
41
+ "@loopback/openapi-spec-builder": "^3.1.1",
42
+ "@loopback/rest": "^9.2.1",
43
+ "@loopback/testlab": "^3.3.1",
44
+ "@types/node": "^10.17.56",
45
45
  "jsonwebtoken": "^8.5.1"
46
46
  },
47
47
  "keywords": [
@@ -59,5 +59,5 @@
59
59
  "url": "https://github.com/strongloop/loopback-next.git",
60
60
  "directory": "packages/authentication"
61
61
  },
62
- "gitHead": "85a45b8a0d6c1216c11256cb511ec6cfbfb4c226"
62
+ "gitHead": "156ca0fcf8fa246ca380ab28b44b3708896be2b6"
63
63
  }
@@ -3,7 +3,14 @@
3
3
  // This file is licensed under the MIT License.
4
4
  // License text available at https://opensource.org/licenses/MIT
5
5
 
6
- import {Getter, inject, injectable, Provider, Setter} from '@loopback/core';
6
+ import {
7
+ config,
8
+ Getter,
9
+ inject,
10
+ injectable,
11
+ Provider,
12
+ Setter,
13
+ } from '@loopback/core';
7
14
  import {
8
15
  asMiddleware,
9
16
  Middleware,
@@ -15,13 +22,18 @@ import {SecurityBindings, UserProfile} from '@loopback/security';
15
22
  import {AuthenticationBindings} from '../keys';
16
23
  import {
17
24
  AuthenticateFn,
25
+ AuthenticationOptions,
18
26
  AuthenticationStrategy,
19
27
  AUTHENTICATION_STRATEGY_NOT_FOUND,
20
28
  USER_PROFILE_NOT_FOUND,
21
29
  } from '../types';
30
+
22
31
  /**
23
32
  * Provides the authentication action for a sequence
24
- * @example `context.bind('authentication.actions.authenticate').toProvider(AuthenticateActionProvider)`
33
+ * @example
34
+ * ```ts
35
+ * context.bind('authentication.actions.authenticate').toProvider(AuthenticateActionProvider)
36
+ * ```
25
37
  */
26
38
  export class AuthenticateActionProvider implements Provider<AuthenticateFn> {
27
39
  constructor(
@@ -42,6 +54,8 @@ export class AuthenticateActionProvider implements Provider<AuthenticateFn> {
42
54
  readonly setRedirectUrl: Setter<string>,
43
55
  @inject.setter(AuthenticationBindings.AUTHENTICATION_REDIRECT_STATUS)
44
56
  readonly setRedirectStatus: Setter<number>,
57
+ @config({fromBinding: AuthenticationBindings.COMPONENT})
58
+ private readonly options: AuthenticationOptions = {},
45
59
  ) {}
46
60
 
47
61
  /**
@@ -57,59 +71,54 @@ export class AuthenticateActionProvider implements Provider<AuthenticateFn> {
57
71
  */
58
72
  async action(request: Request): Promise<UserProfile | undefined> {
59
73
  let strategies = await this.getStrategies();
60
- if (!strategies) {
74
+ if (strategies == null) {
61
75
  // The invoked operation does not require authentication.
62
76
  return undefined;
63
77
  }
64
78
  // convert to array if required
65
79
  strategies = Array.isArray(strategies) ? strategies : [strategies];
66
80
 
67
- let authenticated = false;
68
- let redirected = false;
69
- let authError: Error | undefined;
70
- let authResponse: UserProfile | RedirectRoute | undefined;
71
- let userProfile: UserProfile | undefined;
72
-
81
+ const authErrors: unknown[] = [];
73
82
  for (const strategy of strategies) {
74
- // the first strategy to succeed or redirect will halt the execution chain
75
- if (authenticated || redirected) break;
76
-
83
+ let authResponse: UserProfile | RedirectRoute | undefined = undefined;
77
84
  try {
78
85
  authResponse = await strategy.authenticate(request);
79
-
80
- // response from `strategy.authenticate()` could return an object of type UserProfile or RedirectRoute
81
- if (RedirectRoute.isRedirectRoute(authResponse)) {
82
- redirected = true;
83
- const redirectOptions = authResponse;
84
- // bind redirection url and status to the context
85
- // controller should handle actual redirection
86
- this.setRedirectUrl(redirectOptions.targetLocation);
87
- this.setRedirectStatus(redirectOptions.statusCode);
88
- } else if (authResponse) {
89
- authenticated = true;
90
- // if `strategy.authenticate()` returns an object of type UserProfile, set it as current user
91
- userProfile = authResponse as UserProfile;
92
- } else if (!authResponse) {
93
- // important to throw a non-protocol-specific error here
94
- const error = new Error(
95
- `User profile not returned from strategy's authenticate function`,
96
- );
97
- Object.assign(error, {
98
- code: USER_PROFILE_NOT_FOUND,
99
- });
100
- throw error;
86
+ } catch (err) {
87
+ if (this.options.failOnError) {
88
+ throw err;
101
89
  }
102
- } catch (error) {
103
- authError = authError ?? error;
90
+ authErrors.push(err);
104
91
  }
105
- }
106
92
 
107
- if (!authenticated && !redirected) throw authError;
93
+ // response from `strategy.authenticate()` could return an object of
94
+ // type UserProfile or RedirectRoute
95
+ if (RedirectRoute.isRedirectRoute(authResponse)) {
96
+ const redirectOptions = authResponse;
97
+ // bind redirection url and status to the context
98
+ // controller should handle actual redirection
99
+ this.setRedirectUrl(redirectOptions.targetLocation);
100
+ this.setRedirectStatus(redirectOptions.statusCode);
101
+ return;
102
+ } else if (authResponse != null) {
103
+ // if `strategy.authenticate()` returns an object of type UserProfile,
104
+ // set it as current user
105
+ const userProfile = authResponse as UserProfile;
106
+ this.setCurrentUser(userProfile);
107
+ return userProfile;
108
+ }
109
+ }
108
110
 
109
- if (userProfile) {
110
- this.setCurrentUser(userProfile);
111
- return userProfile;
111
+ if (authErrors.length) {
112
+ throw authErrors[0];
112
113
  }
114
+ // important to throw a non-protocol-specific error here
115
+ const error = new Error(
116
+ `User profile not returned from strategy's authenticate function`,
117
+ );
118
+ Object.assign(error, {
119
+ code: USER_PROFILE_NOT_FOUND,
120
+ });
121
+ throw error;
113
122
  }
114
123
  }
115
124
 
package/src/types.ts CHANGED
@@ -60,6 +60,15 @@ export interface AuthenticationOptions {
60
60
  * those methods without authentication metadata.
61
61
  */
62
62
  defaultMetadata?: AuthenticationMetadata[];
63
+
64
+ /**
65
+ * This flag allows an authentication strategy to abort the authentication by
66
+ * throwing an error if `failOnError` is set to `true`. By default, the
67
+ * authentication process continues to the next one even when a strategy
68
+ * throws an error. If one of other strategies succeed, the error will be
69
+ * discarded.
70
+ */
71
+ failOnError?: boolean;
63
72
  }
64
73
 
65
74
  /**