Private/Get-GhostHeader.ps1
|
function Get-GhostHeader { <# .SYNOPSIS Returns the request headers for the connected Ghost site. .DESCRIPTION A token is valid for five minutes, so it is created here, right before a request, and reused until it is about to expire. That keeps long running pipelines authenticated without signing a token per request. .EXAMPLE Get-GhostHeader Returns a hashtable with the Authorization and Accept-Version headers. #> [CmdletBinding()] [OutputType([hashtable])] param() $connection = $script:GhostConnection $now = [datetimeoffset]::UtcNow if (-not $connection['Token'] -or $connection['TokenExpires'] -le $now.AddSeconds(30)) { $connection['Token'] = New-GhostToken -KeyId $connection['KeyId'] -Secret $connection['Secret'] -IssuedAt $now $connection['TokenExpires'] = $now.AddSeconds(300) } $header = @{ Authorization = 'Ghost {0}' -f $connection['Token'] } if ($connection['ApiVersion']) { $header['Accept-Version'] = $connection['ApiVersion'] } $header } |