Skip to content

Commit

Permalink
Merge pull request #37 from peternied/SilentAuth
Browse files Browse the repository at this point in the history
Change to AcquireToken to have refresh tokens cached properly
  • Loading branch information
Peter Nied committed Feb 3, 2016
2 parents 2334579 + 43f039b commit 585fb5a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 18 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Integrate the [OneDrive API](https://dev.onedrive.com/README.htm) into your Andr

## 1. Installation
### 1.1 Install AAR via Gradle
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
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

```gradle
repository {
Expand All @@ -16,14 +16,14 @@ repository {
dependency {
// Include the sdk as a dependency
compile('com.onedrive.sdk:onedrive-sdk-android:1.1.0')
compile('com.onedrive.sdk:onedrive-sdk-android:1.1.1')
// Include the gson dependency
compile 'com.google.code.gson:gson:2.3.1'
// Include supported authentication methods for your application
compile 'com.microsoft.services.msa:msa-auth:0.8.4'
compile 'com.microsoft.aad:adal:1.1.7'
compile 'com.microsoft.aad:adal:1.1.11'
}
```

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ org.gradle.jvmargs=-XX:MaxPermSize=512m
mavenRepoUrl = https://api.bintray.com/maven/onedrive/Maven/onedrive-sdk-android
mavenGroupId = com.onedrive.sdk
mavenArtifactId = onedrive-sdk-android
mavenVersion = 1.1.0
mavenVersion = 1.1.1
nightliesUrl = http://dl.bintray.com/onedrive/Maven
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.microsoft.aad.adal.AuthenticationCallback;
import com.microsoft.aad.adal.AuthenticationCancelError;
import com.microsoft.aad.adal.AuthenticationContext;
import com.microsoft.aad.adal.AuthenticationException;
import com.microsoft.aad.adal.AuthenticationResult;
import com.microsoft.aad.adal.PromptBehavior;
import com.onedrive.sdk.core.ClientException;
Expand Down Expand Up @@ -295,7 +296,7 @@ public synchronized IAccountInfo login(final String emailAddressHint) throws Cli

// Request a fresh auth token for the OneDrive service.
final AuthenticationResult oneDriveServiceAuthToken =
getOneDriveServiceAuthResult(discoveryServiceAuthToken, oneDriveServiceInfo);
getOneDriveServiceAuthResult(oneDriveServiceInfo);

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

@Override
public void onError(final Exception e) {
error.set(new ClientAuthenticatorException("Silent authentication failure from ADAL",
String message = "Silent authentication failure from ADAL";
if (e instanceof AuthenticationException) {
message = String.format("%s; Code %s",
message,
((AuthenticationException)e).getCode().getDescription());
}
error.set(new ClientAuthenticatorException(message,
e,
OneDriveErrorCodes.AuthenticationFailure));
loginSilentWaiter.signal();
Expand Down Expand Up @@ -509,7 +516,13 @@ public void onError(final Exception ex) {
code = OneDriveErrorCodes.AuthenticationCancelled;
}

final String message = "Error while retrieving the discovery service auth token";
String message = "Error while retrieving the discovery service auth token";
if (ex instanceof AuthenticationException) {
message = String.format("%s; Code %s",
message,
((AuthenticationException)ex).getCode().getDescription());
}

error.set(new ClientAuthenticatorException(message, ex, code));
mLogger.logError("Error while attempting to login interactively", error.get());
discoveryCallbackWaiter.signal();
Expand Down Expand Up @@ -591,12 +604,10 @@ private ServiceInfo getOneDriveServiceInfoFromDiscoveryService(final String acce

/**
* Gets the authentication token for the OneDrive service.
* @param authenticationResult The authentication result from the Discovery Service.
* @param oneDriveServiceInfo The OneDrive services info.
* @return The authentication result for this OneDrive service.
*/
private AuthenticationResult getOneDriveServiceAuthResult(final AuthenticationResult authenticationResult,
final ServiceInfo oneDriveServiceInfo) {
private AuthenticationResult getOneDriveServiceAuthResult(final ServiceInfo oneDriveServiceInfo) {
final SimpleWaiter authorityCallbackWaiter = new SimpleWaiter();
final AtomicReference<ClientException> error = new AtomicReference<>();
final AtomicReference<AuthenticationResult> oneDriveServiceAuthToken = new AtomicReference<>();
Expand All @@ -611,26 +622,31 @@ public void onSuccess(final AuthenticationResult authenticationResult) {

@Override
public void onError(final Exception e) {
String message = "Error while retrieving the service specific auth token";
OneDriveErrorCodes code = OneDriveErrorCodes.AuthenticationFailure;
if (e instanceof AuthenticationCancelError) {
code = OneDriveErrorCodes.AuthenticationCancelled;
}
if (e instanceof AuthenticationException) {
message = String.format("%s; Code %s",
message,
((AuthenticationException)e).getCode().getDescription());
}

error.set(new ClientAuthenticatorException("Error while retrieving the service specific auth token",
error.set(new ClientAuthenticatorException(message,
e,
code));
mLogger.logError("Unable to refresh token into OneDrive service access token", error.get());
authorityCallbackWaiter.signal();
}
};
final String refreshToken = authenticationResult.getRefreshToken();

mLogger.logDebug("Starting OneDrive resource refresh token request");
mAdalContext.acquireTokenByRefreshToken(
refreshToken,
getClientId(),
oneDriveServiceInfo.serviceResourceId,
authorityCallback);
mAdalContext.acquireToken(mActivity,
oneDriveServiceInfo.serviceResourceId,
getClientId(),
getRedirectUrl(),
PromptBehavior.Auto,
authorityCallback);

mLogger.logDebug("Waiting for token refresh");
authorityCallbackWaiter.waitForSignal();
Expand Down

0 comments on commit 585fb5a

Please sign in to comment.