-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
371 additions
and
25 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
...ntity/src/androidTest/java/com/microsoft/appcenter/identity/storage/TokenStorageTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.microsoft.appcenter.identity.storage; | ||
|
||
import android.content.Context; | ||
import android.support.test.InstrumentationRegistry; | ||
|
||
import com.microsoft.appcenter.utils.UUIDUtils; | ||
import com.microsoft.appcenter.utils.storage.SharedPreferencesManager; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNull; | ||
|
||
public class TokenStorageTest { | ||
|
||
@Test | ||
public void testPreferenceTokenStorage() { | ||
|
||
/* Mock token. */ | ||
Context context = InstrumentationRegistry.getTargetContext(); | ||
SharedPreferencesManager.initialize(context); | ||
AuthTokenStorage tokenStorage = TokenStorageFactory.getTokenStorage(context); | ||
String mockToken = UUIDUtils.randomUUID().toString(); | ||
String mockAccountId = UUIDUtils.randomUUID().toString(); | ||
|
||
/* Save the token into storage. */ | ||
tokenStorage.saveToken(mockToken, mockAccountId); | ||
|
||
/* Assert that storage returns the same token.*/ | ||
assertEquals(mockToken, tokenStorage.getToken()); | ||
|
||
/* Remove the token from storage. */ | ||
tokenStorage.removeToken(); | ||
|
||
/* Assert that there's no token in storage. */ | ||
assertNull(tokenStorage.getToken()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
...ter-identity/src/main/java/com/microsoft/appcenter/identity/storage/AuthTokenStorage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.microsoft.appcenter.identity.storage; | ||
|
||
/** | ||
* Interface for storage that works with token. | ||
*/ | ||
public interface AuthTokenStorage { | ||
|
||
/** | ||
* Stores token value along with the corresponding account id. | ||
* | ||
* @param token auth token. | ||
* @param homeAccountId unique identifier of user. | ||
*/ | ||
void saveToken(String token, String homeAccountId); | ||
|
||
/** | ||
* Retrieves token value. | ||
* | ||
* @return auth token. | ||
*/ | ||
String getToken(); | ||
|
||
/** | ||
* Removes token value. | ||
*/ | ||
void removeToken(); | ||
|
||
/** | ||
* Gets token and the last account id from storage and caches it. | ||
*/ | ||
void cacheToken(); | ||
} |
84 changes: 84 additions & 0 deletions
84
...entity/src/main/java/com/microsoft/appcenter/identity/storage/PreferenceTokenStorage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package com.microsoft.appcenter.identity.storage; | ||
|
||
import android.content.Context; | ||
import android.support.annotation.NonNull; | ||
import android.support.annotation.VisibleForTesting; | ||
|
||
import com.microsoft.appcenter.utils.context.AuthTokenContext; | ||
import com.microsoft.appcenter.utils.crypto.CryptoUtils; | ||
import com.microsoft.appcenter.utils.storage.SharedPreferencesManager; | ||
|
||
/** | ||
* Storage for tokens that uses {@link SharedPreferencesManager}. Handles saving and encryption. | ||
*/ | ||
public class PreferenceTokenStorage implements AuthTokenStorage { | ||
|
||
/** | ||
* {@link Context} instance. | ||
*/ | ||
private final Context mContext; | ||
|
||
/** | ||
* Default constructor. | ||
* | ||
* @param context {@link Context} instance. | ||
*/ | ||
PreferenceTokenStorage(@NonNull Context context) { | ||
mContext = context; | ||
} | ||
|
||
/** | ||
* Used for authentication requests, string field for auth token. | ||
*/ | ||
@VisibleForTesting | ||
static final String PREFERENCE_KEY_AUTH_TOKEN = "AppCenter.auth_token"; | ||
|
||
/** | ||
* Used for distinguishing users, string field for home account id. | ||
*/ | ||
@VisibleForTesting | ||
static final String PREFERENCE_KEY_HOME_ACCOUNT_ID = "AppCenter.account_id"; | ||
|
||
@Override | ||
public void saveToken(String token, String homeAccountId) { | ||
AuthTokenContext.getInstance().setAuthToken(token, homeAccountId); | ||
String encryptedToken = CryptoUtils.getInstance(mContext).encrypt(token); | ||
SharedPreferencesManager.putString(PREFERENCE_KEY_AUTH_TOKEN, encryptedToken); | ||
SharedPreferencesManager.putString(PREFERENCE_KEY_HOME_ACCOUNT_ID, homeAccountId); | ||
} | ||
|
||
@Override | ||
public String getToken() { | ||
String encryptedToken = SharedPreferencesManager.getString(PREFERENCE_KEY_AUTH_TOKEN, null); | ||
if (encryptedToken == null || encryptedToken.length() == 0) { | ||
return null; | ||
} | ||
CryptoUtils.DecryptedData decryptedData = CryptoUtils.getInstance(mContext).decrypt(encryptedToken, false); | ||
return decryptedData.getDecryptedData(); | ||
} | ||
|
||
/** | ||
* Retrieves unique user id. | ||
* | ||
* @return unique user id. | ||
*/ | ||
private String getHomeAccountId() { | ||
return SharedPreferencesManager.getString(PREFERENCE_KEY_HOME_ACCOUNT_ID, null); | ||
} | ||
|
||
@Override | ||
public void cacheToken() { | ||
String tokenFromStorage = getToken(); | ||
String accountId = getHomeAccountId(); | ||
|
||
/* We need to update Token context here. */ | ||
AuthTokenContext.getInstance().setAuthToken(tokenFromStorage, accountId); | ||
} | ||
|
||
@Override | ||
public void removeToken() { | ||
SharedPreferencesManager.remove(PREFERENCE_KEY_AUTH_TOKEN); | ||
SharedPreferencesManager.remove(PREFERENCE_KEY_HOME_ACCOUNT_ID); | ||
AuthTokenContext.getInstance().clearToken(); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...-identity/src/main/java/com/microsoft/appcenter/identity/storage/TokenStorageFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.microsoft.appcenter.identity.storage; | ||
|
||
import android.content.Context; | ||
|
||
/** | ||
* Factory class to produce instance of {@link AuthTokenStorage}. | ||
*/ | ||
public class TokenStorageFactory { | ||
|
||
/** | ||
* Instance of {@link AuthTokenStorage}. | ||
*/ | ||
private static AuthTokenStorage sTokenStorageInstance; | ||
|
||
/** | ||
* Retrieves current implementation of {@link AuthTokenStorage}. | ||
* | ||
* @param context application context. | ||
* @return instance of {@link AuthTokenStorage}. | ||
*/ | ||
public static AuthTokenStorage getTokenStorage(Context context) { | ||
if (sTokenStorageInstance == null) { | ||
sTokenStorageInstance = new PreferenceTokenStorage(context); | ||
} | ||
return sTokenStorageInstance; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.