-
Notifications
You must be signed in to change notification settings - Fork 156
Expand file tree
/
Copy pathAcquireTokenByDeviceCodeFlowSupplier.java
More file actions
84 lines (65 loc) · 3.63 KB
/
AcquireTokenByDeviceCodeFlowSupplier.java
File metadata and controls
84 lines (65 loc) · 3.63 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.microsoft.aad.msal4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.TimeUnit;
import static com.microsoft.aad.msal4j.AuthenticationErrorCode.AUTHORIZATION_PENDING;
class AcquireTokenByDeviceCodeFlowSupplier extends AuthenticationResultSupplier {
private static final Logger LOG = LoggerFactory.getLogger(AcquireTokenByDeviceCodeFlowSupplier.class);
private DeviceCodeFlowRequest deviceCodeFlowRequest;
AcquireTokenByDeviceCodeFlowSupplier(PublicClientApplication clientApplication,
DeviceCodeFlowRequest deviceCodeFlowRequest) {
super(clientApplication, deviceCodeFlowRequest);
this.deviceCodeFlowRequest = deviceCodeFlowRequest;
}
AuthenticationResult execute() throws Exception {
Authority requestAuthority = clientApplication.authenticationAuthority;
requestAuthority = getAuthorityWithPrefNetworkHost(requestAuthority.authority());
DeviceCode deviceCode = getDeviceCode(requestAuthority);
return acquireTokenWithDeviceCode(deviceCode, requestAuthority);
}
private DeviceCode getDeviceCode(Authority requestAuthority) {
DeviceCode deviceCode = deviceCodeFlowRequest.acquireDeviceCode(
requestAuthority.deviceCodeEndpoint(),
clientApplication.clientId(),
deviceCodeFlowRequest.headers().getReadonlyHeaderMap(),
this.clientApplication.serviceBundle());
deviceCodeFlowRequest.parameters().deviceCodeConsumer().accept(deviceCode);
return deviceCode;
}
private AuthenticationResult acquireTokenWithDeviceCode(DeviceCode deviceCode,
Authority requestAuthority) throws Exception {
deviceCodeFlowRequest.createAuthenticationGrant(deviceCode);
long expirationTimeInSeconds = getCurrentSystemTimeInSeconds() + deviceCode.expiresIn();
AcquireTokenByAuthorizationGrantSupplier acquireTokenByAuthorisationGrantSupplier =
new AcquireTokenByAuthorizationGrantSupplier(
clientApplication,
deviceCodeFlowRequest,
requestAuthority);
while (getCurrentSystemTimeInSeconds() < expirationTimeInSeconds) {
if (deviceCodeFlowRequest.futureReference().get().isCancelled()) {
throw new InterruptedException("Device code flow was cancelled before acquiring a token");
}
if (deviceCodeFlowRequest.futureReference().get().isCompletedExceptionally()) {
throw new InterruptedException("Device code flow had an exception before acquiring a token");
}
try {
return acquireTokenByAuthorisationGrantSupplier.execute();
} catch (MsalServiceException ex) {
if (ex.errorCode() != null && ex.errorCode().equals(AUTHORIZATION_PENDING)) {
TimeUnit.SECONDS.sleep(deviceCode.interval());
} else {
throw ex;
}
}
}
String message = "Expired Device code";
LOG.error(LogHelper.createMessage(message, deviceCodeFlowRequest.requestContext().correlationId()));
throw new MsalClientException(message, AuthenticationErrorCode.CODE_EXPIRED,
deviceCodeFlowRequest.requestContext().correlationId());
}
private Long getCurrentSystemTimeInSeconds() {
return TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis());
}
}