This repository was archived by the owner on Nov 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathAuthenticationManager.java
More file actions
200 lines (174 loc) · 7.61 KB
/
AuthenticationManager.java
File metadata and controls
200 lines (174 loc) · 7.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
* Copyright (c) Microsoft. All rights reserved. Licensed under the MIT license.
* See LICENSE in the project root for license information.
*/
package com.microsoft.graph.snippets;
import android.accounts.AuthenticatorException;
import android.accounts.OperationCanceledException;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import com.microsoft.graph.authentication.IAuthenticationProvider;
import com.microsoft.graph.authentication.MSALAuthenticationProvider;
import com.microsoft.graph.http.IHttpRequest;
import com.microsoft.graph.snippets.application.SnippetApp;
import com.microsoft.graph.snippets.util.IManifestReader;
import com.microsoft.graph.snippets.util.ManifestReader;
import com.microsoft.identity.client.AuthenticationCallback;
import com.microsoft.identity.client.AuthenticationResult;
import com.microsoft.identity.client.IAccount;
import com.microsoft.identity.client.PublicClientApplication;
import com.microsoft.identity.client.exception.MsalException;
import java.io.IOException;
/**
* Handles setup of OAuth library in API clients.
*/
public class AuthenticationManager implements IAuthenticationProvider {
private static final String TAG = "AuthenticationManager";
private static AuthenticationManager INSTANCE;
private static PublicClientApplication mPublicClientApplication;
private AuthenticationResult mAuthResult;
private MSALAuthenticationCallback mActivityCallback;
private AuthenticationManager() {
}
public static synchronized AuthenticationManager getInstance() {
if (INSTANCE == null) {
INSTANCE = new AuthenticationManager();
if (mPublicClientApplication == null) {
IManifestReader metaDataReader = new ManifestReader();
String clientID = metaDataReader.getApplicationMetadataValueString("com.microsoft.identity.client.ClientId");
mPublicClientApplication = new PublicClientApplication(SnippetApp.getContext(), clientID);
}
}
return INSTANCE;
}
public static synchronized void resetInstance() {
INSTANCE = null;
}
/**
* Returns the access token obtained in authentication
*
* @return mAccessToken
*/
public String getAccessToken() throws AuthenticatorException, IOException, OperationCanceledException {
return mAuthResult.getAccessToken();
}
public PublicClientApplication getPublicClient(){
return mPublicClientApplication;
}
/**
* Disconnects the app from Office 365 by clearing the token cache, setting the client objects
* to null, and removing the user id from shred preferences.
*/
public void disconnect() {
mPublicClientApplication.removeAccount(mAuthResult.getAccount());
// Reset the AuthenticationManager object
AuthenticationManager.resetInstance();
}
/**
* Authenticates the user and lets the user authorize the app for the requested permissions.
* An authentication token is returned via the getAuthInteractiveCalback method
* @param activity
* @param authenticationCallback
*/
public void callAcquireToken(Activity activity, final MSALAuthenticationCallback authenticationCallback) {
mActivityCallback = authenticationCallback;
mPublicClientApplication.acquireToken(
activity, ServiceConstants.SCOPES, getAuthInteractiveCallback());
}
public void callAcquireTokenSilent(IAccount user, boolean forceRefresh, MSALAuthenticationCallback msalAuthenticationCallback) {
mActivityCallback = msalAuthenticationCallback;
mPublicClientApplication.acquireTokenSilentAsync(ServiceConstants.SCOPES, user, null, forceRefresh, getAuthSilentCallback());
}
//
// App callbacks for MSAL
// ======================
// getActivity() - returns activity so we can acquireToken within a callback
// getAuthSilentCallback() - callback defined to handle acquireTokenSilent() case
// getAuthInteractiveCallback() - callback defined to handle acquireToken() case
//
public Context getActivity() {
return SnippetApp.getContext();
}
/* Callback method for acquireTokenSilent calls
* Looks if tokens are in the cache (refreshes if necessary and if we don't forceRefresh)
* else errors that we need to do an interactive request.
*/
private AuthenticationCallback getAuthSilentCallback() {
return new AuthenticationCallback() {
@Override
public void onSuccess(AuthenticationResult authenticationResult) {
/* Successfully got a token, call Graph now */
Log.d(TAG, "Successfully authenticated");
/* Store the authResult */
mAuthResult = authenticationResult;
//invoke UI callback
if (mActivityCallback != null)
mActivityCallback.onSuccess(mAuthResult);
}
@Override
public void onError(MsalException exception) {
/* Failed to acquireToken */
Log.d(TAG, "Authentication failed: " + exception.toString());
if (mActivityCallback != null)
mActivityCallback.onError(exception);
}
@Override
public void onCancel() {
/* User canceled the authentication */
Log.d(TAG, "User cancelled login.");
}
};
}
/* Callback used for interactive request. If succeeds we use the access
* token to call the Microsoft Graph. Does not check cache
*/
private AuthenticationCallback getAuthInteractiveCallback() {
return new AuthenticationCallback() {
@Override
public void onSuccess(AuthenticationResult authenticationResult) {
/* Successfully got a token, call graph now */
Log.d(TAG, "Successfully authenticated");
Log.d(TAG, "ID Token: " + authenticationResult.getIdToken());
/* Store the auth result */
mAuthResult = authenticationResult;
if (mActivityCallback != null)
mActivityCallback.onSuccess(mAuthResult);
}
@Override
public void onError(MsalException exception) {
/* Failed to acquireToken */
Log.d(TAG, "Authentication failed: " + exception.toString());
if (mActivityCallback != null)
mActivityCallback.onError(exception);
}
@Override
public void onCancel() {
/* User canceled the authentication */
Log.d(TAG, "User cancelled login.");
if (mActivityCallback != null)
mActivityCallback.onCancel();
}
};
}
@Override
public void authenticateRequest(IHttpRequest request) {
try {
request.addHeader("Authorization", "Bearer "
+ AuthenticationManager.getInstance()
.getAccessToken());
// This header has been added to identify this sample in the Microsoft Graph service.
// If you're using this code for your project please remove the following line.
request.addHeader("SampleID", "android-java-connect-sample");
Log.i("Connect","Request: " + request.toString());
} catch (AuthenticatorException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (OperationCanceledException e) {
e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
}
}
}