-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfigure.ps1
422 lines (355 loc) · 20.7 KB
/
Configure.ps1
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
[CmdletBinding()]
param(
[PSCredential] $Credential,
[Parameter(Mandatory=$False, HelpMessage='Tenant ID (This is a GUID which represents the "Directory ID" of the AzureAD tenant into which you want to create the apps')]
[string] $tenantId,
[Parameter(Mandatory=$False, HelpMessage='Azure environment to use while running the script (it defaults to AzureCloud)')]
[string] $azureEnvironmentName
)
#Requires -Modules AzureAD -RunAsAdministrator
<#
This script creates the Azure AD applications needed for this sample and updates the configuration files
for the visual Studio projects from the data in the Azure AD applications.
Before running this script you need to install the AzureAD cmdlets as an administrator.
For this:
1) Run Powershell as an administrator
2) in the PowerShell window, type: Install-Module AzureAD
There are four ways to run this script. For more information, read the AppCreationScripts.md file in the same folder as this script.
#>
# Create a password that can be used as an application key
Function ComputePassword
{
$aesManaged = New-Object "System.Security.Cryptography.AesManaged"
$aesManaged.Mode = [System.Security.Cryptography.CipherMode]::CBC
$aesManaged.Padding = [System.Security.Cryptography.PaddingMode]::Zeros
$aesManaged.BlockSize = 128
$aesManaged.KeySize = 256
$aesManaged.GenerateKey()
return [System.Convert]::ToBase64String($aesManaged.Key)
}
# Create an application key
# See https://www.sabin.io/blog/adding-an-azure-active-directory-application-and-key-using-powershell/
Function CreateAppKey([DateTime] $fromDate, [double] $durationInYears, [string]$pw)
{
$endDate = $fromDate.AddYears($durationInYears)
$keyId = (New-Guid).ToString();
$key = New-Object Microsoft.Open.AzureAD.Model.PasswordCredential
$key.StartDate = $fromDate
$key.EndDate = $endDate
$key.Value = $pw
$key.KeyId = $keyId
return $key
}
# Adds the requiredAccesses (expressed as a pipe separated string) to the requiredAccess structure
# The exposed permissions are in the $exposedPermissions collection, and the type of permission (Scope | Role) is
# described in $permissionType
Function AddResourcePermission($requiredAccess, `
$exposedPermissions, [string]$requiredAccesses, [string]$permissionType)
{
foreach($permission in $requiredAccesses.Trim().Split("|"))
{
foreach($exposedPermission in $exposedPermissions)
{
if ($exposedPermission.Value -eq $permission)
{
$resourceAccess = New-Object Microsoft.Open.AzureAD.Model.ResourceAccess
$resourceAccess.Type = $permissionType # Scope = Delegated permissions | Role = Application permissions
$resourceAccess.Id = $exposedPermission.Id # Read directory data
$requiredAccess.ResourceAccess.Add($resourceAccess)
}
}
}
}
#
# Example: GetRequiredPermissions "Microsoft Graph" "Graph.Read|User.Read"
# See also: http://stackoverflow.com/questions/42164581/how-to-configure-a-new-azure-ad-application-through-powershell
Function GetRequiredPermissions([string] $applicationDisplayName, [string] $requiredDelegatedPermissions, [string]$requiredApplicationPermissions, $servicePrincipal)
{
# If we are passed the service principal we use it directly, otherwise we find it from the display name (which might not be unique)
if ($servicePrincipal)
{
$sp = $servicePrincipal
}
else
{
$sp = Get-AzureADServicePrincipal -Filter "DisplayName eq '$applicationDisplayName'"
}
$appid = $sp.AppId
$requiredAccess = New-Object Microsoft.Open.AzureAD.Model.RequiredResourceAccess
$requiredAccess.ResourceAppId = $appid
$requiredAccess.ResourceAccess = New-Object System.Collections.Generic.List[Microsoft.Open.AzureAD.Model.ResourceAccess]
# $sp.Oauth2Permissions | Select Id,AdminConsentDisplayName,Value: To see the list of all the Delegated permissions for the application:
if ($requiredDelegatedPermissions)
{
AddResourcePermission $requiredAccess -exposedPermissions $sp.Oauth2Permissions -requiredAccesses $requiredDelegatedPermissions -permissionType "Scope"
}
# $sp.AppRoles | Select Id,AdminConsentDisplayName,Value: To see the list of all the Application permissions for the application
if ($requiredApplicationPermissions)
{
AddResourcePermission $requiredAccess -exposedPermissions $sp.AppRoles -requiredAccesses $requiredApplicationPermissions -permissionType "Role"
}
return $requiredAccess
}
Function ReplaceInLine([string] $line, [string] $key, [string] $value)
{
$index = $line.IndexOf($key)
if ($index -ige 0)
{
$index2 = $index+$key.Length
$line = $line.Substring(0, $index) + $value + $line.Substring($index2)
}
return $line
}
Function ReplaceInTextFile([string] $configFilePath, [System.Collections.HashTable] $dictionary)
{
$lines = Get-Content $configFilePath
$index = 0
while($index -lt $lines.Length)
{
$line = $lines[$index]
foreach($key in $dictionary.Keys)
{
if ($line.Contains($key))
{
$lines[$index] = ReplaceInLine $line $key $dictionary[$key]
}
}
$index++
}
Set-Content -Path $configFilePath -Value $lines -Force
}
<#.Description
This function creates a new Azure AD scope (OAuth2Permission) with default and provided values
#>
Function CreateScope( [string] $value, [string] $userConsentDisplayName, [string] $userConsentDescription, [string] $adminConsentDisplayName, [string] $adminConsentDescription)
{
$scope = New-Object Microsoft.Open.AzureAD.Model.OAuth2Permission
$scope.Id = New-Guid
$scope.Value = $value
$scope.UserConsentDisplayName = $userConsentDisplayName
$scope.UserConsentDescription = $userConsentDescription
$scope.AdminConsentDisplayName = $adminConsentDisplayName
$scope.AdminConsentDescription = $adminConsentDescription
$scope.IsEnabled = $true
$scope.Type = "User"
return $scope
}
<#.Description
This function creates a new Azure AD AppRole with default and provided values
#>
Function CreateAppRole([string] $types, [string] $name, [string] $description)
{
$appRole = New-Object Microsoft.Open.AzureAD.Model.AppRole
$appRole.AllowedMemberTypes = New-Object System.Collections.Generic.List[string]
$typesArr = $types.Split(',')
foreach($type in $typesArr)
{
$appRole.AllowedMemberTypes.Add($type);
}
$appRole.DisplayName = $name
$appRole.Id = New-Guid
$appRole.IsEnabled = $true
$appRole.Description = $description
$appRole.Value = $name;
return $appRole
}
Set-Content -Value "<html><body><table>" -Path createdApps.html
Add-Content -Value "<thead><tr><th>Application</th><th>AppId</th><th>Url in the Azure portal</th></tr></thead><tbody>" -Path createdApps.html
$ErrorActionPreference = "Stop"
Function ConfigureApplications
{
<#.Description
This function creates the Azure AD applications for the sample in the provided Azure AD tenant and updates the
configuration files in the client and service project of the visual studio solution (App.Config and Web.Config)
so that they are consistent with the Applications parameters
#>
$commonendpoint = "common"
if (!$azureEnvironmentName)
{
$azureEnvironmentName = "AzureCloud"
}
# $tenantId is the Active Directory Tenant. This is a GUID which represents the "Directory ID" of the AzureAD tenant
# into which you want to create the apps. Look it up in the Azure portal in the "Properties" of the Azure AD.
# Login to Azure PowerShell (interactive if credentials are not already provided:
# you'll need to sign-in with creds enabling your to create apps in the tenant)
if (!$Credential -and $TenantId)
{
$creds = Connect-AzureAD -TenantId $tenantId -AzureEnvironmentName $azureEnvironmentName
}
else
{
if (!$TenantId)
{
$creds = Connect-AzureAD -Credential $Credential -AzureEnvironmentName $azureEnvironmentName
}
else
{
$creds = Connect-AzureAD -TenantId $tenantId -Credential $Credential -AzureEnvironmentName $azureEnvironmentName
}
}
if (!$tenantId)
{
$tenantId = $creds.Tenant.Id
}
$tenant = Get-AzureADTenantDetail
$tenantName = ($tenant.VerifiedDomains | Where { $_._Default -eq $True }).Name
# Get the user running the script to add the user as the app owner
$user = Get-AzureADUser -ObjectId $creds.Account.Id
# Create the service AAD application
Write-Host "Creating the AAD application (msal-node-api)"
# Get a 2 years application key for the service Application
$pw = ComputePassword
$fromDate = [DateTime]::Now;
$key = CreateAppKey -fromDate $fromDate -durationInYears 2 -pw $pw
$serviceAppKey = $pw
# create the application
$serviceAadApplication = New-AzureADApplication -DisplayName "msal-node-api" `
-HomePage "http://localhost:5000/api" `
-PasswordCredentials $key `
-GroupMembershipClaims "SecurityGroup" `
-PublicClient $False
$serviceIdentifierUri = 'api://'+$serviceAadApplication.AppId
Set-AzureADApplication -ObjectId $serviceAadApplication.ObjectId -IdentifierUris $serviceIdentifierUri
# create the service principal of the newly created application
$currentAppId = $serviceAadApplication.AppId
$serviceServicePrincipal = New-AzureADServicePrincipal -AppId $currentAppId -Tags {WindowsAzureActiveDirectoryIntegratedApp}
# add the user running the script as an app owner if needed
$owner = Get-AzureADApplicationOwner -ObjectId $serviceAadApplication.ObjectId
if ($owner -eq $null)
{
Add-AzureADApplicationOwner -ObjectId $serviceAadApplication.ObjectId -RefObjectId $user.ObjectId
Write-Host "'$($user.UserPrincipalName)' added as an application owner to app '$($serviceServicePrincipal.DisplayName)'"
}
# rename the user_impersonation scope if it exists to match the readme steps or add a new scope
$scopes = New-Object System.Collections.Generic.List[Microsoft.Open.AzureAD.Model.OAuth2Permission]
# delete default scope i.e. User_impersonation
$scope = $serviceAadApplication.Oauth2Permissions | Where-Object { $_.Value -eq "User_impersonation" }
if($scope -ne $null)
{
# disable the scope
$scope.IsEnabled = $false
$scopes.Add($scope)
Set-AzureADApplication -ObjectId $serviceAadApplication.ObjectId -Oauth2Permissions $scopes
# clear the scope
$scopes.Clear()
Set-AzureADApplication -ObjectId $serviceAadApplication.ObjectId -Oauth2Permissions $scopes
}
if ($scopes.Count -ge 0)
{
$scope = CreateScope -value access_as_user `
-userConsentDisplayName "Access msal-node-api" `
-userConsentDescription "Allow the application to access msal-node-api on your behalf." `
-adminConsentDisplayName "Access msal-node-api" `
-adminConsentDescription "Allows the app to have the same access to information in the directory on behalf of the signed-in user."
$scopes.Add($scope)
}
# add/update scopes
Set-AzureADApplication -ObjectId $serviceAadApplication.ObjectId -OAuth2Permission $scopes
Write-Host "Done creating the service application (msal-node-api)"
# URL of the AAD application in the Azure portal
# Future? $servicePortalUrl = "https://portal.azure.com/#@"+$tenantName+"/blade/Microsoft_AAD_RegisteredApps/ApplicationMenuBlade/Overview/appId/"+$serviceAadApplication.AppId+"/objectId/"+$serviceAadApplication.ObjectId+"/isMSAApp/"
$servicePortalUrl = "https://portal.azure.com/#blade/Microsoft_AAD_RegisteredApps/ApplicationMenuBlade/CallAnAPI/appId/"+$serviceAadApplication.AppId+"/objectId/"+$serviceAadApplication.ObjectId+"/isMSAApp/"
Add-Content -Value "<tr><td>service</td><td>$currentAppId</td><td><a href='$servicePortalUrl'>msal-node-api</a></td></tr>" -Path createdApps.html
$requiredResourcesAccess = New-Object System.Collections.Generic.List[Microsoft.Open.AzureAD.Model.RequiredResourceAccess]
# Add Required Resources Access (from 'service' to 'Microsoft Graph')
Write-Host "Getting access from 'service' to 'Microsoft Graph'"
$requiredPermissions = GetRequiredPermissions -applicationDisplayName "Microsoft Graph" `
-requiredDelegatedPermissions "User.Read|GroupMember.Read.All" `
$requiredResourcesAccess.Add($requiredPermissions)
Set-AzureADApplication -ObjectId $serviceAadApplication.ObjectId -RequiredResourceAccess $requiredResourcesAccess
Write-Host "Granted permissions."
# Create the client AAD application
Write-Host "Creating the AAD application (msal-react-spa)"
# create the application
$clientAadApplication = New-AzureADApplication -DisplayName "msal-react-spa" `
-HomePage "http://localhost:3000/" `
-ReplyUrls "http://localhost:3000/" `
-IdentifierUris "https://$tenantName/msal-react-spa" `
-GroupMembershipClaims "SecurityGroup" `
-PublicClient $False
# create the service principal of the newly created application
$currentAppId = $clientAadApplication.AppId
$clientServicePrincipal = New-AzureADServicePrincipal -AppId $currentAppId -Tags {WindowsAzureActiveDirectoryIntegratedApp}
# add the user running the script as an app owner if needed
$owner = Get-AzureADApplicationOwner -ObjectId $clientAadApplication.ObjectId
if ($owner -eq $null)
{
Add-AzureADApplicationOwner -ObjectId $clientAadApplication.ObjectId -RefObjectId $user.ObjectId
Write-Host "'$($user.UserPrincipalName)' added as an application owner to app '$($clientServicePrincipal.DisplayName)'"
}
Write-Host "Done creating the client application (msal-react-spa)"
# URL of the AAD application in the Azure portal
# Future? $clientPortalUrl = "https://portal.azure.com/#@"+$tenantName+"/blade/Microsoft_AAD_RegisteredApps/ApplicationMenuBlade/Overview/appId/"+$clientAadApplication.AppId+"/objectId/"+$clientAadApplication.ObjectId+"/isMSAApp/"
$clientPortalUrl = "https://portal.azure.com/#blade/Microsoft_AAD_RegisteredApps/ApplicationMenuBlade/CallAnAPI/appId/"+$clientAadApplication.AppId+"/objectId/"+$clientAadApplication.ObjectId+"/isMSAApp/"
Add-Content -Value "<tr><td>client</td><td>$currentAppId</td><td><a href='$clientPortalUrl'>msal-react-spa</a></td></tr>" -Path createdApps.html
$requiredResourcesAccess = New-Object System.Collections.Generic.List[Microsoft.Open.AzureAD.Model.RequiredResourceAccess]
# Add Required Resources Access (from 'client' to 'service')
Write-Host "Getting access from 'client' to 'service'"
$requiredPermissions = GetRequiredPermissions -applicationDisplayName "msal-node-api" `
-requiredDelegatedPermissions "access_as_user" `
$requiredResourcesAccess.Add($requiredPermissions)
# Add Required Resources Access (from 'client' to 'Microsoft Graph')
Write-Host "Getting access from 'client' to 'Microsoft Graph'"
$requiredPermissions = GetRequiredPermissions -applicationDisplayName "Microsoft Graph" `
-requiredDelegatedPermissions "User.Read|GroupMember.Read.All" `
$requiredResourcesAccess.Add($requiredPermissions)
Set-AzureADApplication -ObjectId $clientAadApplication.ObjectId -RequiredResourceAccess $requiredResourcesAccess
Write-Host "Granted permissions."
# Configure known client applications for service
Write-Host "Configure known client applications for the 'service'"
$knowApplications = New-Object System.Collections.Generic.List[System.String]
$knowApplications.Add($clientAadApplication.AppId)
Set-AzureADApplication -ObjectId $serviceAadApplication.ObjectId -KnownClientApplications $knowApplications
Write-Host "Configured."
if($null -eq (Get-AzureADGroup -SearchString "GroupAdmin")) {
Write-Host "Creating group"
$newsg = New-AzureADGroup -Description "GroupAdmin" -DisplayName "GroupAdmin" -MailEnabled $false -SecurityEnabled $true -MailNickName "GroupAdmin"
Write-Host "Successfully created $($newsg.DisplayName)"
}
if($null -eq (Get-AzureADGroup -SearchString "GroupMember")) {
Write-Host "Creating group"
$newsg = New-AzureADGroup -Description "GroupMember" -DisplayName "GroupMember" -MailEnabled $false -SecurityEnabled $true -MailNickName "GroupMember"
Write-Host "Successfully created $($newsg.DisplayName)"
}
$groupAdmin = Get-AzureADGroup -SearchString "GroupAdmin"
$groupMember = Get-AzureADGroup -SearchString "GroupMember"
# Update config file for 'service'
$configFile = $pwd.Path + "\..\API\authConfig.json"
Write-Host "Updating the sample code ($configFile)"
$dictionary = @{ "Enter_the_Tenant_Info_Here" = $tenantId;"Enter_the_Application_Id_Here" = $serviceAadApplication.AppId;"Enter_the_Client_Secret_Here" = $serviceAppKey; "Enter_the_Object_Id_of_GroupAdmin_Group_Here" = $groupAdmin.objectId; "Enter_the_Object_Id_of_GroupMember_Group_Here" = $groupMember.objectId };
ReplaceInTextFile -configFilePath $configFile -dictionary $dictionary
# Update config file for 'client'
$configFile = $pwd.Path + "\..\SPA\src\authConfig.js"
Write-Host "Updating the sample code ($configFile)"
$dictionary = @{ "Enter_the_Application_Id_Here" = $clientAadApplication.AppId;"Enter_the_Tenant_Info_Here" = $tenantId;"Enter_the_Web_Api_Scope_here" = ("api://"+$serviceAadApplication.AppId+"/access_as_user"); "Enter_the_Object_Id_of_GroupAdmin_Group_Here" = $groupAdmin.objectId; "Enter_the_Object_Id_of_GroupMember_Group_Here" = $groupMember.objectId };
ReplaceInTextFile -configFilePath $configFile -dictionary $dictionary
Write-Host ""
Write-Host -ForegroundColor Green "------------------------------------------------------------------------------------------------"
Write-Host "IMPORTANT: Please follow the instructions below to complete a few manual step(s) in the Azure portal":
Write-Host "- For 'service'"
Write-Host " - Navigate to '$servicePortalUrl'"
Write-Host " - Navigate to the portal and set the 'accessTokenAcceptedVersion' to '2' in the application manifest" -ForegroundColor Red
Write-Host " - This script has created a group named GroupAdmin for you. On Azure portal, assign some users to it, and configure your ID and Access token to emit GroupID in your app registration." -ForegroundColor Red
Write-Host " - This script has created a group named GroupMember for you. On Azure portal, assign some users to it, and configure your ID and Access token to emit GroupID in your app registration." -ForegroundColor Red
Write-Host "- For 'client'"
Write-Host " - Navigate to '$clientPortalUrl'"
Write-Host " - Navigate to the portal and set the 'replyUrlsWithType' to 'Spa' in the application manifest" -ForegroundColor Red
Write-Host " - This script has created a group named GroupAdmin for you. On Azure portal, assign some users to it, and configure your ID and Access token to emit GroupID in your app registration." -ForegroundColor Red
Write-Host " - This script has created a group named GroupMember for you. On Azure portal, assign some users to it, and configure your ID and Access token to emit GroupID in your app registration." -ForegroundColor Red
Write-Host -ForegroundColor Green "------------------------------------------------------------------------------------------------"
if($isOpenSSL -eq 'Y')
{
Write-Host -ForegroundColor Green "------------------------------------------------------------------------------------------------"
Write-Host "You have generated certificate using OpenSSL so follow below steps: "
Write-Host "Install the certificate on your system from current folder."
Write-Host -ForegroundColor Green "------------------------------------------------------------------------------------------------"
}
Add-Content -Value "</tbody></table></body></html>" -Path createdApps.html
}
# Pre-requisites
if ((Get-Module -ListAvailable -Name "AzureAD") -eq $null) {
Install-Module "AzureAD" -Scope CurrentUser
}
Import-Module AzureAD
# Run interactively (will ask you for the tenant ID)
ConfigureApplications -Credential $Credential -tenantId $TenantId