Skip to content

Commit 3822556

Browse files
direct tenant add
1 parent 875df46 commit 3822556

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

Modules/CIPPCore/Public/Get-CIPPAuthentication.ps1

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,18 @@ function Get-CIPPAuthentication {
1919
}
2020
}
2121
Write-Host "Got secrets from dev storage. ApplicationID: $env:ApplicationID"
22+
#Get list of tenants that have 'directTenant' set to true
23+
$tenants = Get-Tenants | Where-Object -Property delegatedPrivilegeStatus -EQ 'directTenant'
24+
if ($tenants) {
25+
Write-Host "Found $($tenants.Count) tenants with directTenant set to true"
26+
$tenants | ForEach-Object {
27+
$name = $_.customerId -replace '-', '_'
28+
if ($secret.$name) {
29+
$name = $_.customerId
30+
Set-Item -Path env:$name -Value $secret.$name -Force
31+
}
32+
}
33+
}
2234
} else {
2335
Write-Information 'Connecting to Azure'
2436
Connect-AzAccount -Identity
@@ -37,6 +49,19 @@ function Get-CIPPAuthentication {
3749
}
3850

3951
$keyvaultname = ($env:WEBSITE_DEPLOYMENT_ID -split '-')[0]
52+
#Get list of tenants that have 'directTenant' set to true
53+
$tenants = Get-Tenants | Where-Object -Property delegatedPrivilegeStatus -EQ 'directTenant'
54+
if ($tenants) {
55+
$tenants | ForEach-Object {
56+
$name = $_.tenantId -replace '-', '_'
57+
$secret = Get-AzKeyVaultSecret -VaultName $keyvaultname -Name $name -AsPlainText -ErrorAction Stop
58+
if ($secret) {
59+
#set the name back to the original tenantId
60+
$name = $_.customerId
61+
Set-Item -Path env:$name -Value $secret -Force
62+
}
63+
}
64+
}
4065
$Variables | ForEach-Object {
4166
Set-Item -Path env:$_ -Value (Get-AzKeyVaultSecret -VaultName $keyvaultname -Name $_ -AsPlainText -ErrorAction Stop) -Force
4267
}

Modules/CIPPCore/Public/GraphHelper/Get-GraphToken.ps1

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,18 @@ function Get-GraphToken($tenantid, $scope, $AsApp, $AppID, $AppSecret, $refreshT
66
if (!$scope) { $scope = 'https://graph.microsoft.com/.default' }
77
if (!$env:SetFromProfile) { $CIPPAuth = Get-CIPPAuthentication; Write-Host 'Could not get Refreshtoken from environment variable. Reloading token.' }
88
#If the $env:<$tenantid> is set, use that instead of the refreshtoken for all tenants.
9+
$ClientRefreshToken = Get-Item env:$tenantid -ErrorAction SilentlyContinue
10+
if ($ClientRefreshToken) {
11+
$refreshToken = $ClientRefreshToken
12+
} else {
13+
$refreshToken = $env:RefreshToken
14+
}
15+
916
$AuthBody = @{
1017
client_id = $env:ApplicationID
1118
client_secret = $env:ApplicationSecret
1219
scope = $Scope
13-
refresh_token = $env:RefreshToken
20+
refresh_token = $refreshToken
1421
grant_type = 'refresh_token'
1522
}
1623
if ($asApp -eq $true) {
@@ -25,7 +32,7 @@ function Get-GraphToken($tenantid, $scope, $AsApp, $AppID, $AppSecret, $refreshT
2532
if ($null -ne $AppID -and $null -ne $refreshToken) {
2633
$AuthBody = @{
2734
client_id = $appid
28-
refresh_token = $RefreshToken
35+
refresh_token = $refreshToken
2936
scope = $Scope
3037
grant_type = 'refresh_token'
3138
}

0 commit comments

Comments
 (0)