mirror of
https://github.com/Micke-K/IntuneManagement.git
synced 2026-11-27 09:22:26 +01:00
Add -Federated switch for GitHub Actions federated credential auth (#440)
Silent and batch runs can now sign in with a GitHub Actions OIDC token instead of a client secret or certificate. With -Federated, the tool requests the token and passes it to MSAL with WithClientAssertion. The access token is reused until it is within five minutes of expiry, so a bulk export signs in once rather than on every Graph request. Secret and certificate sign-in are unchanged. Contributed by @mattskare.
This commit is contained in:
@@ -753,9 +753,14 @@ function Add-MSALPrereq_old
|
||||
|
||||
function Connect-MSALClientApp
|
||||
{
|
||||
param($clientId, $tenantId, $secret, $Certificate)
|
||||
param($clientId, $tenantId, $secret, $Certificate, [switch]$Federated)
|
||||
$scopes = [String[]]".default"
|
||||
|
||||
|
||||
if($Federated) {
|
||||
if($global:MSALToken -and $global:MSALToken.ExpiresOn.UtcDateTime -gt [DateTime]::UtcNow.AddMinutes(5)) { return }
|
||||
$script:MSALApp = $null
|
||||
}
|
||||
|
||||
if(-not $script:MSALApp)
|
||||
{
|
||||
if($global:UseGraphEnvironment) {
|
||||
@@ -793,7 +798,19 @@ function Connect-MSALClientApp
|
||||
}
|
||||
$ClientApplicationBuilder = [Microsoft.Identity.Client.ConfidentialClientApplicationBuilder]::Create($clientId).WithCertificate($cert).WithAuthority([URI]::new($authority)) #.WithRedirectUri($redirectUri)
|
||||
}
|
||||
else
|
||||
elseif($Federated)
|
||||
{
|
||||
if(-not $env:ACTIONS_ID_TOKEN_REQUEST_URL)
|
||||
{
|
||||
Write-LogError "ACTIONS_ID_TOKEN_REQUEST_URL is missing. Run in GitHub Actions with 'permissions: id-token: write'"
|
||||
return
|
||||
}
|
||||
$uri = "$($env:ACTIONS_ID_TOKEN_REQUEST_URL)&audience=api://AzureADTokenExchange"
|
||||
$assertion = (Invoke-RestMethod -Uri $uri -Headers @{ Authorization = "Bearer $env:ACTIONS_ID_TOKEN_REQUEST_TOKEN" }).value
|
||||
# shortcut: string overload is flagged "not recommended" by MSAL, a delegate overload cannot run without a runspace
|
||||
$ClientApplicationBuilder = [Microsoft.Identity.Client.ConfidentialClientApplicationBuilder]::Create($clientId).WithClientAssertion($assertion).WithAuthority([URI]::new($authority))
|
||||
}
|
||||
else
|
||||
{
|
||||
return
|
||||
}
|
||||
@@ -1037,6 +1054,10 @@ function Connect-MSALUser
|
||||
{
|
||||
Connect-MSALClientApp $global:AzureAppId $global:TenantId -certificate $global:ClientCert
|
||||
}
|
||||
elseif($global:AzureAppId -and $global:UseFederated -and $global:TenantId)
|
||||
{
|
||||
Connect-MSALClientApp $global:AzureAppId $global:TenantId -Federated
|
||||
}
|
||||
else
|
||||
{
|
||||
Write-Log "Azure AppId, Tenant Id and Sercret/Cert must be specified for batch jobs" 3
|
||||
@@ -1052,7 +1073,7 @@ function Connect-MSALUser
|
||||
|
||||
if($global:MainAppStarted -eq $false)
|
||||
{
|
||||
$script:AppLogin = (Get-SettingValue "GraphAzureAppLogin") -or ($global:TenantId -and $global:AzureAppId -and ($global:ClientSecret -or $global:ClientCert))
|
||||
$script:AppLogin = (Get-SettingValue "GraphAzureAppLogin") -or ($global:TenantId -and $global:AzureAppId -and ($global:ClientSecret -or $global:ClientCert -or $global:UseFederated))
|
||||
|
||||
if((Get-SettingValue "GraphAzureAppLogin") -and -not $global:TenantId)
|
||||
{
|
||||
@@ -1069,8 +1090,8 @@ function Connect-MSALUser
|
||||
|
||||
# Get login info for silent job from settings
|
||||
if(-not $global:AzureAppId) { $global:AzureAppId = Get-SettingValue "GraphAzureAppId" -TenantID $global:TenantId }
|
||||
if(-not $global:ClientSecret -and -not $global:ClientCert) { $global:ClientSecret = Get-SettingValue "GraphAzureAppSecret" -TenantID $global:TenantId }
|
||||
if(-not $global:ClientSecret -and -not $global:ClientCert) { $global:ClientCert = Get-SettingValue "GraphAzureAppCert" -TenantID $global:TenantId }
|
||||
if(-not $global:ClientSecret -and -not $global:ClientCert -and -not $global:UseFederated) { $global:ClientSecret = Get-SettingValue "GraphAzureAppSecret" -TenantID $global:TenantId }
|
||||
if(-not $global:ClientSecret -and -not $global:ClientCert -and -not $global:UseFederated) { $global:ClientCert = Get-SettingValue "GraphAzureAppCert" -TenantID $global:TenantId }
|
||||
|
||||
if($global:AzureAppId -and $global:ClientSecret -and $global:TenantId)
|
||||
{
|
||||
@@ -1080,6 +1101,10 @@ function Connect-MSALUser
|
||||
{
|
||||
Connect-MSALClientApp $global:AzureAppId $global:TenantId -certificate $global:ClientCert
|
||||
}
|
||||
elseif($global:AzureAppId -and $global:UseFederated -and $global:TenantId)
|
||||
{
|
||||
Connect-MSALClientApp $global:AzureAppId $global:TenantId -Federated
|
||||
}
|
||||
else
|
||||
{
|
||||
Write-Log "Azure AppId, Tenant Id and Sercret/Cert must be specified for App logins" 3
|
||||
@@ -2442,4 +2467,4 @@ function Show-MSALDecodedToken {
|
||||
Show-ModalForm $title $dg
|
||||
}
|
||||
|
||||
Export-ModuleMember -alias * -function *
|
||||
Export-ModuleMember -alias * -function *
|
||||
|
||||
@@ -1405,7 +1405,7 @@ function Invoke-InitSilentBatchJob
|
||||
return
|
||||
}
|
||||
|
||||
if(-not $global:AzureAppId -or (-not $global:ClientSecret -and -not $global:ClientCert))
|
||||
if(-not $global:AzureAppId -or (-not $global:ClientSecret -and -not $global:ClientCert -and -not $global:UseFederated))
|
||||
{
|
||||
# Get login info for silent job from settings
|
||||
$global:AzureAppId = Get-SettingValue "GraphAzureAppId" -TenantID $global:TenantId
|
||||
@@ -1419,7 +1419,7 @@ function Invoke-InitSilentBatchJob
|
||||
return
|
||||
}
|
||||
|
||||
if(-not $global:ClientSecret -and -not $global:ClientCert)
|
||||
if(-not $global:ClientSecret -and -not $global:ClientCert -and -not $global:UseFederated)
|
||||
{
|
||||
Write-Log "Secret or Certificate must be specified. Either specify Secret/Certificate in Settings or Command Line" 3
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user