Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix NPE in SingleAccountPublicClientApplication.getPersistedCurrentAccount #1933

Merged
merged 6 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
MSAL Wiki : https://github.com/AzureAD/microsoft-authentication-library-for-android/wiki
vNext
----------
- [PATCH] Fix NPE in SingleAccountPublicClientApplication.getPersistedCurrentAccount (#1933)
- [PATCH] Updating JSON version (#1932)

Version 4.9.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ public void getCurrentAccountAsync(@NonNull final CurrentAccountCallback callbac

private void getCurrentAccountAsyncInternal(@NonNull final CurrentAccountCallback callback,
@NonNull final String publicApiId) {
final String methodTag = TAG + ":getCurrentAccountAsyncInternal";

TokenMigrationCallback migrationCallback = new TokenMigrationCallback() {
@Override
public void onMigrationFinished(int numberOfAccountsMigrated) {
Expand All @@ -136,9 +138,21 @@ public void onTaskCompleted(final List<ICacheRecord> result) {
// To simplify the logic, if more than one account is returned, the first account will be picked.
// We do not support switching from MULTIPLE to SINGLE.
// See getAccountFromICacheRecordList() for more details.
final MultiTenantAccount oldAccount = getPersistedCurrentAccount();

MultiTenantAccount oldAccount = null;
boolean forceNotify = false;
try {
oldAccount = getPersistedCurrentAccount();
} catch (final NullPointerException e){
// There is an issue where the cached value could be malformed.
// If that happens, wipe the value, and trigger the callback.
Logger.error(methodTag, "Failed to load Persisted Current Account", e);
sharedPreferencesFileManager.remove(CURRENT_ACCOUNT_SHARED_PREFERENCE_KEY);
forceNotify = true;
}

persistCurrentAccount(result);
checkCurrentAccountNotifyCallback(callback, result, oldAccount);
checkCurrentAccountNotifyCallback(callback, result, oldAccount, forceNotify);
}

@Override
Expand Down Expand Up @@ -220,12 +234,13 @@ public void onError(@NonNull final MsalException exception) {

private void checkCurrentAccountNotifyCallback(@NonNull final CurrentAccountCallback callback,
@Nullable final List<ICacheRecord> newAccountRecords,
@Nullable final MultiTenantAccount oldAccount) {
@Nullable final MultiTenantAccount oldAccount,
final boolean forceNotify) {
final MultiTenantAccount newAccount = newAccountRecords == null
? null
: getAccountFromICacheRecordList(newAccountRecords);

if (!isHomeAccountIdMatching(oldAccount, newAccount)) {
if (forceNotify || !isHomeAccountIdMatching(oldAccount, newAccount)) {
callback.onAccountChanged(oldAccount, newAccount);
}

Expand Down
Loading