Public/Assert-SFAccessToken.ps1

<#
.SYNOPSIS
Ensures a valid SuccessFactors access token exists. Renews it if expired or missing.

.DESCRIPTION
Validates the cached access token and renews it if it has expired or if the ForceRenewal switch is specified. Throws an error if no active connection exists.

.PARAMETER ForceRenewal
Force renewal of the access token even if the current token is still valid.

.OUTPUTS
None - Updates the cached token internally.

.NOTES
The function checks token expiry with a 60-second buffer to ensure renewal before actual expiration. Token state is stored in module scope variables.
#>

function Assert-SFAccessToken {

    if ($Script:AccessToken -and $Script:AccessTokenExpiry -and (Get-Date) -lt $Script:AccessTokenExpiry) {
        return
    }

    if (-not $Script:ConnectorConfiguration) {
        throw "Not connected to SuccessFactors. Call Connect-SF first."
    }

    Connect-SF -ConnectorConfiguration $Script:ConnectorConfiguration -ForceRenewal
}