< Summary - Kestrun — Combined Coverage

Information
Class: Public.Route.Add-KrAntiforgeryTokenRoute
Assembly: Kestrun.PowerShell.Public
File(s): /home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Route/Add-KrAntiforgeryTokenRoute.ps1
Tag: Kestrun/Kestrun@2d87023b37eb91155071c91dd3d6a2eeb3004705
Line coverage
0%
Covered lines: 0
Uncovered lines: 4
Coverable lines: 4
Total lines: 51
Line coverage: 0%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100 10/13/2025 - 16:52:37 Line coverage: 0% (0/4) Total lines: 51 Tag: Kestrun/Kestrun@10d476bee71c71ad215bb8ab59f219887b5b4a5e

Metrics

File(s)

/home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Route/Add-KrAntiforgeryTokenRoute.ps1

#LineLine coverage
 1<#
 2.SYNOPSIS
 3    Adds a GET endpoint that issues the antiforgery cookie and returns a JSON token payload.
 4.DESCRIPTION
 5    Maps a token endpoint (default: /csrf-token) using the C# extension
 6    [Kestrun.Hosting.KestrunHostMapExtensions]::AddAntiforgeryTokenRoute().
 7    The endpoint is exempt from CSRF validation and responds with:
 8        { "token": "<RequestToken>", "headerName": "<ConfiguredHeaderOrNull>" }
 9.PARAMETER Server
 10    The Kestrun server instance (pipeline-friendly).
 11.PARAMETER Path
 12    Route path to expose. Defaults to "/csrf-token".
 13.PARAMETER PassThru
 14    Return the server instance for chaining.
 15.EXAMPLE
 16    $server | Add-KrAntiforgeryMiddleware -CookieName ".Kestrun.AntiXSRF" -HeaderName "X-CSRF-TOKEN" -PassThru |
 17      Add-KrAntiforgeryTokenRoute -Path "/csrf-token" -PassThru
 18.EXAMPLE
 19    # Client test (PowerShell):
 20    $session = New-Object Microsoft.PowerShell.Commands.WebRequestSession
 21    $info = Invoke-RestMethod "http://127.0.0.1:5000/csrf-token" -WebSession $session
 22    $hdr = $info.headerName ?? 'X-CSRF-TOKEN'
 23    Invoke-RestMethod "http://127.0.0.1:5000/profile" -Method Post -WebSession $session `
 24      -Headers @{ $hdr = $info.token } -ContentType 'application/json' -Body (@{name='Max'}|ConvertTo-Json)
 25#>
 26function Add-KrAntiforgeryTokenRoute {
 27    [KestrunRuntimeApi('Definition')]
 28    [CmdletBinding()]
 29    [OutputType([Kestrun.Hosting.KestrunHost])]
 30    param(
 31        [Parameter(ValueFromPipeline = $true)]
 32        [Kestrun.Hosting.KestrunHost] $Server,
 33
 34        [Parameter()]
 35        [ValidateNotNullOrEmpty()]
 36        [string] $Path = "/csrf-token",
 37
 38        [Parameter()]
 39        [switch] $PassThru
 40    )
 41    begin {
 042        $Server = Resolve-KestrunServer -Server $Server
 043        if (-not $Server) { throw "Server is not initialized. Call New-KrServer and Enable-KrConfiguration first." }
 44    }
 45    process {
 46        # Call the C# extension that maps the endpoint and disables antiforgery on it
 047        $null = [Kestrun.Hosting.KestrunHostMapExtensions]::AddAntiforgeryTokenRoute($Server, $Path)
 48
 049        if ($PassThru) { return $Server }
 50    }
 51}

Methods/Properties

Add-KrAntiforgeryTokenRoute()