Skip to content

Commit

Permalink
Merge pull request #699 from AzureAD/servicing
Browse files Browse the repository at this point in the history
manual merge PR 696 from dev to service branch
  • Loading branch information
weijjia authored Aug 11, 2016
2 parents cce7f74 + e22f348 commit f48cf4d
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 4 deletions.
6 changes: 6 additions & 0 deletions src/src/com/microsoft/aad/adal/AcquireTokenSilentHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,12 @@ private AuthenticationResult useMRRT() throws AuthenticationException {
*/
private AuthenticationResult acquireTokenWithCachedItem(final TokenCacheItem cachedItem)
throws AuthenticationException {
if (StringExtensions.IsNullOrBlank(cachedItem.getRefreshToken())) {
Logger.v(TAG, "Token cache item contains empty refresh token, cannot continue refresh "
+ "token request", mAuthRequest.getLogInfo(), null);
return null;
}

final AuthenticationResult result = acquireTokenWithRefreshToken(cachedItem.getRefreshToken());

if (result != null) {
Expand Down
7 changes: 4 additions & 3 deletions src/src/com/microsoft/aad/adal/Oauth2.java
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,9 @@ else if (response.containsKey(AuthenticationConstants.OAuth2.ACCESS_TOKEN)) {
expires_in == null || expires_in.isEmpty() ? AuthenticationConstants.DEFAULT_EXPIRATION_TIME_SEC
: Integer.parseInt(expires_in));

if (response.containsKey(AuthenticationConstants.AAD.RESOURCE)) {
final String refreshToken = response.get(AuthenticationConstants.OAuth2.REFRESH_TOKEN);
if (response.containsKey(AuthenticationConstants.AAD.RESOURCE)
&& !StringExtensions.IsNullOrBlank(refreshToken)) {
isMultiResourcetoken = true;
}

Expand All @@ -265,8 +267,7 @@ else if (response.containsKey(AuthenticationConstants.OAuth2.ACCESS_TOKEN)) {
}

result = new AuthenticationResult(
response.get(AuthenticationConstants.OAuth2.ACCESS_TOKEN),
response.get(AuthenticationConstants.OAuth2.REFRESH_TOKEN), expires.getTime(),
response.get(AuthenticationConstants.OAuth2.ACCESS_TOKEN), refreshToken, expires.getTime(),
isMultiResourcetoken, userinfo, tenantId, rawIdToken);

//Set family client id on authentication result for TokenCacheItem to pick up
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,106 @@ public void testRefreshTokenWithInteractionRequired_CacheNotCleared() throws IOE
clearCache(mockCache);
}

@SmallTest
public void testMRRTItemNotContainRT() {
FileMockContext mockContext = new FileMockContext(getContext());
final ITokenCacheStore mockedCache = new DefaultTokenCacheStore(getContext());
final String resource = "resource";
final String clientId = "clientId";

// Add MRRT in the cache
final TokenCacheItem mrrtTokenCacheItem = Util.getTokenCacheItem(VALID_AUTHORITY, resource,
clientId, TEST_IDTOKEN_USERID, TEST_IDTOKEN_UPN);
mrrtTokenCacheItem.setRefreshToken(null);
mrrtTokenCacheItem.setResource(null);
mrrtTokenCacheItem.setFamilyClientId("familyClientId");
mrrtTokenCacheItem.setIsMultiResourceRefreshToken(true);
saveTokenIntoCache(mockedCache, mrrtTokenCacheItem);

final AuthenticationRequest authenticationRequest = getAuthenticationRequest(
VALID_AUTHORITY, resource, clientId);
authenticationRequest.setUserIdentifierType(UserIdentifierType.UniqueId);
authenticationRequest.setUserId(TEST_IDTOKEN_USERID);
final AcquireTokenSilentHandler acquireTokenSilentHandler = getAcquireTokenHandler(mockContext,
authenticationRequest, mockedCache);

try {
final AuthenticationResult authenticationResult = acquireTokenSilentHandler.getAccessToken();
assertNull(authenticationResult);
} catch (AuthenticationException authException) {
fail("Unexpected Exception");
}

// verify MRRT entry exist
assertNotNull(mockedCache.getItem(CacheKey.createCacheKeyForMRRT(VALID_AUTHORITY, clientId, TEST_IDTOKEN_USERID)));
assertNotNull(mockedCache.getItem(CacheKey.createCacheKeyForMRRT(VALID_AUTHORITY, clientId, TEST_IDTOKEN_UPN)));

clearCache(mockedCache);
}

@SmallTest
public void testAllTokenItemNotContainRT() {
FileMockContext mockContext = new FileMockContext(getContext());
final ITokenCacheStore mockedCache = new DefaultTokenCacheStore(getContext());
final String resource = "resource";
final String clientId = "clientId";

// Add regular RT item without RT in the cache
final TokenCacheItem rtTokenCacheItem = Util.getTokenCacheItem(VALID_AUTHORITY, resource, clientId,
TEST_IDTOKEN_USERID, TEST_IDTOKEN_UPN);
rtTokenCacheItem.setRefreshToken(null);
rtTokenCacheItem.setIsMultiResourceRefreshToken(true);
saveTokenIntoCache(mockedCache, rtTokenCacheItem);

// Add MRRT in the cache
final TokenCacheItem mrrtTokenCacheItem = Util.getTokenCacheItem(VALID_AUTHORITY, resource,
clientId, TEST_IDTOKEN_USERID, TEST_IDTOKEN_UPN);
mrrtTokenCacheItem.setRefreshToken(null);
mrrtTokenCacheItem.setResource(null);
mrrtTokenCacheItem.setFamilyClientId("familyId");
mrrtTokenCacheItem.setIsMultiResourceRefreshToken(true);
saveTokenIntoCache(mockedCache, mrrtTokenCacheItem);

// Add FRT item into cache without rt
final TokenCacheItem frtTokenCacheItem = Util.getTokenCacheItem(VALID_AUTHORITY, resource, clientId,
TEST_IDTOKEN_USERID, TEST_IDTOKEN_UPN);
frtTokenCacheItem.setClientId(null);
frtTokenCacheItem.setRefreshToken(null);
frtTokenCacheItem.setResource(null);
frtTokenCacheItem.setIsMultiResourceRefreshToken(true);
frtTokenCacheItem.setFamilyClientId("familyId");
saveTokenIntoCache(mockedCache, frtTokenCacheItem);

final AuthenticationRequest authenticationRequest = getAuthenticationRequest(
VALID_AUTHORITY, resource, clientId);
authenticationRequest.setUserIdentifierType(UserIdentifierType.UniqueId);
authenticationRequest.setUserId(TEST_IDTOKEN_USERID);
final AcquireTokenSilentHandler acquireTokenSilentHandler = getAcquireTokenHandler(mockContext,
authenticationRequest, mockedCache);

try {
final AuthenticationResult authenticationResult = acquireTokenSilentHandler.getAccessToken();
assertNull(authenticationResult);
} catch (AuthenticationException authException) {
fail("Unexpected Exception");
}

// verify RT entry exist
assertNotNull(mockedCache.getItem(CacheKey.createCacheKeyForRTEntry(VALID_AUTHORITY, resource, clientId,
TEST_IDTOKEN_USERID)));
assertNotNull(mockedCache.getItem(CacheKey.createCacheKeyForRTEntry(VALID_AUTHORITY, resource, clientId,
TEST_IDTOKEN_UPN)));

// verify MRRT entry exist
assertNotNull(mockedCache.getItem(CacheKey.createCacheKeyForMRRT(VALID_AUTHORITY, clientId, TEST_IDTOKEN_USERID)));
assertNotNull(mockedCache.getItem(CacheKey.createCacheKeyForMRRT(VALID_AUTHORITY, clientId, TEST_IDTOKEN_UPN)));

// verify FRT entry exist
assertNotNull(mockedCache.getItem(CacheKey.createCacheKeyForFRT(VALID_AUTHORITY, "familyId", TEST_IDTOKEN_USERID)));
assertNotNull(mockedCache.getItem(CacheKey.createCacheKeyForFRT(VALID_AUTHORITY, "familyId", TEST_IDTOKEN_UPN)));
clearCache(mockedCache);
}

private void saveTokenIntoCache(final ITokenCacheStore mockedCache, final TokenCacheItem token) {
if (!StringExtensions.IsNullOrBlank(token.getResource())) {
mockedCache.setItem(CacheKey.createCacheKeyForRTEntry(VALID_AUTHORITY, token.getResource(), token.getClientId(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1327,6 +1327,35 @@ public void testAcquireTokenSilentSync_Negative() throws NoSuchAlgorithmExceptio
clearCache(context);
}

@SmallTest
public void testSilentRequestTokenItemNotContainRT() throws InterruptedException {
final FileMockContext mockContext = new FileMockContext(getContext());
final ITokenCacheStore mockedCache = new DefaultTokenCacheStore(getContext());
final String resource = "resource";
final String clientId = "clientId";

// Add MRRT in the cache
final TokenCacheItem mrrtTokenCacheItem = Util.getTokenCacheItem(VALID_AUTHORITY, resource,
clientId, TEST_IDTOKEN_USERID, TEST_IDTOKEN_UPN);
mrrtTokenCacheItem.setRefreshToken(null);
mrrtTokenCacheItem.setResource(null);
mrrtTokenCacheItem.setFamilyClientId("familyClientId");
mrrtTokenCacheItem.setIsMultiResourceRefreshToken(true);
mockedCache.setItem(CacheKey.createCacheKeyForMRRT(VALID_AUTHORITY, clientId, TEST_IDTOKEN_USERID), mrrtTokenCacheItem);
mockedCache.setItem(CacheKey.createCacheKeyForMRRT(VALID_AUTHORITY, clientId, TEST_IDTOKEN_UPN), mrrtTokenCacheItem);

final AuthenticationContext context = getAuthenticationContext(mockContext,
VALID_AUTHORITY, false, mockedCache);
try {
context.acquireTokenSilentSync(resource, clientId, TEST_IDTOKEN_USERID);
fail("Expecting exception to be thrown");
} catch (final AuthenticationException e) {
assertTrue(e.getCode() == ADALError.AUTH_REFRESH_FAILED_PROMPT_NOT_ALLOWED);
} finally {
clearCache(context);
}
}

private void verifyRefreshTokenResponse(ITokenCacheStore mockCache, Exception resultException,
AuthenticationResult result) {
assertNull("Error is null", resultException);
Expand Down
10 changes: 9 additions & 1 deletion tests/Functional/src/com/microsoft/aad/adal/OauthTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -636,11 +636,19 @@ public void testprocessUIResponseParams() throws IllegalArgumentException,
assertEquals("Token is same", "token", result.getAccessToken());
assertFalse("MultiResource token", result.getIsMultiResourceRefreshToken());

// multi resource token
// resource returned in JSON response, but RT is not returned.
response.put(AuthenticationConstants.AAD.RESOURCE, "resource");
result = (AuthenticationResult)m.invoke(null, response);
assertEquals("Success status", AuthenticationStatus.Succeeded, result.getStatus());
assertEquals("Token is same", "token", result.getAccessToken());
assertFalse("MultiResource token", result.getIsMultiResourceRefreshToken());

// resource returned in JSON response and RT is also returned.
response.put(AuthenticationConstants.OAuth2.REFRESH_TOKEN, "refresh_token");
result = (AuthenticationResult)m.invoke(null, response);
assertEquals("Success status", AuthenticationStatus.Succeeded, result.getStatus());
assertEquals("Token is same", "token", result.getAccessToken());
assertEquals("RT is the same", "refresh_token", result.getRefreshToken());
assertTrue("MultiResource token", result.getIsMultiResourceRefreshToken());
}

Expand Down

0 comments on commit f48cf4d

Please sign in to comment.