-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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 : refreshToken request automatically sent by NbAuthService if isAuthenticatedOrRefresh is called #649
Changes from all commits
cef7a1d
9f0dbab
44d7d2e
f57b737
ed4628e
d7f01df
af52e88
554f7dc
fa0548f
b72adae
733efc0
57fab69
c50df3a
dda5436
ef0be25
e180dc3
6aa1fdb
96c7344
cf82c42
dcff930
315662f
57a0230
d69d633
1be8d91
4b67825
deea3e2
acd0241
25250f0
c0fdd31
bd564b3
44ed61a
9c16387
2dd26c4
b284377
e462c7d
a3ee10b
b38ae55
62a2aff
66ca493
3bd5a50
398c058
ee706d3
5eb5821
1a08225
1d697bc
1c25956
ff0e8d3
e7af99e
95632b1
bc4001f
10dfe71
4390bf8
dc2a96a
9811ed3
1da13c9
f5a4eda
49557c2
0e23673
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,7 +24,7 @@ describe('auth-service', () => { | |
let tokenService: NbTokenService; | ||
let dummyAuthStrategy: NbDummyAuthStrategy; | ||
const testTokenValue = 'test-token'; | ||
const ownerStrategyName = 'strategy'; | ||
const ownerStrategyName = 'dummy'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we need this change? looks unrelated There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes we do since i added |
||
|
||
|
||
const resp401 = new HttpResponse<Object>({body: {}, status: 401}); | ||
|
@@ -143,6 +143,62 @@ describe('auth-service', () => { | |
}, | ||
); | ||
|
||
it('isAuthenticatedOrRefresh, token valid, strategy refreshToken not called, returns true', (done) => { | ||
const spy = spyOn(dummyAuthStrategy, 'refreshToken') | ||
|
||
spyOn(tokenService, 'get') | ||
.and | ||
.returnValue(observableOf(testToken)); | ||
|
||
authService.isAuthenticatedOrRefresh() | ||
.pipe(first()) | ||
.subscribe((isAuth: boolean) => { | ||
expect(spy).not.toHaveBeenCalled(); | ||
expect(isAuth).toBeTruthy(); | ||
done(); | ||
}); | ||
}, | ||
); | ||
|
||
it('isAuthenticatedOrRefresh, token invalid, strategy refreshToken called, returns true', (done) => { | ||
|
||
const spy = spyOn(dummyAuthStrategy, 'refreshToken') | ||
.and | ||
.returnValue(observableOf(successResult)); | ||
|
||
spyOn(tokenService, 'get') | ||
.and | ||
.returnValues(observableOf(emptyToken), observableOf(testToken)); | ||
|
||
authService.isAuthenticatedOrRefresh() | ||
.pipe(first()) | ||
.subscribe((isAuth: boolean) => { | ||
expect(spy).toHaveBeenCalled(); | ||
expect(isAuth).toBeTruthy(); | ||
done(); | ||
}); | ||
}, | ||
); | ||
|
||
it('isAuthenticatedOrRefresh, token invalid, strategy refreshToken called, returns false', (done) => { | ||
const spy = spyOn(dummyAuthStrategy, 'refreshToken') | ||
.and | ||
.returnValue(observableOf(failResult)); | ||
|
||
spyOn(tokenService, 'get') | ||
.and | ||
.returnValues(observableOf(emptyToken), observableOf(emptyToken)); | ||
|
||
authService.isAuthenticatedOrRefresh() | ||
.pipe(first()) | ||
.subscribe((isAuth: boolean) => { | ||
expect(spy).toHaveBeenCalled(); | ||
expect(isAuth).toBeFalsy(); | ||
done(); | ||
}); | ||
}, | ||
); | ||
|
||
it('onTokenChange return correct stream and gets test token', (done) => { | ||
const spy = spyOn(tokenService, 'tokenChange') | ||
.and | ||
|
@@ -166,7 +222,7 @@ describe('auth-service', () => { | |
delay(1000), | ||
)); | ||
|
||
authService.authenticate('dummy').subscribe((authRes: NbAuthResult) => { | ||
authService.authenticate(ownerStrategyName).subscribe((authRes: NbAuthResult) => { | ||
expect(spy).toHaveBeenCalled(); | ||
expect(authRes.isFailure()).toBeTruthy(); | ||
expect(authRes.isSuccess()).toBeFalsy(); | ||
|
@@ -193,7 +249,7 @@ describe('auth-service', () => { | |
.returnValue(observableOf(null)); | ||
|
||
|
||
authService.authenticate('dummy').subscribe((authRes: NbAuthResult) => { | ||
authService.authenticate(ownerStrategyName).subscribe((authRes: NbAuthResult) => { | ||
expect(strategySpy).toHaveBeenCalled(); | ||
expect(tokenServiceSetSpy).toHaveBeenCalled(); | ||
|
||
|
@@ -218,7 +274,7 @@ describe('auth-service', () => { | |
delay(1000), | ||
)); | ||
|
||
authService.register('dummy').subscribe((authRes: NbAuthResult) => { | ||
authService.register(ownerStrategyName).subscribe((authRes: NbAuthResult) => { | ||
expect(spy).toHaveBeenCalled(); | ||
expect(authRes.isFailure()).toBeTruthy(); | ||
expect(authRes.isSuccess()).toBeFalsy(); | ||
|
@@ -244,7 +300,7 @@ describe('auth-service', () => { | |
.and | ||
.returnValue(observableOf(null)); | ||
|
||
authService.register('dummy').subscribe((authRes: NbAuthResult) => { | ||
authService.register(ownerStrategyName).subscribe((authRes: NbAuthResult) => { | ||
expect(strategySpy).toHaveBeenCalled(); | ||
expect(tokenServiceSetSpy).toHaveBeenCalled(); | ||
|
||
|
@@ -268,7 +324,7 @@ describe('auth-service', () => { | |
delay(1000), | ||
)); | ||
|
||
authService.logout('dummy').subscribe((authRes: NbAuthResult) => { | ||
authService.logout(ownerStrategyName).subscribe((authRes: NbAuthResult) => { | ||
expect(spy).toHaveBeenCalled(); | ||
|
||
expect(authRes.isFailure()).toBeTruthy(); | ||
|
@@ -292,7 +348,7 @@ describe('auth-service', () => { | |
)); | ||
const tokenServiceClearSpy = spyOn(tokenService, 'clear').and.returnValue(observableOf('STUB')); | ||
|
||
authService.logout('dummy').subscribe((authRes: NbAuthResult) => { | ||
authService.logout(ownerStrategyName).subscribe((authRes: NbAuthResult) => { | ||
expect(strategyLogoutSpy).toHaveBeenCalled(); | ||
expect(tokenServiceClearSpy).toHaveBeenCalled(); | ||
|
||
|
@@ -316,7 +372,7 @@ describe('auth-service', () => { | |
delay(1000), | ||
)); | ||
|
||
authService.requestPassword('dummy').subscribe((authRes: NbAuthResult) => { | ||
authService.requestPassword(ownerStrategyName).subscribe((authRes: NbAuthResult) => { | ||
expect(spy).toHaveBeenCalled(); | ||
|
||
expect(authRes.isFailure()).toBeTruthy(); | ||
|
@@ -339,7 +395,7 @@ describe('auth-service', () => { | |
delay(1000), | ||
)); | ||
|
||
authService.requestPassword('dummy').subscribe((authRes: NbAuthResult) => { | ||
authService.requestPassword(ownerStrategyName).subscribe((authRes: NbAuthResult) => { | ||
expect(strategyLogoutSpy).toHaveBeenCalled(); | ||
|
||
expect(authRes.isFailure()).toBeFalsy(); | ||
|
@@ -362,7 +418,7 @@ describe('auth-service', () => { | |
delay(1000), | ||
)); | ||
|
||
authService.resetPassword('dummy').subscribe((authRes: NbAuthResult) => { | ||
authService.resetPassword(ownerStrategyName).subscribe((authRes: NbAuthResult) => { | ||
expect(spy).toHaveBeenCalled(); | ||
|
||
expect(authRes.isFailure()).toBeTruthy(); | ||
|
@@ -385,7 +441,7 @@ describe('auth-service', () => { | |
delay(1000), | ||
)); | ||
|
||
authService.resetPassword('dummy').subscribe((authRes: NbAuthResult) => { | ||
authService.resetPassword(ownerStrategyName).subscribe((authRes: NbAuthResult) => { | ||
expect(strategyLogoutSpy).toHaveBeenCalled(); | ||
|
||
expect(authRes.isFailure()).toBeFalsy(); | ||
|
@@ -408,7 +464,7 @@ describe('auth-service', () => { | |
delay(1000), | ||
)); | ||
|
||
authService.refreshToken('dummy').subscribe((authRes: NbAuthResult) => { | ||
authService.refreshToken(ownerStrategyName).subscribe((authRes: NbAuthResult) => { | ||
expect(spy).toHaveBeenCalled(); | ||
expect(authRes.isFailure()).toBeTruthy(); | ||
expect(authRes.isSuccess()).toBeFalsy(); | ||
|
@@ -434,7 +490,7 @@ describe('auth-service', () => { | |
.and | ||
.returnValue(observableOf(null)); | ||
|
||
authService.refreshToken('dummy').subscribe((authRes: NbAuthResult) => { | ||
authService.refreshToken(ownerStrategyName).subscribe((authRes: NbAuthResult) => { | ||
expect(strategySpy).toHaveBeenCalled(); | ||
expect(tokenServiceSetSpy).toHaveBeenCalled(); | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this work with any kind of token type? Is the refreshToken endpoint always expecting the same token that was stored?
I am using an API that during authentication returns an object consisting of a refresh token, an access token and other properties. However, on refreshing the token I am expected to only supply the refresh token.
To get it working, I had to convert the token to an NbAuthOAuth2Token and explicitly call "oAuth2Token.getRefreshToken()" on this line instead of just "token".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @cloakedch ,
The refresh_token is implemented according to RFC6749 section 1.5.
This RFC specifies the OAuth2 authentication scheme. So the code works with auths server returning tokens according to the RFC section 4.2
The refreshToken endpoint is waiting for the refresh_token that was sent during login or during the latest refresh_token call. Both
grant_type=refresh_token
andrefresh_token=theRefreshToken
are sent to the refreshToken endpoint for getting a new access-token, according to the RFC.I would say that you have to use the
NbOAuth2AuthStrategy
withpassword
grantType(if you send login/password at login) andNbAuthOAuth2Token
class in your case.Someting like
Hope it helps
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, I guess the API I am using is then not working according to the RFC.
Thanks for clearing that up!