< Summary - Kestrun — Combined Coverage

Information
Class: Public.Authentication.Add-KrJwtBearerAuthentication
Assembly: Kestrun.PowerShell.Public
File(s): /home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Authentication/Add-KrJwtBearerAuthentication.ps1
Tag: Kestrun/Kestrun@0d738bf294e6281b936d031e1979d928007495ff
Line coverage
0%
Covered lines: 0
Uncovered lines: 29
Coverable lines: 29
Total lines: 199
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 08/26/2025 - 14:53:17 Line coverage: 23.8% (5/21) Total lines: 149 Tag: Kestrun/Kestrun@78d1e497d8ba989d121b57aa39aa3c6b22de743109/04/2025 - 22:37:32 Line coverage: 23.8% (5/21) Total lines: 150 Tag: Kestrun/Kestrun@afb7aadc0a8a42bfa2b51ea62c8a6e2cf63faec609/08/2025 - 20:34:03 Line coverage: 26% (6/23) Total lines: 155 Tag: Kestrun/Kestrun@3790ee5884494a7a2a829344a47743e0bf492e7210/13/2025 - 16:52:37 Line coverage: 0% (0/21) Total lines: 151 Tag: Kestrun/Kestrun@10d476bee71c71ad215bb8ab59f219887b5b4a5e12/12/2025 - 17:27:19 Line coverage: 0% (0/29) Total lines: 199 Tag: Kestrun/Kestrun@826bf9dcf9db118c5de4c78a3259bce9549f0dcd

Metrics

File(s)

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

#LineLine coverage
 1<#
 2    .SYNOPSIS
 3        Adds JWT Bearer authentication to the Kestrun server.
 4    .DESCRIPTION
 5        Configures the Kestrun server to use JWT Bearer authentication for incoming requests.
 6    .PARAMETER Server
 7        The Kestrun server instance to configure.
 8    .PARAMETER AuthenticationScheme
 9        The name of the authentication scheme.
 10        This name is used to identify the authentication scheme in the Kestrun server configuration.
 11    .PARAMETER DisplayName
 12        The display name for the authentication scheme.
 13        This name is shown in user interfaces and documentation.
 14    .PARAMETER DocId
 15        The documentation IDs to associate with this authentication scheme in OpenAPI documentation.
 16    .PARAMETER Description
 17        A description of the JWT Bearer authentication scheme.
 18    .PARAMETER Options
 19        An instance of Kestrun.Authentication.JwtAuthOptions containing the JWT Bearer authentication configuration.
 20        This parameter is mandatory when using the 'Options' parameter set.
 21    .PARAMETER ValidationParameter
 22        The token validation parameters used to validate incoming JWT tokens.
 23        This parameter is mandatory when using the 'ValParamOption' parameter set.
 24    .PARAMETER ClaimPolicy
 25        The claim policy configuration for the authentication scheme.
 26    .PARAMETER ValidIssuer
 27        The valid issuer for the JWT tokens.
 28        This parameter is used to validate the issuer of incoming tokens.
 29    .PARAMETER ValidIssuers
 30        An array of valid issuers for the JWT tokens.
 31        This parameter is used to validate the issuer of incoming tokens.
 32    .PARAMETER ValidAudience
 33        The valid audience for the JWT tokens.
 34        This parameter is used to validate the audience of incoming tokens.
 35    .PARAMETER ValidAudiences
 36        An array of valid audiences for the JWT tokens.
 37        This parameter is used to validate the audience of incoming tokens.
 38    .PARAMETER ValidAlgorithms
 39        An array of valid algorithms for the JWT tokens.
 40        This parameter is used to validate the algorithm of incoming tokens.
 41    .PARAMETER SkipValidateIssuer
 42        A switch parameter that, when specified, skips validation of the issuer.
 43    .PARAMETER SkipValidateAudience
 44        A switch parameter that, when specified, skips validation of the audience.
 45    .PARAMETER SkipValidateLifetime
 46        A switch parameter that, when specified, skips validation of the token lifetime.
 47    .PARAMETER ValidateIssuerSigningKey
 48        A switch parameter that, when specified, validates the issuer signing key.
 49    .PARAMETER DoesNotRequireSignedTokens
 50        A switch parameter that, when specified, indicates that signed tokens are not required.
 51    .PARAMETER IssuerSigningKey
 52        The security key used to validate the issuer signing key.
 53    .PARAMETER IssuerSigningKeys
 54        An array of security keys used to validate the issuer signing key.
 55    .PARAMETER ClockSkew
 56        The amount of time the token validation should allow for clock skew.
 57    .PARAMETER DoesNotRequireExpirationTime
 58        A switch parameter that, when specified, indicates that expiration time validation is not required.
 59    .PARAMETER MapInboundClaims
 60        A switch parameter that, when specified, maps inbound claims to Microsoft identity model claims.
 61    .PARAMETER SaveToken
 62        A switch parameter that, when specified, saves the token in the authentication properties after a successful aut
 63    .PARAMETER PassThru
 64        A switch parameter that, when specified, returns the Kestrun server instance.
 65    .EXAMPLE
 66        Add-KrJWTBearerAuthentication -Server $server -Name "MyAuth" -ValidationParameter $validationParameter -ClaimPol
 67        Configure Kestrun server to use JWT Bearer authentication with the specified validation parameters and claim pol
 68    .EXAMPLE
 69        Add-KrJWTBearerAuthentication -Server $server -Name "MyAuth" -ValidIssuer "https://issuer" -ValidAudience "api" 
 70        Configure Kestrun server to use JWT Bearer authentication with the specified issuer, audience, and algorithms, s
 71    .EXAMPLE
 72        Add-KrJWTBearerAuthentication -Server $server -Name "MyAuth" -ValidIssuer "https://issuer" -ValidAudience "api" 
 73        Configure Kestrun server to use JWT Bearer authentication with the specified issuer, audience, and algorithms, s
 74    .EXAMPLE
 75        Add-KrJWTBearerAuthentication -Server $server -Name "MyAuth" -ValidIssuer "https://issuer" -ValidAudience "api" 
 76        Configure Kestrun server to use JWT Bearer authentication with the specified issuer, audience, and algorithms, s
 77    .LINK
 78        https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.authentication.jwtbearer.jwtbearerauthenticati
 79    .NOTES
 80        This function is part of the Kestrun.Authentication module and is used to configure JWT Bearer authentication fo
 81        Maps to Kestrun.Hosting.KestrunHostAuthnExtensions.AddJwtBearerAuthentication
 82#>
 83function Add-KrJWTBearerAuthentication {
 84    [KestrunRuntimeApi('Definition')]
 85    [CmdletBinding(defaultParameterSetName = 'Items')]
 86    [OutputType([Kestrun.Hosting.KestrunHost])]
 87    param(
 88        [Parameter(Mandatory = $false, ValueFromPipeline)]
 89        [Kestrun.Hosting.KestrunHost]$Server,
 90
 91        [Parameter()]
 92        [string]$AuthenticationScheme = [Kestrun.Authentication.AuthenticationDefaults]::JwtBearerAuthenticationSchemeNa
 93
 94        [Parameter()]
 95        [string]$DisplayName = [Kestrun.Authentication.AuthenticationDefaults]::JwtBearerDisplayName,
 96
 97        [Parameter()]
 98        [string[]]$DocId = [Kestrun.Authentication.IOpenApiAuthenticationOptions]::DefaultDocumentationIds,
 99
 100        [Parameter(ParameterSetName = 'Items')]
 101        [string] $Description,
 102
 103        [Parameter(Mandatory = $true, ParameterSetName = 'Options')]
 104        [Kestrun.Authentication.JwtAuthOptions]$Options,
 105
 106        [Parameter(Mandatory = $true, ParameterSetName = 'ValParamOption')]
 107        [Microsoft.IdentityModel.Tokens.TokenValidationParameters]$ValidationParameter,
 108
 109        [Parameter()]
 110        [Kestrun.Claims.ClaimPolicyConfig]$ClaimPolicy,
 111
 112        [Parameter(ParameterSetName = 'Items')]
 113        [string] $ValidIssuer,
 114        [Parameter(ParameterSetName = 'Items')]
 115        [string[]]$ValidIssuers,
 116        [Parameter(ParameterSetName = 'Items')]
 117        [string] $ValidAudience,
 118        [Parameter(ParameterSetName = 'Items')]
 119        [string[]]$ValidAudiences,
 120        [Parameter(ParameterSetName = 'Items')]
 121        [string[]]$ValidAlgorithms,
 122        [Parameter(ParameterSetName = 'Items')]
 123        [switch] $SkipValidateIssuer ,
 124        [Parameter(ParameterSetName = 'Items')]
 125        [switch] $SkipValidateAudience ,
 126        [Parameter(ParameterSetName = 'Items')]
 127        [switch] $SkipValidateLifetime ,
 128        [Parameter(ParameterSetName = 'Items')]
 129        [switch] $ValidateIssuerSigningKey,
 130        [Parameter(ParameterSetName = 'Items')]
 131        [switch] $DoesNotRequireExpirationTime ,
 132        [Parameter(ParameterSetName = 'Items')]
 133        [switch] $DoesNotRequireSignedTokens,
 134        [Parameter(ParameterSetName = 'Items')]
 135        [Microsoft.IdentityModel.Tokens.SecurityKey]$IssuerSigningKey,
 136        [Parameter(ParameterSetName = 'Items')]
 137        [Microsoft.IdentityModel.Tokens.SecurityKey[]]$IssuerSigningKeys,
 138        [Parameter(ParameterSetName = 'Items')]
 139        [TimeSpan]$ClockSkew,
 140        [Parameter(ParameterSetName = 'Items')]
 141        [Parameter(ParameterSetName = 'ValParamOption')]
 142        [switch]$MapInboundClaims,
 143        [Parameter(ParameterSetName = 'Items')]
 144        [Parameter(ParameterSetName = 'ValParamOption')]
 145        [switch]$SaveToken,
 146
 147        [Parameter()]
 148        [switch]$PassThru
 149    )
 150    begin {
 151        # Ensure the server instance is resolved
 0152        $Server = Resolve-KestrunServer -Server $Server
 153    }
 154    process {
 155        # Build Options only when not provided directly
 0156        if ($PSCmdlet.ParameterSetName -ne 'Options') {
 0157            $Options = [Kestrun.Authentication.JwtAuthOptions]::new()
 158
 159            # Build ValidationParameter only when not provided directly
 0160            if ($PSCmdlet.ParameterSetName -ne 'ValParamOption') {
 0161                $ValidationParameter = [Microsoft.IdentityModel.Tokens.TokenValidationParameters]::new()
 0162                if ($PSBoundParameters.ContainsKey('ValidIssuer')) { $ValidationParameter.ValidIssuer = $ValidIssuer }
 0163                if ($PSBoundParameters.ContainsKey('ValidIssuers')) { $ValidationParameter.ValidIssuers = $ValidIssuers 
 0164                if ($PSBoundParameters.ContainsKey('ValidAudience')) { $ValidationParameter.ValidAudience = $ValidAudien
 0165                if ($PSBoundParameters.ContainsKey('ValidAudiences')) { $ValidationParameter.ValidAudiences = $ValidAudi
 0166                if ($PSBoundParameters.ContainsKey('ValidAlgorithms')) { $ValidationParameter.ValidAlgorithms = $ValidAl
 0167                if ($PSBoundParameters.ContainsKey('SkipValidateIssuer')) { $ValidationParameter.ValidateIssuer = -not $
 0168                if ($PSBoundParameters.ContainsKey('SkipValidateAudience')) { $ValidationParameter.ValidateAudience = -n
 0169                if ($PSBoundParameters.ContainsKey('SkipValidateLifetime')) { $ValidationParameter.ValidateLifetime = -n
 0170                if ($PSBoundParameters.ContainsKey('ValidateIssuerSigningKey')) { $ValidationParameter.ValidateIssuerSig
 171
 0172                if ($PSBoundParameters.ContainsKey('RequireExpirationTime')) { $ValidationParameter.RequireExpirationTim
 0173                if ($PSBoundParameters.ContainsKey('RequireSignedTokens')) { $ValidationParameter.RequireSignedTokens = 
 174
 0175                if ($PSBoundParameters.ContainsKey('IssuerSigningKey')) { $ValidationParameter.IssuerSigningKey = $Issue
 0176                if ($PSBoundParameters.ContainsKey('IssuerSigningKeys')) { $ValidationParameter.IssuerSigningKeys = $Iss
 177
 0178                if ($PSBoundParameters.ContainsKey('ClockSkew')) { $ValidationParameter.ClockSkew = $ClockSkew }
 0179                if (-not ([string]::IsNullOrWhiteSpace($Description))) { $Options.Description = $Description }
 180                # Map inbound claims
 0181                $ValidationParameter.MapInboundClaims = $MapInboundClaims.IsPresent
 182                # Save token
 0183                $Options.SaveToken = $SaveToken.IsPresent
 184            }
 185
 0186            $Options.TokenValidationParameters = $ValidationParameter
 187            # OpenAPI documentation IDs
 0188            $Options.DocumentationId = $DocId
 189            # Claim policy
 0190            $Options.ClaimPolicy = $ClaimPolicy
 191        }
 0192        [Kestrun.Hosting.KestrunHostAuthnExtensions]::AddJwtBearerAuthentication(
 0193            $Server, $AuthenticationScheme, $DisplayName, $Options ) | Out-Null
 0194        if ($PassThru.IsPresent) {
 195            # if the PassThru switch is specified, return the modified server instance
 0196            return $Server
 197        }
 198    }
 199}

Methods/Properties

Add-KrJWTBearerAuthentication()