-
Notifications
You must be signed in to change notification settings - Fork 22
Storing the code verifier using shared preference to make it persistent #390
base: MAS-2.1.00
Are you sure you want to change the base?
Changes from all commits
3539bf8
5e4d89e
cc6e418
43d9ef5
fae84fe
ff87134
e1dac15
1f70116
37ea576
13dd503
b006774
da6df54
8d891b8
828171a
4397ebe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ | |
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.logging.Logger; | ||
|
||
import static com.ca.mas.foundation.MAS.TAG; | ||
import static com.ca.mas.foundation.MAS.getContext; | ||
|
@@ -38,40 +39,59 @@ public class AccountManagerUtil implements StorageActions { | |
public AccountManagerUtil(Context context, String accountName, boolean sharedStorage){ | ||
|
||
// Gets the account type from the manifest | ||
String accountType = getAccountType(context); | ||
final String accountType = getAccountType(context); | ||
final StringBuilder messageBuilder = new StringBuilder(); | ||
|
||
if (accountType == null || accountType.isEmpty()) { | ||
throw new IllegalArgumentException(MASFoundationStrings.SHARED_STORAGE_NULL_ACCOUNT_TYPE); | ||
} | ||
|
||
if (accountName == null) { | ||
throw new IllegalArgumentException(MASFoundationStrings.SHARED_STORAGE_NULL_ACCOUNT_NAME); | ||
} | ||
|
||
shared = sharedStorage; | ||
|
||
try { | ||
SharedStorageIdentifier identifier = new SharedStorageIdentifier(); | ||
|
||
mAccountManager = AccountManager.get(MAS.getContext()); | ||
//Attempt to retrieve the account | ||
Account[] accounts = mAccountManager.getAccountsByType(accountType); | ||
for (Account account : accounts) { | ||
if (accountName.equals(account.name)) { | ||
String password = mAccountManager.getPassword(account); | ||
String savedPassword = identifier.toString(); | ||
if (password.equals(savedPassword)) { | ||
mAccount = account; | ||
}else { | ||
// - case migration from old AccountManagerStoreDataSource | ||
mAccount = null; | ||
identifier = new SharedStorageIdentifier(); | ||
synchronized (mutex) { | ||
SharedStorageIdentifier identifier = new SharedStorageIdentifier(); | ||
|
||
mAccountManager = AccountManager.get(MAS.getContext()); | ||
|
||
//Attempt to retrieve the account | ||
Account[] accounts = mAccountManager.getAccountsByType(accountType); | ||
messageBuilder.append(" existing accounts (" + accountType + ")=" + accounts.length); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I assume use of message builder is an instrumentation purpose for detailing the logs. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These changes are as part of another PR. I will take these and update the other PR. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And yes, this is as part of the logging mechanism and is temporary. We will remove that as soon we get a solution of the other SecurityException issue. |
||
for (Account account : accounts) { | ||
messageBuilder.append(" trying account:" + account.name); | ||
|
||
if (accountName.equals(account.name)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I hope, we don't find duplicates with the same accountName. Hence, break the loop as soon as we encounter the account with the given name. |
||
String password = mAccountManager.getPassword(account); | ||
String savedPassword = identifier.toString(); | ||
if (password != null && password.equals(savedPassword)) { | ||
mAccount = account; | ||
}else { | ||
// - case migration from old AccountManagerStoreDataSource | ||
mAccount = null; | ||
messageBuilder.append(" password is null or password not equals to saved password:"); | ||
identifier = new SharedStorageIdentifier(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
//Create the account if it wasn't retrieved, | ||
if (mAccount == null) { | ||
mAccount = new Account(accountName, accountType); | ||
mAccountManager.addAccountExplicitly(mAccount, identifier.toString(), null); | ||
//Create the account if it wasn't retrieved, | ||
if (mAccount == null) { | ||
messageBuilder.append(" account identifier when mAccount is null:" +identifier); | ||
messageBuilder.append(" attempt to create an account explicitly name=" + accountName + ", accountType=" + accountType); | ||
mAccount = new Account(accountName, accountType); | ||
boolean accountCreated = mAccountManager.addAccountExplicitly(mAccount, identifier.toString(), null); | ||
messageBuilder.append("created account status=" + accountCreated); | ||
} | ||
} | ||
Log.e(TAG, "Retrieved account details name="+mAccount.name+ | ||
" type=" +mAccount.type+" hashcode=" + mAccount.hashCode()); | ||
} catch (Exception e) { | ||
throw new MASSharedStorageException(e.getMessage(), e); | ||
Log.e(TAG, "Failed to retrieve account, " + e.getMessage() + " - " + messageBuilder.toString()); | ||
throw new MASSharedStorageException(e.getMessage() +" - " + messageBuilder.toString(), e); | ||
} | ||
} | ||
|
||
|
@@ -223,4 +243,4 @@ private String proxyKey(String key){ | |
|
||
return retVal; | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
usually, I use synchronized first and then try-catch block.
Rule of thump: Keep the try-catch constructs as close to the code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whats wrong in synchronising entire constructor ?