From 5cb885dc1485a3e3faf51de16d1659e112ecc4cc Mon Sep 17 00:00:00 2001 From: Lord Hepipud Date: Thu, 30 Apr 2020 13:48:45 +0200 Subject: [PATCH] Adds tool function for easily creating basic auth headers --- lib/core/tools/New-IcingaBasicAuthHeader.psm1 | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 lib/core/tools/New-IcingaBasicAuthHeader.psm1 diff --git a/lib/core/tools/New-IcingaBasicAuthHeader.psm1 b/lib/core/tools/New-IcingaBasicAuthHeader.psm1 new file mode 100644 index 0000000..0728747 --- /dev/null +++ b/lib/core/tools/New-IcingaBasicAuthHeader.psm1 @@ -0,0 +1,53 @@ +<# +.SYNOPSIS + Creates a basic auth header for web requests in case the Get-Credential + method is not supported or working properly +.DESCRIPTION + Creates a basic auth header for web requests in case the Get-Credential + method is not supported or working properly +.FUNCTIONALITY + Creates a hashtable with a basic authorization header as Base64 encoded +.EXAMPLE + PS>New-IcingaBasicAuthHeader -Username 'example_user' -Password $SecurePasswordString; +.EXAMPLE + PS>New-IcingaBasicAuthHeader -Username 'example_user' -Password (Read-Host -Prompt 'Please enter your password' -AsSecureString); +.EXAMPLE + PS>New-IcingaBasicAuthHeader -Username 'example_user' -Password (ConvertTo-IcingaSecureString 'my_secret_password'); +.PARAMETER Username + The user we will use to authenticate for +.PARAMETER Password + The password for the user provided as SecureString +.INPUTS + System.String +.OUTPUTS + System.Hashtable +.LINK + https://github.com/Icinga/icinga-powershell-framework +#> + +function New-IcingaBasicAuthHeader() +{ + param( + [string]$Username = $null, + [SecureString]$Password = $null + ); + + if ($null -eq $Password -or [string]::IsNullOrEmpty($Username)) { + Write-Host 'Please specify your Username and Password to continue'; + return @{}; + } + + $Credentials = [System.Convert]::ToBase64String( + [System.Text.Encoding]::ASCII.GetBytes( + [string]::Format( + '{0}:{1}', + $Username, + (ConvertFrom-IcingaSecureString $Password) + ) + ) + ); + + return @{ + 'Authorization' = [string]::Format('Basic {0}', $Credentials) + }; +}