< Summary - Kestrun — Combined Coverage

Information
Class: Public.Cookies.Invoke-KrChallenge
Assembly: Kestrun.PowerShell.Public
File(s): /home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Cookies/Invoke-KrChallenge.ps1
Tag: Kestrun/Kestrun@0d738bf294e6281b936d031e1979d928007495ff
Line coverage
0%
Covered lines: 0
Uncovered lines: 17
Coverable lines: 17
Total lines: 93
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 11/19/2025 - 02:25:56 Line coverage: 0% (0/17) Total lines: 93 Tag: Kestrun/Kestrun@98ff905e5605a920343154665980a71211a03c6d

Metrics

File(s)

/home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Cookies/Invoke-KrChallenge.ps1

#LineLine coverage
 1<#
 2.SYNOPSIS
 3    Challenges the current request to authenticate with the specified authentication scheme.
 4.DESCRIPTION
 5    Wraps ChallengeAsync on the current HTTP context to trigger an authentication challenge.
 6    This is typically used to redirect users to an external identity provider (e.g., OIDC, OAuth2).
 7    Designed for use inside Kestrun route script blocks where $Context is available.
 8.PARAMETER Scheme
 9    Authentication scheme to challenge (e.g., 'oidc', 'Google', 'AzureAD').
 10.PARAMETER RedirectUri
 11    URI to redirect to after successful authentication (default is current request path).
 12.PARAMETER Properties
 13    Additional authentication properties to pass to the challenge.
 14.PARAMETER WhatIf
 15    Shows what would happen if the command runs. The command is not run.
 16.PARAMETER Confirm
 17    Prompts you for confirmation before running the command.
 18.EXAMPLE
 19    Invoke-KrChallenge -Scheme 'oidc' -RedirectUri '/dashboard'
 20
 21    Challenges the user to authenticate with OIDC, redirecting to /dashboard after login.
 22.EXAMPLE
 23    Invoke-KrChallenge -Scheme 'Google'
 24
 25    Challenges the user to authenticate with Google OAuth, redirecting back to the current page.
 26.EXAMPLE
 27    $props = @{
 28        prompt = 'login'
 29        login_hint = 'user@example.com'
 30    }
 31    Invoke-KrChallenge -Scheme 'oidc' -RedirectUri '/hello' -Properties $props
 32
 33    Challenges with additional properties (forces login prompt and hints the username).
 34.OUTPUTS
 35    None. This function initiates an authentication challenge and does not return a value.
 36.NOTES
 37    This function must be called from within a route handler where $Context is available.
 38    After calling this function, the route should return immediately to allow the authentication
 39    middleware to complete the redirect.
 40#>
 41function Invoke-KrChallenge {
 42    [KestrunRuntimeApi('Route')]
 43    [CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'Low')]
 44    [OutputType([void])]
 45    param(
 46        [Parameter(Mandatory = $true)]
 47        [string]$Scheme,
 48
 49        [Parameter()]
 50        [string]$RedirectUri,
 51
 52        [Parameter()]
 53        [hashtable]$Properties
 54    )
 55
 56    # Only works inside a route script block where $Context is available
 057    if ($null -eq $Context -or $null -eq $KrServer) {
 058        Write-KrOutsideRouteWarning
 59        return
 60    }
 61
 062    if ($PSCmdlet.ShouldProcess($Scheme, 'Challenge')) {
 063        Write-KrLog -Level Information -Message 'Initiating authentication challenge for scheme {scheme}' -Values $Schem
 64
 65        # Create AuthenticationProperties
 066        $authProperties = [Microsoft.AspNetCore.Authentication.AuthenticationProperties]::new()
 67
 68        # Set redirect URI
 069        if ($RedirectUri) {
 070            $authProperties.RedirectUri = $RedirectUri
 071            Write-KrLog -Level Debug -Message 'Challenge redirect URI set to {uri}' -Values $RedirectUri
 72        }
 73
 74        # Add any additional properties from hashtable
 075        if ($Properties) {
 076            foreach ($key in $Properties.Keys) {
 077                $value = $Properties[$key]
 078                if ($null -ne $value) {
 079                    $authProperties.Items[$key] = $value.ToString()
 080                    Write-KrLog -Level Debug -Message 'Added challenge property: {key}={value}' -Values $key, $value
 81                }
 82            }
 83        }
 84
 85        # Call ChallengeAsync using the ASP.NET Core authentication extensions
 086        $Context.Challenge($Scheme, $authProperties)
 087        Write-KrLog -Level Information -Message 'Authentication challenge initiated for scheme {scheme}' -Values $Scheme
 88
 89        # CRITICAL: Send a 302 status to prevent Kestrun from sending its own 200 OK response
 90        # The authentication handler has already set up the redirect Location header
 091        Write-KrStatusResponse -StatusCode 302
 92    }
 93}

Methods/Properties

Invoke-KrChallenge()