-
Notifications
You must be signed in to change notification settings - Fork 22
/
AadUserLoginHandler.java
104 lines (87 loc) · 3.19 KB
/
AadUserLoginHandler.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See the LICENSE file in the project root for full license information.
package com.microsoft.store.partnercenter.samples;
import java.io.IOException;
import java.util.Collections;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
import com.microsoft.aad.msal4j.DeviceCode;
import com.microsoft.aad.msal4j.DeviceCodeFlowParameters;
import com.microsoft.aad.msal4j.IAuthenticationResult;
import com.microsoft.aad.msal4j.PublicClientApplication;
import com.microsoft.store.partnercenter.AuthenticationToken;
import com.microsoft.store.partnercenter.IAadLoginHandler;
import com.microsoft.store.partnercenter.samples.configuration.ConfigurationHolder;
import org.joda.time.DateTime;
public class AadUserLoginHandler
implements IAadLoginHandler
{
/**
* Logs into Azure active directory.
*
* @return The authentication result.
*/
@Override
public AuthenticationToken authenticate()
{
CompletableFuture<IAuthenticationResult> future;
Consumer<DeviceCode> deviceCodeConsumer;
IAuthenticationResult authResult = null;
PublicClientApplication app;
String authority;
String clientId;
String commonDomain;
String scope;
try
{
authority = ConfigurationHolder
.getInstance()
.getConfiguration()
.getPartnerServiceSettings()
.get("AuthenticationAuthorityEndpoint");
clientId = ConfigurationHolder
.getInstance()
.getConfiguration()
.getUserAuthentication()
.get("ClientId");
commonDomain = ConfigurationHolder
.getInstance()
.getConfiguration()
.getPartnerServiceSettings()
.get("CommonDomain");
scope = ConfigurationHolder
.getInstance()
.getConfiguration()
.getUserAuthentication()
.get("Scope");
app = PublicClientApplication.builder(clientId)
.authority(authority + "/" + commonDomain)
.build();
deviceCodeConsumer = (DeviceCode deviceCode) -> {
System.out.println(deviceCode.message());
};
future = app.acquireToken(
DeviceCodeFlowParameters.builder(
Collections.singleton(scope),
deviceCodeConsumer)
.build());
future.handle((result, ex) -> {
if(ex != null)
{
ex.printStackTrace();
}
return result;
});
authResult = future.join();
if ( authResult == null )
{
throw new NullPointerException( "authentication result was null" );
}
}
catch(IOException ex)
{
ex.printStackTrace();
}
return new AuthenticationToken(authResult.accessToken(), new DateTime(authResult.expiresOnDate()));
}
}