|
1 |
| -### Auth strategy interface |
| 1 | +### Authentication strategy interface |
2 | 2 |
|
3 | 3 | ```ts
|
4 | 4 | import {Request} from '@loopback/rest';
|
5 | 5 |
|
6 |
| -interface AuthenticationStrategy { |
7 |
| - // The resolver will read the `options` object from metadata, then invoke the |
8 |
| - // `authenticate` with `options` if it exists. |
9 |
| - authenticate( |
10 |
| - request: Request, |
11 |
| - options: object, |
12 |
| - ): Promise<UserProfile | undefined>; |
| 6 | +/** |
| 7 | + * An interface that describes the common authentication strategy. |
| 8 | + * |
| 9 | + * An authentication strategy is a class with an |
| 10 | + * 'authenticate' method that verifies a user's credentials and |
| 11 | + * returns the corresponding user profile. |
| 12 | + * |
| 13 | + */ |
| 14 | +export interface AuthenticationStrategy { |
| 15 | + /** |
| 16 | + * The 'name' property is a unique identifier for the |
| 17 | + * authentication strategy ( for example : 'basic', 'jwt', etc) |
| 18 | + */ |
| 19 | + name: string; |
13 | 20 |
|
14 |
| - // This is a private function that extracts credential fields from a request, |
15 |
| - // it is called in function `authenticate`. You could organize the extraction |
16 |
| - // logic in this function or write them in `authenticate` directly without defining |
17 |
| - // this extra utility. |
18 |
| - private extractCredentials?(request: Request): Promise<Credentials>; |
| 21 | + /** |
| 22 | + * The 'authenticate' method takes in a given request and returns a user profile |
| 23 | + * which is an instance of 'UserProfile'. |
| 24 | + * (A user profile is a minimal subset of a user object) |
| 25 | + * If the user credentials are valid, this method should return a 'UserProfile' instance. |
| 26 | + * If the user credentials are invalid, this method should throw an error |
| 27 | + * If the user credentials are missing, this method should throw an error, or return 'undefined' |
| 28 | + * and let the authentication 'action' in the 'sequence' deal with it. |
| 29 | + * |
| 30 | + * @param request |
| 31 | + */ |
| 32 | + authenticate(request: Request): Promise<UserProfile | undefined>; |
19 | 33 | }
|
20 | 34 | ```
|
| 35 | + |
| 36 | +An authentication strategy resolver can make use of the `name` property to |
| 37 | +`find` the registered authentication strategy. |
| 38 | + |
| 39 | +The authentication strategy interface has an `authenticate` function which takes |
| 40 | +in a request and returns a user profile. |
| 41 | + |
| 42 | +Authentication strategies that implement this interface can use dependency |
| 43 | +injection in the constructor to obtain **global** or **request-specific** |
| 44 | +`options` or any `services` it may require (a service to extract credentials |
| 45 | +from a request, for example). |
0 commit comments