forked from pnp/powershell
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRegisterAzureADApp.cs
More file actions
764 lines (669 loc) · 39.6 KB
/
RegisterAzureADApp.cs
File metadata and controls
764 lines (669 loc) · 39.6 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
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
using PnP.Framework;
using PnP.PowerShell.Commands.Base;
using PnP.PowerShell.Commands.Enums;
using PnP.PowerShell.Commands.Model;
using PnP.PowerShell.Commands.Utilities;
using PnP.PowerShell.Commands.Utilities.REST;
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.IO;
using System.Linq;
using System.Management.Automation;
using System.Net.Http;
using System.Security;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Threading;
using System.Threading.Tasks;
using TextCopy;
using OperatingSystem = PnP.PowerShell.Commands.Utilities.OperatingSystem;
using Resources = PnP.PowerShell.Commands.Properties.Resources;
namespace PnP.PowerShell.Commands.AzureAD
{
[Cmdlet(VerbsLifecycle.Register, "PnPAzureADApp")]
[Alias("Register-PnPEntraIDApp")]
public class RegisterAzureADApp : BasePSCmdlet, IDynamicParameters
{
private const string ParameterSet_EXISTINGCERT = "Existing Certificate";
private const string ParameterSet_NEWCERT = "Generate Certificate";
private CancellationTokenSource cancellationTokenSource;
[Parameter(Mandatory = true, ParameterSetName = ParameterAttribute.AllParameterSets)]
public string ApplicationName;
[Parameter(Mandatory = true, ParameterSetName = ParameterAttribute.AllParameterSets)]
public string Tenant;
[Parameter(Mandatory = true, ParameterSetName = ParameterSet_EXISTINGCERT)]
public string CertificatePath;
[Parameter(Mandatory = false, Position = 0, ParameterSetName = ParameterSet_NEWCERT)]
public string CommonName;
[Parameter(Mandatory = false, Position = 1, ParameterSetName = ParameterSet_NEWCERT)]
public string Country = String.Empty;
[Parameter(Mandatory = false, Position = 2, ParameterSetName = ParameterSet_NEWCERT)]
public string State = string.Empty;
[Parameter(Mandatory = false, Position = 3, ParameterSetName = ParameterSet_NEWCERT)]
public string Locality = string.Empty;
[Parameter(Mandatory = false, Position = 4, ParameterSetName = ParameterSet_NEWCERT)]
public string Organization = string.Empty;
[Parameter(Mandatory = false, Position = 5, ParameterSetName = ParameterSet_NEWCERT)]
public string OrganizationUnit = string.Empty;
[Parameter(Mandatory = false, Position = 7, ParameterSetName = ParameterSet_NEWCERT)]
public int ValidYears = 10;
[Parameter(Mandatory = false, Position = 8, ParameterSetName = ParameterSet_NEWCERT)]
[Parameter(Mandatory = false, Position = 8, ParameterSetName = ParameterSet_EXISTINGCERT)]
public SecureString CertificatePassword;
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_NEWCERT)]
public string OutPath;
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_NEWCERT)]
public StoreLocation Store;
[Parameter(Mandatory = false)]
public AzureEnvironment AzureEnvironment = AzureEnvironment.Production;
[Parameter(Mandatory = false)]
public SwitchParameter DeviceLogin;
[Parameter(Mandatory = false)]
public string LogoFilePath;
[Parameter(Mandatory = false)]
public SwitchParameter SkipCertCreation;
[Parameter(Mandatory = false)]
public string MicrosoftGraphEndPoint;
[Parameter(Mandatory = false)]
public string EntraIDLoginEndPoint;
[Parameter(Mandatory = false)]
public EntraIDSignInAudience SignInAudience;
protected override void ProcessRecord()
{
if (ParameterSpecified(nameof(Store)) && !OperatingSystem.IsWindows())
{
throw new PSArgumentException("The Store parameter is only supported on Microsoft Windows");
}
if (!string.IsNullOrWhiteSpace(OutPath))
{
if (!Path.IsPathRooted(OutPath))
{
OutPath = Path.Combine(SessionState.Path.CurrentFileSystemLocation.Path, OutPath);
}
}
else
{
OutPath = SessionState.Path.CurrentFileSystemLocation.Path;
}
var redirectUri = "http://localhost";
// if (ParameterSpecified(nameof(DeviceLogin)) || OperatingSystem.IsMacOS())
if (ParameterSpecified(nameof(DeviceLogin)) || OperatingSystem.IsMacOS())
{
redirectUri = "https://pnp.github.io/powershell/consent.html";
}
var messageWriter = new CmdletMessageWriter(this);
cancellationTokenSource = new CancellationTokenSource();
CancellationToken cancellationToken = cancellationTokenSource.Token;
var loginEndPoint = string.Empty;
using (var authenticationManager = new AuthenticationManager())
{
loginEndPoint = authenticationManager.GetAzureADLoginEndPoint(AzureEnvironment) ?? EntraIDLoginEndPoint;
}
var permissionScopes = new PermissionScopes();
var scopes = new List<PermissionScope>();
if (this.Scopes != null)
{
foreach (var scopeIdentifier in this.Scopes)
{
PermissionScope scope = null;
scope = permissionScopes.GetScope(PermissionScopes.ResourceAppId_Graph, scopeIdentifier.Replace("MSGraph.", ""), "Role");
if (scope == null)
{
scope = permissionScopes.GetScope(PermissionScopes.ResourceAppId_SPO, scopeIdentifier.Replace("SPO.", ""), "Role");
}
if (scope == null)
{
scope = permissionScopes.GetScope(PermissionScopes.ResourceAppID_O365Management, scopeIdentifier.Replace("O365.", ""), "Role");
}
if (scope != null)
{
scopes.Add(scope);
}
}
}
else
{
if (GraphApplicationPermissions != null)
{
foreach (var scopeIdentifier in this.GraphApplicationPermissions)
{
scopes.Add(permissionScopes.GetScope(PermissionScopes.ResourceAppId_Graph, scopeIdentifier, "Role"));
}
}
if (GraphDelegatePermissions != null)
{
foreach (var scopeIdentifier in this.GraphDelegatePermissions)
{
scopes.Add(permissionScopes.GetScope(PermissionScopes.ResourceAppId_Graph, scopeIdentifier, "Scope"));
}
}
if (SharePointApplicationPermissions != null)
{
foreach (var scopeIdentifier in this.SharePointApplicationPermissions)
{
scopes.Add(permissionScopes.GetScope(PermissionScopes.ResourceAppId_SPO, scopeIdentifier, "Role"));
}
}
if (SharePointDelegatePermissions != null)
{
foreach (var scopeIdentifier in this.SharePointDelegatePermissions)
{
scopes.Add(permissionScopes.GetScope(PermissionScopes.ResourceAppId_SPO, scopeIdentifier, "Scope"));
}
}
}
if (!scopes.Any())
{
messageWriter.LogWarning("No permissions specified, using default permissions");
scopes.Add(permissionScopes.GetScope(PermissionScopes.ResourceAppId_SPO, "Sites.FullControl.All", "Role")); // AppOnly
scopes.Add(permissionScopes.GetScope(PermissionScopes.ResourceAppId_SPO, "AllSites.FullControl", "Scope")); // AppOnly
scopes.Add(permissionScopes.GetScope(PermissionScopes.ResourceAppId_Graph, "Group.ReadWrite.All", "Role")); // AppOnly
scopes.Add(permissionScopes.GetScope(PermissionScopes.ResourceAppId_SPO, "User.ReadWrite.All", "Role")); // AppOnly
scopes.Add(permissionScopes.GetScope(PermissionScopes.ResourceAppId_Graph, "User.ReadWrite.All", "Role")); // AppOnly
}
var record = new PSObject();
string token = GetAuthToken(messageWriter);
if (!string.IsNullOrEmpty(token))
{
X509Certificate2 cert = null;
if (!SkipCertCreation)
{
cert = GetCertificate(record);
}
var httpClient = Framework.Http.PnPHttpClient.Instance.GetHttpClient();
if (!AppExists(ApplicationName, httpClient, token))
{
var azureApp = CreateApp(loginEndPoint, httpClient, token, cert, redirectUri, scopes);
record.Properties.Add(new PSVariableProperty(new PSVariable("AzureAppId/ClientId", azureApp.AppId)));
if (cert != null)
{
record.Properties.Add(new PSVariableProperty(new PSVariable("Certificate Thumbprint", cert.GetCertHashString())));
byte[] certPfxData = cert.Export(X509ContentType.Pfx, CertificatePassword);
var base64String = Convert.ToBase64String(certPfxData);
record.Properties.Add(new PSVariableProperty(new PSVariable("Base64Encoded", base64String)));
}
StartConsentFlow(loginEndPoint, azureApp, redirectUri, token, httpClient, record, messageWriter, scopes);
if (ParameterSpecified(nameof(LogoFilePath)) && !string.IsNullOrEmpty(LogoFilePath))
{
SetLogo(azureApp, token);
}
}
else
{
throw new PSInvalidOperationException($"The application with name {ApplicationName} already exists.");
}
}
}
protected override void StopProcessing()
{
cancellationTokenSource.Cancel();
}
private static object GetScopesPayload(List<PermissionScope> scopes)
{
var resourcePermissions = new List<AppResource>();
var distinctResources = scopes.GroupBy(s => s.resourceAppId).Select(r => r.First()).ToList();
foreach (var distinctResource in distinctResources)
{
var id = distinctResource.resourceAppId;
var appResource = new AppResource() { Id = id };
appResource.ResourceAccess.AddRange(scopes.Where(s => s.resourceAppId == id).ToList());
resourcePermissions.Add(appResource);
}
return resourcePermissions;
}
protected IEnumerable<string> Scopes
{
get
{
if (ParameterSpecified(nameof(Scopes)) && MyInvocation.BoundParameters["Scopes"] != null)
{
return MyInvocation.BoundParameters["Scopes"] as string[];
}
else
{
return null;
}
}
}
protected IEnumerable<string> GraphApplicationPermissions
{
get
{
if (ParameterSpecified(nameof(GraphApplicationPermissions)) && MyInvocation.BoundParameters[nameof(GraphApplicationPermissions)] != null)
{
return MyInvocation.BoundParameters[nameof(GraphApplicationPermissions)] as string[];
}
else
{
return null;
}
}
}
protected IEnumerable<string> GraphDelegatePermissions
{
get
{
if (ParameterSpecified(nameof(GraphDelegatePermissions)) && MyInvocation.BoundParameters[nameof(GraphDelegatePermissions)] != null)
{
return MyInvocation.BoundParameters[nameof(GraphDelegatePermissions)] as string[];
}
else
{
return null;
}
}
}
protected IEnumerable<string> SharePointApplicationPermissions
{
get
{
if (ParameterSpecified(nameof(SharePointApplicationPermissions)) && MyInvocation.BoundParameters[nameof(SharePointApplicationPermissions)] != null)
{
return MyInvocation.BoundParameters[nameof(SharePointApplicationPermissions)] as string[];
}
else
{
return null;
}
}
}
protected IEnumerable<string> SharePointDelegatePermissions
{
get
{
if (ParameterSpecified(nameof(SharePointDelegatePermissions)) && MyInvocation.BoundParameters[nameof(SharePointDelegatePermissions)] != null)
{
return MyInvocation.BoundParameters[nameof(SharePointDelegatePermissions)] as string[];
}
else
{
return null;
}
}
}
protected IEnumerable<string> O365ManagementApplicationPermissions
{
get
{
if (ParameterSpecified(nameof(O365ManagementApplicationPermissions)) && MyInvocation.BoundParameters[nameof(O365ManagementApplicationPermissions)] != null)
{
return MyInvocation.BoundParameters[nameof(O365ManagementApplicationPermissions)] as string[];
}
else
{
return null;
}
}
}
protected IEnumerable<string> O365ManagementDelegatePermissions
{
get
{
if (ParameterSpecified(nameof(O365ManagementDelegatePermissions)) && MyInvocation.BoundParameters[nameof(O365ManagementDelegatePermissions)] != null)
{
return MyInvocation.BoundParameters[nameof(O365ManagementDelegatePermissions)] as string[];
}
else
{
return null;
}
}
}
public object GetDynamicParameters()
{
// var classAttribute = this.GetType().GetCustomAttributes(false).FirstOrDefault(a => a is PropertyLoadingAttribute);
const string parameterName = "Scopes";
var parameterDictionary = new RuntimeDefinedParameterDictionary();
var attributeCollection = new System.Collections.ObjectModel.Collection<Attribute>();
// Scopes
var parameterAttribute = new ParameterAttribute
{
ValueFromPipeline = false,
ValueFromPipelineByPropertyName = false,
Mandatory = false
};
attributeCollection.Add(parameterAttribute);
attributeCollection.Add(new ObsoleteAttribute("Use either -GraphApplicationPermissions, -GraphDelegatePermissions, -SharePointApplicationPermissions or -SharePointDelegatePermissions"));
var identifiers = new PermissionScopes().GetIdentifiers();
var validateSetAttribute = new ValidateSetAttribute(identifiers);
attributeCollection.Add(validateSetAttribute);
var runtimeParameter = new RuntimeDefinedParameter(parameterName, typeof(string[]), attributeCollection);
parameterDictionary.Add(parameterName, runtimeParameter);
// Graph
parameterDictionary.Add("GraphApplicationPermissions", GetParameter("GraphApplicationPermissions", PermissionScopes.ResourceAppId_Graph, "Role"));
parameterDictionary.Add("GraphDelegatePermissions", GetParameter("GraphDelegatePermissions", PermissionScopes.ResourceAppId_Graph, "Scope"));
// SharePoint
parameterDictionary.Add("SharePointApplicationPermissions", GetParameter("SharePointApplicationPermissions", PermissionScopes.ResourceAppId_SPO, "Role"));
parameterDictionary.Add("SharePointDelegatePermissions", GetParameter("SharePointDelegatePermissions", PermissionScopes.ResourceAppId_SPO, "Scope"));
// O365 Management
parameterDictionary.Add("O365ManagementApplicationPermissions", GetParameter("O365ManagementApplicationPermissions", PermissionScopes.ResourceAppID_O365Management, "Role"));
parameterDictionary.Add("O365ManagementDelegatePermissions", GetParameter("O365ManagementDelegatePermissions", PermissionScopes.ResourceAppID_O365Management, "Scope"));
return parameterDictionary;
}
private RuntimeDefinedParameter GetParameter(string parameterName, string resourceAppId, string type)
{
var attributeCollection = new System.Collections.ObjectModel.Collection<Attribute>();
var parameterAttribute = new ParameterAttribute
{
ValueFromPipeline = false,
ValueFromPipelineByPropertyName = false,
Mandatory = false
};
attributeCollection.Add(parameterAttribute);
var validateSetAttribute = new ValidateSetAttribute(new PermissionScopes().GetIdentifiers(resourceAppId, type));
attributeCollection.Add(validateSetAttribute);
var parameter = new RuntimeDefinedParameter(parameterName, typeof(string[]), attributeCollection);
return parameter;
}
private string GetAuthToken(CmdletMessageWriter messageWriter)
{
var token = string.Empty;
if (DeviceLogin.IsPresent)
{
Task.Factory.StartNew(() =>
{
token = AzureAuthHelper.AuthenticateDeviceLogin(cancellationTokenSource, messageWriter, AzureEnvironment, MicrosoftGraphEndPoint);
if (token == null)
{
messageWriter.LogWarning("Operation cancelled or no token retrieved.");
}
messageWriter.Stop();
});
messageWriter.Start();
}
else
{
Task.Factory.StartNew(() =>
{
token = AzureAuthHelper.AuthenticateInteractive(cancellationTokenSource, messageWriter, AzureEnvironment, Tenant, MicrosoftGraphEndPoint);
if (token == null)
{
messageWriter.LogWarning("Operation cancelled or no token retrieved.");
}
messageWriter.Stop();
});
messageWriter.Start();
}
return token;
}
private X509Certificate2 GetCertificate(PSObject record)
{
X509Certificate2 cert = null;
if (ParameterSetName == ParameterSet_EXISTINGCERT)
{
if (!Path.IsPathRooted(CertificatePath))
{
CertificatePath = Path.Combine(SessionState.Path.CurrentFileSystemLocation.Path, CertificatePath);
}
// Ensure a file exists at the provided CertificatePath
if (!File.Exists(CertificatePath))
{
throw new PSArgumentException(string.Format(Resources.CertificateNotFoundAtPath, CertificatePath), nameof(CertificatePath));
}
try
{
cert = new X509Certificate2(CertificatePath, CertificatePassword, X509KeyStorageFlags.Exportable | X509KeyStorageFlags.UserKeySet | X509KeyStorageFlags.PersistKeySet);
}
catch (CryptographicException e) when (e.Message.Contains("The specified password is not correct"))
{
throw new PSArgumentNullException(nameof(CertificatePassword), string.Format(Resources.PrivateKeyCertificateImportFailedPasswordIncorrect, nameof(CertificatePassword)));
}
// Ensure the certificate at the provided CertificatePath holds a private key
if (!cert.HasPrivateKey)
{
throw new PSArgumentException(string.Format(Resources.CertificateAtPathHasNoPrivateKey, CertificatePath), nameof(CertificatePath));
}
}
else
{
if (!MyInvocation.BoundParameters.ContainsKey("CommonName"))
{
CommonName = ApplicationName;
}
DateTime validFrom = DateTime.Today;
DateTime validTo = validFrom.AddYears(ValidYears);
cert = CertificateHelper.CreateSelfSignedCertificate(CommonName, Country, State, Locality, Organization, OrganizationUnit, CertificatePassword, CommonName, validFrom, validTo, Array.Empty<string>());
if (Directory.Exists(OutPath))
{
string pfxPath = Path.Combine(OutPath, $"{ApplicationName}.pfx");
string cerPath = Path.Combine(OutPath, $"{ApplicationName}.cer");
byte[] certPfxData = cert.Export(X509ContentType.Pfx, CertificatePassword);
File.WriteAllBytes(pfxPath, certPfxData);
record.Properties.Add(new PSVariableProperty(new PSVariable("Pfx file", pfxPath)));
byte[] certCerData = cert.Export(X509ContentType.Cert);
File.WriteAllBytes(cerPath, certCerData);
record.Properties.Add(new PSVariableProperty(new PSVariable("Cer file", cerPath)));
}
if (ParameterSpecified(nameof(Store)))
{
if (OperatingSystem.IsWindows())
{
using (var store = new X509Store("My", Store))
{
store.Open(OpenFlags.ReadWrite);
store.Add(cert);
store.Close();
}
Host.UI.WriteLine(ConsoleColor.Yellow, Host.UI.RawUI.BackgroundColor, "Certificate added to store");
}
}
}
return cert;
}
private bool AppExists(string appName, HttpClient httpClient, string token)
{
Host.UI.Write(ConsoleColor.Yellow, Host.UI.RawUI.BackgroundColor, $"Checking if application '{appName}' does not exist yet...");
var graphEndpoint = $"https://{AuthenticationManager.GetGraphEndPoint(AzureEnvironment)}";
if (AzureEnvironment == AzureEnvironment.Custom)
{
graphEndpoint = Environment.GetEnvironmentVariable("MicrosoftGraphEndPoint", EnvironmentVariableTarget.Process) ?? MicrosoftGraphEndPoint;
}
var azureApps = RestHelper.Get<RestResultCollection<AzureADApp>>(httpClient, $"{graphEndpoint}/v1.0/applications?$filter=displayName eq '{appName}'&$select=Id", token);
if (azureApps != null && azureApps.Items.Any())
{
Host.UI.WriteLine();
return true;
}
Host.UI.WriteLine(ConsoleColor.Green, Host.UI.RawUI.BackgroundColor, $"Success. Application '{appName}' can be registered.");
return false;
}
private AzureADApp CreateApp(string loginEndPoint, HttpClient httpClient, string token, X509Certificate2 cert, string redirectUri, List<PermissionScope> scopes)
{
var scopesPayload = GetScopesPayload(scopes);
var redirectUris = new List<string>() { $"{loginEndPoint}/common/oauth2/nativeclient", redirectUri };
if (redirectUri != "http://localhost")
{
redirectUris.Add("http://localhost");
}
string audience = "AzureADMyOrg";
if (ParameterSpecified(nameof(SignInAudience)))
{
audience = SignInAudience.ToString();
}
dynamic payload = new ExpandoObject();
payload.isFallbackPublicClient = true;
payload.displayName = ApplicationName;
payload.signInAudience = audience;
payload.publicClient = new { redirectUris = redirectUris.ToArray() };
payload.requiredResourceAccess = scopesPayload;
if (cert != null)
{
var expirationDate = cert.NotAfter.ToUniversalTime();
var startDate = cert.NotBefore.ToUniversalTime();
payload.keyCredentials = new[] {
new {
customKeyIdentifier = cert.GetCertHashString(),
endDateTime = expirationDate,
keyId = Guid.NewGuid().ToString(),
startDateTime = startDate,
type= "AsymmetricX509Cert",
usage= "Verify",
key = Convert.ToBase64String(cert.GetRawCertData()),
displayName = cert.Subject,
}
};
}
var graphEndpoint = $"https://{AuthenticationManager.GetGraphEndPoint(AzureEnvironment)}";
if (AzureEnvironment == AzureEnvironment.Custom)
{
graphEndpoint = Environment.GetEnvironmentVariable("MicrosoftGraphEndPoint", EnvironmentVariableTarget.Process) ?? MicrosoftGraphEndPoint;
}
var azureApp = RestHelper.Post<AzureADApp>(httpClient, $"{graphEndpoint}/v1.0/applications", token, payload);
var retry = true;
var iteration = 0;
while (retry)
{
try
{
// Add redirectURI to support windows broker
dynamic redirectUriPayload = new ExpandoObject();
redirectUris.Add($"ms-appx-web://microsoft.aad.brokerplugin/{azureApp.AppId}");
redirectUriPayload.publicClient = new { redirectUris = redirectUris.ToArray() };
RestHelper.Patch(httpClient, $"{graphEndpoint}/v1.0/applications/{azureApp.Id}", token, redirectUriPayload);
retry = false;
}
catch (Exception)
{
Thread.Sleep(10000);
iteration++;
}
if (iteration > 3) // don't try more than 3 times
{
retry = false;
}
}
if (azureApp != null)
{
Host.UI.WriteLine(ConsoleColor.Yellow, Host.UI.RawUI.BackgroundColor, $"App {azureApp.DisplayName} with id {azureApp.AppId} created.");
}
return azureApp;
}
private void StartConsentFlow(string loginEndPoint, AzureADApp azureApp, string redirectUri, string token, HttpClient httpClient, PSObject record, CmdletMessageWriter messageWriter, List<PermissionScope> scopes)
{
var htmlMessageConsentSuccess = $"<html lang=en><meta charset=utf-8><title>PnP PowerShell - Consent</title><meta content=\"width=device-width,initial-scale=1\"name=viewport><style>html{{height:100%}}.message-container{{flex-grow:1;display:flex;align-items:center;justify-content:center;margin:0 30px}}body{{box-sizing:border-box;min-height:100%;display:flex;flex-direction:column;color:#fff;font-family:\"Segoe UI\",\"Helvetica Neue\",Helvetica,Arial,sans-serif;background-color:#2c2c32;margin:0;padding:15px 30px}}.message{{font-weight:300;font-size:1.4rem}}.branding{{background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g==);background-repeat:no-repeat;padding-left:26px;font-size:20px;letter-spacing:-.04rem;font-weight:400;height:26px;color:#fff;background-position:left center;text-decoration:none}}</style><a class=branding href=https://pnp.github.io/powershell>PnP PowerShell</a><div class=message-container><div class=message>You successfully provided consent now and can close this page.</div></div>";
var htmlMessageConsentFailed = $"<html lang=en><meta charset=utf-8><title>PnP PowerShell - Consent</title><meta content=\"width=device-width,initial-scale=1\"name=viewport><style>html{{height:100%}}.error-text{{color:red;font-size:1rem}}.message-container{{flex-grow:1;display:flex;align-items:center;justify-content:center;margin:0 30px}}body{{box-sizing:border-box;min-height:100%;display:flex;flex-direction:column;color:#fff;font-family:\"Segoe UI\",\"Helvetica Neue\",Helvetica,Arial,sans-serif;background-color:#2c2c32;margin:0;padding:15px 30px}}.message{{font-weight:300;font-size:1.4rem}}.branding{{background-image:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAaCAYAAAC3g3x9AAAABHNCSVQICAgIfAhkiAAABhhJREFUSIl1lXuMVHcVxz/3d+/MnXtnX8PudqmyLx672+WtCKWx8irUloKJtY2xsdmo1T9MWmM0Go0JarEoRSQh6R8KFRuiVm3RSAMSCsijsmWXx2KB3QWW2cfszs7uPO7M3DtzHz//QJoA4Zucf84535PvyUnOFx4Ay7JW2bb9muu6Jz3Ps13XLZZKpUO5XO7xB3EeiLGxMdN13fcymbS8g4nxhAyCQBYKBd+27Z89iKvdmxgYGNDr6+v/6ZbLazzXo1Qq0fvhWVKTSTo651NbVy/ylvUTx3H0SCTyg3v5yr0Jx3F+q2naN4ZuXCeTSXPovYMsXLQIx3aYTk0yPZXi5e/9EDMalbZtvxiNRvcDKIoi75NbKBS+EARBIKWUvee65Ttv/0levHBBFosFeePGdTkyHJc7f/WaHOy/JqWU0vO8UmpysmhZ1rfvW/nmzZsRXdd32batpNPTjI2M0DF/AT2953l9xw5SqSnWrl3LU09v4sL5HurqHyKdSYcbG5umUqnU7+9b2XGc7+i6vhPg9L+PUx2rZWBwkG3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bpim3tw6m4rKSuK3hqiI97HXvYpb9OjxK7k8o5lccoLY2DBBySFwG+hcsJCp1CRSSqSU0nGc3aK+vr7fdd1vAV7EMAiQtMyZS7WTBtdDzmzB+8rLtG18ls0/387i519AAYaHRtm95y2yVh7f9/E8b7iiouKEBmAYxr5cLjdgGMaO2tq6R/uvD/Hqu5epS8VYs+opFi1YjGPbRAyD0ObnGXd0suks8fgIuXyB6uoqNE1rzGQyjXd5im3bXbquv3ns5GnePXiE1uZGntv8eYS423qEEIRCIRQUNE0jWhEFyaWtW7cuvasxn88vDILAKxaLMpvLSitvSc/zPg7f9+X/LUdKKWUQBIHnedlyubwvmUzOvMsC7qBYLD4XCoV+rChKi5QyEQTBP3zfHxdCiCAIPEVRMr7vJwqFwmAul7P2798/tWXLluAO/38rUwksVQPdogAAAABJRU5ErkJggg==);background-repeat:no-repeat;height:26px;padding-left:26px;font-size:20px;letter-spacing:-.04rem;font-weight:400;color:#fff;background-position:left center;text-decoration:none}}</style><a class=branding href=https://pnp.github.io/powershell>PnP PowerShell</a><div class=message-container><div class=message>You failed to provide consent. Please try again. You can close this page.</div></div>";
var graphEndpoint = $"https://{AuthenticationManager.GetGraphEndPoint(AzureEnvironment)}";
if (AzureEnvironment == AzureEnvironment.Custom)
{
graphEndpoint = Environment.GetEnvironmentVariable("MicrosoftGraphEndPoint", EnvironmentVariableTarget.Process) ?? MicrosoftGraphEndPoint;
}
var resource = scopes.FirstOrDefault(s => s.resourceAppId == PermissionScopes.ResourceAppId_Graph) != null ? $"{graphEndpoint}/.default" : "https://microsoft.sharepoint-df.com/.default";
var consentUrl = $"{loginEndPoint}/{Tenant}/v2.0/adminconsent?client_id={azureApp.AppId}&scope={resource}&redirect_uri={redirectUri}";
var waitTime = 30;
var progressRecord = new ProgressRecord(1, "Please wait...", $"Waiting {waitTime} seconds to update Entra ID and launch consent flow");
for (var i = 0; i < waitTime; i++)
{
progressRecord.PercentComplete = Convert.ToInt32((Convert.ToDouble(i) / Convert.ToDouble(waitTime)) * 100);
WriteProgress(progressRecord);
Thread.Sleep(1000);
// Check if CTRL+C has been pressed and if so, abort the wait
if (Stopping)
{
Host.UI.WriteLine("Wait cancelled. You can provide consent manually by navigating to");
Host.UI.WriteLine(consentUrl);
break;
}
}
progressRecord.RecordType = ProgressRecordType.Completed;
WriteProgress(progressRecord);
if (!Stopping)
{
if (ParameterSpecified(nameof(DeviceLogin)))
{
using (var authManager = AuthenticationManager.CreateWithDeviceLogin(azureApp.AppId, Tenant, (deviceCodeResult) =>
{
try
{
ClipboardService.SetText(deviceCodeResult.UserCode);
}
catch
{
}
Host.UI.WriteWarningLine($"\n\nPlease login.\n\nWe opened a browser and navigated to {deviceCodeResult.VerificationUrl}\n\nEnter code: {deviceCodeResult.UserCode} (we copied this code to your clipboard)\n\nNOTICE: close the browser tab after you authenticated successfully to continue the process.");
BrowserHelper.OpenBrowserForInteractiveLogin(deviceCodeResult.VerificationUrl, BrowserHelper.FindFreeLocalhostRedirectUri(), cancellationTokenSource);
return Task.FromResult(0);
}, AzureEnvironment))
{
authManager.ClearTokenCache();
authManager.GetAccessToken(resource, Microsoft.Identity.Client.Prompt.Consent);
}
}
else
{
using (var authManager = AuthenticationManager.CreateWithInteractiveWebBrowserLogin(azureApp.AppId, (url, port) =>
{
BrowserHelper.OpenBrowserForInteractiveLogin(url, port, cancellationTokenSource);
}, Tenant, htmlMessageConsentSuccess, htmlMessageConsentFailed, azureEnvironment: AzureEnvironment, useWAM: false))
{
authManager.ClearTokenCache();
authManager.GetAccessToken(resource, Microsoft.Identity.Client.Prompt.Consent);
}
}
}
WriteObject(record);
}
private void SetLogo(AzureADApp azureApp, string token)
{
if (!Path.IsPathRooted(LogoFilePath))
{
LogoFilePath = Path.Combine(SessionState.Path.CurrentFileSystemLocation.Path, LogoFilePath);
}
if (File.Exists(LogoFilePath))
{
try
{
LogDebug("Setting the logo for the EntraID app");
var graphEndpoint = $"https://{AuthenticationManager.GetGraphEndPoint(AzureEnvironment)}";
if (AzureEnvironment == AzureEnvironment.Custom)
{
graphEndpoint = Environment.GetEnvironmentVariable("MicrosoftGraphEndPoint", EnvironmentVariableTarget.Process) ?? MicrosoftGraphEndPoint;
}
var endpoint = $"{graphEndpoint}/v1.0/applications/{azureApp.Id}/logo";
var bytes = File.ReadAllBytes(LogoFilePath);
var fileInfo = new FileInfo(LogoFilePath);
var mediaType = string.Empty;
switch (fileInfo.Extension.ToLower())
{
case ".jpg":
case ".jpeg":
{
mediaType = "image/jpeg";
break;
}
case ".gif":
{
mediaType = "image/gif";
break;
}
case ".png":
{
mediaType = "image/png";
break;
}
}
if (!string.IsNullOrEmpty(mediaType))
{
var byteArrayContent = new ByteArrayContent(bytes);
byteArrayContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(mediaType);
var requestHelper = new ApiRequestHelper(GetType(), PnPConnection.Current);
requestHelper.Put2(endpoint, byteArrayContent, token);
LogDebug("Successfully set the logo for the Entra ID app");
}
else
{
throw new Exception("Unrecognized image format. Supported formats are .png, .jpg, .jpeg and .gif");
}
}
catch (Exception ex)
{
LogWarning("Something went wrong setting the logo " + ex.Message);
}
}
else
{
LogWarning("Logo File does not exist, ignoring setting the logo");
}
}
}
}