Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request logging 'User' attribute is always set to '-'; cannot be customized #1130

Closed
HXK8 opened this issue Aug 24, 2023 · 0 comments · Fixed by #1162
Closed

Request logging 'User' attribute is always set to '-'; cannot be customized #1130

HXK8 opened this issue Aug 24, 2023 · 0 comments · Fixed by #1162
Assignees
Labels
Milestone

Comments

@HXK8
Copy link

HXK8 commented Aug 24, 2023

Describe the Bug

The User attribute does not pick up the logged-in user's username and is always - in request logging. Relevant snippet:

  • # build a request object
    $item = @{
    Host = $Request.RemoteEndPoint.Address.IPAddressToString
    RfcUserIdentity = '-'
    User = '-'
    Date = [DateTime]::Now.ToString('dd/MMM/yyyy:HH:mm:ss zzz')
    Request = @{
    Method = $Request.HttpMethod.ToUpperInvariant()
    Resource = $Path
    Protocol = "HTTP/$($Request.ProtocolVersion)"
    Referrer = $Request.UrlReferrer
    Agent = $Request.UserAgent
    }
    Response = @{
    StatusCode = $Response.StatusCode
    StatusDescription = $Response.StatusDescription
    Size = '-'
    }
    }

Steps to Reproduce

Steps to reproduce the behavior:

  • Configure an authentication scheme, e.g., Azure AD:
    $azureAdAuthParams = @{ ... }

    $scheme = New-PodeAuthAzureADScheme @azureAdAuthParams

    $scheme | Add-PodeAuth -Name 'Login' -SuccessUseOrigin -SuccessUrl '/' -ScriptBlock {
        param($user, $accessToken, $refreshToken, $response)

        $WebEvent.Session.Data.id_token = Convertfrom-PodeJWT -Token $response.id_token -IgnoreSignature

        return @{ User = $user }
    }
  • Create an authenticated route:
    Add-PodeRoute -Method Get -Path '/authtest' -Authentication 'Login' -ScriptBlock {
        Write-PodeJsonResponse -Value $WebEvent.Session.Data
    } -PassThru |
    Set-PodeOARouteInfo -Summary 'Testing response from service (authtest)'
  • Launch the server and send a request to /authtest and authenticate
  • Review the server console logs
  • Notice the User attribute is always -

Expected Behavior

The User attribute picks up the username of the logged-in user (if it exists). Additionally, provide an option to customize the User attribute if possible.

Screenshots

Example request log entry:

[{"Host":"127.0.0.1","Request":{"Referrer":"","Method":"GET","Protocol":"HTTP/1.1","Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36 Edg/115.0.1901.203","Resource":"/authtest"},"RfcUserIdentity":"-","Date":"23-Aug.-2023:14:43:34 -04:00","User":"-","Response":{"StatusCode":200,"StatusDescription":"OK","Size":875}}]

Platform

  • OS: Linux/Windows
  • Browser: Chrome
  • Versions:
    • Pode: Pode v2.8.0
    • PowerShell: 7.3.6

Additional Context

Attempts to customize the logging method using $WebEvent.Session.Data did not work and led to a null value for User.

    $method = New-PodeLoggingMethod -Custom -Batch 10 -BatchTimeout 5 -ScriptBlock {
        param($item)

        $item | ForEach-Object {
            $itemInstance = $_

            $itemInstance.Keys | ForEach-Object {
                if ("$_" -eq "User") {
                    if ([string]::IsNotNullOrEmpty($WebEvent.Session.Data.id_token.preferred_username)) {
                        Write-Host "Setting `"User`" to `"$($WebEvent.Session.Data.id_token.preferred_username)`" ..."
                        $itemInstance["User"] = $WebEvent.Session.Data.id_token.preferred_username
                    }
                    else {
                        Write-Host "`"`$WebEvent.Session.Data.id_token.preferred_username`" is null."
                    }
                }
            }
        }
        $logValue = ConvertTo-Json -InputObject $item -Depth 25 -Compress
        $logValue = Protect-PodeLogItem -Item $logValue
        $logValue | Out-Default
    }

    $method | Enable-PodeRequestLogging -Raw
    $method | Enable-PodeErrorLogging -Raw
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

2 participants