mirror of
https://github.com/Icinga/icinga-powershell-framework.git
synced 2025-12-20 23:00:35 -05:00
Adds tool function for easily creating basic auth headers
This commit is contained in:
parent
9e7ab31608
commit
5cb885dc14
1 changed files with 53 additions and 0 deletions
53
lib/core/tools/New-IcingaBasicAuthHeader.psm1
Normal file
53
lib/core/tools/New-IcingaBasicAuthHeader.psm1
Normal file
|
|
@ -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)
|
||||
};
|
||||
}
|
||||
Loading…
Reference in a new issue