Skip to content

Commit 847286e

Browse files
authored
Pesomka/cach miss exception (#105)
* acquire token silently throwing exception for cache miss
1 parent 3794ea7 commit 847286e

File tree

5 files changed

+91
-24
lines changed

5 files changed

+91
-24
lines changed

src/main/java/com/microsoft/aad/msal4j/AcquireTokenSilentSupplier.java

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,31 +28,35 @@ AuthenticationResult execute() throws Exception {
2828
requestAuthority,
2929
silentRequest.parameters().scopes(),
3030
clientApplication.clientId());
31-
return StringHelper.isBlank(res.accessToken()) ? null : res;
3231
}
32+
else {
33+
res = clientApplication.tokenCache.getCachedAuthenticationResult(
34+
silentRequest.parameters().account(),
35+
requestAuthority,
36+
silentRequest.parameters().scopes(),
37+
clientApplication.clientId());
3338

34-
res = clientApplication.tokenCache.getCachedAuthenticationResult(
35-
silentRequest.parameters().account(),
36-
requestAuthority,
37-
silentRequest.parameters().scopes(),
38-
clientApplication.clientId());
39-
40-
if (!silentRequest.parameters().forceRefresh() && !StringHelper.isBlank(res.accessToken())) {
41-
return res;
42-
}
39+
if (silentRequest.parameters().forceRefresh() || StringHelper.isBlank(res.accessToken())) {
4340

44-
if (!StringHelper.isBlank(res.refreshToken())) {
45-
RefreshTokenRequest refreshTokenRequest = new RefreshTokenRequest(
46-
RefreshTokenParameters.builder(silentRequest.parameters().scopes(), res.refreshToken()).build(),
47-
silentRequest.application(),
48-
silentRequest.requestContext());
41+
if (!StringHelper.isBlank(res.refreshToken())) {
42+
RefreshTokenRequest refreshTokenRequest = new RefreshTokenRequest(
43+
RefreshTokenParameters.builder(silentRequest.parameters().scopes(), res.refreshToken()).build(),
44+
silentRequest.application(),
45+
silentRequest.requestContext());
4946

50-
AcquireTokenByAuthorizationGrantSupplier acquireTokenByAuthorisationGrantSupplier =
51-
new AcquireTokenByAuthorizationGrantSupplier(clientApplication, refreshTokenRequest, requestAuthority);
47+
AcquireTokenByAuthorizationGrantSupplier acquireTokenByAuthorisationGrantSupplier =
48+
new AcquireTokenByAuthorizationGrantSupplier(clientApplication, refreshTokenRequest, requestAuthority);
5249

53-
return acquireTokenByAuthorisationGrantSupplier.execute();
54-
} else {
55-
return null;
50+
res = acquireTokenByAuthorisationGrantSupplier.execute();
51+
}
52+
else{
53+
res = null;
54+
}
55+
}
56+
}
57+
if(res == null || StringHelper.isBlank(res.accessToken())){
58+
throw new MsalClientException(AuthenticationErrorMessage.NO_TOKEN_IN_CACHE, AuthenticationErrorCode.CACHE_MISS);
5659
}
60+
return res;
5761
}
5862
}

src/main/java/com/microsoft/aad/msal4j/AuthenticationErrorCode.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ public class AuthenticationErrorCode {
4141
*/
4242
public final static String USER_REALM_DISCOVERY_FAILED = "user_realm_discovery_failed";
4343

44+
/**
45+
* Not found in the cache
46+
*/
47+
public final static String CACHE_MISS = "cache_miss";
48+
4449
/**
4550
* Unknown error occurred
4651
*/
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
package com.microsoft.aad.msal4j;
5+
6+
public class AuthenticationErrorMessage {
7+
8+
/**
9+
* Token not found it the cache
10+
*/
11+
public final static String NO_TOKEN_IN_CACHE = "Token not found it the cache";
12+
}

src/samples/msal-web-sample/src/main/java/com/microsoft/azure/msalwebsample/AuthHelper.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,6 @@ IAuthenticationResult getAuthResultBySilentFlow(HttpServletRequest httpRequest,
7676
throw e.getCause();
7777
}
7878

79-
if (updatedResult == null) {
80-
throw new ServiceUnavailableException("authentication result was null");
81-
}
82-
8379
//update session with latest token cache
8480
storeTokenCacheInSession(httpRequest, app.tokenCache().serialize());
8581

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
package com.microsoft.aad.msal4j;
5+
6+
import org.powermock.modules.testng.PowerMockTestCase;
7+
import org.testng.annotations.Test;
8+
9+
import java.util.Collections;
10+
import java.util.concurrent.CompletionException;
11+
12+
@Test(groups = { "checkin" })
13+
public class AcquireTokenSilentlyTest extends PowerMockTestCase {
14+
15+
@Test(expectedExceptions = MsalClientException.class,
16+
expectedExceptionsMessageRegExp = AuthenticationErrorMessage.NO_TOKEN_IN_CACHE)
17+
public void publicAppAcquireTokenSilently_emptyCache_MsalClientException() throws Throwable {
18+
19+
PublicClientApplication application = PublicClientApplication
20+
.builder(TestConfiguration.AAD_CLIENT_ID)
21+
.b2cAuthority(TestConfiguration.B2C_AUTHORITY).build();
22+
23+
SilentParameters parameters = SilentParameters.builder(Collections.singleton("scope")).build();
24+
25+
try {
26+
application.acquireTokenSilently(parameters).join();
27+
}
28+
catch (CompletionException ex){
29+
throw ex.getCause();
30+
}
31+
}
32+
33+
@Test(expectedExceptions = MsalClientException.class,
34+
expectedExceptionsMessageRegExp = AuthenticationErrorMessage.NO_TOKEN_IN_CACHE)
35+
public void confidentialAppAcquireTokenSilently_emptyCache_MsalClientException() throws Throwable {
36+
37+
ConfidentialClientApplication application = ConfidentialClientApplication
38+
.builder(TestConfiguration.AAD_CLIENT_ID, ClientCredentialFactory.create(TestConfiguration.AAD_CLIENT_SECRET))
39+
.b2cAuthority(TestConfiguration.B2C_AUTHORITY).build();
40+
41+
SilentParameters parameters = SilentParameters.builder(Collections.singleton("scope")).build();
42+
43+
try {
44+
application.acquireTokenSilently(parameters).join();
45+
}
46+
catch (CompletionException ex){
47+
throw ex.getCause();
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)