Private/Invoke-DuneApiAuthBearer.ps1
|
<#
.SYNOPSIS Authenticate to the Dune API using a bearer token. .DESCRIPTION Creates a Dune session using the supplied bearer token. The token must be a valid JWT; the session expiry is derived from its `exp` claim. Stores the token as a SecureString in the script-scoped DuneSession variable. .PARAMETER DuneInstance The target Dune instance. Valid values: Prod, Dev, Test, Local. .PARAMETER Tenant The tenant name to authenticate against. .PARAMETER BearerToken The OAuth bearer token string. .EXAMPLE PS> Invoke-DuneApiAuthBearer -DuneInstance Prod -Tenant "yendico" -BearerToken "eyJhbGciOi..." Creates a bearer-token session for the yendico tenant. #> function Invoke-DuneApiAuthBearer { [CmdletBinding()] param( [Parameter(Mandatory)] [ValidateSet("Prod", "Dev","Test","Local")] [string]$DuneInstance, [Parameter(Mandatory)] [string]$Tenant, [Parameter(Mandatory)] [string]$BearerToken ) $DuneApiUrl = Get-DuneApiUrl -DuneInstance $DuneInstance $ParsedToken = Parse-JwtToken -Token $BearerToken # Use local time: the expiry is compared against Get-Date (local) in Assert-DuneSession. $TokenExpiryDate = [System.DateTimeOffset]::FromUnixTimeSeconds($ParsedToken.exp).LocalDateTime $Script:DuneSession = [PSCustomObject]@{ Type = 'BearerToken' DuneApiUrl = $DuneApiUrl Token = ($BearerToken | ConvertTo-SecureString -AsPlainText -Force) ExpiryDate = $TokenExpiryDate Tenant = $Tenant } Write-Verbose "Login successfull" } |