Skip to content

Commit 9bb5241

Browse files
committed
fixup!: address feedback
1 parent 69d2562 commit 9bb5241

File tree

4 files changed

+40
-24
lines changed

4 files changed

+40
-24
lines changed

packages/authentication/docs/authentication-strategy.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44
import {Request} from '@loopback/rest';
55

66
interface AuthenticationStrategy {
7-
// The resolver will read the options object from metadata, call `strategy.setOptions`
8-
options: object;
9-
authenticate(request: Request): Promise<UserProfile | undefined>;
10-
setOptions(options: object);
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>;
13+
1114
// This is a private function that extracts credential fields from a request,
1215
// it is called in function `authenticate`. You could organize the extraction
1316
// logic in this function or write them in `authenticate` directly without defining

packages/authentication/docs/controller-functions.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ the beginning of markdown file
55
[authentication-system](./authentication-system.md).
66

77
Please note how they are decorated with `@authenticate()`, the syntax is:
8-
`@authenticate(<strategy_name>, {action: <action_name>, session: <enabled_or_not>})`
8+
`@authenticate(strategy_name, options)`
99

1010
- /login
1111

@@ -48,10 +48,16 @@ class LoginController{
4848
@inject(AuthenticationBindings.SERVICES.JWT_TOKEN) JWTtokenService: TokenService,
4949
) {}
5050

51+
// I was about to create a local login example, while if the credentials are
52+
// provided in the request body, all the authenticate logic will happen in the
53+
// controller, the auth action isn't even involved.
54+
// See the login endpoint in shopping example
55+
// https://github.com/strongloop/loopback4-example-shopping/blob/master/src/controllers/user.controller.ts#L137
56+
5157
// Describe the response using OpenAPI spec
52-
@post('/loginOAI/local', RESPONSE_SPEC_FOR_JWT_LOGIN)
58+
@post('/loginOAI/basicAuth', RESPONSE_SPEC_FOR_JWT_LOGIN)
5359
@authenticate('basicAuth')
54-
localLoginReturningJWTToken() {
60+
basicAuthLoginReturningJWTToken() {
5561
await token = JWTtokenService.generateToken(this.userProfile);
5662
// Action `send` will serialize token into response according to the OpenAPI spec.
5763
return token;
@@ -60,9 +66,9 @@ class LoginController{
6066
// OR
6167
// Serialize the token into response in the controller directly without describing it
6268
// with OpenAPI spec
63-
@post('/loginWithoutOAI/local')
69+
@post('/loginWithoutOAI/basicAuth')
6470
@authenticate('basicAuth')
65-
localLoginReturningJWTToken() {
71+
basicAuthLoginReturningJWTToken() {
6672
await token = JWTtokenService.generateToken(this.userProfile);
6773
// It's on users to serialize the token into the response.
6874
await writeTokenToResponse();

packages/authentication/docs/strategies/basic-auth.md

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,28 @@ You could find the `AuthenticationStrategy` interface in file
44
```ts
55
import {Request} from '@loopback/rest';
66

7+
interface BasicAuthOptions = {
8+
// Define it as anyobject in the pseudo code
9+
[property: string]: any;
10+
};
11+
712
class BasicAuthenticationStrategy implements AuthenticationStrategy {
813
options: object;
914
constructor(
10-
@inject(AUTHENTICATION_BINDINGS.SERVICES.USER) userService: UserService,
11-
@inject(AUTHENTICATION_BINDINGS.BASIC.OPTIONS) options?: object,
15+
@inject(AUTHENTICATION_BINDINGS.USER_SERVICE) userService: UserService,
16+
@inject(AUTHENTICATION_BINDINGS.BASIC_AUTH_OPTIONS) options?: BasicAuthOptions,
1217
) {}
1318

14-
authenticate(request: Request): Promise<UserProfile | undefined> {
19+
authenticate(request: Request, options: BasicAuthOptions): Promise<UserProfile | undefined> {
20+
// override the global set options with the one passed from the caller
21+
options = options || this.options;
1522
// extract the username and password from request
1623
const credentials = await this.extractCredentials(request);
1724
// `verifyCredentials` throws error accordingly: user doesn't exist OR invalid credentials
1825
const user = await userService.verifyCredentials(credentials);
1926
return await userService.convertToUserProfile(user);
2027
}
2128

22-
setOptions(newOptions: object) {
23-
Object.assign(options, newOptions);
24-
}
25-
2629
extractCredentials(request): Promise<Credentials> {
2730
// code to extract username and password from request header
2831
}

packages/authentication/docs/strategies/jwt.md

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,29 @@ You could find the `AuthenticationStrategy` interface in file
44
```ts
55
import {Request} from '@loopback/rest';
66

7+
interface JWTAuthOptions = {
8+
// Define it as anyobject in the pseudo code
9+
[property: string]: any;
10+
};
711
class JWTAuthenticationStrategy implements AuthenticationStrategy {
8-
options: object;
912
constructor(
10-
@inject(AUTHENTICATION_BINDINGS.SERVICES.USER) tokenService: TokenService,
11-
@inject(AUTHENTICATION_BINDINGS.BASIC.OPTIONS) options?: object,
13+
@inject(AUTHENTICATION_BINDINGS.USER_SERVICE) tokenService: TokenService,
14+
@inject(AUTHENTICATION_BINDINGS.JWT_AUTH_OPTIONS) options?: JWTAuthOptions,
1215
) {}
1316

14-
authenticate(request: Request): Promise<UserProfile | undefined> {
17+
authenticate(
18+
request: Request,
19+
options: JWTAuthOptions,
20+
): Promise<UserProfile | undefined> {
21+
// override the global set options with the one passed from the caller
22+
options = options || this.options;
1523
// extract the username and password from request
1624
const token = await this.extractCredentials(request);
1725
// `verifyToken` should decode the payload from the token and convert the token payload to
1826
// userProfile object.
1927
return await tokenService.verifyToken(token);
2028
}
2129

22-
setOptions(newOptions: object) {
23-
Object.assign(options, newOptions);
24-
}
25-
2630
extractCredentials(request): Promise<string> {
2731
// code to extract json web token from request header/cookie/query
2832
}

0 commit comments

Comments
 (0)