Skip to content

Commit bc71243

Browse files
committed
direct tenant token refreshes
1 parent 95f17b3 commit bc71243

File tree

1 file changed

+46
-2
lines changed

1 file changed

+46
-2
lines changed

Modules/CIPPCore/Public/Entrypoints/Timer Functions/Start-UpdateTokensTimer.ps1

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ function Start-UpdateTokensTimer {
77
[CmdletBinding(SupportsShouldProcess = $true)]
88
param()
99
if ($PSCmdlet.ShouldProcess('Start-UpdateTokensTimer', 'Starting Update Tokens Timer')) {
10-
10+
Write-Information 'Starting Update Tokens Timer'
11+
Write-Information "Getting new refresh token for $($env:TenantId)"
1112
# Get the current universal time in the default string format.
1213
$currentUTCtime = (Get-Date).ToUniversalTime()
1314
try {
1415
$Refreshtoken = (Get-GraphToken -ReturnRefresh $true).Refresh_token
15-
1616
if ($env:AzureWebJobsStorage -eq 'UseDevelopmentStorage=true') {
1717
$Table = Get-CIPPTable -tablename 'DevSecrets'
1818
$Secret = Get-CIPPAzDataTableEntity @Table -Filter "PartitionKey eq 'Secret' and RowKey eq 'Secret'"
@@ -39,6 +39,50 @@ function Start-UpdateTokensTimer {
3939
} catch {
4040
Write-LogMessage -API 'Update Tokens' -message 'Error updating refresh token, see Log Data for details. Will try again in 7 days.' -sev 'CRITICAL' -LogData (Get-CippException -Exception $_)
4141
}
42+
43+
# Get new refresh token for each direct added tenant
44+
$TenantList = Get-Tenants -IncludeAll | Where-Object { $_.Excluded -eq $false -and $_.delegatedPrivilegeStatus -eq 'directTenant' }
45+
if ($TenantList.Count -eq 0) {
46+
Write-Information 'No direct tenants found for refresh token update.'
47+
} else {
48+
Write-Information "Found $($TenantList.Count) direct tenants for refresh token update."
49+
foreach ($Tenant in $TenantList) {
50+
try {
51+
Write-Information "Updating refresh token for tenant $($Tenant.displayName) - $($Tenant.customerId)"
52+
$Refreshtoken = (Get-GraphToken -ReturnRefresh $true -TenantId $Tenant.customerId).Refresh_token
53+
if ($env:AzureWebJobsStorage -eq 'UseDevelopmentStorage=true') {
54+
$Table = Get-CIPPTable -tablename 'DevSecrets'
55+
$Secret = Get-CIPPAzDataTableEntity @Table -Filter "PartitionKey eq 'Secret' and RowKey eq 'Secret'"
56+
if ($Secret) {
57+
$name = $Tenant.customerId -replace '-', '_'
58+
$Secret | Add-Member -MemberType NoteProperty -Name $name -Value $Refreshtoken -Force
59+
Add-AzDataTableEntity @Table -Entity $Secret -Force
60+
} else {
61+
Write-Warning "Could not update refresh token for tenant $($Tenant.displayName) ($($Tenant.customerId))."
62+
Write-LogMessage -API 'Update Tokens' -tenant $Tenant.defaultDomainName -tenantid $Tenant.customerId -message "Could not update refresh token for tenant $($Tenant.displayName). Will try again in 7 days." -sev 'CRITICAL'
63+
}
64+
} else {
65+
if ($env:MSI_SECRET) {
66+
Disable-AzContextAutosave -Scope Process | Out-Null
67+
$null = Connect-AzAccount -Identity
68+
$SubscriptionId = $env:WEBSITE_OWNER_NAME -split '\+' | Select-Object -First 1
69+
$null = Set-AzContext -SubscriptionId $SubscriptionId
70+
}
71+
$KV = ($env:WEBSITE_DEPLOYMENT_ID -split '-')[0]
72+
if ($Refreshtoken) {
73+
$name = $Tenant.customerId
74+
Set-AzKeyVaultSecret -VaultName $KV -Name $name -SecretValue (ConvertTo-SecureString -String $Refreshtoken -AsPlainText -Force)
75+
} else {
76+
Write-Warning "Could not update refresh token for tenant $($Tenant.displayName) ($($Tenant.customerId))."
77+
Write-LogMessage -API 'Update Tokens' -tenant $Tenant.defaultDomainName -tenantid $Tenant.customerId -message "Could not update refresh token for tenant $($Tenant.displayName). Will try again in 7 days." -sev 'CRITICAL'
78+
}
79+
}
80+
} catch {
81+
Write-LogMessage -API 'Update Tokens' -tenant $Tenant.defaultDomainName -tenantid $Tenant.customerId -message "Error updating refresh token for tenant $($Tenant.displayName), see Log Data for details. Will try again in 7 days." -sev 'CRITICAL' -LogData (Get-CippException -Exception $_)
82+
}
83+
}
84+
}
85+
4286
# Write an information log with the current time.
4387
Write-Information "PowerShell timer trigger function ran! TIME: $currentUTCtime"
4488

0 commit comments

Comments
 (0)