Public/Connect-Collection.ps1

function Connect-Collection {
    [CmdletBinding(DefaultParameterSetName = 'Interactive')]
    param (
        [Parameter(Mandatory = $false, ParameterSetName = "ATP")]
        [String] $AccessTokenProfile = $null,
        
        [Parameter(Mandatory = $false, ParameterSetName = "Interactive")]
        [String] $TenantId = "common",

        [Parameter(Mandatory = $false)]
        [ValidateSet("beta")]
        $APIVersion = "beta",
        
        [Parameter(Mandatory = $false, ParameterSetName = "Interactive")]
        [Parameter(Mandatory = $false, ParameterSetName = "ATP")]
        [ValidateSet("Production", "Development")]
        [Alias("Instance")]
        [String] $Environment = "Production",
        
        [Parameter(Mandatory = $false)]
        [String] $APIRoot = "https://api.fortytwo.io/{0}/beta{1}"
    )

    process {
        if ($Environment -eq "Development" -and !$PSBoundParameters.ContainsKey('APIRoot')) {
            $APIRoot = "https://dev-api.byfortytwo.com/{0}/$($APIVersion){1}" # Default to dev API root for non-production instances if APIRoot is not explicitly set
        }

        if ([String]::IsNullOrEmpty($AccessTokenProfile)) {
            $AccessTokenProfile = "Fortytwo.IAM.Collections"
        
            if ($Environment -eq "Production") {
                Add-EntraIDInteractiveUserAccessTokenProfile -Name $AccessTokenProfile -TenantId $TenantId -ClientId "68bf2f1d-b9e1-4477-8b90-81314861f05f" -Scope "https://api.fortytwo.io/.default"
            }
            else {
                Add-EntraIDInteractiveUserAccessTokenProfile -Name $AccessTokenProfile -TenantId $TenantId -ClientId "b24eb00a-7f91-489b-b321-3b018da0e8a8" -Scope "api://c61cb4dd-35bf-4db9-b152-58e223782c11/.default"
                if (!$PSBoundParameters.ContainsKey('APIRoot')) {
                    $APIRoot = "https://dev-api.byfortytwo.com/{0}/$($APIVersion){1}" # Default to dev API root for non-production instances if APIRoot is not explicitly set
                }
            }
        }
        elseif (!(Get-EntraIDAccessTokenProfile -Profile $AccessTokenProfile)) {
            throw "Access token profile '$AccessTokenProfile' not found. Please create add it using one of the Add-EntraID*AccessTokenProfile cmdlets."
        }

        if ($TenantId -notmatch "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$") {
            Write-Verbose "TenantId '$TenantId' is not a valid GUID. Attempting to resolve from access token profile '$AccessTokenProfile'."
            $TenantId = (Get-EntraIDAccessToken -Profile $AccessTokenProfile | Get-EntraIDAccessTokenPayload).tid

            if ([String]::IsNullOrEmpty($TenantId) -or $TenantId -notmatch "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$") {
                throw "Unable to resolve TenantId from access token profile '$AccessTokenProfile'. Please provide a valid TenantId."
            }

            Write-Verbose "Resolved TenantId: $TenantId"
        }

        if ($APIRoot.EndsWith('/')) {
            $APIRoot = $APIRoot.TrimEnd('/')
        }

        $Script:APIRoot = $APIRoot
        $Script:AccessTokenProfile = $AccessTokenProfile
        $Script:TenantId = $TenantId
        Write-Verbose "Connected to Fortytwo Collections API at '$APIRoot' using access token profile '$AccessTokenProfile'."
    }
}