< 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@ca54e35c77799b76774b3805b6f075cdbc0c5fbe
Line coverage
0%
Covered lines: 0
Uncovered lines: 30
Coverable lines: 30
Total lines: 210
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 09/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@826bf9dcf9db118c5de4c78a3259bce9549f0dcd12/21/2025 - 06:07:10 Line coverage: 0% (0/30) Total lines: 210 Tag: Kestrun/Kestrun@8cf7f77e55fd1fd046ea4e5413eb9ef96e49fe6a

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

Methods/Properties

Add-KrJWTBearerAuthentication()