Skip to content

Commit 6ebb283

Browse files
committed
feat: introduce an authentication strategy interface
Introduce an authentication strategy interface
1 parent c774ed1 commit 6ebb283

File tree

2 files changed

+67
-13
lines changed

2 files changed

+67
-13
lines changed
Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,45 @@
1-
### Auth strategy interface
1+
### Authentication strategy interface
22

33
```ts
44
import {Request} from '@loopback/rest';
55

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;
1320

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>;
1933
}
2034
```
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).

packages/authentication/src/types.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,32 @@ export interface UserProfile {
2222
name?: string;
2323
email?: string;
2424
}
25+
26+
/**
27+
* An interface that describes the common authentication strategy.
28+
*
29+
* An authentication strategy is a class with an
30+
* 'authenticate' method that verifies a user's credentials and
31+
* returns the corresponding user profile.
32+
*
33+
*/
34+
export interface AuthenticationStrategy {
35+
/**
36+
* The 'name' property is a unique identifier for the
37+
* authentication strategy ( for example : 'basic', 'jwt', etc)
38+
*/
39+
name: string;
40+
41+
/**
42+
* The 'authenticate' method takes in a given request and returns a user profile
43+
* which is an instance of 'UserProfile'.
44+
* (A user profile is a minimal subset of a user object)
45+
* If the user credentials are valid, this method should return a 'UserProfile' instance.
46+
* If the user credentials are invalid, this method should throw an error
47+
* If the user credentials are missing, this method should throw an error, or return 'undefined'
48+
* and let the authentication action deal with it.
49+
*
50+
* @param request
51+
*/
52+
authenticate(request: Request): Promise<UserProfile | undefined>;
53+
}

0 commit comments

Comments
 (0)