Skip to content

Commit

Permalink
refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lbalmaceda committed Jan 24, 2019
1 parent bb59b13 commit c6001da
Show file tree
Hide file tree
Showing 6 changed files with 427 additions and 241 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,14 @@ public class CredentialsManagerException extends Auth0Exception {
CredentialsManagerException(String message) {
super(message);
}

/**
* Returns true when this Android device doesn't support the cryptographic algorithms used
* to handle encryption and decryption, false otherwise.
*
* @return whether this device is compatible with {@link SecureCredentialsManager} or not.
*/
public boolean isDeviceIncompatible() {
return (getCause() instanceof IncompatibleDeviceException);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,10 @@ private void deleteKeys() {
* @param encryptedInput the input bytes to decrypt
* @return the decrypted bytes output
* @throws IncompatibleDeviceException in the event the device can't understand the cryptographic settings required
* @throws CryptoException if the stored RSA keys can't be recovered and should be deemed invalid
*/
@VisibleForTesting
byte[] RSADecrypt(byte[] encryptedInput) throws IncompatibleDeviceException {
byte[] RSADecrypt(byte[] encryptedInput) throws IncompatibleDeviceException, CryptoException {
try {
PrivateKey privateKey = getRSAKeyEntry().getPrivateKey();
Cipher cipher = Cipher.getInstance(RSA_TRANSFORMATION);
Expand Down Expand Up @@ -270,9 +271,10 @@ byte[] RSADecrypt(byte[] encryptedInput) throws IncompatibleDeviceException {
* @param decryptedInput the input bytes to encrypt
* @return the encrypted bytes output
* @throws IncompatibleDeviceException in the event the device can't understand the cryptographic settings required
* @throws CryptoException if the stored RSA keys can't be recovered and should be deemed invalid
*/
@VisibleForTesting
byte[] RSAEncrypt(byte[] decryptedInput) throws IncompatibleDeviceException {
byte[] RSAEncrypt(byte[] decryptedInput) throws IncompatibleDeviceException, CryptoException {
try {
Certificate certificate = getRSAKeyEntry().getCertificate();
Cipher cipher = Cipher.getInstance(RSA_TRANSFORMATION);
Expand Down Expand Up @@ -306,9 +308,10 @@ byte[] RSAEncrypt(byte[] decryptedInput) throws IncompatibleDeviceException {
*
* @return a valid AES Key bytes
* @throws IncompatibleDeviceException in the event the device can't understand the cryptographic settings required
* @throws CryptoException if the stored RSA keys can't be recovered and should be deemed invalid
*/
@VisibleForTesting
byte[] getAESKey() throws IncompatibleDeviceException {
byte[] getAESKey() throws IncompatibleDeviceException, CryptoException {
final String encodedEncryptedAES = storage.retrieveString(KEY_ALIAS);
if (encodedEncryptedAES != null) {
//Return existing key
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Exception thrown by the {@link CryptoUtil} class whenever the Keys are deemed invalid
* and so the content encrypted with them unrecoverable.
*/
public class IncompatibleDeviceException extends RuntimeException {
class IncompatibleDeviceException extends CryptoException {
IncompatibleDeviceException(Throwable cause) {
super(String.format("The device is not compatible with the %s class.", CryptoUtil.class.getSimpleName()), cause);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,16 @@ public void saveCredentials(@NonNull Credentials credentials) throws Credentials
storage.store(KEY_CREDENTIALS, encryptedEncoded);
storage.store(KEY_EXPIRES_AT, expiresAt);
storage.store(KEY_CAN_REFRESH, canRefresh);
} catch (IncompatibleDeviceException e) {
throw new CredentialsManagerException(String.format("This device is not compatible with the %s class.", SecureCredentialsManager.class.getSimpleName()), e);
} catch (CryptoException e) {
/*
* If the keys were invalidated in the call above a good new pair is going to be available
* to use on the next call. Retrying this operation will succeed.
* to use on the next call. We clear any existing credentials so #hasValidCredentials returns
* a true value. Retrying this operation will succeed.
*/
clearCredentials();
throw new CredentialsManagerException("A change on the Lock Screen security settings have deemed the encryption keys invalid and have been recreated. Please, try saving the credentials again.", e);
} catch (IncompatibleDeviceException e) {
throw new CredentialsManagerException(String.format("This device is not compatible with the %s class.", SecureCredentialsManager.class.getSimpleName()), e);
}
}

Expand Down Expand Up @@ -213,17 +215,17 @@ private void continueGetCredentials(final BaseCallback<Credentials, CredentialsM
String json;
try {
json = new String(crypto.decrypt(encrypted));
} catch (IncompatibleDeviceException e) {
callback.onFailure(new CredentialsManagerException(String.format("This device is not compatible with the %s class.", SecureCredentialsManager.class.getSimpleName()), e));
decryptCallback = null;
return;
} catch (CryptoException e) {
//If keys were invalidated, existing credentials will not be recoverable.
clearCredentials();
callback.onFailure(new CredentialsManagerException("A change on the Lock Screen security settings have deemed the encryption keys invalid and have been recreated. " +
"Any previously stored content is now lost. Please, try saving the credentials again.", e));
decryptCallback = null;
return;
} catch (IncompatibleDeviceException e) {
callback.onFailure(new CredentialsManagerException(String.format("This device is not compatible with the %s class.", SecureCredentialsManager.class.getSimpleName()), e));
decryptCallback = null;
return;
}
final Credentials credentials = gson.fromJson(json, Credentials.class);
if (isEmpty(credentials.getAccessToken()) && isEmpty(credentials.getIdToken()) || credentials.getExpiresAt() == null) {
Expand Down
Loading

0 comments on commit c6001da

Please sign in to comment.