Skip to content

Commit d1440ea

Browse files
committed
Fix Az Token for AzureUSGovernment
1 parent 2baff10 commit d1440ea

2 files changed

Lines changed: 24 additions & 11 deletions

File tree

Identity/AzureStack.Identity.Common.psm1

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -201,26 +201,33 @@ function Export-AzTokenFromCache {
201201
# Resolve target account
202202
#
203203

204-
$targetAccount = $accounts | Where Username -EQ $AccountId
204+
$targetAccountIdentifier = $accounts | Where Username -EQ $AccountId | ForEach { $_.HomeAccountId.Identifier } | Select -Unique
205205

206-
if (-not $targetAccount -or $targetAccount.Count -gt 1) {
206+
if (-not $targetAccountIdentifier -and $data.Account)
207+
{
208+
# Fallback to resolve account identifier from data
209+
$targetAccountIdentifier = ($data.Account | Get-Member -MemberType NoteProperty).Name | ForEach { $data.Account."$_" } | Where Username -EQ $AccountId | Select -ExpandProperty home_account_id -Unique
210+
}
211+
212+
if (-not $targetAccountIdentifier -or $targetAccountIdentifier.Count -gt 1)
213+
{
207214
Write-Error "Unable to resolve acccount for identity '$AccountId'; available accounts: $(ConvertTo-Json $accounts.Username -Compress)"
208215
return
209216
}
210217

211-
Write-Verbose "Target account resolved to: $(ConvertTo-Json $targetAccount -Compress)"
218+
Write-Verbose "Target account resolved to: $targetAccountIdentifier"
212219

213220
#
214221
# Resolve target token(s)
215222
#
216223

217224
$resolvedRefreshToken = $data.RefreshToken."$(Get-Member -InputObject $data.RefreshToken -MemberType NoteProperty |
218-
Where { "$($_.Name)".StartsWith($targetAccount.HomeAccountId.Identifier, [System.StringComparison]::OrdinalIgnoreCase) } |
225+
Where { "$($_.Name)".StartsWith($targetAccountIdentifier, [System.StringComparison]::OrdinalIgnoreCase) } |
219226
Select -ExpandProperty Name)".secret
220227

221228
$resolvedAccessToken = Get-Member -InputObject $data.AccessToken -MemberType NoteProperty |
222229
ForEach { $data.AccessToken."$($_.Name)" } |
223-
Where home_account_id -EQ $targetAccount.HomeAccountId.Identifier |
230+
Where home_account_id -EQ $targetAccountIdentifier |
224231
Where { (-not $_.realm) -or ($_.realm -eq $TenantId) } |
225232
Where target -Like "*$Resource*" |
226233
Sort expires_on -Descending |

Registration/RegisterWithAzure.psm1

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1700,27 +1700,33 @@ function Export-AzRefreshToken
17001700
# Resolve target account
17011701
#
17021702

1703-
$targetAccount = $accounts | Where Username -EQ $AccountId
1703+
$targetAccountIdentifier = $accounts | Where Username -EQ $AccountId | ForEach { $_.HomeAccountId.Identifier } | Select -Unique
17041704

1705-
if (-not $targetAccount -or $targetAccount.Count -gt 1)
1705+
if (-not $targetAccountIdentifier -and $data.Account)
1706+
{
1707+
# Fallback to resolve account identifier from data
1708+
$targetAccountIdentifier = ($data.Account | Get-Member -MemberType NoteProperty).Name | ForEach { $data.Account."$_" } | Where Username -EQ $AccountId | Select -ExpandProperty home_account_id -Unique
1709+
}
1710+
1711+
if (-not $targetAccountIdentifier -or $targetAccountIdentifier.Count -gt 1)
17061712
{
17071713
Write-Error "Unable to resolve acccount for identity '$AccountId'; available accounts: $(ConvertTo-Json $accounts.Username -Compress)"
17081714
return
17091715
}
17101716

1711-
Write-Verbose "Target account resolved to: $(ConvertTo-Json $targetAccount -Compress)"
1717+
Write-Verbose "Target account resolved to: $targetAccountIdentifier"
17121718

17131719
#
17141720
# Resolve target token(s)
17151721
#
17161722

17171723
$resolvedRefreshToken = $data.RefreshToken."$(Get-Member -InputObject $data.RefreshToken -MemberType NoteProperty |
1718-
Where { "$($_.Name)".StartsWith($targetAccount.HomeAccountId.Identifier, [System.StringComparison]::OrdinalIgnoreCase) } |
1719-
Select -ExpandProperty Name)".secret
1724+
Where { "$($_.Name)".StartsWith($targetAccountIdentifier, [System.StringComparison]::OrdinalIgnoreCase) } |
1725+
Select -ExpandProperty Name)".secret
17201726

17211727
if (-not $resolvedRefreshToken)
17221728
{
1723-
Write-Error "Unable to resolve a refresh token for identity '$identityId' with the specified properties..."
1729+
Write-Error "Unable to resolve a refresh token for identity '$AccountId' with the specified properties..."
17241730
return
17251731
}
17261732

0 commit comments

Comments
 (0)