< Summary - Kestrun — Combined Coverage

Information
Class: Public.Authentication.Add-KrOAuth2Authentication
Assembly: Kestrun.PowerShell.Public
File(s): /home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Authentication/Add-KrOAuth2Authentication.ps1
Tag: Kestrun/Kestrun@5f1d2b981c9d7292c11fd448428c6ab6c811c5de
Line coverage
0%
Covered lines: 0
Uncovered lines: 18
Coverable lines: 18
Total lines: 136
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 - 17:40:50 Line coverage: 0% (0/4) Total lines: 49 Tag: Kestrun/Kestrun@fcf33342333cef0516fe0d0912a86709874fd02612/12/2025 - 17:27:19 Line coverage: 0% (0/16) Total lines: 116 Tag: Kestrun/Kestrun@826bf9dcf9db118c5de4c78a3259bce9549f0dcd12/21/2025 - 06:07:10 Line coverage: 0% (0/17) Total lines: 125 Tag: Kestrun/Kestrun@8cf7f77e55fd1fd046ea4e5413eb9ef96e49fe6a03/26/2026 - 03:54:59 Line coverage: 0% (0/18) Total lines: 136 Tag: Kestrun/Kestrun@844b5179fb0492dc6b1182bae3ff65fa7365521d

Coverage delta

Coverage delta 1 -1

Metrics

File(s)

/home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Authentication/Add-KrOAuth2Authentication.ps1

#LineLine coverage
 1<#
 2.SYNOPSIS
 3    Adds OAuth 2.0 (Authorization Code) authentication to the Kestrun server.
 4.DESCRIPTION
 5    Configures the Kestrun server to use a generic OAuth 2.0 authorization-code flow.
 6    You can pass a prebuilt OAuthOptions object, or specify individual items (authority, paths, client, etc.).
 7    For OAuth2 metadata/OpenAPI support, set -OAuth2MetadataUrl or set OAuth2MetadataUrl on the provided OAuth2Options o
 8    To auto-resolve missing endpoints from metadata at startup, also set the ResolveEndpointsFromMetadata property on th
 9    Metadata discovery requires HTTPS by default. To explicitly allow HTTP metadata URLs in trusted non-production envir
 10.PARAMETER Server
 11    The Kestrun server instance to configure. If not specified, the current server instance is used.
 12.PARAMETER AuthenticationScheme
 13    The name of the OAuth authentication scheme (e.g., 'MyOAuth').
 14.PARAMETER DisplayName
 15    The display name for the authentication scheme (e.g., 'GitHub Login').
 16.PARAMETER Description
 17    A description of the OAuth authentication scheme.
 18.PARAMETER Deprecated
 19    If specified, marks the authentication scheme as deprecated in OpenAPI documentation.
 20.PARAMETER ClientId
 21    The OAuth client ID.
 22.PARAMETER ClientSecret
 23    The OAuth client secret.
 24.PARAMETER AuthorizationEndpoint
 25    The OAuth authorization endpoint URL.
 26.PARAMETER TokenEndpoint
 27    The OAuth token endpoint URL.
 28.PARAMETER OAuth2MetadataUrl
 29    Optional OAuth2 authorization server metadata URL (RFC 8414).
 30    Used for OpenAPI metadata and optional endpoint discovery.
 31.PARAMETER CallbackPath
 32    The callback path for OAuth responses.
 33.PARAMETER SaveTokens
 34    If specified, saves the OAuth tokens in the authentication properties.
 35.PARAMETER UsePkce
 36    If specified, enables Proof Key for Code Exchange (PKCE) for enhanced security.
 37.PARAMETER ClaimPolicy
 38    An optional Kestrun.Claims.ClaimPolicyConfig to apply claim policies during authentication.
 39.PARAMETER Options
 40    An instance of Kestrun.Authentication.OAuth2Options containing the OAuth configuration.
 41.PARAMETER PassThru
 42    If specified, returns the modified Kestrun server object.
 43.EXAMPLE
 44    Add-KrOAuth2Authentication -AuthenticationScheme 'MyOAuth' -Options $oauthOptions
 45    Adds an OAuth2 authentication scheme named 'MyOAuth' using the provided options.
 46.NOTES
 47    This is a convenience wrapper around the C# extension AddOAuth2Authentication.
 48    OAuth2MetadataUrl is OpenAPI metadata and is not passed directly to ASP.NET Core AddOAuth.
 49#>
 50function Add-KrOAuth2Authentication {
 51    [KestrunRuntimeApi('Definition')]
 52    [CmdletBinding()]
 53    [OutputType([Kestrun.Hosting.KestrunHost])]
 54    param(
 55        [Parameter(Mandatory = $false, ValueFromPipeline = $true)]
 56        [Kestrun.Hosting.KestrunHost]$Server,
 57
 58        [Parameter(Mandatory = $false)]
 59        [string]$AuthenticationScheme = [Kestrun.Authentication.AuthenticationDefaults]::OAuth2SchemeName,
 60
 61        [Parameter(Mandatory = $false)]
 62        [string]$DisplayName = [Kestrun.Authentication.AuthenticationDefaults]::OAuth2DisplayName,
 63
 64        [Parameter(Mandatory = $false)]
 65        [string]$Description,
 66
 67        [Parameter(Mandatory = $false)]
 68        [switch]$Deprecated,
 69
 70        [Parameter(Mandatory = $false)]
 71        [string]$ClientId,
 72
 73        [Parameter(Mandatory = $false)]
 74        [string]$ClientSecret,
 75
 76        [Parameter(Mandatory = $false)]
 77        [string]$AuthorizationEndpoint,
 78
 79        [Parameter(Mandatory = $false)]
 80        [string]$TokenEndpoint,
 81
 82        [Parameter(Mandatory = $false)]
 83        [string]$OAuth2MetadataUrl,
 84
 85        [Parameter(Mandatory = $false)]
 86        [string]$CallbackPath,
 87
 88        [Parameter(Mandatory = $false)]
 89        [switch]$SaveTokens,
 90
 91        [Parameter(Mandatory = $false)]
 92        [switch]$UsePkce,
 93
 94        [Parameter(Mandatory = $false)]
 95        [Kestrun.Claims.ClaimPolicyConfig]$ClaimPolicy,
 96
 97        [Parameter(Mandatory = $false)]
 98        [Kestrun.Authentication.OAuth2Options]$Options,
 99
 100        [Parameter(Mandatory = $false)]
 101        [switch]$PassThru
 102    )
 103    begin {
 104        # Ensure the server instance is resolved
 0105        $Server = Resolve-KestrunServer -Server $Server
 106    }
 107    process {
 0108        if ($null -eq $Options) {
 109            # Build options from individual parameters if not provided
 0110            $Options = [Kestrun.Authentication.OAuth2Options]::new()
 111        }
 112
 0113        if ($ClientId) { $Options.ClientId = $ClientId }
 0114        if ($ClientSecret) { $Options.ClientSecret = $ClientSecret }
 0115        if ($AuthorizationEndpoint) { $Options.AuthorizationEndpoint = $AuthorizationEndpoint }
 0116        if ($TokenEndpoint) { $Options.TokenEndpoint = $TokenEndpoint }
 0117        if ($OAuth2MetadataUrl) { $Options.OAuth2MetadataUrl = $OAuth2MetadataUrl }
 0118        if ($CallbackPath) { $Options.CallbackPath = $CallbackPath }
 0119        if ($ClaimPolicy) { $Options.ClaimPolicy = $ClaimPolicy }
 0120        if ($Description) { $Options.Description = $Description }
 121
 122        # Set the Deprecated option
 0123        $Options.Deprecated = $Deprecated.IsPresent
 124
 125        # Set other switches
 0126        $Options.SaveTokens = $SaveTokens.IsPresent
 0127        $Options.UsePkce = $UsePkce.IsPresent
 128
 129        # Bridge to your C# extension (parallel to AddCookieAuthentication)
 0130        [Kestrun.Hosting.KestrunHostAuthnExtensions]::AddOAuth2Authentication(
 0131            $Server, $AuthenticationScheme, $DisplayName, $Options) | Out-Null
 0132        if ($PassThru.IsPresent) {
 0133            return $Server
 134        }
 135    }
 136}

Methods/Properties

Add-KrOAuth2Authentication()