-
Notifications
You must be signed in to change notification settings - Fork 147
/
Copy pathAbstractApplicationBase.java
356 lines (295 loc) · 12.9 KB
/
AbstractApplicationBase.java
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.microsoft.aad.msal4j;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.experimental.Accessors;
import org.slf4j.Logger;
import javax.net.ssl.SSLSocketFactory;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
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;
@Accessors(fluent = true)
@Getter
private String correlationId;
@Accessors(fluent = true)
@Getter
private boolean logPii;
@Accessors(fluent = true)
@Getter
private Proxy proxy;
@Accessors(fluent = true)
@Getter
private SSLSocketFactory sslSocketFactory;
@Accessors(fluent = true)
@Getter
private IHttpClient httpClient;
@Accessors(fluent = true)
@Getter
private Integer connectTimeoutForDefaultHttpClient;
@Accessors(fluent = true)
@Getter
private Integer readTimeoutForDefaultHttpClient;
@Accessors(fluent = true)
@Getter(AccessLevel.PACKAGE)
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
@Accessors(fluent = true)
@Getter(AccessLevel.PACKAGE)
private boolean validateAuthority;
@Accessors(fluent = true)
@Getter(AccessLevel.PACKAGE)
private String clientId;
@Accessors(fluent = true)
@Getter(AccessLevel.PACKAGE)
private String authority;
@Accessors(fluent = true)
@Getter(AccessLevel.PACKAGE)
private ServiceBundle serviceBundle;
@Accessors(fluent = true)
@Getter(AccessLevel.PACKAGE)
private Consumer<List<HashMap<String, String>>> telemetryConsumer;
@Accessors(fluent = true)
@Getter(AccessLevel.PACKAGE)
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 ManagedIdentityRequest) {
supplier = new AcquireTokenByManagedIdentitySupplier(
(ManagedIdentityApplication) this,
(ManagedIdentityRequest) msalRequest);
} else {
supplier = new AcquireTokenByAuthorizationGrantSupplier(
this,
msalRequest, null);
}
return supplier;
}
public abstract static class Builder<T extends Builder<T>> {
// Optional parameters - initialized to default values
private String correlationId;
private boolean logPii = false;
private ExecutorService executorService;
private Proxy proxy;
private SSLSocketFactory sslSocketFactory;
private IHttpClient httpClient;
private Consumer<List<HashMap<String, String>>> telemetryConsumer;
private Boolean onlySendFailureTelemetry = false;
private Integer connectTimeoutForDefaultHttpClient;
private Integer readTimeoutForDefaultHttpClient;
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();
}
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;
serviceBundle = new ServiceBundle(
builder.executorService,
builder.httpClient == null ?
new DefaultHttpClient(builder.proxy, builder.sslSocketFactory, builder.connectTimeoutForDefaultHttpClient, builder.readTimeoutForDefaultHttpClient) :
builder.httpClient,
new TelemetryManager(telemetryConsumer, builder.onlySendFailureTelemetry));
authenticationAuthority = builder.authenticationAuthority;
clientId = builder.clientId;
}
}