| | | 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 | | #> |
| | | 83 | | function 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 |
| | 0 | 152 | | $Server = Resolve-KestrunServer -Server $Server |
| | | 153 | | } |
| | | 154 | | process { |
| | | 155 | | # Build Options only when not provided directly |
| | 0 | 156 | | if ($PSCmdlet.ParameterSetName -ne 'Options') { |
| | 0 | 157 | | $Options = [Kestrun.Authentication.JwtAuthOptions]::new() |
| | | 158 | | |
| | | 159 | | # Build ValidationParameter only when not provided directly |
| | 0 | 160 | | if ($PSCmdlet.ParameterSetName -ne 'ValParamOption') { |
| | 0 | 161 | | $ValidationParameter = [Microsoft.IdentityModel.Tokens.TokenValidationParameters]::new() |
| | 0 | 162 | | if ($PSBoundParameters.ContainsKey('ValidIssuer')) { $ValidationParameter.ValidIssuer = $ValidIssuer } |
| | 0 | 163 | | if ($PSBoundParameters.ContainsKey('ValidIssuers')) { $ValidationParameter.ValidIssuers = $ValidIssuers |
| | 0 | 164 | | if ($PSBoundParameters.ContainsKey('ValidAudience')) { $ValidationParameter.ValidAudience = $ValidAudien |
| | 0 | 165 | | if ($PSBoundParameters.ContainsKey('ValidAudiences')) { $ValidationParameter.ValidAudiences = $ValidAudi |
| | 0 | 166 | | if ($PSBoundParameters.ContainsKey('ValidAlgorithms')) { $ValidationParameter.ValidAlgorithms = $ValidAl |
| | 0 | 167 | | if ($PSBoundParameters.ContainsKey('SkipValidateIssuer')) { $ValidationParameter.ValidateIssuer = -not $ |
| | 0 | 168 | | if ($PSBoundParameters.ContainsKey('SkipValidateAudience')) { $ValidationParameter.ValidateAudience = -n |
| | 0 | 169 | | if ($PSBoundParameters.ContainsKey('SkipValidateLifetime')) { $ValidationParameter.ValidateLifetime = -n |
| | 0 | 170 | | if ($PSBoundParameters.ContainsKey('ValidateIssuerSigningKey')) { $ValidationParameter.ValidateIssuerSig |
| | | 171 | | |
| | 0 | 172 | | if ($PSBoundParameters.ContainsKey('RequireExpirationTime')) { $ValidationParameter.RequireExpirationTim |
| | 0 | 173 | | if ($PSBoundParameters.ContainsKey('RequireSignedTokens')) { $ValidationParameter.RequireSignedTokens = |
| | | 174 | | |
| | 0 | 175 | | if ($PSBoundParameters.ContainsKey('IssuerSigningKey')) { $ValidationParameter.IssuerSigningKey = $Issue |
| | 0 | 176 | | if ($PSBoundParameters.ContainsKey('IssuerSigningKeys')) { $ValidationParameter.IssuerSigningKeys = $Iss |
| | | 177 | | |
| | 0 | 178 | | if ($PSBoundParameters.ContainsKey('ClockSkew')) { $ValidationParameter.ClockSkew = $ClockSkew } |
| | 0 | 179 | | if (-not ([string]::IsNullOrWhiteSpace($Description))) { $Options.Description = $Description } |
| | | 180 | | # Map inbound claims |
| | 0 | 181 | | $ValidationParameter.MapInboundClaims = $MapInboundClaims.IsPresent |
| | | 182 | | # Save token |
| | 0 | 183 | | $Options.SaveToken = $SaveToken.IsPresent |
| | | 184 | | } |
| | | 185 | | |
| | 0 | 186 | | $Options.TokenValidationParameters = $ValidationParameter |
| | | 187 | | # OpenAPI documentation IDs |
| | 0 | 188 | | $Options.DocumentationId = $DocId |
| | | 189 | | # Claim policy |
| | 0 | 190 | | $Options.ClaimPolicy = $ClaimPolicy |
| | | 191 | | } |
| | 0 | 192 | | [Kestrun.Hosting.KestrunHostAuthnExtensions]::AddJwtBearerAuthentication( |
| | 0 | 193 | | $Server, $AuthenticationScheme, $DisplayName, $Options ) | Out-Null |
| | 0 | 194 | | if ($PassThru.IsPresent) { |
| | | 195 | | # if the PassThru switch is specified, return the modified server instance |
| | 0 | 196 | | return $Server |
| | | 197 | | } |
| | | 198 | | } |
| | | 199 | | } |