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

Commit 585fb5a

Browse files
author
Peter Nied
committed
Merge pull request #37 from peternied/SilentAuth
Change to AcquireToken to have refresh tokens cached properly
2 parents 2334579 + 43f039b commit 585fb5a

File tree

3 files changed

+34
-18
lines changed

3 files changed

+34
-18
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Integrate the [OneDrive API](https://dev.onedrive.com/README.htm) into your Andr
77

88
## 1. Installation
99
### 1.1 Install AAR via Gradle
10-
Add the maven central repository to your projects build.gradle file then add a compile dependency for com.onedrive.sdk:onedrive-sdk-android:1.1.0
10+
Add the maven central repository to your projects build.gradle file then add a compile dependency for com.onedrive.sdk:onedrive-sdk-android:1.1.1
1111

1212
```gradle
1313
repository {
@@ -16,14 +16,14 @@ repository {
1616
1717
dependency {
1818
// Include the sdk as a dependency
19-
compile('com.onedrive.sdk:onedrive-sdk-android:1.1.0')
19+
compile('com.onedrive.sdk:onedrive-sdk-android:1.1.1')
2020
2121
// Include the gson dependency
2222
compile 'com.google.code.gson:gson:2.3.1'
2323
2424
// Include supported authentication methods for your application
2525
compile 'com.microsoft.services.msa:msa-auth:0.8.4'
26-
compile 'com.microsoft.aad:adal:1.1.7'
26+
compile 'com.microsoft.aad:adal:1.1.11'
2727
}
2828
```
2929

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@ org.gradle.jvmargs=-XX:MaxPermSize=512m
2121
mavenRepoUrl = https://api.bintray.com/maven/onedrive/Maven/onedrive-sdk-android
2222
mavenGroupId = com.onedrive.sdk
2323
mavenArtifactId = onedrive-sdk-android
24-
mavenVersion = 1.1.0
24+
mavenVersion = 1.1.1
2525
nightliesUrl = http://dl.bintray.com/onedrive/Maven

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

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import com.microsoft.aad.adal.AuthenticationCallback;
3232
import com.microsoft.aad.adal.AuthenticationCancelError;
3333
import com.microsoft.aad.adal.AuthenticationContext;
34+
import com.microsoft.aad.adal.AuthenticationException;
3435
import com.microsoft.aad.adal.AuthenticationResult;
3536
import com.microsoft.aad.adal.PromptBehavior;
3637
import com.onedrive.sdk.core.ClientException;
@@ -295,7 +296,7 @@ public synchronized IAccountInfo login(final String emailAddressHint) throws Cli
295296

296297
// Request a fresh auth token for the OneDrive service.
297298
final AuthenticationResult oneDriveServiceAuthToken =
298-
getOneDriveServiceAuthResult(discoveryServiceAuthToken, oneDriveServiceInfo);
299+
getOneDriveServiceAuthResult(oneDriveServiceInfo);
299300

300301
// Get the OneDrive auth token and save a reference to it.
301302
final String serviceInfoAsString = mHttpProvider.getSerializer()
@@ -394,7 +395,13 @@ public void onSuccess(final AuthenticationResult authenticationResult) {
394395

395396
@Override
396397
public void onError(final Exception e) {
397-
error.set(new ClientAuthenticatorException("Silent authentication failure from ADAL",
398+
String message = "Silent authentication failure from ADAL";
399+
if (e instanceof AuthenticationException) {
400+
message = String.format("%s; Code %s",
401+
message,
402+
((AuthenticationException)e).getCode().getDescription());
403+
}
404+
error.set(new ClientAuthenticatorException(message,
398405
e,
399406
OneDriveErrorCodes.AuthenticationFailure));
400407
loginSilentWaiter.signal();
@@ -509,7 +516,13 @@ public void onError(final Exception ex) {
509516
code = OneDriveErrorCodes.AuthenticationCancelled;
510517
}
511518

512-
final String message = "Error while retrieving the discovery service auth token";
519+
String message = "Error while retrieving the discovery service auth token";
520+
if (ex instanceof AuthenticationException) {
521+
message = String.format("%s; Code %s",
522+
message,
523+
((AuthenticationException)ex).getCode().getDescription());
524+
}
525+
513526
error.set(new ClientAuthenticatorException(message, ex, code));
514527
mLogger.logError("Error while attempting to login interactively", error.get());
515528
discoveryCallbackWaiter.signal();
@@ -591,12 +604,10 @@ private ServiceInfo getOneDriveServiceInfoFromDiscoveryService(final String acce
591604

592605
/**
593606
* Gets the authentication token for the OneDrive service.
594-
* @param authenticationResult The authentication result from the Discovery Service.
595607
* @param oneDriveServiceInfo The OneDrive services info.
596608
* @return The authentication result for this OneDrive service.
597609
*/
598-
private AuthenticationResult getOneDriveServiceAuthResult(final AuthenticationResult authenticationResult,
599-
final ServiceInfo oneDriveServiceInfo) {
610+
private AuthenticationResult getOneDriveServiceAuthResult(final ServiceInfo oneDriveServiceInfo) {
600611
final SimpleWaiter authorityCallbackWaiter = new SimpleWaiter();
601612
final AtomicReference<ClientException> error = new AtomicReference<>();
602613
final AtomicReference<AuthenticationResult> oneDriveServiceAuthToken = new AtomicReference<>();
@@ -611,26 +622,31 @@ public void onSuccess(final AuthenticationResult authenticationResult) {
611622

612623
@Override
613624
public void onError(final Exception e) {
625+
String message = "Error while retrieving the service specific auth token";
614626
OneDriveErrorCodes code = OneDriveErrorCodes.AuthenticationFailure;
615627
if (e instanceof AuthenticationCancelError) {
616628
code = OneDriveErrorCodes.AuthenticationCancelled;
617629
}
630+
if (e instanceof AuthenticationException) {
631+
message = String.format("%s; Code %s",
632+
message,
633+
((AuthenticationException)e).getCode().getDescription());
634+
}
618635

619-
error.set(new ClientAuthenticatorException("Error while retrieving the service specific auth token",
636+
error.set(new ClientAuthenticatorException(message,
620637
e,
621638
code));
622639
mLogger.logError("Unable to refresh token into OneDrive service access token", error.get());
623640
authorityCallbackWaiter.signal();
624641
}
625642
};
626-
final String refreshToken = authenticationResult.getRefreshToken();
627-
628643
mLogger.logDebug("Starting OneDrive resource refresh token request");
629-
mAdalContext.acquireTokenByRefreshToken(
630-
refreshToken,
631-
getClientId(),
632-
oneDriveServiceInfo.serviceResourceId,
633-
authorityCallback);
644+
mAdalContext.acquireToken(mActivity,
645+
oneDriveServiceInfo.serviceResourceId,
646+
getClientId(),
647+
getRedirectUrl(),
648+
PromptBehavior.Auto,
649+
authorityCallback);
634650

635651
mLogger.logDebug("Waiting for token refresh");
636652
authorityCallbackWaiter.waitForSignal();

0 commit comments

Comments
 (0)