@@ -16,28 +16,27 @@ Function Invoke-ExecCreateSAMApp {
16
16
try {
17
17
$Token = $Request.body
18
18
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
20
20
$TenantId = (Invoke-RestMethod ' https://graph.microsoft.com/v1.0/organization' - Headers @ { authorization = " Bearer $ ( $Token.access_token ) " } - Method GET - ContentType ' application/json' ).value.id
21
21
# Find Existing app registration
22
22
$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
23
23
# Check if the appId has the redirect URI, if not, add it.
24
24
if ($AppId ) {
25
25
Write-Host " Found existing app: $ ( $AppId.id ) . Reusing."
26
26
$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'
35
34
} else {
36
35
$state = ' created'
37
36
$ModuleBase = Get-Module - Name CIPPCore | Select-Object - ExpandProperty ModuleBase
38
37
$SamManifestFile = Get-Item (Join-Path $ModuleBase ' Public\SAMManifest.json' )
39
38
$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 " )
41
40
$app = $app | ConvertTo-Json - Depth 15
42
41
$AppId = (Invoke-RestMethod ' https://graph.microsoft.com/v1.0/applications' - Headers @ { authorization = " Bearer $ ( $Token.access_token ) " } - Method POST - Body $app - ContentType ' application/json' )
43
42
$attempt = 0
@@ -76,17 +75,17 @@ Function Invoke-ExecCreateSAMApp {
76
75
if ($env: AzureWebJobsStorage -eq ' UseDevelopmentStorage=true' ) {
77
76
$DevSecretsTable = Get-CIPPTable - tablename ' DevSecrets'
78
77
$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
82
81
Add-CIPPAzDataTableEntity @DevSecretsTable - Entity $Secret - Force
83
82
Write-Information ($Secret | ConvertTo-Json - Depth 5 )
84
83
} else {
85
84
Set-AzKeyVaultSecret - VaultName $kv - Name ' tenantid' - SecretValue (ConvertTo-SecureString - String $TenantId - AsPlainText - Force)
86
85
Set-AzKeyVaultSecret - VaultName $kv - Name ' applicationid' - SecretValue (ConvertTo-SecureString - String $Appid.appId - AsPlainText - Force)
87
86
Set-AzKeyVaultSecret - VaultName $kv - Name ' applicationsecret' - SecretValue (ConvertTo-SecureString - String $AppPassword - AsPlainText - Force)
88
87
}
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' }
90
89
}
91
90
92
91
} catch {
0 commit comments