Last active
April 7, 2023 13:20
-
-
Save andyhuey/68bade6eceaff64454eaeabae2351552 to your computer and use it in GitHub Desktop.
Get the auth hdr and send it to the clipboard. (old)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# get-auth-hdr-0.ps1 | |
# Get the auth hdr and send it to the clipboard. | |
# ajh 2022-08-29: rewrite to use MSAL.PS. | |
# ajh 2022-11-23: read secret from env vars. | |
#Requires -Version 5.1 | |
#Requires -Modules @{ ModuleName="MSAL.PS"; ModuleVersion="4.0" } | |
# force TLS 1.2 | |
$TLS12Protocol = [System.Net.SecurityProtocolType] 'Tls12' | |
[System.Net.ServicePointManager]::SecurityProtocol = $TLS12Protocol | |
echo $null | clip # clear the clipboard. | |
# read the settings file. | |
$configFilePath = ".\App.config" | |
[xml]$configXML = Get-Content $configFilePath | |
$configXML.configuration.appSettings.add | foreach { | |
$add = $_ | |
switch($add.key) { | |
"ida:Authority" {$authority = $add.value; break} | |
"xyz:ServiceResourceId" {$svcResourceId = $add.value; break} | |
"env:ClientId" {$client_id_var = $add.value; break} | |
"env:ClientSecret" {$client_secret_var = $add.value; break} | |
} | |
} | |
if (!$client_id_var -or !$client_secret_var -or !$authority -or !$svcResourceId) { | |
Write-Error "One or more settings are missing from $configFilePath." | |
return | |
} | |
# and the env vars. | |
$client_id = [Environment]::GetEnvironmentVariable($client_id_var, 'Machine') | |
$client_secret = [Environment]::GetEnvironmentVariable($client_secret_var, 'Machine') | |
if (!$client_id -or !$client_secret) { | |
Write-Error "One or more env vars are missing." | |
return | |
} | |
$scope = $svcResourceId + "/.default" | |
$secSecret = ConvertTo-SecureString $client_secret -AsPlainText -Force | |
$msalToken = Get-MsalToken -ClientId $client_id -ClientSecret $secSecret -Scope $scope -Authority $authority | |
$authHdr = $msalToken.CreateAuthorizationHeader() | |
$fullAuthHdr = "Authorization: $($authHdr)" | |
$fullAuthHdr | clip | |
"auth header has been copied to the clipboard." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment