Private/Invoke-DuneApiAuthBearer.ps1

<#
.SYNOPSIS
Authenticate to the Dune API using a bearer token.
 
.DESCRIPTION
Creates a Dune session using the supplied OAuth bearer token. Stores the token as a SecureString in the script-scoped DuneSession with a one-day expiry.
 
.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

    $BearerTokenExpiryDate = (Get-Date).AddDays(1) #man-made expiry date (session duration)
    $Script:DuneSession = [PSCustomObject]@{
        Type         = 'BearerToken'
        DuneApiUrl   = $DuneApiUrl
        Token        = ($BearerToken | ConvertTo-SecureString -AsPlainText -Force)
        ExpiryDate   = $BearerTokenExpiryDate
        Tenant       = $Tenant
    }
    Write-Verbose "Login successfull"
}