Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/Auth : Playground Sample : automatic refresh token #658

Merged
merged 71 commits into from
Aug 23, 2018
Merged

Feat/Auth : Playground Sample : automatic refresh token #658

merged 71 commits into from
Aug 23, 2018

Conversation

alain-charles
Copy link
Contributor

@alain-charles alain-charles commented Aug 22, 2018

Feat/Auth : Playground / Backend sample : automatic refreshToken

This pull requests implements in both backend and playground an example of Oauth2 authentication with automatic refreshtoken requests sent to the backend when the local stored accesstoken has expired.
This allows the user not to type his login/password as long as his refresh-token is valid.
The refreshtoken requests are sent transparently.

You can easily test the code by calling the url http:https://localhost:4200/#/auth/api-calls.component
Then the AuthGuard service redirects you to the login page.

The strategy used to log in is Oauth2 with password grant-type, so you can use [email protected].

You are then logged with an access token whose ttl is 60 seconds and a refresh token whose ttl is 120 seconds.
These values can be modified in the configuration of the backend (config.js).

capture d ecran 2018-08-15 a 10 04 43

Call of the secured api

Once logged, go immediately to http:https://localhost:4200/#/auth/api-calls.component and click the call API button.

The XHR request is send with the authorization header containing Bearer + the access-token.
You can see that Alain's got good wines in his cellar.

capture d ecran 2018-08-15 a 10 05 40

Automatic refresh-token request

Wait a minute, and click once again on the call-api button.

Observe the network logs of you browser : the acces-token was expired, so the framework sent automatically a refresh request to the backend
capture d ecran 2018-08-15 a 10 06 25
with the next content :
capture d ecran 2018-08-15 a 10 07 02
and it got a new access-token:
capture d ecran 2018-08-15 a 10 07 10

then it called with success the protected api.

Redirect to login or anything else when refresh_token has expired

If you wait once a minute, and if you click the call-api button, the framework sends a new refreshToken request
capture d ecran 2018-08-15 a 10 07 41

The component responsible for the API call intercepts the backend 401 error and you are redirected to the login page
capture d ecran 2018-08-15 a 10 07 56

Hope you will like this work.

PS: this can be played in the playground with NbAuthPasswordStrategy as well, just replace in the ** playground** auth-module.ts:

login: {
          strategy: 'password',
...}

by

login: {
          strategy: 'email',
...}

alain-charles and others added 30 commits June 29, 2018 08:33
Now : If existing, NbAuthResult contains backend error description
other Changes requested by Dmitry (first review)
Now : If existing, NbAuthResult contains backend error description
other Changes requested by Dmitry (first review)
+tslint missing trailing comma arghhh
…strategy

 used to create the token (future use)
The token now contains ownerStrategyName, with is a back link to the strategy
used to create the token (future use).

BREAKING CHANGE :
NbAuthCreateToken (token.ts) now takes a third parameter, which is the ownerStrategyName
Tokens are created by strategies that are called from services, so it is *potentially* a breaking change.
The token now contains ownerStrategyName, with is a back link to the strategy
used to create the token (future use).
updated unit tests files and oauth2-password-login.component (breaking change below)

BREAKING CHANGE :
NbAuthCreateToken (token.ts) now takes a third parameter, which is the ownerStrategyName
Tokens are created by strategies that are called from services, so it is *potentially* a breaking change.
removed useless code and cleaned one unit test file

BREAKING CHANGE :
NbAuthCreateToken (token.ts) now takes a third parameter, which is the ownerStrategyName
Tokens are created by strategies that are called from services, so it is *potentially* a breaking change.
alain-charles and others added 16 commits July 27, 2018 10:05
# Conflicts:
#	src/framework/auth/services/token/token.ts

Optimized token.ts code
…uth-automaticRefreshToken

# Conflicts:
#	src/framework/auth/services/token/token.spec.ts
#	src/framework/auth/services/token/token.ts
In cas of invalid token, the interceptor & authService now try *automatically* to refresh the token.
Redirection to the login page is now made only if refreshToken fails to get a valid access-token.

playground :
new Url ''/auth/api-calls.component' to play the above functionality.

backend : updated with necessary stuff to respond to the api-calls.component.
…cture

This version implements a filter function for any urls the client code wants to exclude from NbAuthJWTInterceptor.
Auths url base endpoints must be added in filters so that the interceptor does not run into infinite loop.

NOT TO BE MERGED since another possible version is commented out in this code.
Automatic refreshToken Call example in the playground.
var token = req.body.refresh_token;

// token issued via email strategy
if (token === 'eb4e15840117437cbfd7343f257c4aae') {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's move this 'eb4e15840117437cbfd7343f257c4aae' into a variable

@@ -3,7 +3,7 @@
* Copyright Akveo. All Rights Reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*/
import { Inject, Injectable } from '@angular/core';
import {Inject, Injectable } from '@angular/core';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we really need that style checker!

@@ -23,7 +23,7 @@ import {
auth2StrategyOptions,
NbOAuth2GrantType, NbOAuth2ClientAuthMethod,
} from './oauth2-strategy.options';
import { NbAuthStrategyClass } from '../../auth.options';
import { NbAuthStrategyClass} from '../../auth.options';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and here

* Licensed under the MIT License. See License.txt in the project root for license information.
*/

import {Component, Inject} from '@angular/core';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spaces here

@Component({
selector: 'nb-playground-api-calls',
styles: [`
.rTable {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is rTable? is this some kind of 3rd party styles? naming convention looks strange

}
`],
template: `
<router-outlet></router-outlet>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need the outlet here?

<nb-layout>
<nb-layout-column>
<nb-card>
<nb-card-body class="col-xl-4 col-lg-6 col-md-8 col-sm-12" style="margin:auto;">
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also would be really great if we can get rid of bootstrap as we are keen to remove it at some step

})



Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need in these new lines

NbOAuth2GrantType,
NbOAuth2AuthStrategy,
NbAuthOAuth2Token,
NB_AUTH_TOKEN_INTERCEPTOR_FILTER } from '@nebular/auth';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

} from '@nebular/auth'; should go on a new line

],
providers: [
NbAuthGuard,
{ provide: HTTP_INTERCEPTORS, useClass: NbAuthJWTInterceptor, multi: true },
{ provide: NB_AUTH_TOKEN_INTERCEPTOR_FILTER, useValue: filterInterceptorRequest},
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spaces before }

@nnixaa
Copy link
Collaborator

nnixaa commented Aug 23, 2018

@nnixaa nnixaa merged commit b4fc624 into akveo:master Aug 23, 2018
@alain-charles alain-charles deleted the feat/auth-automaticRefreshToken branch August 26, 2018 08:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants