Public/ps1/Html/Get-ApprxrAuthenticationRoute.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) {
        Log "[Get-ApprxrAuthenticationRoute] DEBUG: Starting with parameters - hostURI: $hostURI, channelId: $channelId, id: $id, type: $type"
    } else {
        Log "[Get-ApprxrAuthenticationRoute] INFO: Starting authentication route lookup"
    }

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


    # Determine the authentication id based on the provided parameters
    # Try each lookup method in order and check if credentials are found
    $authenticationId = $null
    $authenticationInformation = $null
    $lookupMethods = @()

    # Method 1: Try explicit id first
    if ($authenticationPath.id) {
        $lookupMethods += @{
            name = "Explicit ID"
            id = $authenticationPath.id
            debug = "Using explicit id parameter: $($authenticationPath.id)"
        }
    }
    
    # Method 2: Try hostUri + channelId
    if ($authenticationPath.hostUri -and $authenticationPath.channelId) {
        $lookupMethods += @{
            name = "HostUri + ChannelId"
            id = $authenticationPath.hostUri + $authenticationPath.channelId
            debug = "Using hostUri + channelId: $($authenticationPath.hostUri) + $($authenticationPath.channelId)"
        }
    }
    
    # Method 3: Try hostUri + type
    if ($authenticationPath.hostUri -and $authenticationPath.type) {
        $lookupMethods += @{
            name = "HostUri + Type"
            id = $authenticationPath.hostUri + $authenticationPath.type
            debug = "Using hostUri + type: $($authenticationPath.hostUri) + $($authenticationPath.type)"
        }
    }
    
    # Method 4: Try hostUri only
    if ($authenticationPath.hostUri) {
        $lookupMethods += @{
            name = "HostUri only"
            id = $authenticationPath.hostUri
            debug = "Using hostUri only: $($authenticationPath.hostUri)"
        }
    }

    # Try each lookup method in sequence until credentials are found
    foreach ($method in $lookupMethods) {
        if ($debugMode) {
            Log "[Get-ApprxrAuthenticationRoute] DEBUG: Trying lookup method '$($method.name)' - $($method.debug)"
        }
        
        $candidateId = Get-ApprxrAuthenticationId -channelId $authenticationPath.channelId -id $method.id -hostURI $authenticationPath.hostUri -type $authenticationPath.type
        
        if ($debugMode) {
            Log "[Get-ApprxrAuthenticationRoute] DEBUG: Generated authenticationId: $candidateId"
        }
        
        # Try to retrieve configuration for this ID
        $candidateInformation = Get-ApprxrConfigurationValue -name $candidateId -secure -ErrorAction SilentlyContinue
        
        if ($candidateInformation) {
            if ($debugMode) {
                Log "[Get-ApprxrAuthenticationRoute] DEBUG: Found credentials for method '$($method.name)' with ID '$candidateId'"
            }
            $authenticationId = $candidateId
            $authenticationInformation = $candidateInformation
            break
        } else {
            if ($debugMode) {
                Log "[Get-ApprxrAuthenticationRoute] DEBUG: No credentials found for method '$($method.name)' with ID '$candidateId', trying next method..."
            }
        }
    }

    # If no exact match found and hostUri is available, try to find any configuration starting with hostUri
    if (-not $authenticationInformation -and $authenticationPath.hostUri) {
        if ($debugMode) {
            Log "[Get-ApprxrAuthenticationRoute] DEBUG: No exact match found, searching for configuration keys starting with hostUri: $($authenticationPath.hostUri)"
        }
        
        # Search for keys starting with hostUri (generic fallback)
        try {
            $allConfigs = Get-ApprxrConfiguration
            
            foreach ($configKey in $allConfigs.PSObject.Properties | Where-Object { $_.Name.StartsWith($authenticationPath.hostUri) }) {
                $candidateId = $configKey.Name
                $candidateInformation = Get-ApprxrConfigurationValue -name $candidateId -secure -ErrorAction SilentlyContinue
                
                if ($candidateInformation) {
                    if ($debugMode) {
                        Log "[Get-ApprxrAuthenticationRoute] DEBUG: Found generic credentials starting with hostUri using ID '$candidateId'"
                    }
                    $authenticationId = $candidateId
                    $authenticationInformation = $candidateInformation
                    break
                }
            }
        } catch {
            if ($debugMode) {
                Log "[Get-ApprxrAuthenticationRoute] DEBUG: Error searching for generic credentials: $_"
            }
        }
    }

    if ($debugMode) {
        if ($authenticationId) {
            Log "[Get-ApprxrAuthenticationRoute] DEBUG: Successfully found authentication with ID '$authenticationId'"
        } else {
            Log "[Get-ApprxrAuthenticationRoute] DEBUG: No credentials found using any lookup method"
        }
    }

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