Public/Connect-Dune.ps1

<#
.SYNOPSIS
Authenticate to the Dune API for a given tenant.
 
.DESCRIPTION
Establishes an authenticated Dune session using one of three authentication modes: interactive/social login (default), credential (username/password), or bearer token. Sets up session state used by other Dune cmdlets.
 
.PARAMETER Tenant
The tenant name or identifier to authenticate against. This parameter is required.
 
.PARAMETER DuneInstance
The target Dune instance to use. Valid values: Prod, Dev, Test, Local. Defaults to Prod.
 
.PARAMETER Credential
A PSCredential object used for credential-based authentication. Required when using the Credential parameter set.
 
.PARAMETER BearerToken
An OAuth bearer token string used for token-based authentication. Required when using the BearerToken parameter set.
 
.PARAMETER SkipCachedSession
By default the social/interactive login flow first loads a previously cached session from disk; when it matches the requested tenant and Dune instance, re-authentication is skipped. Specify this switch to ignore the cache and force a fresh login. Credential and bearer-token authentication always authenticate fresh and never consult the cache.
 
.EXAMPLE
PS> Connect-Dune -Tenant "yendico"
Uses the default social/interactive login flow for tenant yendico, reusing a matching cached session if one exists.
 
.EXAMPLE
PS> Connect-Dune -Tenant "yendico" -SkipCachedSession
Forces a fresh social/interactive login, ignoring any cached session.
 
.EXAMPLE
PS> $cred = Get-Credential
PS> Connect-Dune -Tenant "yendico" -Credential $cred -DuneInstance Dev
Authenticates to the Dev instance using the supplied credential (Credential parameter set).
 
.EXAMPLE
PS> Connect-Dune -Tenant "yendico" -BearerToken "eyJhbGciOi..."
Authenticates using a bearer token (BearerToken parameter set).
#>

function Connect-Dune {
    [CmdletBinding(DefaultParameterSetName='SocialLogin')]
    param(
        [Parameter(Mandatory)]
        [string]$Tenant,
        [Parameter()]
        [ValidateSet("Prod", "Dev","Test","Local")]
        [string]$DuneInstance = "Prod",
        [Parameter(ParameterSetName="Credential")]
        [PSCredential]$Credential,
        [Parameter(ParameterSetName="BearerToken")]
        [string]$BearerToken,
        [Parameter()]
        [switch]$SkipCachedSession
    )

    begin {}

    process {
        $DefaultApiAuthParams = @{
            DuneInstance = $DuneInstance
            Tenant       = $Tenant.toLower()
        }
        # Only the interactive flow reuses the cache; explicit credentials always authenticate fresh.
        $UseCachedSession = ($PSCmdlet.ParameterSetName -eq 'SocialLogin') -and -not $SkipCachedSession
        if ($UseCachedSession) { Load-DuneSession }
        if (-not $UseCachedSession -or -not $DuneSession -or ($DuneSession.Tenant -ne $Tenant) -or ($DuneSession.DuneApiUrl -ne (Get-DuneApiUrl -DuneInstance $DuneInstance))) {
            switch ($PSCmdlet.ParameterSetName) {
                "SocialLogin" {
                    if ($AuthUrl) {}
                    Invoke-DuneApiAuthSocial @DefaultApiAuthParams
                }
                "Credential" {
                    Invoke-DuneApiAuthCredential @DefaultApiAuthParams -Credential $Credential
                }
                "BearerToken" {
                    Invoke-DuneApiAuthBearer @DefaultApiAuthParams -BearerToken $BearerToken
                }
            }
        }
    }

    end {}
}