Public/ps1/Html/Get-AuthenticationRoute.ps1

function Get-ApprxrAuthenticationRoute {
    param(
        $hostURI,
        $channelId,
        $id,
        $type
    )

    # Check if debug mode is enabled
    $debugMode = $false
    try {
        $debugConfig = Get-ApprxrConfigurationValue -name "ApprxrHtmlDebugMode" -ErrorAction SilentlyContinue
        if ($debugConfig -and ($debugConfig -eq $true -or $debugConfig -eq "true" -or $debugConfig -eq "1")) {
            $debugMode = $true
        }
    } catch {
        # Configuration value not found, continue without debug mode
    }

    if ($debugMode) {
        Write-Host "[Get-ApprxrAuthenticationRoute] DEBUG: Starting with parameters - hostURI: $hostURI, channelId: $channelId, id: $id, type: $type" -ForegroundColor Cyan
    }

    $authenticationPath = @{
        hostUri = $hostURI
        channelId = $channelId
        id = $id
        type = $type
    }


    # Determine the authentication id based on the provided parameters
    $authenticationId = $null
    $authenticationInformation = $null

    if ($authenticationPath.id) {
        # Lookup by id
        $authenticationId = Get-ApprxrAuthenticationId -channelId $authenticationPath.channelId -id $authenticationPath.id -hostURI $authenticationPath.hostUri -type $authenticationPath.type
        if ($debugMode) {
            Write-Host "[Get-ApprxrAuthenticationRoute] DEBUG: Lookup by ID - Generated authenticationId: $authenticationId" -ForegroundColor Cyan
        }
    } elseif ($authenticationPath.hostUri -and $authenticationPath.type) {
        # Lookup by hosturi and type
        $authenticationId = Get-ApprxrAuthenticationId -channelId $authenticationPath.channelId -hostURI $authenticationPath.hostUri -type $authenticationPath.type
        if ($debugMode) {
            Write-Host "[Get-ApprxrAuthenticationRoute] DEBUG: Lookup by hostUri and type - Generated authenticationId: $authenticationId" -ForegroundColor Cyan
        }
    } elseif ($authenticationPath.hostUri) {
        # Lookup by hosturi only
        $authenticationId = Get-ApprxrAuthenticationId -channelId $authenticationPath.channelId -hostURI $authenticationPath.hostUri
        if ($debugMode) {
            Write-Host "[Get-ApprxrAuthenticationRoute] DEBUG: Lookup by hostUri only - Generated authenticationId: $authenticationId" -ForegroundColor Cyan
        }
    }

    if ($authenticationId) {
        $authenticationInformation = Get-ConfigurationValue -name $authenticationId -secure
        if ($debugMode) {
            Write-Host "[Get-ApprxrAuthenticationRoute] DEBUG: Retrieved configuration for ID '$authenticationId' - Found: $($null -ne $authenticationInformation)" -ForegroundColor Cyan
        }
    }

    if ($authenticationInformation) {
        $authObj = $authenticationInformation | ConvertFrom-Json
        
        if ($debugMode) {
            Write-Host "[Get-ApprxrAuthenticationRoute] DEBUG: Authentication type: $($authObj.type)" -ForegroundColor Cyan
        }
        
        if ($authObj.type -eq 'NTLM') {
            if ($debugMode) {
                Write-Host "[Get-ApprxrAuthenticationRoute] DEBUG: Returning NTLM authentication with Negotiate scheme" -ForegroundColor Cyan
            }
            $returnObj = [PSCustomObject]@{
                username = $authObj.username
                password = $authObj.password
                Authentication = "Negotiate"
            }
            return $returnObj
        } else {
            # Return authentication object for other types
            if ($debugMode) {
                Write-Host "[Get-ApprxrAuthenticationRoute] DEBUG: Returning authentication object for type: $($authObj.type)" -ForegroundColor Cyan
            }
            return $authObj
        }
    } else {
        # No authentication found
        if ($debugMode) {
            Write-Host "[Get-ApprxrAuthenticationRoute] DEBUG: No authentication information found" -ForegroundColor Cyan
        }
        return $null
    }
}