-
Notifications
You must be signed in to change notification settings - Fork 159
Expand file tree
/
Copy pathAbstractApplicationBase.java
More file actions
397 lines (332 loc) · 14.2 KB
/
AbstractApplicationBase.java
File metadata and controls
397 lines (332 loc) · 14.2 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.microsoft.aad.msal4j;
import org.slf4j.Logger;
import javax.net.ssl.SSLSocketFactory;
import java.net.Proxy;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.function.Consumer;
import static com.microsoft.aad.msal4j.ParameterValidationUtils.validateNotBlank;
import static com.microsoft.aad.msal4j.ParameterValidationUtils.validateNotNull;
/**
* Abstract class containing common methods and properties for {@link PublicClientApplication},
* {@link ConfidentialClientApplication}, and {@link ManagedIdentityApplication}
*/
public abstract class AbstractApplicationBase implements IApplicationBase {
protected Logger log;
protected Authority authenticationAuthority;
private String correlationId;
private boolean logPii;
private Proxy proxy;
private SSLSocketFactory sslSocketFactory;
private IHttpClient httpClient;
private Integer connectTimeoutForDefaultHttpClient;
private Integer readTimeoutForDefaultHttpClient;
private boolean retryDisabled;
String tenant;
//The following fields are set in only some applications and/or set internally by the library. To avoid excessive
// type casting throughout the library they are defined here as package-private, but will not be part of this class's Builder
private boolean validateAuthority;
private String clientId;
private String authority;
ServiceBundle serviceBundle;
Consumer<List<HashMap<String, String>>> telemetryConsumer;
protected TokenCache tokenCache;
CompletableFuture<IAuthenticationResult> executeRequest(
MsalRequest msalRequest) {
AuthenticationResultSupplier supplier = getAuthenticationResultSupplier(msalRequest);
ExecutorService executorService = serviceBundle.getExecutorService();
return executorService != null ?
CompletableFuture.supplyAsync(supplier, executorService) :
CompletableFuture.supplyAsync(supplier);
}
AuthenticationResult acquireTokenCommon(MsalRequest msalRequest, Authority requestAuthority)
throws Exception {
HttpHeaders headers = msalRequest.headers();
if (logPii) {
log.debug(LogHelper.createMessage(
String.format("Using Client Http Headers: %s", headers),
headers.getHeaderCorrelationIdValue()));
}
TokenRequestExecutor requestExecutor = new TokenRequestExecutor(
requestAuthority,
msalRequest,
serviceBundle);
AuthenticationResult result = requestExecutor.executeTokenRequest();
if (authenticationAuthority.authorityType.equals(AuthorityType.AAD)) {
InstanceDiscoveryMetadataEntry instanceDiscoveryMetadata =
AadInstanceDiscoveryProvider.getMetadataEntry(
requestAuthority.canonicalAuthorityUrl(),
validateAuthority,
msalRequest,
serviceBundle);
tokenCache.saveTokens(requestExecutor, result, instanceDiscoveryMetadata.preferredCache);
} else {
tokenCache.saveTokens(requestExecutor, result, authenticationAuthority.host);
}
return result;
}
private AuthenticationResultSupplier getAuthenticationResultSupplier(MsalRequest msalRequest) {
AuthenticationResultSupplier supplier;
if (msalRequest instanceof DeviceCodeFlowRequest) {
supplier = new AcquireTokenByDeviceCodeFlowSupplier(
(PublicClientApplication) this,
(DeviceCodeFlowRequest) msalRequest);
} else if (msalRequest instanceof SilentRequest) {
supplier = new AcquireTokenSilentSupplier(this, (SilentRequest) msalRequest);
} else if (msalRequest instanceof InteractiveRequest) {
supplier = new AcquireTokenByInteractiveFlowSupplier(
(PublicClientApplication) this,
(InteractiveRequest) msalRequest);
} else if (msalRequest instanceof ClientCredentialRequest) {
supplier = new AcquireTokenByClientCredentialSupplier(
(ConfidentialClientApplication) this,
(ClientCredentialRequest) msalRequest);
} else if (msalRequest instanceof OnBehalfOfRequest) {
supplier = new AcquireTokenByOnBehalfOfSupplier(
(ConfidentialClientApplication) this,
(OnBehalfOfRequest) msalRequest);
} else if (msalRequest instanceof UserFederatedIdentityCredentialRequest) {
supplier = new AcquireTokenByUserFederatedIdentityCredentialSupplier(
(ConfidentialClientApplication) this,
(UserFederatedIdentityCredentialRequest) msalRequest);
} else if (msalRequest instanceof AcquireTokenForAgentRequest) {
supplier = new AcquireTokenForAgentSupplier(
(ConfidentialClientApplication) this,
(AcquireTokenForAgentRequest) msalRequest);
} else if (msalRequest instanceof ManagedIdentityRequest) {
supplier = new AcquireTokenByManagedIdentitySupplier(
(ManagedIdentityApplication) this,
(ManagedIdentityRequest) msalRequest);
} else {
supplier = new AcquireTokenByAuthorizationGrantSupplier(
this,
msalRequest, null);
}
return supplier;
}
public String correlationId() {
return this.correlationId;
}
public boolean logPii() {
return this.logPii;
}
public Proxy proxy() {
return this.proxy;
}
public SSLSocketFactory sslSocketFactory() {
return this.sslSocketFactory;
}
public IHttpClient httpClient() {
return this.httpClient;
}
public Integer connectTimeoutForDefaultHttpClient() {
return this.connectTimeoutForDefaultHttpClient;
}
public Integer readTimeoutForDefaultHttpClient() {
return this.readTimeoutForDefaultHttpClient;
}
boolean isRetryDisabled() {
return this.retryDisabled;
}
String tenant() {
return this.tenant;
}
boolean validateAuthority() {
return this.validateAuthority;
}
String clientId() {
return this.clientId;
}
String authority() {
return this.authority;
}
ServiceBundle serviceBundle() {
return this.serviceBundle;
}
Consumer<List<HashMap<String, String>>> telemetryConsumer() {
return this.telemetryConsumer;
}
TokenCache tokenCache() {
return this.tokenCache;
}
public abstract static class Builder<T extends Builder<T>> {
// Optional parameters - initialized to default values
private String correlationId;
private boolean logPii = false;
ExecutorService executorService;
Proxy proxy;
SSLSocketFactory sslSocketFactory;
IHttpClient httpClient;
private Consumer<List<HashMap<String, String>>> telemetryConsumer;
Boolean onlySendFailureTelemetry = false;
Integer connectTimeoutForDefaultHttpClient;
Integer readTimeoutForDefaultHttpClient;
boolean disableInternalRetries;
private String clientId;
private Authority authenticationAuthority = createDefaultAADAuthority();
public Builder() {
}
public Builder(String clientId) {
validateNotBlank("clientId", clientId);
this.clientId = clientId;
}
abstract T self();
/**
* Set optional correlation id to be used by the API.
* If not provided, the API generates a random UUID.
*
* @param val a string value of correlation id
* @return instance of the Builder on which method was called
*/
public T correlationId(String val) {
validateNotBlank("correlationId", val);
correlationId = val;
return self();
}
/**
* Set logPii - boolean value, which determines
* whether Pii (personally identifiable information) will be logged in.
* The default value is false.
*
* @param val a boolean value for logPii
* @return instance of the Builder on which method was called
*/
public T logPii(boolean val) {
logPii = val;
return self();
}
/**
* Sets ExecutorService to be used to execute the requests.
* Developer is responsible for maintaining the lifecycle of the ExecutorService.
*
* @param val an instance of ExecutorService
* @return instance of the Builder on which method was called
*/
public T executorService(ExecutorService val) {
validateNotNull("executorService", val);
executorService = val;
return self();
}
/**
* Sets Proxy configuration to be used by the client application (MSAL4J by default uses
* {@link javax.net.ssl.HttpsURLConnection}) for all network communication.
* If no proxy value is passed in, system defined properties are used. If HTTP client is set on
* the client application (via ClientApplication.builder().httpClient()),
* proxy configuration should be done on the HTTP client object being passed in,
* and not through this method.
*
* @param val an instance of Proxy
* @return instance of the Builder on which method was called
*/
public T proxy(Proxy val) {
validateNotNull("proxy", val);
proxy = val;
return self();
}
/**
* Sets HTTP client to be used by the client application for all HTTP requests. Allows for fine
* grained configuration of HTTP client.
*
* @param val Implementation of {@link IHttpClient}
* @return instance of the Builder on which method was called
*/
public T httpClient(IHttpClient val) {
validateNotNull("httpClient", val);
httpClient = val;
return self();
}
/**
* Sets SSLSocketFactory to be used by the client application for all network communication.
* If HTTP client is set on the client application (via ClientApplication.builder().httpClient()),
* any configuration of SSL should be done on the HTTP client and not through this method.
*
* @param val an instance of SSLSocketFactory
* @return instance of the Builder on which method was called
*/
public T sslSocketFactory(SSLSocketFactory val) {
validateNotNull("sslSocketFactory", val);
sslSocketFactory = val;
return self();
}
/**
* Sets the connect timeout value used in HttpsURLConnection connections made by {@link DefaultHttpClient},
* and is not needed if using a custom HTTP client
*
* @param val timeout value in milliseconds
* @return instance of the Builder on which method was called
*/
public T connectTimeoutForDefaultHttpClient(Integer val) {
validateNotNull("connectTimeoutForDefaultHttpClient", val);
connectTimeoutForDefaultHttpClient = val;
return self();
}
/**
* Sets the read timeout value used in HttpsURLConnection connections made by {@link DefaultHttpClient},
* and is not needed if using a custom HTTP client
*
* @param val timeout value in milliseconds
* @return instance of the Builder on which method was called
*/
public T readTimeoutForDefaultHttpClient(Integer val) {
validateNotNull("readTimeoutForDefaultHttpClient", val);
readTimeoutForDefaultHttpClient = val;
return self();
}
/**
* The library has a number of policies for retrying HTTP calls in different scenarios.
* <p>
* This will disable all internal retry behavior, allowing customized retry behavior via your own implementation of {@link IHttpClient}
*
* @return instance of the Builder on which method was called
*/
public T disableInternalRetries() {
disableInternalRetries = true;
return self();
}
T telemetryConsumer(Consumer<List<HashMap<String, String>>> val) {
validateNotNull("telemetryConsumer", val);
telemetryConsumer = val;
return self();
}
T onlySendFailureTelemetry(Boolean val) {
onlySendFailureTelemetry = val;
return self();
}
private static Authority createDefaultAADAuthority() {
Authority authority;
try {
authority = new AADAuthority(new URL(DEFAULT_AUTHORITY));
} catch (Exception e) {
throw new MsalClientException(e);
}
return authority;
}
abstract AbstractApplicationBase build();
}
AbstractApplicationBase(Builder<?> builder) {
correlationId = builder.correlationId;
logPii = builder.logPii;
telemetryConsumer = builder.telemetryConsumer;
proxy = builder.proxy;
sslSocketFactory = builder.sslSocketFactory;
connectTimeoutForDefaultHttpClient = builder.connectTimeoutForDefaultHttpClient;
readTimeoutForDefaultHttpClient = builder.readTimeoutForDefaultHttpClient;
authenticationAuthority = builder.authenticationAuthority;
clientId = builder.clientId;
retryDisabled = builder.disableInternalRetries;
if (builder.httpClient == null) {
httpClient = new DefaultHttpClient(
builder.proxy,
builder.sslSocketFactory,
builder.connectTimeoutForDefaultHttpClient,
builder.readTimeoutForDefaultHttpClient);
} else {
httpClient = builder.httpClient;
}
}
}