|
| 1 | +package com.microsoft.androidoffice365calendar; |
| 2 | + |
| 3 | +import android.app.Activity; |
| 4 | +import android.util.Log; |
| 5 | + |
| 6 | +import com.google.common.util.concurrent.SettableFuture; |
| 7 | +import com.microsoft.aad.adal.AuthenticationCallback; |
| 8 | +import com.microsoft.aad.adal.AuthenticationContext; |
| 9 | +import com.microsoft.aad.adal.AuthenticationResult; |
| 10 | +import com.microsoft.aad.adal.PromptBehavior; |
| 11 | +import com.microsoft.services.orc.auth.AuthenticationCredentials; |
| 12 | +import com.microsoft.services.orc.core.DependencyResolver; |
| 13 | +import com.microsoft.services.orc.http.Credentials; |
| 14 | +import com.microsoft.services.orc.http.impl.OAuthCredentials; |
| 15 | +import com.microsoft.services.orc.http.impl.OkHttpTransport; |
| 16 | +import com.microsoft.services.orc.serialization.impl.GsonSerializer; |
| 17 | + |
| 18 | +public class AuthenticationController { |
| 19 | + private final String TAG = "Authentication"; |
| 20 | + private AuthenticationContext authContext; |
| 21 | + private DependencyResolver dependencyResolver; |
| 22 | + private Activity contextActivity; |
| 23 | + private String resourceId; |
| 24 | + |
| 25 | + public static synchronized AuthenticationController getInstance() { |
| 26 | + if (INSTANCE == null) { |
| 27 | + INSTANCE = new AuthenticationController(); |
| 28 | + } |
| 29 | + return INSTANCE; |
| 30 | + } |
| 31 | + private static AuthenticationController INSTANCE; |
| 32 | + |
| 33 | + private AuthenticationController() { |
| 34 | + resourceId = Constants.GRAPH_RESOURCE_ID; |
| 35 | + } |
| 36 | + |
| 37 | + public void setContextActivity(final Activity contextActivity) { |
| 38 | + this.contextActivity = contextActivity; |
| 39 | + } |
| 40 | + |
| 41 | + public SettableFuture<Boolean> initialize() { |
| 42 | + final SettableFuture<Boolean> result = SettableFuture.create(); |
| 43 | + |
| 44 | + if (verifyAuthenticationContext()) { |
| 45 | + getAuthenticationContext().acquireToken( |
| 46 | + this.contextActivity, |
| 47 | + this.resourceId, |
| 48 | + Constants.AAD_CLIENT_ID, |
| 49 | + Constants.AAD_REDIRECT_URL, |
| 50 | + PromptBehavior.Auto, |
| 51 | + new AuthenticationCallback<AuthenticationResult>() { |
| 52 | + @Override |
| 53 | + public void onSuccess(final AuthenticationResult authenticationResult) { |
| 54 | + if (authenticationResult != null && authenticationResult.getStatus() == AuthenticationResult.AuthenticationStatus.Succeeded) { |
| 55 | + dependencyResolver = new DependencyResolver.Builder( |
| 56 | + new OkHttpTransport(), new GsonSerializer(), |
| 57 | + new AuthenticationCredentials() { |
| 58 | + @Override |
| 59 | + public Credentials getCredentials() { |
| 60 | + return new OAuthCredentials(authenticationResult.getAccessToken()); |
| 61 | + } |
| 62 | + }).build(); |
| 63 | + result.set(true); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public void onError(Exception t) { |
| 69 | + Log.e(TAG, "Acquire token failed. " + t.getMessage()); |
| 70 | + result.setException(t); |
| 71 | + } |
| 72 | + }); |
| 73 | + } else { |
| 74 | + result.setException(new Throwable("Auth context verification failed.")); |
| 75 | + } |
| 76 | + return result; |
| 77 | + } |
| 78 | + |
| 79 | + public AuthenticationContext getAuthenticationContext() { |
| 80 | + if (authContext == null) { |
| 81 | + try { |
| 82 | + authContext = new AuthenticationContext(this.contextActivity, Constants.AAD_AUTHORITY, false); |
| 83 | + } catch (Throwable t) { |
| 84 | + Log.e(TAG, "Get AuthenticationContext failed. " + t.toString()); |
| 85 | + } |
| 86 | + } |
| 87 | + return authContext; |
| 88 | + } |
| 89 | + |
| 90 | + public DependencyResolver getDependencyResolver() { |
| 91 | + return getInstance().dependencyResolver; |
| 92 | + } |
| 93 | + |
| 94 | + private boolean verifyAuthenticationContext() { |
| 95 | + if (this.contextActivity == null) { |
| 96 | + return false; |
| 97 | + } |
| 98 | + return true; |
| 99 | + } |
| 100 | +} |
0 commit comments