Skip to content

Commit 4d5c105

Browse files
author
rvdwegen
committed
Add Az identity token function
1 parent dacfb38 commit 4d5c105

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
function Get-CIPPAzIdentityToken {
2+
<#
3+
.SYNOPSIS
4+
Get the Azure Identity token for Managed Identity
5+
.DESCRIPTION
6+
This function retrieves the Azure Identity token using the Managed Identity endpoint
7+
.EXAMPLE
8+
Get-CIPPAzIdentityToken
9+
#>
10+
[CmdletBinding()]
11+
param()
12+
13+
$Endpoint = $env:IDENTITY_ENDPOINT
14+
$Secret = $env:IDENTITY_HEADER
15+
$ResourceURI = 'https://management.azure.com/'
16+
17+
if (-not $Endpoint -or -not $Secret) {
18+
throw 'Managed Identity environment variables (IDENTITY_ENDPOINT/IDENTITY_HEADER) not found. Is Managed Identity enabled on the Function App?'
19+
}
20+
21+
$TokenUri = "$($Endpoint)?resource=$($ResourceURI)&api-version=2019-08-01"
22+
$Headers = @{
23+
'X-IDENTITY-HEADER' = $Secret
24+
}
25+
26+
$TokenResponse = Invoke-RestMethod -Method Get -Headers $Headers -Uri $TokenUri
27+
return $TokenResponse.access_token
28+
}

0 commit comments

Comments
 (0)