1-
1+
22[CmdletBinding ()]
33param (
4- [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' )]
4+ [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' )]
55 [string ] $tenantId ,
6- [Parameter (Mandatory = $False , HelpMessage = ' Azure environment to use while running the script. Default = Global' )]
6+ [Parameter (Mandatory = $False , HelpMessage = ' Azure environment to use while running the script. Default = Global' )]
77 [string ] $azureEnvironmentName
88)
99
@@ -20,28 +20,35 @@ param(
2020# The exposed permissions are in the $exposedPermissions collection, and the type of permission (Scope | Role) is
2121# described in $permissionType
2222Function AddResourcePermission ($requiredAccess , `
23- $exposedPermissions , [string ]$requiredAccesses , [string ]$permissionType ) {
24- foreach ($permission in $requiredAccesses.Trim ().Split(" |" )) {
25- foreach ($exposedPermission in $exposedPermissions ) {
26- if ($exposedPermission.Value -eq $permission ) {
23+ $exposedPermissions , [string ]$requiredAccesses , [string ]$permissionType )
24+ {
25+ foreach ($permission in $requiredAccesses.Trim ().Split(" |" ))
26+ {
27+ foreach ($exposedPermission in $exposedPermissions )
28+ {
29+ if ($exposedPermission.Value -eq $permission )
30+ {
2731 $resourceAccess = New-Object Microsoft.Graph.PowerShell.Models.MicrosoftGraphResourceAccess
2832 $resourceAccess.Type = $permissionType # Scope = Delegated permissions | Role = Application permissions
2933 $resourceAccess.Id = $exposedPermission.Id # Read directory data
3034 $requiredAccess.ResourceAccess += $resourceAccess
31- }
35+ }
3236 }
3337 }
3438}
3539
3640#
3741# Example: GetRequiredPermissions "Microsoft Graph" "Graph.Read|User.Read"
3842# See also: http://stackoverflow.com/questions/42164581/how-to-configure-a-new-azure-ad-application-through-powershell
39- Function GetRequiredPermissions ([string ] $applicationDisplayName , [string ] $requiredDelegatedPermissions , [string ]$requiredApplicationPermissions , $servicePrincipal ) {
43+ Function GetRequiredPermissions ([string ] $applicationDisplayName , [string ] $requiredDelegatedPermissions , [string ]$requiredApplicationPermissions , $servicePrincipal )
44+ {
4045 # If we are passed the service principal we use it directly, otherwise we find it from the display name (which might not be unique)
41- if ($servicePrincipal ) {
46+ if ($servicePrincipal )
47+ {
4248 $sp = $servicePrincipal
4349 }
44- else {
50+ else
51+ {
4552 $sp = Get-MgServicePrincipal - Filter " DisplayName eq '$applicationDisplayName '"
4653 }
4754 $appid = $sp.AppId
@@ -50,34 +57,42 @@ Function GetRequiredPermissions([string] $applicationDisplayName, [string] $requ
5057 $requiredAccess.ResourceAccess = New-Object System.Collections.Generic.List[Microsoft.Graph.PowerShell.Models.MicrosoftGraphResourceAccess ]
5158
5259 # $sp.Oauth2Permissions | Select Id,AdminConsentDisplayName,Value: To see the list of all the Delegated permissions for the application:
53- if ($requiredDelegatedPermissions ) {
60+ if ($requiredDelegatedPermissions )
61+ {
5462 AddResourcePermission $requiredAccess - exposedPermissions $sp.Oauth2PermissionScopes - requiredAccesses $requiredDelegatedPermissions - permissionType " Scope"
5563 }
5664
5765 # $sp.AppRoles | Select Id,AdminConsentDisplayName,Value: To see the list of all the Application permissions for the application
58- if ($requiredApplicationPermissions ) {
66+ if ($requiredApplicationPermissions )
67+ {
5968 AddResourcePermission $requiredAccess - exposedPermissions $sp.AppRoles - requiredAccesses $requiredApplicationPermissions - permissionType " Role"
6069 }
6170 return $requiredAccess
6271}
6372
6473
65- Function ReplaceInLine ([string ] $line , [string ] $key , [string ] $value ) {
74+ Function ReplaceInLine ([string ] $line , [string ] $key , [string ] $value )
75+ {
6676 $index = $line.IndexOf ($key )
67- if ($index -ige 0 ) {
68- $index2 = $index + $key.Length
77+ if ($index -ige 0 )
78+ {
79+ $index2 = $index + $key.Length
6980 $line = $line.Substring (0 , $index ) + $value + $line.Substring ($index2 )
7081 }
7182 return $line
7283}
7384
74- Function ReplaceInTextFile ([string ] $configFilePath , [System.Collections.HashTable ] $dictionary ) {
85+ Function ReplaceInTextFile ([string ] $configFilePath , [System.Collections.HashTable ] $dictionary )
86+ {
7587 $lines = Get-Content $configFilePath
7688 $index = 0
77- while ($index -lt $lines.Length ) {
89+ while ($index -lt $lines.Length )
90+ {
7891 $line = $lines [$index ]
79- foreach ($key in $dictionary.Keys ) {
80- if ($line.Contains ($key )) {
92+ foreach ($key in $dictionary.Keys )
93+ {
94+ if ($line.Contains ($key ))
95+ {
8196 $lines [$index ] = ReplaceInLine $line $key $dictionary [$key ]
8297 }
8398 }
@@ -86,15 +101,32 @@ Function ReplaceInTextFile([string] $configFilePath, [System.Collections.HashTab
86101
87102 Set-Content - Path $configFilePath - Value $lines - Force
88103}
104+ Function CreateOptionalClaim ([string ] $name )
105+ {
106+ <# . Description
107+ This function creates a new Azure AD optional claims with default and provided values
108+ #>
109+
110+ $appClaim = New-Object Microsoft.Graph.PowerShell.Models.MicrosoftGraphOptionalClaim
111+ $appClaim.AdditionalProperties = New-Object System.Collections.Generic.List[string ]
112+ $appClaim.Source = $null
113+ $appClaim.Essential = $false
114+ $appClaim.Name = $name
115+ return $appClaim
116+ }
117+
118+ Function ConfigureApplications
119+ {
120+ $isOpenSSl = ' N' # temporary disable open certificate creation
89121
90- Function ConfigureApplications {
91122 <# . Description
92123 This function creates the Azure AD applications for the sample in the provided Azure AD tenant and updates the
93124 configuration files in the client and service project of the visual studio solution (App.Config and Web.Config)
94125 so that they are consistent with the Applications parameters
95126 #>
96127
97- if (! $azureEnvironmentName ) {
128+ if (! $azureEnvironmentName )
129+ {
98130 $azureEnvironmentName = " Global"
99131 }
100132
@@ -109,39 +141,52 @@ Function ConfigureApplications {
109141 }
110142
111143
112- # Create the spa AAD application
113- Write-Host " Creating the AAD application (msal-angular-spa)"
144+ # Create the spa AAD application
145+ Write-Host " Creating the AAD application (msal-angular-spa)"
114146
115- # create the application
116- $spaAadApplication = New-MgApplication - DisplayName " msal-angular-spa" `
117- - Spa `
118- @ { `
119- RedirectUris = " http://localhost:4200/" ; `
120-
121- } `
122- - SignInAudience AzureADMyOrg `
123- # end of command
124- $tenantName = (Get-MgApplication - ApplicationId $spaAadApplication.Id ).PublisherDomain
147+ # create the application
148+ $spaAadApplication = New-MgApplication - DisplayName " msal-angular-spa" `
149+ - Spa `
150+ @ { `
151+ RedirectUris = " http://localhost:4200/" ; `
152+ } `
153+ - SignInAudience AzureADMyOrg `
154+ # end of command
155+ $tenantName = (Get-MgApplication - ApplicationId $spaAadApplication.Id ).PublisherDomain
125156 Update-MgApplication - ApplicationId $spaAadApplication.Id - IdentifierUris @ (" https://$tenantName /msal-angular-spa" )
126157
127158 # create the service principal of the newly created application
128159 $currentAppId = $spaAadApplication.AppId
129- $spaServicePrincipal = New-MgServicePrincipal - AppId $currentAppId - Tags { WindowsAzureActiveDirectoryIntegratedApp }
160+ $spaServicePrincipal = New-MgServicePrincipal - AppId $currentAppId - Tags {WindowsAzureActiveDirectoryIntegratedApp}
130161
131162 # add the user running the script as an app owner if needed
132163 $owner = Get-MgApplicationOwner - ApplicationId $spaAadApplication.Id
133- if ($owner -eq $null ) {
134- New-MgApplicationOwnerByRef - ApplicationId $spaAadApplication.Id - BodyParameter = @ {" @odata.id" = " htps://graph.microsoft.com/v1.0/directoryObjects/$user .ObjectId" }
164+ if ($owner -eq $null )
165+ {
166+ New-MgApplicationOwnerByRef - ApplicationId $spaAadApplication.Id - BodyParameter = @ {" @odata.id" = " htps://graph.microsoft.com/v1.0/directoryObjects/$user .ObjectId" }
135167 Write-Host " '$ ( $user.UserPrincipalName ) ' added as an application owner to app '$ ( $spaServicePrincipal.DisplayName ) '"
136168 }
169+
170+ # Add Claims
171+
172+ $optionalClaims = New-Object Microsoft.Graph.PowerShell.Models.MicrosoftGraphOptionalClaims
173+ $optionalClaims.AccessToken = New-Object System.Collections.Generic.List[Microsoft.Graph.PowerShell.Models.MicrosoftGraphOptionalClaim ]
174+ $optionalClaims.IdToken = New-Object System.Collections.Generic.List[Microsoft.Graph.PowerShell.Models.MicrosoftGraphOptionalClaim ]
175+ $optionalClaims.Saml2Token = New-Object System.Collections.Generic.List[Microsoft.Graph.PowerShell.Models.MicrosoftGraphOptionalClaim ]
176+
177+
178+ # Add Optional Claims
179+
180+ $newClaim = CreateOptionalClaim - name " acct"
181+ $optionalClaims.IdToken += ($newClaim )
182+ Update-MgApplication - ApplicationId $spaAadApplication.Id - OptionalClaims $optionalClaims
137183 Write-Host " Done creating the spa application (msal-angular-spa)"
138184
139185 # URL of the AAD application in the Azure portal
140186 # Future? $spaPortalUrl = "https://portal.azure.com/#@"+$tenantName+"/blade/Microsoft_AAD_RegisteredApps/ApplicationMenuBlade/Overview/appId/"+$spaAadApplication.AppId+"/objectId/"+$spaAadApplication.Id+"/isMSAApp/"
141- $spaPortalUrl = " https://portal.azure.com/#blade/Microsoft_AAD_RegisteredApps/ApplicationMenuBlade/CallAnAPI/appId/" + $spaAadApplication.AppId + " /objectId/" + $spaAadApplication.Id + " /isMSAApp/"
187+ $spaPortalUrl = " https://portal.azure.com/#blade/Microsoft_AAD_RegisteredApps/ApplicationMenuBlade/CallAnAPI/appId/" + $spaAadApplication.AppId + " /objectId/" + $spaAadApplication.Id + " /isMSAApp/"
142188 Add-Content - Value " <tr><td>spa</td><td>$currentAppId </td><td><a href='$spaPortalUrl '>msal-angular-spa</a></td></tr>" - Path createdApps.html
143189 $requiredResourcesAccess = New-Object System.Collections.Generic.List[Microsoft.Graph.PowerShell.Models.MicrosoftGraphRequiredResourceAccess ]
144-
145190
146191 # Add Required Resources Access (from 'spa' to 'Microsoft Graph')
147192 Write-Host " Getting access from 'spa' to 'Microsoft Graph'"
@@ -155,19 +200,20 @@ Function ConfigureApplications {
155200
156201 # Update config file for 'spa'
157202 $configFile = $pwd.Path + " \..\SPA\src\app\auth-config.ts"
158- $dictionary = @ { " Enter_the_Application_Id_Here" = $spaAadApplication.AppId ; " Enter_the_Tenant_Info_Here" = $tenantId };
203+ $dictionary = @ { " Enter_the_Application_Id_Here" = $spaAadApplication.AppId ;" Enter_the_Tenant_Info_Here" = $tenantId };
159204
160205 Write-Host " Updating the sample code ($configFile )"
161206
162207 ReplaceInTextFile - configFilePath $configFile - dictionary $dictionary
163- if ($isOpenSSL -eq ' Y' ) {
208+ if ($isOpenSSL -eq ' Y' )
209+ {
164210 Write-Host - ForegroundColor Green " ------------------------------------------------------------------------------------------------"
165211 Write-Host " You have generated certificate using OpenSSL so follow below steps: "
166212 Write-Host " Install the certificate on your system from current folder."
167213 Write-Host - ForegroundColor Green " ------------------------------------------------------------------------------------------------"
168214 }
169215 Add-Content - Value " </tbody></table></body></html>" - Path createdApps.html
170- }
216+ } # end of ConfigureApplications function
171217
172218# Pre-requisites
173219if ($null -eq (Get-Module - ListAvailable - Name " Microsoft.Graph.Applications" )) {
0 commit comments