Skip to content

Commit 78e86fe

Browse files
committed
Changing app creation scripts
1 parent c9cfa41 commit 78e86fe

File tree

4 files changed

+39
-106
lines changed

4 files changed

+39
-106
lines changed

AppCreationScripts/AppCreationScripts.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Registering the sample apps with Microsoft Identity Platform and updating the configuration files using PowerShell scripts
1+
# Registering the sample apps with Microsoft identity platform and updating the configuration files using PowerShell scripts
22

33
## Overview
44

AppCreationScripts/Cleanup.ps1

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ param(
55
[string] $tenantId
66
)
77

8-
if ((Get-Module -ListAvailable -Name "AzureAD") -eq $null) {
8+
if ($null -eq (Get-Module -ListAvailable -Name "AzureAD")) {
99
Install-Module "AzureAD" -Scope CurrentUser
1010
}
1111
Import-Module AzureAD
12-
$ErrorActionPreference = 'Stop'
12+
$ErrorActionPreference = "Stop"
1313

1414
Function Cleanup
1515
{
@@ -44,20 +44,27 @@ This function removes the Azure AD applications for the sample. These applicatio
4444
$tenantId = $creds.Tenant.Id
4545
}
4646
$tenant = Get-AzureADTenantDetail
47-
$tenantName = ($tenant.VerifiedDomains | Where { $_._Default -eq $True }).Name
47+
$tenantName = ($tenant.VerifiedDomains | Where-Object { $_._Default -eq $True }).Name
4848

4949
# Removes the applications
5050
Write-Host "Cleaning-up applications from tenant '$tenantName'"
5151

52-
Write-Host "Removing 'client' (python-daemon-console) if needed"
53-
$app=Get-AzureADApplication -Filter "DisplayName eq 'python-daemon-console'"
52+
Write-Host "Removing 'client' (python-devicecode) if needed"
53+
Get-AzureADApplication -Filter "DisplayName eq 'python-devicecode'" | ForEach-Object {Remove-AzureADApplication -ObjectId $_.ObjectId }
54+
$apps = Get-AzureADApplication -Filter "DisplayName eq 'python-devicecode'"
55+
if ($apps)
56+
{
57+
Remove-AzureADApplication -ObjectId $apps.ObjectId
58+
}
5459

55-
if ($app)
60+
foreach ($app in $apps)
5661
{
5762
Remove-AzureADApplication -ObjectId $app.ObjectId
58-
Write-Host "Removed."
63+
Write-Host "Removed python-devicecode.."
5964
}
60-
65+
# also remove service principals of this app
66+
Get-AzureADServicePrincipal -filter "DisplayName eq 'python-devicecode'" | ForEach-Object {Remove-AzureADServicePrincipal -ObjectId $_.Id -Confirm:$false}
67+
6168
}
6269

63-
Cleanup -Credential $Credential -tenantId $TenantId
70+
Cleanup -Credential $Credential -tenantId $TenantId

AppCreationScripts/Configure.ps1

Lines changed: 20 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -17,32 +17,6 @@ param(
1717
There are four ways to run this script. For more information, read the AppCreationScripts.md file in the same folder as this script.
1818
#>
1919

20-
# Create a password that can be used as an application key
21-
Function ComputePassword
22-
{
23-
$aesManaged = New-Object "System.Security.Cryptography.AesManaged"
24-
$aesManaged.Mode = [System.Security.Cryptography.CipherMode]::CBC
25-
$aesManaged.Padding = [System.Security.Cryptography.PaddingMode]::Zeros
26-
$aesManaged.BlockSize = 128
27-
$aesManaged.KeySize = 256
28-
$aesManaged.GenerateKey()
29-
return [System.Convert]::ToBase64String($aesManaged.Key)
30-
}
31-
32-
# Create an application key
33-
# See https://www.sabin.io/blog/adding-an-azure-active-directory-application-and-key-using-powershell/
34-
Function CreateAppKey([DateTime] $fromDate, [double] $durationInYears, [string]$pw)
35-
{
36-
$endDate = $fromDate.AddYears($durationInYears)
37-
$keyId = (New-Guid).ToString();
38-
$key = New-Object Microsoft.Open.AzureAD.Model.PasswordCredential
39-
$key.StartDate = $fromDate
40-
$key.EndDate = $endDate
41-
$key.Value = $pw
42-
$key.KeyId = $keyId
43-
return $key
44-
}
45-
4620
# Adds the requiredAccesses (expressed as a pipe separated string) to the requiredAccess structure
4721
# The exposed permissions are in the $exposedPermissions collection, and the type of permission (Scope | Role) is
4822
# described in $permissionType
@@ -65,7 +39,7 @@ Function AddResourcePermission($requiredAccess, `
6539
}
6640

6741
#
68-
# Exemple: GetRequiredPermissions "Microsoft Graph" "Graph.Read|User.Read"
42+
# Example: GetRequiredPermissions "Microsoft Graph" "Graph.Read|User.Read"
6943
# See also: http://stackoverflow.com/questions/42164581/how-to-configure-a-new-azure-ad-application-through-powershell
7044
Function GetRequiredPermissions([string] $applicationDisplayName, [string] $requiredDelegatedPermissions, [string]$requiredApplicationPermissions, $servicePrincipal)
7145
{
@@ -134,49 +108,18 @@ Function UpdateTextFile([string] $configFilePath, [System.Collections.HashTable]
134108
Set-Content -Path $configFilePath -Value $lines -Force
135109
}
136110

137-
Function ReplaceInLine([string] $line, [string] $key, [string] $value)
138-
{
139-
$index = $line.IndexOf($key)
140-
if ($index -ige 0)
141-
{
142-
$index2 = $index+$key.Length
143-
$line = $line.Substring(0, $index) + $value + $line.Substring($index2)
144-
}
145-
return $line
146-
}
147-
148-
Function ReplaceInTextFile([string] $configFilePath, [System.Collections.HashTable] $dictionary)
149-
{
150-
$lines = Get-Content $configFilePath
151-
$index = 0
152-
while($index -lt $lines.Length)
153-
{
154-
$line = $lines[$index]
155-
foreach($key in $dictionary.Keys)
156-
{
157-
if ($line.Contains($key))
158-
{
159-
$lines[$index] = ReplaceInLine $line $key $dictionary[$key]
160-
}
161-
}
162-
$index++
163-
}
164-
165-
Set-Content -Path $configFilePath -Value $lines -Force
166-
}
167-
168-
169111
Set-Content -Value "<html><body><table>" -Path createdApps.html
170112
Add-Content -Value "<thead><tr><th>Application</th><th>AppId</th><th>Url in the Azure portal</th></tr></thead><tbody>" -Path createdApps.html
171113

114+
$ErrorActionPreference = "Stop"
115+
172116
Function ConfigureApplications
173117
{
174118
<#.Description
175119
This function creates the Azure AD applications for the sample in the provided Azure AD tenant and updates the
176120
configuration files in the client and service project of the visual studio solution (App.Config and Web.Config)
177121
so that they are consistent with the Applications parameters
178122
#>
179-
180123
$commonendpoint = "common"
181124

182125
# $tenantId is the Active Directory Tenant. This is a GUID which represents the "Directory ID" of the AzureAD tenant
@@ -208,18 +151,18 @@ Function ConfigureApplications
208151
$tenant = Get-AzureADTenantDetail
209152
$tenantName = ($tenant.VerifiedDomains | Where { $_._Default -eq $True }).Name
210153

211-
# Get the user running the script
154+
# Get the user running the script to add the user as the app owner
212155
$user = Get-AzureADUser -ObjectId $creds.Account.Id
213156

214157
# Create the client AAD application
215-
Write-Host "Creating the AAD application (python-daemon-console)"
216-
217-
$clientAadApplication = New-AzureADApplication -DisplayName "python-device-flow" `
218-
-ReplyUrls "https://device" `
219-
-IdentifierUris "https://$tenantName/python-device-flow" `
220-
-PasswordCredentials $key `
221-
-PublicClient $False
222-
158+
Write-Host "Creating the AAD application (python-devicecode)"
159+
# create the application
160+
$clientAadApplication = New-AzureADApplication -DisplayName "python-devicecode" `
161+
-ReplyUrls "https://login.microsoftonline.com/common/oauth2/nativeclient" `
162+
-AvailableToOtherTenants $True `
163+
-PublicClient $True
164+
165+
# create the service principal of the newly created application
223166
$currentAppId = $clientAadApplication.AppId
224167
$clientServicePrincipal = New-AzureADServicePrincipal -AppId $currentAppId -Tags {WindowsAzureActiveDirectoryIntegratedApp}
225168

@@ -232,19 +175,19 @@ Function ConfigureApplications
232175
}
233176

234177

235-
Write-Host "Done creating the client application (python-device-flow)"
178+
Write-Host "Done creating the client application (python-devicecode)"
236179

237180
# URL of the AAD application in the Azure portal
238181
# Future? $clientPortalUrl = "https://portal.azure.com/#@"+$tenantName+"/blade/Microsoft_AAD_RegisteredApps/ApplicationMenuBlade/Overview/appId/"+$clientAadApplication.AppId+"/objectId/"+$clientAadApplication.ObjectId+"/isMSAApp/"
239182
$clientPortalUrl = "https://portal.azure.com/#blade/Microsoft_AAD_RegisteredApps/ApplicationMenuBlade/CallAnAPI/appId/"+$clientAadApplication.AppId+"/objectId/"+$clientAadApplication.ObjectId+"/isMSAApp/"
240-
Add-Content -Value "<tr><td>client</td><td>$currentAppId</td><td><a href='$clientPortalUrl'>python-device-flow</a></td></tr>" -Path createdApps.html
183+
Add-Content -Value "<tr><td>client</td><td>$currentAppId</td><td><a href='$clientPortalUrl'>python-devicecode</a></td></tr>" -Path createdApps.html
241184

242185
$requiredResourcesAccess = New-Object System.Collections.Generic.List[Microsoft.Open.AzureAD.Model.RequiredResourceAccess]
243186

244187
# Add Required Resources Access (from 'client' to 'Microsoft Graph')
245188
Write-Host "Getting access from 'client' to 'Microsoft Graph'"
246189
$requiredPermissions = GetRequiredPermissions -applicationDisplayName "Microsoft Graph" `
247-
-requiredApplicationPermissions "User.Read.All" `
190+
-requiredDelegatedPermissions "User.Read" `
248191

249192
$requiredResourcesAccess.Add($requiredPermissions)
250193

@@ -255,31 +198,18 @@ Function ConfigureApplications
255198
# Update config file for 'client'
256199
$configFile = $pwd.Path + "\..\parameters.json"
257200
Write-Host "Updating the sample code ($configFile)"
258-
$dictionary = @{ "client_id" = $clientAadApplication.AppId;};
201+
$dictionary = @{ "client_id" = $clientAadApplication.AppId };
259202
UpdateTextFile -configFilePath $configFile -dictionary $dictionary
260-
261-
# Update config file for 'client'
262-
$configFile = $pwd.Path + "\..\parameters.json"
263-
Write-Host "Updating the sample code ($configFile)"
264-
$dictionary = @{ "organizations" = $tenantName };
265-
ReplaceInTextFile -configFilePath $configFile -dictionary $dictionary
266-
Write-Host ""
267-
Write-Host -ForegroundColor Green "------------------------------------------------------------------------------------------------"
268-
Write-Host "IMPORTANT: Please follow the instructions below to complete a few manual step(s) in the Azure portal":
269-
Write-Host "- For 'client'"
270-
Write-Host " - Navigate to '$clientPortalUrl'"
271-
Write-Host " - Navigate to the API permissions page and click on 'Grant admin consent for {tenant}'" -ForegroundColor Red
272-
273-
Write-Host -ForegroundColor Green "------------------------------------------------------------------------------------------------"
274-
203+
275204
Add-Content -Value "</tbody></table></body></html>" -Path createdApps.html
276205
}
277206

278207
# Pre-requisites
279208
if ((Get-Module -ListAvailable -Name "AzureAD") -eq $null) {
280209
Install-Module "AzureAD" -Scope CurrentUser
281-
}
210+
}
211+
282212
Import-Module AzureAD
283213

284214
# Run interactively (will ask you for the tenant ID)
285-
ConfigureApplications -Credential $Credential -tenantId $TenantId
215+
ConfigureApplications -Credential $Credential -tenantId $TenantId

AppCreationScripts/sample.json

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,12 @@
1717
"Name": "python-devicecode",
1818
"Kind": "Desktop",
1919
"Audience": "AzureADMultipleOrgs",
20+
"IsPublicClient": true,
2021
"UsesROPCOrIWA": false,
2122
"RequiredResourcesAccess": [
2223
{
2324
"Resource": "Microsoft Graph",
24-
"ApplicationPermissions": [ "User.Read.All" ]
25-
}
26-
],
27-
"ManualSteps": [
28-
{
29-
"Comment" : "Navigate to the API permissions page and click on 'Grant admin consent for {tenant}'"
25+
"DelegatedPermissions": ["User.Read"]
3026
}
3127
]
3228
}

0 commit comments

Comments
 (0)