-
Notifications
You must be signed in to change notification settings - Fork 137
/
PublicClientApplication.java
219 lines (167 loc) · 7.16 KB
/
PublicClientApplication.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.microsoft.aad.msal4j;
import com.nimbusds.oauth2.sdk.auth.ClientAuthentication;
import com.nimbusds.oauth2.sdk.auth.ClientAuthenticationMethod;
import com.nimbusds.oauth2.sdk.id.ClientID;
import org.slf4j.LoggerFactory;
import java.net.MalformedURLException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicReference;
import static com.microsoft.aad.msal4j.ParameterValidationUtils.validateNotBlank;
import static com.microsoft.aad.msal4j.ParameterValidationUtils.validateNotNull;
/**
* Class to be used to acquire tokens for public client applications (Desktop, Mobile).
* For details see {@link IPublicClientApplication}
* <p>
* Conditionally thread-safe
*/
public class PublicClientApplication extends AbstractClientApplicationBase implements IPublicClientApplication {
private final ClientAuthenticationPost clientAuthentication;
private IBroker broker;
private boolean brokerEnabled;
@Override
public CompletableFuture<IAuthenticationResult> acquireToken(UserNamePasswordParameters parameters) {
validateNotNull("parameters", parameters);
RequestContext context = new RequestContext(
this,
PublicApi.ACQUIRE_TOKEN_BY_USERNAME_PASSWORD,
parameters,
UserIdentifier.fromUpn(parameters.username()));
CompletableFuture<IAuthenticationResult> future;
if (brokerEnabled) {
future = broker.acquireToken(this, parameters);
} else {
UserNamePasswordRequest userNamePasswordRequest =
new UserNamePasswordRequest(parameters,
this,
context);
future = this.executeRequest(userNamePasswordRequest);
}
return future;
}
@Override
public CompletableFuture<IAuthenticationResult> acquireToken(IntegratedWindowsAuthenticationParameters parameters) {
validateNotNull("parameters", parameters);
RequestContext context = new RequestContext(
this,
PublicApi.ACQUIRE_TOKEN_BY_INTEGRATED_WINDOWS_AUTH,
parameters,
UserIdentifier.fromUpn(parameters.username()));
IntegratedWindowsAuthenticationRequest integratedWindowsAuthenticationRequest =
new IntegratedWindowsAuthenticationRequest(
parameters,
this,
context);
return this.executeRequest(integratedWindowsAuthenticationRequest);
}
@Override
public CompletableFuture<IAuthenticationResult> acquireToken(DeviceCodeFlowParameters parameters) {
if (!(AuthorityType.AAD.equals(authenticationAuthority.authorityType()) ||
AuthorityType.ADFS.equals(authenticationAuthority.authorityType()))) {
throw new IllegalArgumentException(
"Invalid authority type. Device Flow is only supported by AAD and ADFS authorities");
}
validateNotNull("parameters", parameters);
RequestContext context = new RequestContext(
this,
PublicApi.ACQUIRE_TOKEN_BY_DEVICE_CODE_FLOW,
parameters);
AtomicReference<CompletableFuture<IAuthenticationResult>> futureReference =
new AtomicReference<>();
DeviceCodeFlowRequest deviceCodeRequest = new DeviceCodeFlowRequest(
parameters,
futureReference,
this,
context);
CompletableFuture<IAuthenticationResult> future = executeRequest(deviceCodeRequest);
futureReference.set(future);
return future;
}
@Override
public CompletableFuture<IAuthenticationResult> acquireToken(InteractiveRequestParameters parameters) {
validateNotNull("parameters", parameters);
AtomicReference<CompletableFuture<IAuthenticationResult>> futureReference = new AtomicReference<>();
RequestContext context = new RequestContext(
this,
PublicApi.ACQUIRE_TOKEN_INTERACTIVE,
parameters,
UserIdentifier.fromUpn(parameters.loginHint()));
InteractiveRequest interactiveRequest = new InteractiveRequest(
parameters,
futureReference,
this,
context);
CompletableFuture<IAuthenticationResult> future;
if (brokerEnabled) {
future = broker.acquireToken(this, parameters);
} else {
future = executeRequest(interactiveRequest);
}
futureReference.set(future);
return future;
}
@Override
public CompletableFuture<IAuthenticationResult> acquireTokenSilently(SilentParameters parameters) throws MalformedURLException {
CompletableFuture<IAuthenticationResult> future;
if (brokerEnabled) {
future = broker.acquireToken(this, parameters);
} else {
future = super.acquireTokenSilently(parameters);
}
return future;
}
@Override
public CompletableFuture<Void> removeAccount(IAccount account) {
if (brokerEnabled) {
broker.removeAccount(this, account);
}
return super.removeAccount(account);
}
private PublicClientApplication(Builder builder) {
super(builder);
validateNotBlank("clientId", clientId());
log = LoggerFactory.getLogger(PublicClientApplication.class);
this.clientAuthentication = new ClientAuthenticationPost(ClientAuthenticationMethod.NONE,
new ClientID(clientId()));
this.broker = builder.broker;
this.brokerEnabled = builder.brokerEnabled;
}
@Override
protected ClientAuthentication clientAuthentication() {
return clientAuthentication;
}
/**
* @param clientId Client ID (Application ID) of the application as registered
* in the application registration portal (portal.azure.com)
* @return instance of Builder of PublicClientApplication
*/
public static Builder builder(String clientId) {
return new Builder(clientId);
}
public static class Builder extends AbstractClientApplicationBase.Builder<Builder> {
private Builder(String clientId) {
super(clientId);
}
private IBroker broker = null;
private boolean brokerEnabled = false;
/**
* Implementation of IBroker that will be used to retrieve tokens
* <p>
* Setting this will cause MSAL Java to use the given broker implementation to retrieve tokens from a broker (such as WAM/MSALRuntime) in flows that support it
*/
public PublicClientApplication.Builder broker(IBroker val) {
this.broker = val;
this.brokerEnabled = this.broker.isBrokerAvailable();
return self();
}
@Override
public PublicClientApplication build() {
return new PublicClientApplication(this);
}
@Override
protected Builder self() {
return this;
}
}
}