Skip to content

Commit

Permalink
address comments for token cache storage
Browse files Browse the repository at this point in the history
  • Loading branch information
weijjia committed Sep 20, 2016
1 parent dd3dcfa commit 3ae4763
Show file tree
Hide file tree
Showing 28 changed files with 670 additions and 680 deletions.
1 change: 1 addition & 0 deletions config/checkstyle/suppressions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
<suppressions>
<suppress checks="Javadoc.*" files=".*Test.java"/>
<suppress checks="VisibilityModifierCheck" files="BaseRequest.java" />
<suppress checks="VisibilityModifierCheck" files="BaseTokenCacheItem.java" />
</suppressions>
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@

package com.microsoft.identity.client;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Base64;

import java.io.ByteArrayInputStream;
Expand Down Expand Up @@ -109,7 +112,7 @@ static String getSuccessResponseWithNoAccessToken() {
return tokenResponse;
}

static final String getSuccessResponse(final String idToken, final String scopes) {
static String getSuccessResponse(final String idToken, final String scopes) {
final String tokenResponse = "{\"id_token\":\""
+ idToken
+ "\",\"access_token\":\"" + ACCESS_TOKEN + "\", \"token_type\":\"Bearer\",\"refresh_token\":\"" + REFRESH_TOKEN + "\","
Expand Down Expand Up @@ -152,4 +155,24 @@ static Date getExpirationDate(int tokenExpiredDateInMinuite) {

return expiredTime.getTime();
}

static void removeAllTokens(final Context appContext) {
final SharedPreferences accessTokenSharedPreference = appContext.getSharedPreferences("com.microsoft.identity.client.token",
Activity.MODE_PRIVATE);
final SharedPreferences.Editor accessTokenSharedPreferenceEditor = accessTokenSharedPreference.edit();
accessTokenSharedPreferenceEditor.clear();
accessTokenSharedPreferenceEditor.apply();

final SharedPreferences refreshTokenSharedPreference = appContext.getSharedPreferences("com.microsoft.identity.client.refreshToken",
Activity.MODE_PRIVATE);
final SharedPreferences.Editor refreshTokenSharedPreferenceEditor = refreshTokenSharedPreference.edit();
refreshTokenSharedPreferenceEditor.clear();
refreshTokenSharedPreferenceEditor.apply();
}

static String getRawIdToken(final String displaybleId, final String uniqueId, final String homeOID)
throws UnsupportedEncodingException {
return AndroidTestUtil.createIdToken(AUDIENCE, ISSUER, NAME, uniqueId, displaybleId, SUBJECT, TENANT_ID,
VERSION, homeOID);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void testTokenResponseContainEmptyScope() throws AuthenticationException
final TokenResponse tokenResponse = new TokenResponse(ACCESS_TOKEN, "", REFRESH_TOKEN, EXPIRES_ON, EXPIRES_ON,
EXPIRES_ON, "", TOKEN_TYPE, null);

final AuthenticationResult authenticationResult = new AuthenticationResult(tokenResponse);
final AuthenticationResult authenticationResult = AuthenticationResult.create(new SuccessTokenResponse(tokenResponse));
verifyScopeIsEmpty(authenticationResult);
}

Expand All @@ -73,7 +73,7 @@ public void testTokenResponseContainNullScope() throws AuthenticationException {
final TokenResponse tokenResponse = new TokenResponse(ACCESS_TOKEN, "", REFRESH_TOKEN, EXPIRES_ON, EXPIRES_ON,
EXPIRES_ON, null, TOKEN_TYPE, null);

final AuthenticationResult authenticationResult = new AuthenticationResult(tokenResponse);
final AuthenticationResult authenticationResult = AuthenticationResult.create(new SuccessTokenResponse(tokenResponse));
verifyScopeIsEmpty(authenticationResult);
}

Expand All @@ -87,7 +87,7 @@ public void testTokenResponseContainsScopeWithTrailingSpace() throws Authenticat
final TokenResponse tokenResponse = new TokenResponse(ACCESS_TOKEN, "", REFRESH_TOKEN, EXPIRES_ON, EXPIRES_ON,
EXPIRES_ON, scopes, TOKEN_TYPE, null);

final AuthenticationResult authenticationResult = new AuthenticationResult(tokenResponse);
final AuthenticationResult authenticationResult = AuthenticationResult.create(new SuccessTokenResponse(tokenResponse));
final String[] scopeArray = authenticationResult.getScope();
Assert.assertNotNull(scopeArray);
Assert.assertTrue(scopeArray.length == SCOPE_LENGTH);
Expand All @@ -107,7 +107,7 @@ public void testBothATAndIdTokenReturned() throws AuthenticationException {

final TokenResponse tokenResponse = new TokenResponse(ACCESS_TOKEN, AndroidTestUtil.TEST_IDTOKEN,
REFRESH_TOKEN, expiresOn, idTokenExpiresOn, EXPIRES_ON, SCOPE, TOKEN_TYPE, null);
final AuthenticationResult authenticationResult = new AuthenticationResult(tokenResponse);
final AuthenticationResult authenticationResult = AuthenticationResult.create(new SuccessTokenResponse(tokenResponse));
Assert.assertTrue(authenticationResult.getToken().equals(ACCESS_TOKEN));
Assert.assertTrue(authenticationResult.getExpiresOn().equals(expiresOn));
}
Expand All @@ -122,7 +122,7 @@ public void testOnlyIdTokenReturned() throws AuthenticationException {
final Date idTokenExpiresOn = getExpiresOn(-TIME_OFFSET);
final TokenResponse tokenResponse = new TokenResponse(null, AndroidTestUtil.TEST_IDTOKEN,
REFRESH_TOKEN, expiresOn, idTokenExpiresOn, EXPIRES_ON, SCOPE, TOKEN_TYPE, null);
final AuthenticationResult authenticationResult = new AuthenticationResult(tokenResponse);
final AuthenticationResult authenticationResult = AuthenticationResult.create(new SuccessTokenResponse(tokenResponse));

Assert.assertTrue(authenticationResult.getToken().equals(AndroidTestUtil.TEST_IDTOKEN));
Assert.assertTrue(authenticationResult.getExpiresOn().equals(idTokenExpiresOn));
Expand All @@ -133,7 +133,7 @@ public void testHomeOidNotReturned() throws UnsupportedEncodingException, Authen
final String uniqueId = "unique";
final TokenResponse tokenResponse = new TokenResponse(null, PublicClientApplicationTest.getIdToken("displayable",
uniqueId, ""), REFRESH_TOKEN, new Date(), new Date(), new Date(), SCOPE, TOKEN_TYPE, null);
final AuthenticationResult result = new AuthenticationResult(tokenResponse);
final AuthenticationResult result = AuthenticationResult.create(new SuccessTokenResponse(tokenResponse));

final User user = result.getUser();
Assert.assertNotNull(user);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public void setUp() throws Exception {
public void tearDown() throws Exception {
super.tearDown();
HttpUrlConnectionFactory.clearMockedConnectionQueue();
mTokenCache.removeAll();
AndroidTestUtil.removeAllTokens(mAppContext);
}

@Test(expected = IllegalArgumentException.class)
Expand Down Expand Up @@ -661,15 +661,15 @@ String getFinalUrl() throws UnsupportedEncodingException {

private AuthenticationRequestParameters getAuthenticationParams(final String policy, final UIOptions uiOptions) {
return AuthenticationRequestParameters.create(new Authority(AUTHORITY, false), new TokenCache(mAppContext), getScopes(),
CLIENT_ID, mRedirectUri, policy, true, LOGIN_HINT, "", uiOptions, CORRELATION_ID, new Settings());
CLIENT_ID, mRedirectUri, policy, true, LOGIN_HINT, "", uiOptions, CORRELATION_ID);
}

private AuthenticationRequestParameters getAuthRequestParameters(final Set<String> scopes,
final String redirectUri,
final String loginHint,
final UIOptions uiOptions) {
return AuthenticationRequestParameters.create(new Authority(AUTHORITY, false), mTokenCache, scopes,
CLIENT_ID, redirectUri, POLICY, true, loginHint, "", uiOptions, CORRELATION_ID, new Settings());
CLIENT_ID, redirectUri, POLICY, true, loginHint, "", uiOptions, CORRELATION_ID);
}

private Set<String> getScopes() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public final class PublicClientApplicationTest extends AndroidTestCase {
private static final String DEFAULT_AUTHORITY = "https://login.microsoftonline.com/common";
private static final String ALTERNATE_AUTHORITY = "https://login.microsoftonline.com/alternateAuthority";
private static final String[] SCOPE = {"scope1", "scope2"};
private static final int EXPECTED_USER_SIZE = 3;

@Before
public void setUp() throws Exception {
Expand All @@ -65,7 +66,7 @@ public void setUp() throws Exception {
public void tearDown() throws Exception {
super.tearDown();
HttpUrlConnectionFactory.clearMockedConnectionQueue();
mTokenCache.removeAll();
AndroidTestUtil.removeAllTokens(mAppContext);
}

/**
Expand Down Expand Up @@ -186,12 +187,12 @@ public void testGetUsers() throws UnsupportedEncodingException, AuthenticationEx
final String homeOid1 = "HomeOid1";
String idToken = getIdToken(displayable1, uniqueId1, homeOid1);

mTokenCache.saveTokenResponse(TokenLookupEngineTest.AUTHORITY, CLIENT_ID, "", getTokenResponse(idToken));
mTokenCache.saveTokenResponse(TokenCacheTest.AUTHORITY, CLIENT_ID, "", getTokenResponse(idToken));

// prepare token cache for same client id, same displayable, uniqueId but different oid
final String homeOid2 = "HomeOid2";
idToken = getIdToken(displayable1, uniqueId1, homeOid2);
mTokenCache.saveTokenResponse(TokenLookupEngineTest.AUTHORITY, CLIENT_ID, "", getTokenResponse(idToken));
mTokenCache.saveTokenResponse(TokenCacheTest.AUTHORITY, CLIENT_ID, "", getTokenResponse(idToken));

List<User> users = application.getUsers();
assertTrue(users.size() == 2);
Expand All @@ -201,10 +202,10 @@ public void testGetUsers() throws UnsupportedEncodingException, AuthenticationEx
final String uniqueId3 = "UniqueId3";
final String homeOid3 = "HomeOid3";
idToken = getIdToken(displayable3, uniqueId3, homeOid3);
mTokenCache.saveTokenResponse(TokenLookupEngineTest.AUTHORITY, CLIENT_ID, "", getTokenResponse(idToken));
mTokenCache.saveTokenResponse(TokenCacheTest.AUTHORITY, CLIENT_ID, "", getTokenResponse(idToken));

users = application.getUsers();
assertTrue(users.size()== 3);
assertTrue(users.size() == EXPECTED_USER_SIZE);
final User userForDisplayable3 = application.getUser(displayable3);
assertNotNull(userForDisplayable3);
assertNotNull(userForDisplayable3.getTokenCache());
Expand All @@ -215,9 +216,9 @@ public void testGetUsers() throws UnsupportedEncodingException, AuthenticationEx

// prepare token cache for different client id, same displayable3 user
final String anotherClientId = "anotherClientId";
mTokenCache.saveTokenResponse(TokenLookupEngineTest.AUTHORITY, anotherClientId, "", getTokenResponse(idToken));
mTokenCache.saveTokenResponse(TokenCacheTest.AUTHORITY, anotherClientId, "", getTokenResponse(idToken));
final PublicClientApplication anotherApplication = new PublicClientApplication(getMockedActivity(anotherClientId));
assertTrue(application.getUsers().size() == 3);
assertTrue(application.getUsers().size() == EXPECTED_USER_SIZE);
users = anotherApplication.getUsers();
assertTrue(users.size() == 1);
final User userForAnotherClient = anotherApplication.getUser(uniqueId3);
Expand Down Expand Up @@ -504,13 +505,11 @@ public void testSilentRequestFailure() throws UnsupportedEncodingException, Auth
final PublicClientApplication application = new PublicClientApplication(activity);

// prepare token in the cache
mTokenCache.saveTokenResponse(AndroidTestUtil.DEFAULT_AUTHORITY, CLIENT_ID, "", TokenLookupEngineTest.getTokenResponseForDefaultUser(
mTokenCache.saveTokenResponse(AndroidTestUtil.DEFAULT_AUTHORITY, CLIENT_ID, "", TokenCacheTest.getTokenResponseForDefaultUser(
AndroidTestUtil.ACCESS_TOKEN, AndroidTestUtil.REFRESH_TOKEN, "scope1 scope2", AndroidTestUtil.getExpiredDate()));

final User user = new User();
user.setDisplayableId("another Displayable");
user.setUniqueId("another uniqueId");
user.setHomeObjectId("another homeobj");
final IdToken idToken = new IdToken(AndroidTestUtil.getRawIdToken("another Displayable", "another uniqueId", "another homeobj"));
final User user = new User(idToken);

final CountDownLatch silentLock = new CountDownLatch(1);
application.acquireTokenSilentAsync(new String[]{"scope1", "scope2"}, user, new AuthenticationCallback() {
Expand Down Expand Up @@ -539,9 +538,10 @@ static String getIdToken(final String displayable, final String uniqueId, final
AndroidTestUtil.SUBJECT, AndroidTestUtil.TENANT_ID, AndroidTestUtil.VERSION, homeOid);
}

private TokenResponse getTokenResponse(final String idToken) {
return new TokenResponse(AndroidTestUtil.ACCESS_TOKEN, idToken, AndroidTestUtil.REFRESH_TOKEN, new Date(), new Date(),
private SuccessTokenResponse getTokenResponse(final String idToken) throws AuthenticationException {
final TokenResponse response = new TokenResponse(AndroidTestUtil.ACCESS_TOKEN, idToken, AndroidTestUtil.REFRESH_TOKEN, new Date(), new Date(),
new Date(), "scope", "Bearer", null);
return new SuccessTokenResponse(response);
}

private void mockPackageManagerWithClientId(final Context context,
Expand Down Expand Up @@ -600,7 +600,7 @@ private String convertScopesArrayToString(final String[] scopes) {
return MSALUtils.convertSetToString(scopesInSet, " ");
}

private Activity getMockedActivity(final String clientId) throws PackageManager.NameNotFoundException{
private Activity getMockedActivity(final String clientId) throws PackageManager.NameNotFoundException {
final Context context = new MockActivityContext(mAppContext);
mockPackageManagerWithClientId(context, false, clientId);
mockHasCustomTabRedirect(context);
Expand Down
Loading

0 comments on commit 3ae4763

Please sign in to comment.