-
Notifications
You must be signed in to change notification settings - Fork 22
/
Program.java
222 lines (184 loc) · 9.06 KB
/
Program.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
// 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.samples.secureappmodel.cpvsample;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.Arrays;
import java.util.Properties;
import com.fasterxml.jackson.core.type.TypeReference;
import com.microsoft.azure.PagedList;
import com.microsoft.azure.management.Azure;
import com.microsoft.azure.management.resources.ResourceGroup;
import com.microsoft.graph.models.extensions.Domain;
import com.microsoft.graph.models.extensions.IGraphServiceClient;
import com.microsoft.graph.requests.extensions.GraphServiceClient;
import com.microsoft.graph.requests.extensions.IDomainCollectionPage;
import com.microsoft.store.partnercenter.IAadLoginHandler;
import com.microsoft.store.partnercenter.IPartner;
import com.microsoft.store.partnercenter.IPartnerCredentials;
import com.microsoft.store.partnercenter.PartnerService;
import com.microsoft.store.partnercenter.extensions.PartnerCredentials;
import com.microsoft.store.partnercenter.models.partners.OrganizationProfile;
import com.microsoft.store.samples.secureappmodel.cpvsample.models.ApplicationConsent;
import com.microsoft.store.samples.secureappmodel.cpvsample.models.ApplicationGrant;
import com.microsoft.store.samples.secureappmodel.cpvsample.security.AccessTokenProvider;
import com.microsoft.store.samples.secureappmodel.cpvsample.security.AzureTokenProvider;
import com.microsoft.store.samples.secureappmodel.cpvsample.security.GraphAuthenticationProvider;
import com.microsoft.store.samples.secureappmodel.cpvsample.security.IAccessTokenProvider;
import com.microsoft.store.samples.secureappmodel.cpvsample.security.SecureLoginHandler;
import org.apache.commons.lang3.StringUtils;
/**
* Sample application that demonstrates how a Control Panel Vendor should
* utilize the secure application model to interact with the Partner Center
* API, Microsoft Azure Resource Manager, and Microsoft Graph.
*/
public class Program
{
/**
* Entry point for the console application.
*
* @param args Arguments passed from the command line.
*/
public static void main(String args[])
{
IAccessTokenProvider tokenProvider;
Properties properties;
String customerId;
String partnerId;
try
{
partnerId = "SPECIFY-THE-PARTNER-TENANT-ID-HERE";
customerId = "SPECIFY-THE-CUSTOMER-TENANT-ID-HERE";
properties = new Properties();
properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream(PropertyName.APP_PROPERTIES));
tokenProvider = new AccessTokenProvider(properties);
RunPartnerCenterTask(properties, tokenProvider, partnerId, customerId);
/**
* Uncomment the following if you want to run the Azure task sample. Please note that this requires
* the Azure AD application to have the Windows Azure Service Management API configured as one of
* the required permissions.
*/
// RunAzureTask(properties, tokenProvider, "SPECIFY-THE-IDENTIFIER-OF-CUSTOMER");
/**
* Uncomment the following if you want to run the Microsoft Graph task sample. Please note that this
* requires the Azure AD application to have Microsoft Graph configured as one of the required permissions.
* If you are receive an error stating the identity of the calling application could not be established,
* ensure the Azure AD application is configured to be multi-tenanted and has been configured for pre-consent.
*/
// RunGraphTask(properties, tokenProvider, "SPECIFY-THE-IDENTIFIER-OF-CUSTOMER");
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
/**
* Demonstrates how a partner can interact with Microsoft Azure Resource Manager in the context of the customer.
*
* @param properties The configuration information stored in the application.properties file.
* @param tokenProvider Provides the ability to request access tokens.
* @param customerId Identifier of the customer.
*/
private static void RunAzureTask(Properties properties, IAccessTokenProvider tokenProvider, String customerId)
{
Azure.Authenticated azureAuth;
Azure azure;
PagedList<ResourceGroup> resourceGroups;
if(properties == null)
{
throw new IllegalArgumentException("properties cannot be null");
}
if(tokenProvider == null)
{
throw new IllegalArgumentException("tokenProvider cannot be null");
}
if(StringUtils.isEmpty(customerId))
{
throw new IllegalArgumentException("customerId is empty or null");
}
try
{
azureAuth = Azure.authenticate(new AzureTokenProvider(properties, tokenProvider, customerId));
azure = azureAuth.withDefaultSubscription();
resourceGroups = azure.resourceGroups().list();
resourceGroups.forEach(group -> {
System.out.println(group.name());
});
}
catch(IOException ex)
{
ex.printStackTrace();
}
}
/**
* Demonstrates how a partner can interact with Microsoft Graph in the context of the customer.
*
* @param properties The configuration information stored in the application.properties file.
* @param tokenProvider Provides the ability to request access tokens.
* @param customerId Identifier of the customer.
*/
private static void RunGraphTask(Properties properties, IAccessTokenProvider tokenProvider, String customerId)
{
if(properties == null)
{
throw new IllegalArgumentException("properties cannot be null");
}
if(tokenProvider == null)
{
throw new IllegalArgumentException("tokenProvider cannot be null");
}
if(StringUtils.isEmpty(customerId))
{
throw new IllegalArgumentException("customerId is empty or null");
}
IGraphServiceClient graphClient = GraphServiceClient
.builder()
.authenticationProvider(new GraphAuthenticationProvider(properties, tokenProvider, customerId))
.buildClient();
IDomainCollectionPage domains = graphClient.domains().buildRequest().get();
for(Domain domain : domains.getCurrentPage())
{
System.out.println(domain.id);
}
}
private static void RunPartnerCenterTask(Properties properties, IAccessTokenProvider tokenProvider, String partnerId, String customerId)
{
IAadLoginHandler loginHandler = new SecureLoginHandler(properties, tokenProvider);
IPartnerCredentials credentials = PartnerCredentials.getInstance().generateByUserCredentials(
properties.getProperty(PropertyName.PARTNER_CENTER_CLIENT_ID),
loginHandler.authenticate(),
loginHandler);
IPartner partnerOperations = PartnerService.getInstance().createPartnerOperations(credentials);
if(StringUtils.isNoneBlank(customerId))
{
ApplicationGrant azureAppGrant = new ApplicationGrant();
azureAppGrant.setEnterpriseApplication("797f4846-ba00-4fd7-ba43-dac1f8f63013");
azureAppGrant.setScope("user_impersonation");
ApplicationGrant graphAppGrant = new ApplicationGrant();
graphAppGrant.setEnterpriseApplication("00000002-0000-0000-c000-000000000000");
graphAppGrant.setScope("Domain.ReadWrite.All,User.ReadWrite.All,Directory.Read.All");
ApplicationConsent consent = new ApplicationConsent();
consent.setApplicationGrants(Arrays.asList(azureAppGrant, graphAppGrant));
consent.setApplicationId(properties.getProperty(PropertyName.PARTNER_CENTER_CLIENT_ID));
consent.setDisplayName(properties.getProperty(PropertyName.PARTNER_CENTER_DISPLAY_NAME));
// Deletes the existing grant into the customer it is present.
partnerOperations.getServiceClient().delete(
partnerOperations,
new TypeReference<ApplicationConsent>(){},
MessageFormat.format(
"customers/{0}/applicationconsents/{1}",
customerId,
properties.getProperty(PropertyName.PARTNER_CENTER_CLIENT_ID)));
// Consent to the defined applications and the respective scopes.
partnerOperations.getServiceClient().post(
partnerOperations,
new TypeReference<ApplicationConsent>(){},
MessageFormat.format(
"customers/{0}/applicationconsents",
customerId),
consent);
}
OrganizationProfile profile = partnerOperations.getProfiles().getOrganizationProfile().get();
System.out.println(profile.getCompanyName());
}
}