Skip to content
This repository was archived by the owner on Oct 2, 2024. It is now read-only.

Commit cdacec9

Browse files
author
Peter Nied
committed
Merge pull request #62 from peternied/disambig
Save account session state in DisambiugationAuthenticator and fix ADAL Silent Auth
2 parents eb015c9 + 4f9392e commit cdacec9

File tree

3 files changed

+100
-17
lines changed

3 files changed

+100
-17
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@ mavenGroupId = com.onedrive.sdk
2323
mavenArtifactId = onedrive-sdk-android
2424
mavenMajorVersion = 1
2525
mavenMinorVersion = 1
26-
mavenPatchVersion = 3
26+
mavenPatchVersion = 4
2727
nightliesUrl = http://dl.bintray.com/onedrive/Maven

onedrivesdk/src/main/java/com/onedrive/sdk/authentication/ADALAuthenticator.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,8 @@ public void onError(final Exception e) {
421421
loginSilentWaiter.signal();
422422
}
423423
};
424-
mAdalContext.acquireTokenSilent(mResourceUrl.get(), getClientId(), mUserId.get(), callback);
424+
425+
mAdalContext.acquireTokenSilent(mOneDriveServiceInfo.get().serviceResourceId, getClientId(), mUserId.get(), callback);
425426

426427
loginSilentWaiter.waitForSignal();
427428
//noinspection ThrowableResultOfMethodCallIgnored

onedrivesdk/src/main/java/com/onedrive/sdk/authentication/DisambiguationAuthenticator.java

Lines changed: 97 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
package com.onedrive.sdk.authentication;
2424

25+
import com.microsoft.onedrivesdk.BuildConfig;
2526
import com.onedrive.sdk.concurrency.ICallback;
2627
import com.onedrive.sdk.concurrency.IExecutors;
2728
import com.onedrive.sdk.concurrency.SimpleWaiter;
@@ -31,6 +32,9 @@
3132
import com.onedrive.sdk.logger.ILogger;
3233

3334
import android.app.Activity;
35+
import android.content.Context;
36+
import android.content.SharedPreferences;
37+
import android.support.annotation.Nullable;
3438

3539
import java.security.InvalidParameterException;
3640
import java.util.concurrent.atomic.AtomicReference;
@@ -40,6 +44,21 @@
4044
*/
4145
public class DisambiguationAuthenticator implements IAuthenticator {
4246

47+
/**
48+
* The preferences for this authenticator.
49+
*/
50+
private static final String DISAMBIGUATION_AUTHENTICATOR_PREFS = "DisambiguationAuthenticatorPrefs";
51+
52+
/**
53+
* The key for the version code
54+
*/
55+
public static final String VERSION_CODE_KEY = "versionCode";
56+
57+
/**
58+
* The key for the account type
59+
*/
60+
public static final String ACCOUNT_TYPE_KEY = "accountType";
61+
4362
/**
4463
* The current account info object.
4564
*/
@@ -173,31 +192,41 @@ public void failure(final ClientException error2) {
173192
}
174193
};
175194

176-
mLogger.logDebug("Creating disambiguation ui, waiting for user to sign in");
177-
new DisambiguationRequest(mActivity, disambiguationCallback, mLogger).execute();
178-
disambiguationWaiter.waitForSignal();
179-
180-
//noinspection ThrowableResultOfMethodCallIgnored
181-
if (error.get() != null) {
182-
throw error.get();
195+
AccountType accountType = getAccountTypeInPreferences();
196+
String accountName = null;
197+
if (accountType != null) {
198+
mLogger.logDebug(String.format("Found saved account information %s type of account", accountType));
199+
} else {
200+
mLogger.logDebug("Creating disambiguation ui, waiting for user to sign in");
201+
new DisambiguationRequest(mActivity, disambiguationCallback, mLogger).execute();
202+
disambiguationWaiter.waitForSignal();
203+
204+
//noinspection ThrowableResultOfMethodCallIgnored
205+
if (error.get() != null) {
206+
throw error.get();
207+
}
208+
final DisambiguationResponse disambiguationResponse = response.get();
209+
accountType = disambiguationResponse.getAccountType();
210+
accountName = disambiguationResponse.getAccount();
183211
}
184-
final DisambiguationResponse disambiguationResponse = response.get();
212+
185213
final IAccountInfo accountInfo;
186-
switch (disambiguationResponse.getAccountType()) {
214+
switch (accountType) {
187215
case ActiveDirectory:
188-
accountInfo = mADALAuthenticator.login(disambiguationResponse.getAccount());
216+
accountInfo = mADALAuthenticator.login(accountName);
189217
break;
190218
case MicrosoftAccount:
191-
accountInfo = mMSAAuthenticator.login(disambiguationResponse.getAccount());
219+
accountInfo = mMSAAuthenticator.login(accountName);
192220
break;
193221
default:
194222
final UnsupportedOperationException unsupportedOperationException
195223
= new UnsupportedOperationException("Unrecognized account type "
196-
+ disambiguationResponse.getAccountType());
224+
+ accountType);
197225
mLogger.logError("Unrecognized account type", unsupportedOperationException);
198226
throw unsupportedOperationException;
199227
}
200228

229+
setAccountTypeInPreferences(accountType);
201230
mAccountInfo.set(accountInfo);
202231
return mAccountInfo.get();
203232
}
@@ -241,10 +270,17 @@ public synchronized IAccountInfo loginSilent() throws ClientException {
241270
}
242271

243272
mLogger.logDebug("Starting login silent");
273+
274+
final AccountType accountType = getAccountTypeInPreferences();
275+
if (accountType != null) {
276+
mLogger.logDebug(String.format("Expecting %s type of account", accountType));
277+
}
278+
244279
mLogger.logDebug("Checking MSA");
245280
IAccountInfo accountInfo = mMSAAuthenticator.loginSilent();
246281
if (accountInfo != null) {
247282
mLogger.logDebug("Found account info in MSA");
283+
setAccountTypeInPreferences(accountType);
248284
mAccountInfo.set(accountInfo);
249285
return accountInfo;
250286
}
@@ -254,8 +290,9 @@ public synchronized IAccountInfo loginSilent() throws ClientException {
254290
mAccountInfo.set(accountInfo);
255291
if (accountInfo != null) {
256292
mLogger.logDebug("Found account info in ADAL");
293+
setAccountTypeInPreferences(accountType);
257294
}
258-
return accountInfo;
295+
return mAccountInfo.get();
259296
}
260297

261298
/**
@@ -277,7 +314,7 @@ public void logout(final ICallback<Void> logoutCallback) {
277314
@Override
278315
public void run() {
279316
logout();
280-
mExecutors.performOnForeground((Void)null, logoutCallback);
317+
mExecutors.performOnForeground((Void) null, logoutCallback);
281318
}
282319
});
283320
}
@@ -296,10 +333,18 @@ public synchronized void logout() throws ClientException {
296333
if (mMSAAuthenticator.getAccountInfo() != null) {
297334
mLogger.logDebug("Starting logout of MSA account");
298335
mMSAAuthenticator.logout();
299-
} else if (mADALAuthenticator.getAccountInfo() != null) {
336+
}
337+
338+
if (mADALAuthenticator.getAccountInfo() != null) {
300339
mLogger.logDebug("Starting logout of ADAL account");
301340
mADALAuthenticator.logout();
302341
}
342+
343+
getSharedPreferences()
344+
.edit()
345+
.clear()
346+
.putInt(VERSION_CODE_KEY, BuildConfig.VERSION_CODE)
347+
.commit();
303348
}
304349

305350
/**
@@ -310,4 +355,41 @@ public synchronized void logout() throws ClientException {
310355
public IAccountInfo getAccountInfo() {
311356
return mAccountInfo.get();
312357
}
358+
359+
/**
360+
* Gets the shared preferences for this authenticator.
361+
* @return The shared preferences.
362+
*/
363+
private SharedPreferences getSharedPreferences() {
364+
return mActivity.getSharedPreferences(DISAMBIGUATION_AUTHENTICATOR_PREFS, Context.MODE_PRIVATE);
365+
}
366+
367+
/**
368+
* Sets the AccountType from SharedPreferences
369+
* @param accountType The account type, can be null
370+
*/
371+
private void setAccountTypeInPreferences(@Nullable final AccountType accountType) {
372+
if (accountType == null) {
373+
return;
374+
}
375+
376+
getSharedPreferences()
377+
.edit()
378+
.putString(ACCOUNT_TYPE_KEY, accountType.toString())
379+
.putInt(VERSION_CODE_KEY, BuildConfig.VERSION_CODE)
380+
.commit();
381+
}
382+
383+
/**
384+
* Gets the AccountType from SharedPreferences
385+
* @return The account type, null if none was found
386+
*/
387+
@Nullable
388+
private AccountType getAccountTypeInPreferences() {
389+
final String accountTypeString = getSharedPreferences().getString(ACCOUNT_TYPE_KEY, null);
390+
if (accountTypeString == null) {
391+
return null;
392+
}
393+
return AccountType.valueOf(accountTypeString);
394+
}
313395
}

0 commit comments

Comments
 (0)