AzureAD/Create-AadAppsForNav.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
<#
 .Synopsis
  Create Apps in Azure Active Directory to allow Single Signon with NAV using AAD
 .Description
  This function will create an app in AAD, to allow NAV Web and Windows Client to use AAD for authentication
  Optionally the function can also create apps for the Excel AddIn and/or PowerBI integration
 .Parameter AadAdminCredential
  Credentials for your AAD/Office 365 administrator user, who can create apps in the AAD
 .Parameter appIdUri
  Unique Uri to identify the AAD App (typically we use the URL for the Web Client)
 .Parameter publicWebBaseUrl
  URL for the Web Client (defaults to the value of appIdUri)
 .Parameter iconPath
  Path of the image you want to use for the SSO App
 .Parameter IncludeExcelAadApp
  Add this switch to request the function to also create an AAD app for the Excel AddIn
 .Parameter IncludePowerBiAadApp
  Add this switch to request the function to also create an AAD app for the PowerBI service
 .Example
  Create-AadAppsForNAV -AadAdminCredential Get-Credential -appIdUri https://mycontainer/nav/
#>

function Create-AadAppsForNav
{
    Param
    (
        [Parameter(Mandatory=$true)]
        [System.Management.Automation.PSCredential]$AadAdminCredential,
        [Parameter(Mandatory=$true)]
        [string]$appIdUri,
        [Parameter(Mandatory=$false)]
        [string]$publicWebBaseUrl = $appIdUri,
        [Parameter(Mandatory=$false)]
        [string]$iconPath,
        [switch]$IncludeExcelAadApp,
        [switch]$IncludePowerBiAadApp
    )

    function Create-AesKey {
        $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()
        [System.Convert]::ToBase64String($aesManaged.Key)
    }

    $iconchars = $null
    if ($iconPath) {
        $iconChars = [char[]][System.IO.File]::ReadAllBytes($iconPath)
    }

    if (!(Get-PackageProvider -Name NuGet -ListAvailable -ErrorAction Ignore)) {
        Write-Host "Installing NuGet Package Provider"
        Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force -WarningAction Ignore | Out-Null
    }

    if (!(Get-Package -Name AzureRM.ApiManagement -ErrorAction Ignore)) {
        Write-Host "Installing AzureRM.ApiManagement PowerShell package"
        Install-Package AzureRM.ApiManagement -Force -WarningAction Ignore | Out-Null
    }

    if (!(Get-Package -Name AzureRM.Resources -ErrorAction Ignore)) {
        Write-Host "Installing AzureRM.Resources PowerShell package (if this fails, you probably need to update the PowerShellGet module)"
        Install-Package AzureRM.Resources -Force -WarningAction Ignore | Out-Null
    }

    # Login to AzureRm
    $account = Add-AzureRmAccount -Credential $AadAdminCredential

    $AdProperties = @{}

    $adUserObjectId = 0
    Write-Host "Identifying AAD Tenant ID"
    foreach($tenant in $account.Context.Account.Tenants) {
        try {
            Write-Host -NoNewline "Trying $tenant"
            $AadTenant = $tenant
            $AdProperties["AadTenant"] = $AadTenant
            Set-AzureRmContext -Tenant $AadTenant | Out-Null
            $adUser = Get-AzureRmADUser -UserPrincipalName $account.Context.Account.Id
            $adUserObjectId = $adUser.Id
            Write-Host " - Success"
            break
        } catch {
            Write-Host " - Failure"
        }
    }

    if (!$adUserObjectId) {
        throw "Could not identify Aad Tenant ID"
    }

    $graphUrl = "https://graph.windows.net"
    $apiversion = "1.6"

    $authority = "https://login.microsoftonline.com/$AadTenant"

    $clientId = "1950a258-227b-4e31-a9cf-717495945fc2"  # Set well-known client ID for AzurePowerShell
    $resourceAppIdURI = "$graphUrl/" # resource we want to use
    
    # Create Authentication Context tied to Azure AD Tenant
    Write-Host "Authenticate and create authorization headers (requires Microsoft.IdentityMOdel.Clients.ActiveDirectory)"
    $authContext = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext" -ArgumentList $authority
    $userCred = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.UserCredential" -ArgumentList $AadAdminCredential.UserName, $AadAdminCredential.Password
    
    # Acquire token and create authentication headers
    $authResult = $authContext.AcquireToken($resourceAppIdURI, $clientId, $userCred)
    $authHeader = $authResult.CreateAuthorizationHeader()
    $headers = @{"Authorization" = $authHeader; "Content-Type"="application/json"}    

    # Remove "old" AD Application
    Get-AzureRmADApplication -IdentifierUri $appIdUri | Remove-AzureRmADApplication -Force

    # Create AesKey
    $SsoAdAppKeyValue = Create-AesKey
    $AdProperties["SsoAdAppKeyValue"] = $SsoAdAppKeyValue

    Write-Host "Creating AAD App for WebClient"
    $ssoAdApp = New-AzureRmADApplication –DisplayName "NAV WebClient for $appIdUri" `
                                         -HomePage $publicWebBaseUrl `
                                         -IdentifierUris $appIdUri `
                                         -ReplyUrls $publicWebBaseUrl
    
    $SsoAdAppId = $ssoAdApp.ApplicationId.ToString()
    $AdProperties["SsoAdAppId"] = $SsoAdAppId

    # Add a key to the app
    $startDate = Get-Date
    New-AzureRmADAppCredential -ApplicationId $SsoAdAppId `
                               -Password (ConvertTo-SecureString -string $SsoAdAppKeyValue -AsPlainText -Force) `
                               -StartDate $startDate `
                               -EndDate $startDate.AddYears(10) | Out-Null

    # Get oauth2 permission id for sso app
    $url = ("$graphUrl/$aadTenant/applications/$($ssoAdApp.ObjectID)?api-version=$apiversion")
    $result = Invoke-RestMethod -Uri $url -Method "GET" -Headers $headers
    $oauth2permissionid = $result.oauth2Permissions.id
    
    # Add Required Resource Access
    $ssoUrl = "$graphUrl/$AadTenant/applications/$($ssoAdApp.ObjectID)?api-version=$apiversion"

    $ssoPostData = @{"requiredResourceAccess" = @(
         @{ 
            "resourceAppId" = "00000002-0000-0000-c000-000000000000"; 
            "resourceAccess" = @( @{
              "id" = "311a71cc-e848-46a1-bdf8-97ff7156d8e6";
              "type" = "Scope"
            }
            )
         }
      )} | ConvertTo-Json -Depth 99

    # Invoke-RestMethod will not close the connection properly and as such will only allow 2 subsequent calls to Invoke-RestMethod
    # This is why we use Invoke-WebRequest and getting the response content
    (Invoke-WebRequest -UseBasicParsing -Method PATCH -ContentType 'application/json' -Headers $headers -Uri $ssoUrl -Body $ssoPostData).Content | Out-Null

    # Set Logo Image for App
    if ($iconChars) {
        $url = "$graphUrl/$AadTenant/applications/$($ssoAdApp.ObjectID)/mainLogo?api-version=$apiversion"
        $iconStr = -join $iconChars
        (Invoke-WebRequest -UseBasicParsing -Method PUT -ContentType 'image/Png' -Headers $headers -Uri $url -Body $iconStr).Content | Out-Null
    }


    # Excel Ad App
    if ($IncludeExcelAadApp) {
        # Remove "old" Excel AD Application
        $ExcelIdentifierUri = "${appIdUri}ExcelAddIn"
        Get-AzureRmADApplication -IdentifierUri $ExcelIdentifierUri | Remove-AzureRmADApplication -Force

        # Create AD Application
        Write-Host "Creating AAD App for Excel Add-in"
        $excelAdApp = New-AzureRmADApplication –DisplayName "Excel AddIn for $appIdUri" `
                                               -HomePage $publicWebBaseUrl `
                                               -IdentifierUris $ExcelIdentifierUri `
                                               -ReplyUrls $publicWebBaseUrl, "https://az689774.vo.msecnd.net/dynamicsofficeapp/v1.3.0.0/*"

        $ExcelAdAppId = $excelAdApp.ApplicationId.ToString()
        $AdProperties["ExcelAdAppId"] = $ExcelAdAppId

        # Add Required Resource Access
        $excelUrl = "$graphUrl/$AadTenant/applications/$($excelAdApp.ObjectID)?api-version=$apiversion"

        $excelPostData = @{
          "oauth2AllowImplicitFlow" = $true;
          "requiredResourceAccess" = @(
             @{ 
                "resourceAppId" = "$SsoAdAppId"; 
                "resourceAccess" = @( @{
                  "id" = "$oauth2permissionid";
                  "type" = "Scope"
                })
             },
             @{ 
                "resourceAppId" = "00000002-0000-0000-c000-000000000000"; 
                "resourceAccess" = @( @{
                  "id" = "311a71cc-e848-46a1-bdf8-97ff7156d8e6";
                  "type" = "Scope"
                })
             }
          )} | ConvertTo-Json -Depth 99

        (Invoke-WebRequest -UseBasicParsing -Method PATCH -ContentType 'application/json' -Headers $headers -Uri $excelUrl   -Body $excelPostData).Content | Out-Null

        # Check if owner already exists
        $excelLinkedOwnerUrl = "$graphUrl/$AadTenant/applications/$($excelAdApp.ObjectID)/owners?api-version=$apiversion"
        $JsonResponse = ((Invoke-WebRequest -UseBasicParsing -Method Get -ContentType 'application/json' -Headers $headers -Uri $excelLinkedOwnerUrl).Content | ConvertFrom-Json).value
        if(($JsonResponse | Where-Object {$_.objectId -eq $adUserObjectId}) -eq $null){

            # Add owner to Azure Ad Application
            $excelOwnerUrl = "$graphUrl/$AadTenant/applications/$($excelAdApp.ObjectID)/`$links/owners?api-version=$apiversion"
            $excelOwnerPostData  = @{
            "url" = "$graphUrl/$AadTenant/directoryObjects/$adUserObjectId/Microsoft.DirectoryServices.User?api-version=$apiversion"
            } | ConvertTo-Json -Depth 99
    
            (Invoke-WebRequest -UseBasicParsing -Method POST -ContentType 'application/json' -Headers $headers -Uri $excelOwnerUrl -Body $excelOwnerPostData -ErrorAction Ignore).Content | Out-Null            
        }
    }

    # PowerBI Ad App
    if ($IncludePowerBiAadApp) {
        # Remove "old" PowerBI AD Application
        $PowerBiIdentifierUri = "${appIdUri}PowerBI"
        Get-AzureRmADApplication -IdentifierUri $PowerBiIdentifierUri | Remove-AzureRmADApplication -Force
    
        # Create AesKey
        $PowerBiAdAppKeyValue = Create-AesKey
        $AdProperties["PowerBiAdAppKeyValue "] = $PowerBiAdAppKeyValue 
    
        # Create AD Application
        Write-Host "Creating AAD App for PowerBI Service"
        $powerBiAdApp = New-AzureRmADApplication –DisplayName "PowerBI Service for $appIdUri" `
                                                 -HomePage $publicWebBaseUrl `
                                                 -IdentifierUris $PowerBiIdentifierUri `
                                                 -ReplyUrls "${publicWebBaseUrl}OAuthLanding.htm"
        
        $PowerBiAdAppId = $powerBiAdApp.ApplicationId.ToString()
        $AdProperties["PowerBiAdAppId"] = $PowerBiAdAppId 
    
        # Add a key to the app
        $startDate = Get-Date
        New-AzureRmADAppCredential -ApplicationId $PowerBIAdAppId `
                                   -Password (ConvertTo-SecureString -string $PowerBiAdAppKeyValue -AsPlainText -Force) `
                                   -StartDate $startDate `
                                   -EndDate $startDate.AddYears(10) | Out-Null
        
        # Add Required Resource Access
        $powerBiUrl = "$graphUrl/$AadTenant/applications/$($powerBiAdApp.ObjectID)?api-version=$apiversion"
        $powerBiPostData = @{"requiredResourceAccess" = @(
             @{ 
                "resourceAppId" = "00000009-0000-0000-c000-000000000000"; 
                "resourceAccess" = @( @{
                  "id" = "4ae1bf56-f562-4747-b7bc-2fa0874ed46f";
                  "type" = "Scope"
                })
             },
             @{ 
                "resourceAppId" = "00000002-0000-0000-c000-000000000000"; 
                "resourceAccess" = @( @{
                  "id" = "311a71cc-e848-46a1-bdf8-97ff7156d8e6";
                  "type" = "Scope"
                })
             }
          )} | ConvertTo-Json -Depth 99
        
        (Invoke-WebRequest -UseBasicParsing -Method PATCH -ContentType 'application/json' -Headers $headers -Uri $powerBiUrl -Body $powerBiPostData).Content | Out-Null
    }

    $AdProperties
}
Export-ModuleMember -Function Create-AadAppsForNav