-
Notifications
You must be signed in to change notification settings - Fork 148
Support for refresh_in #305
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
Changes from all commits
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 |
---|---|---|
|
@@ -3,6 +3,8 @@ | |
|
||
package com.microsoft.aad.msal4j; | ||
|
||
import java.util.Date; | ||
|
||
class AcquireTokenSilentSupplier extends AuthenticationResultSupplier { | ||
|
||
private SilentRequest silentRequest; | ||
|
@@ -35,11 +37,20 @@ AuthenticationResult execute() throws Exception { | |
silentRequest.parameters().scopes(), | ||
clientApplication.clientId()); | ||
|
||
if (res == null) { | ||
throw new MsalClientException(AuthenticationErrorMessage.NO_TOKEN_IN_CACHE, AuthenticationErrorCode.CACHE_MISS); | ||
} | ||
|
||
if (!StringHelper.isBlank(res.accessToken())) { | ||
clientApplication.getServiceBundle().getServerSideTelemetry().incrementSilentSuccessfulCount(); | ||
} | ||
|
||
if (silentRequest.parameters().forceRefresh() || StringHelper.isBlank(res.accessToken())) { | ||
//Determine if the current token needs to be refreshed according to the refresh_in value | ||
long currTimeStampSec = new Date().getTime() / 1000; | ||
boolean afterRefreshOn = res.refreshOn() != null && res.refreshOn() > 0 && | ||
Avery-Dunn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
res.refreshOn() < currTimeStampSec && res.expiresOn() >= currTimeStampSec; | ||
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. res.expiresOn() >= currTimeStampSec means you will not do refresh if AT is expired, why ? 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. I didn't want to change any behavior if the token was expired, so if the token was after the |
||
|
||
if (silentRequest.parameters().forceRefresh() || afterRefreshOn || StringHelper.isBlank(res.accessToken())) { | ||
if (!StringHelper.isBlank(res.refreshToken())) { | ||
RefreshTokenRequest refreshTokenRequest = new RefreshTokenRequest( | ||
RefreshTokenParameters.builder(silentRequest.parameters().scopes(), res.refreshToken()).build(), | ||
|
@@ -50,7 +61,16 @@ AuthenticationResult execute() throws Exception { | |
AcquireTokenByAuthorizationGrantSupplier acquireTokenByAuthorisationGrantSupplier = | ||
new AcquireTokenByAuthorizationGrantSupplier(clientApplication, refreshTokenRequest, requestAuthority); | ||
|
||
res = acquireTokenByAuthorisationGrantSupplier.execute(); | ||
try { | ||
res = acquireTokenByAuthorisationGrantSupplier.execute(); | ||
} catch (MsalServiceException ex) { | ||
//If the token refresh attempt threw a MsalServiceException but the refresh attempt was done | ||
// only because of refreshOn, then simply return the existing cached token | ||
if (afterRefreshOn && !(silentRequest.parameters().forceRefresh() || StringHelper.isBlank(res.accessToken()))) { | ||
return res; | ||
} | ||
else throw ex; | ||
} | ||
} else { | ||
res = null; | ||
} | ||
|
Uh oh!
There was an error while loading. Please reload this page.
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.
I think it would be better to have unit test to test this scenario, current test does not cover end to end scenario , also it is hybrid of unit test + integration testing , i would keep them separated
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.
Without a way to get
refresh_in
in an actual token response, I'm not sure if there's a way to have meaningful tests without a hybrid like this. A unit test of setting therefreshOn
field would just be 'setrefreshOn
manually, then assert that it was set'. The only time it's used is halfway through the acquireTokenSilent flow, and a unit test of that would need a lot of hardcoding that the actual token request process takes care of anyway.