Files
GraceSolutions 7669cb01ab Almost complete
2026-08-02 20:57:25 -04:00

111 lines
5.2 KiB
PowerShell

#region Connect-RackpadInstance
Function Connect-RackpadInstance
{
<#
.SYNOPSIS
Authenticates against a Rackpad instance and returns a reusable session object.
.DESCRIPTION
Performs a POST against /api/auth/login and captures the bearer token together with its expiration timestamp. The resulting
session object is supplied to every subsequent Rackpad request so that the token is only acquired once per instance.
.PARAMETER BaseURL
The base URL of the Rackpad instance, for example https://rackpad.example.com
.PARAMETER Username
The Rackpad username.
.PARAMETER Password
The Rackpad password.
.PARAMETER SkipCertificateCheck
Ignores server certificate validation errors.
.EXAMPLE
$RackpadSession = Connect-RackpadInstance -BaseURL 'https://rackpad.example.com' -Username 'administrator' -Password $Password
.NOTES
Author: Grace Solutions
Version: 2026.08.02.0000
#>
[CmdletBinding()]
Param
(
[Parameter(Mandatory=$True)]
[ValidateNotNullOrEmpty()]
[System.URI]$BaseURL,
[Parameter(Mandatory=$True)]
[ValidateNotNullOrEmpty()]
[String]$Username,
[Parameter(Mandatory=$True)]
[ValidateNotNullOrEmpty()]
[String]$Password,
[Parameter(Mandatory=$False)]
[Switch]$SkipCertificateCheck
)
Try
{
$NormalizedBaseURL = [System.URI]"$("$($BaseURL.AbsoluteUri)".TrimEnd('/'))"
$BaseAddress = "$($NormalizedBaseURL.AbsoluteUri)".TrimEnd('/')
$AuthenticationBody = New-Object -TypeName 'System.Collections.Specialized.OrderedDictionary'
$AuthenticationBody.username = "$($Username)"
$AuthenticationBody.password = "$($Password)"
$InvokeRestAPIRequestParameters = New-Object -TypeName 'System.Collections.Specialized.OrderedDictionary'
$InvokeRestAPIRequestParameters.URI = [System.URI]"$($BaseAddress)/api/auth/login"
$InvokeRestAPIRequestParameters.Method = 'POST'
$InvokeRestAPIRequestParameters.Body = $AuthenticationBody
$InvokeRestAPIRequestParameters.SkipCertificateCheck = $SkipCertificateCheck.IsPresent
$AuthenticationResponse = Invoke-RestAPIRequest @InvokeRestAPIRequestParameters
Switch ($AuthenticationResponse.IsSuccess)
{
{($_ -eq $False)}
{
Throw "Authentication against `"$($NormalizedBaseURL.AbsoluteUri)`" failed with status code $($AuthenticationResponse.StatusCode). [$($AuthenticationResponse.StatusDescription)] [$($AuthenticationResponse.ErrorMessage)]"
}
}
Switch ([String]::IsNullOrWhiteSpace($AuthenticationResponse.Content.token))
{
{($_ -eq $True)}
{
Throw "Authentication against `"$($NormalizedBaseURL.AbsoluteUri)`" did not return a bearer token."
}
}
$SessionHeader = New-Object -TypeName 'System.Collections.Specialized.OrderedDictionary'
$SessionHeader.Authorization = "Bearer $($AuthenticationResponse.Content.token)"
$SessionHeader.Accept = 'application/json'
$SessionProperties = New-Object -TypeName 'System.Collections.Specialized.OrderedDictionary'
$SessionProperties.BaseURL = $NormalizedBaseURL
$SessionProperties.Header = $SessionHeader
$SessionProperties.SkipCertificateCheck = $SkipCertificateCheck.IsPresent
$SessionProperties.ExpiresAt = Try {[System.DateTime]::Parse("$($AuthenticationResponse.Content.expiresAt)").ToUniversalTime()} Catch {[System.DateTime]::UtcNow.AddHours(1)}
$SessionProperties.UserID = "$($AuthenticationResponse.Content.user.id)"
$SessionProperties.Username = "$($AuthenticationResponse.Content.user.username)"
$SessionProperties.DisplayName = "$($AuthenticationResponse.Content.user.displayName)"
$SessionProperties.Role = "$($AuthenticationResponse.Content.user.role)"
$SessionObject = New-Object -TypeName 'System.Management.Automation.PSObject' -Property ($SessionProperties)
Write-Verbose -Message "[$([System.DateTime]::UtcNow.ToString('yyyy/MM/dd HH:mm:ss.FFF'))] - [INFO] - Authenticated against `"$($NormalizedBaseURL.AbsoluteUri)`" as `"$($SessionObject.Username)`" with the `"$($SessionObject.Role)`" role. [Token Expiration: $($SessionObject.ExpiresAt.ToString('yyyy/MM/dd HH:mm:ss'))]"
Write-Output -InputObject ($SessionObject)
}
Catch
{
Throw
}
}
#endregion