@@ -16,28 +16,27 @@ Function Invoke-ExecCreateSAMApp {
1616 try {
1717 $Token = $Request.body
1818 if ($Token ) {
19- $URL = ($Request.headers .' x-ms-original-url' ).split(' ? ' ) | Select-Object - First 1
19+ $URL = ($Request.headers .' x-ms-original-url' ).split(' /api ' ) | Select-Object - First 1
2020 $TenantId = (Invoke-RestMethod ' https://graph.microsoft.com/v1.0/organization' - Headers @ { authorization = " Bearer $ ( $Token.access_token ) " } - Method GET - ContentType ' application/json' ).value.id
2121 # Find Existing app registration
2222 $AppId = (Invoke-RestMethod ' https://graph.microsoft.com/v1.0/applications' - Headers @ { authorization = " Bearer $ ( $Token.access_token ) " } - Method GET - ContentType ' application/json' - Body " { `" filter`" : `" displayName eq 'CIPP-SAM'`" }" ).value | Select-Object - Last 1
2323 # Check if the appId has the redirect URI, if not, add it.
2424 if ($AppId ) {
2525 Write-Host " Found existing app: $ ( $AppId.id ) . Reusing."
2626 $state = ' updated'
27- if ($AppId.web.redirectUris -notcontains $URL ) {
28- $ModuleBase = Get-Module - Name CIPPCore | Select-Object - ExpandProperty ModuleBase
29- $SamManifestFile = Get-Item (Join-Path $ModuleBase ' Public\SAMManifest.json' )
30- $app = Get-Content $SamManifestFile.FullName | ConvertFrom-Json
31- $App.web.redirectUris = @ ($App.web.redirectUris + $URL ) # change to SPA URL.
32- $app = $app | ConvertTo-Json - Depth 15
33- Invoke-RestMethod " https://graph.microsoft.com/v1.0/applications/$ ( $AppId.id ) " - Headers @ { authorization = " Bearer $ ( $Token.access_token ) " } - Method PATCH - Body $app - ContentType ' application/json'
34- }
27+ # remove the entire web object from the app registration
28+ $ModuleBase = Get-Module - Name CIPPCore | Select-Object - ExpandProperty ModuleBase
29+ $SamManifestFile = Get-Item (Join-Path $ModuleBase ' Public\SAMManifest.json' )
30+ $app = Get-Content $SamManifestFile.FullName | ConvertFrom-Json
31+ $app.web.redirectUris = @ (" $ ( $url ) /authredirect" )
32+ $app = ConvertTo-Json - Depth 15 - Compress - InputObject $app
33+ Invoke-RestMethod " https://graph.microsoft.com/v1.0/applications/$ ( $AppId.id ) " - Headers @ { authorization = " Bearer $ ( $Token.access_token ) " } - Method PATCH - Body $app - ContentType ' application/json'
3534 } else {
3635 $state = ' created'
3736 $ModuleBase = Get-Module - Name CIPPCore | Select-Object - ExpandProperty ModuleBase
3837 $SamManifestFile = Get-Item (Join-Path $ModuleBase ' Public\SAMManifest.json' )
3938 $app = Get-Content $SamManifestFile.FullName | ConvertFrom-Json
40- $App .web.redirectUris = @ ($App .web.redirectUris + $URL ) # change to SPA URL.
39+ $app .web.redirectUris = @ (" $ ( $url ) /authredirect " )
4140 $app = $app | ConvertTo-Json - Depth 15
4241 $AppId = (Invoke-RestMethod ' https://graph.microsoft.com/v1.0/applications' - Headers @ { authorization = " Bearer $ ( $Token.access_token ) " } - Method POST - Body $app - ContentType ' application/json' )
4342 $attempt = 0
@@ -76,17 +75,17 @@ Function Invoke-ExecCreateSAMApp {
7675 if ($env: AzureWebJobsStorage -eq ' UseDevelopmentStorage=true' ) {
7776 $DevSecretsTable = Get-CIPPTable - tablename ' DevSecrets'
7877 $Secret = Get-CIPPAzDataTableEntity @DevSecretsTable - Filter " PartitionKey eq 'Secret' and RowKey eq 'Secret'"
79- $Secret.TenantId = $TenantId
80- $Secret.ApplicationId = $AppId.appId
81- $Secret.ApplicationSecret = $AppPassword
78+ $Secret | Add-Member - MemberType NoteProperty - Name ' tenantid ' - Value $TenantId - Force
79+ $Secret | Add-Member - MemberType NoteProperty - Name ' applicationid ' - Value $AppId.appId - Force
80+ $Secret | Add-Member - MemberType NoteProperty - Name ' applicationsecret ' - Value $AppPassword - Force
8281 Add-CIPPAzDataTableEntity @DevSecretsTable - Entity $Secret - Force
8382 Write-Information ($Secret | ConvertTo-Json - Depth 5 )
8483 } else {
8584 Set-AzKeyVaultSecret - VaultName $kv - Name ' tenantid' - SecretValue (ConvertTo-SecureString - String $TenantId - AsPlainText - Force)
8685 Set-AzKeyVaultSecret - VaultName $kv - Name ' applicationid' - SecretValue (ConvertTo-SecureString - String $Appid.appId - AsPlainText - Force)
8786 Set-AzKeyVaultSecret - VaultName $kv - Name ' applicationsecret' - SecretValue (ConvertTo-SecureString - String $AppPassword - AsPlainText - Force)
8887 }
89- $Results = @ {' message' = " Succesfully $state the application registration. The application ID is $ ( $AppId.id ) . You may continue to the next step." ; severity = ' success' }
88+ $Results = @ {' message' = " Succesfully $state the application registration. The application ID is $ ( $AppId.appid ) . You may continue to the next step." ; severity = ' success' }
9089 }
9190
9291 } catch {
0 commit comments