diff --git a/CloudAPIPowerShellManagement.psm1 b/CloudAPIPowerShellManagement.psm1 index a8d312f..7d98a83 100644 --- a/CloudAPIPowerShellManagement.psm1 +++ b/CloudAPIPowerShellManagement.psm1 @@ -89,7 +89,9 @@ function Initialize-CloudAPIManagement [string] $GraphEnvironment, [string] - $GCCType + $GCCType, + [switch] + $Federated ) @@ -104,6 +106,7 @@ function Initialize-CloudAPIManagement $global:hideUI = ($Silent -eq $true) $global:SilentBatchFile = $SilentBatchFile + $global:UseFederated = ($Federated -eq $true) if($tenantId) { @@ -123,9 +126,13 @@ function Initialize-CloudAPIManagement Write-Warning "Azure App Id is missing. Use -AppId on the command line" } - if($global:ClientSecret -or $global:ClientCert) + if($global:ClientSecret -or $global:ClientCert -or $global:UseFederated) { - if($global:ClientSecret) + if($global:UseFederated) + { + Write-Host "Using GitHub Actions Federated Credential" + } + elseif($global:ClientSecret) { Write-Host "Using Azure App Secret" } diff --git a/Extensions/MSALAuthentication.psm1 b/Extensions/MSALAuthentication.psm1 index 6ce0d33..52a018b 100644 --- a/Extensions/MSALAuthentication.psm1 +++ b/Extensions/MSALAuthentication.psm1 @@ -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 * \ No newline at end of file +Export-ModuleMember -alias * -function * diff --git a/Extensions/MSGraph.psm1 b/Extensions/MSGraph.psm1 index d2d34db..a8403bb 100644 --- a/Extensions/MSGraph.psm1 +++ b/Extensions/MSGraph.psm1 @@ -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 diff --git a/Start-IntuneManagement.ps1 b/Start-IntuneManagement.ps1 index 0e514db..5c1b842 100644 --- a/Start-IntuneManagement.ps1 +++ b/Start-IntuneManagement.ps1 @@ -17,7 +17,9 @@ param( [string] $Secret, [string] - $Certificate + $Certificate, + [switch] + $Federated ) Import-Module ($PSScriptRoot + "\CloudAPIPowerShellManagement.psd1") -Force $param = $PSBoundParameters