Private/Get-Jwt.ps1
|
function Get-Jwt { [CmdletBinding()] param( [Parameter(Mandatory = $true)] [string]$LoginHost, [Parameter(Mandatory = $true)] [string]$Target ) $jwtUrl = "https://$LoginHost/oauth2/default/v1/token" try { $credential = Get-SecureCredential -Target $Target $clientId = $credential.UserName $clientSecret = $credential.GetNetworkCredential().Password $basicHeader = [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes("${clientId}:${clientSecret}")) $headers = @{ 'Authorization' = "Basic $basicHeader" 'Content-Type' = 'application/x-www-form-urlencoded' } $body = @{ 'grant_type' = 'client_credentials' 'scope' = 'service:integration' } $response = Invoke-WebRequestWithLogging -Uri $jwtUrl -Method 'POST' -Headers $headers -Body $body -UseBasicParsing return ($response.Content | ConvertFrom-Json).access_token } catch { throw "Unable to access token endpoint. Details: $($_.Exception.Message)" } } |