Skip to content

Commit

Permalink
chore(authentication-jwt): readme updated
Browse files Browse the repository at this point in the history
readme.md to use refresh token and extra configurations
  • Loading branch information
madaky committed May 28, 2020
1 parent d65d227 commit 3786cb7
Showing 1 changed file with 65 additions and 2 deletions.
67 changes: 65 additions & 2 deletions extensions/authentication-jwt/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export class MySequence implements SequenceHandler {
</details>

- mount jwt component in application

- bind datasource to user service and refresh token
<details>
<summary><strong>Check The Code</strong></summary>
<p>
Expand Down Expand Up @@ -109,8 +109,10 @@ export class TestApplication extends BootMixin(
this.component(AuthenticationComponent);
// Mount jwt component
this.component(JWTAuthenticationComponent);
// Bind datasource
// Bind datasource for user
this.dataSource(DbDataSource, UserServiceBindings.DATASOURCE_NAME);
// Bind datasource for refresh token
this.dataSource(DbDataSource, RefreshTokenBindings.DATASOURCE_NAME);

this.component(RestExplorerComponent);
this.projectRoot = __dirname;
Expand Down Expand Up @@ -164,6 +166,46 @@ The code snippet for whoAmI function:
}
```

### End points with refresh token

To add refresh token mechanism in your app. you have to call the following
interrceptor.

1. ('refresh-token-generate') : to generate the refresh token and access token
when user logins to your app with provided credentials.

```ts
@intercept('refresh-token-generate')
async refreshLogin(
@requestBody(CredentialsRequestBody) credentials: Credentials,
): Promise<TokenObject> {
// ensure the user exists, and the password is correct
const user = await this.userService.verifyCredentials(credentials);
// convert a User object into a UserProfile object (reduced set of properties)
const userProfile: UserProfile = this.userService.convertToUserProfile(
user,
);
// create a JSON Web Token based on the user profile
const token = {
accessToken: await this.jwtService.generateToken(userProfile),
};
return token;
}
```

2. ('refresh-token-grant'): to genearate the access token by the refresh token
obtained from the the last login end-point.

```ts
@intercept('refresh-token-grant')
async refresh(
@requestBody(RefreshGrantRequestBody) refreshGrant: RefreshGrant,
): Promise<{token: string}> {
const token = '';
return {token};
}
```

The complete file is in
[user.controller.ts](https://github.com/strongloop/loopback-next/tree/master/extensions/authentication-jwt/src/__tests__/fixtures/controllers/user.controller.ts)

Expand Down Expand Up @@ -294,6 +336,27 @@ provide your own `User` model and repository.
}
```

### Extra configurations

1. To change the token secret in your application.ts

```
// for jwt access token
this.bind(TokenServiceBindings.TOKEN_SECRET).to("<yourSecret>");
// for refresh token
this.bind(RefreshTokenInterceptorBindings.TOKEN_SECRET).to("<yourSecret>");
```

2. To change token expiration. to learn more about expiration time here at
[Ziet/ms](https://github.com/zeit/ms)

```
// for jwt access token expiration
this.bind(TokenServiceBindings.TOKEN_EXPIRES_IN).to("<Expiration Time in sec>");
// for refresh token expiration
this.bind(RefreshTokenInterceptorBindings.TOKEN_EXPIRES_IN).to("<Expiration Time in sec>");
```

## Future Work

The security specification is currently manually added in the application file.
Expand Down

0 comments on commit 3786cb7

Please sign in to comment.