| | | 1 | | <# |
| | | 2 | | .SYNOPSIS |
| | | 3 | | Adds API key authentication to the Kestrun server. |
| | | 4 | | .DESCRIPTION |
| | | 5 | | Configures the Kestrun server to use API key authentication for incoming requests. |
| | | 6 | | .PARAMETER Server |
| | | 7 | | The Kestrun server instance to configure. |
| | | 8 | | .PARAMETER AuthenticationScheme |
| | | 9 | | The name of the API key authentication scheme. |
| | | 10 | | .PARAMETER DisplayName |
| | | 11 | | The display name of the API key authentication scheme. |
| | | 12 | | .PARAMETER Description |
| | | 13 | | A description of the API key authentication scheme. |
| | | 14 | | .PARAMETER DocId |
| | | 15 | | The documentation IDs to associate with this authentication scheme in OpenAPI documentation. |
| | | 16 | | .PARAMETER Options |
| | | 17 | | The options to configure the API key authentication. |
| | | 18 | | .PARAMETER ScriptBlock |
| | | 19 | | A script block that contains the logic for validating the API key. |
| | | 20 | | .PARAMETER Code |
| | | 21 | | C# or VBNet code that contains the logic for validating the API key. |
| | | 22 | | .PARAMETER CodeLanguage |
| | | 23 | | The scripting language of the code used for validating the API key. |
| | | 24 | | .PARAMETER CodeFilePath |
| | | 25 | | Path to a file containing C# code that contains the logic for validating the API key. |
| | | 26 | | .PARAMETER StaticApiKey |
| | | 27 | | The expected API key to validate against. |
| | | 28 | | .PARAMETER ApiKeyName |
| | | 29 | | The name of the header to look for the API key. |
| | | 30 | | .PARAMETER AdditionalHeaderNames |
| | | 31 | | Additional headers to check for the API key. |
| | | 32 | | .PARAMETER AllowQueryStringFallback |
| | | 33 | | If specified, allows the API key to be provided in the query string. |
| | | 34 | | .PARAMETER AllowInsecureHttp |
| | | 35 | | If specified, allows the API key to be provided over HTTP instead of HTTPS. |
| | | 36 | | .PARAMETER EmitChallengeHeader |
| | | 37 | | If specified, emits a challenge header when the API key is missing or invalid. |
| | | 38 | | .PARAMETER ChallengeHeaderFormat |
| | | 39 | | The format of the challenge header to emit. |
| | | 40 | | .PARAMETER Logger |
| | | 41 | | A logger to use for logging authentication events. |
| | | 42 | | .PARAMETER ClaimPolicyConfig |
| | | 43 | | Configuration for claim policies to apply during authentication. |
| | | 44 | | .PARAMETER IssueClaimsScriptBlock |
| | | 45 | | A script block that contains the logic for issuing claims after successful authentication. |
| | | 46 | | .PARAMETER IssueClaimsCode |
| | | 47 | | C# or VBNet code that contains the logic for issuing claims after successful authentication. |
| | | 48 | | .PARAMETER IssueClaimsCodeLanguage |
| | | 49 | | The scripting language of the code used for issuing claims. |
| | | 50 | | .PARAMETER IssueClaimsCodeFilePath |
| | | 51 | | Path to a file containing the code that contains the logic for issuing claims after successful authentication |
| | | 52 | | .PARAMETER PassThru |
| | | 53 | | If specified, returns the modified server instance after adding the authentication. |
| | | 54 | | .EXAMPLE |
| | | 55 | | Add-KrApiKeyAuthentication -AuthenticationScheme 'MyApiKey' -StaticApiKey '12345' -ApiKeyName 'X-Api-Key' |
| | | 56 | | This example adds API key authentication to the server with the specified expected key and header name. |
| | | 57 | | .EXAMPLE |
| | | 58 | | Add-KrApiKeyAuthentication -AuthenticationScheme 'MyApiKey' -ScriptBlock { |
| | | 59 | | param($username, $password) |
| | | 60 | | return $username -eq 'admin' -and $password -eq 'password' |
| | | 61 | | } |
| | | 62 | | This example adds API key authentication using a script block to validate the API key. |
| | | 63 | | .EXAMPLE |
| | | 64 | | Add-KrApiKeyAuthentication -AuthenticationScheme 'MyApiKey' -Code @" |
| | | 65 | | return username == "admin" && password == "password"; |
| | | 66 | | "@ |
| | | 67 | | This example adds API key authentication using C# code to validate the API key. |
| | | 68 | | .EXAMPLE |
| | | 69 | | Add-KrApiKeyAuthentication -AuthenticationScheme 'MyApiKey' -CodeFilePath 'C:\path\to\code.cs' |
| | | 70 | | This example adds API key authentication using a C# code file to validate the API key. |
| | | 71 | | .LINK |
| | | 72 | | https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.authentication.apikey.apikeyauthenticationopti |
| | | 73 | | .NOTES |
| | | 74 | | This cmdlet is used to configure API key authentication for the Kestrun server, allowing you to secure your APIs |
| | | 75 | | #> |
| | | 76 | | function Add-KrApiKeyAuthentication { |
| | | 77 | | [KestrunRuntimeApi('Definition')] |
| | | 78 | | [CmdletBinding(DefaultParameterSetName = 'ScriptBlock')] |
| | | 79 | | [OutputType([Kestrun.Hosting.KestrunHost])] |
| | | 80 | | param( |
| | | 81 | | [Parameter(ValueFromPipeline = $true)] |
| | | 82 | | [Kestrun.Hosting.KestrunHost]$Server, |
| | | 83 | | |
| | | 84 | | [Parameter()] |
| | | 85 | | [string]$AuthenticationScheme = [Kestrun.Authentication.AuthenticationDefaults]::ApiKeySchemeName, |
| | | 86 | | |
| | | 87 | | [Parameter()] |
| | | 88 | | [string]$DisplayName = [Kestrun.Authentication.AuthenticationDefaults]::ApiKeyDisplayName, |
| | | 89 | | |
| | | 90 | | [Parameter(Mandatory = $false, ParameterSetName = 'ScriptBlock')] |
| | | 91 | | [Parameter(Mandatory = $false, ParameterSetName = 'CodeInline')] |
| | | 92 | | [Parameter(Mandatory = $false, ParameterSetName = 'StaticKey')] |
| | | 93 | | [Parameter(Mandatory = $false, ParameterSetName = 'CodeFile')] |
| | | 94 | | [string]$Description, |
| | | 95 | | |
| | | 96 | | [Parameter()] |
| | | 97 | | [string[]]$DocId = [Kestrun.Authentication.IOpenApiAuthenticationOptions]::DefaultDocumentationIds, |
| | | 98 | | |
| | | 99 | | # 1. Direct options |
| | | 100 | | [Parameter(Mandatory = $true, ParameterSetName = 'Options')] |
| | | 101 | | [Kestrun.Authentication.ApiKeyAuthenticationOptions]$Options, |
| | | 102 | | |
| | | 103 | | # 2. Validation via ScriptBlock |
| | | 104 | | [Parameter(Mandatory = $true, ParameterSetName = 'ScriptBlock')] |
| | | 105 | | [scriptblock]$ScriptBlock, |
| | | 106 | | |
| | | 107 | | # 3. Validation via inline code (C#/VB) |
| | | 108 | | [Parameter(Mandatory = $true, ParameterSetName = 'CodeInline')] |
| | | 109 | | [string]$Code, |
| | | 110 | | |
| | | 111 | | [Parameter(ParameterSetName = 'CodeInline')] |
| | | 112 | | [Kestrun.Scripting.ScriptLanguage]$CodeLanguage = [Kestrun.Scripting.ScriptLanguage]::CSharp, |
| | | 113 | | |
| | | 114 | | # 4. Validation via code file |
| | | 115 | | [Parameter(Mandatory = $true, ParameterSetName = 'CodeFile')] |
| | | 116 | | [string]$CodeFilePath, |
| | | 117 | | |
| | | 118 | | # 5. Validation via static API key |
| | | 119 | | [Parameter(Mandatory = $true, ParameterSetName = 'StaticKey')] |
| | | 120 | | [string]$StaticApiKey, |
| | | 121 | | |
| | | 122 | | [Parameter()] |
| | | 123 | | # Common API key config (all parameter sets) |
| | | 124 | | [Microsoft.OpenApi.ParameterLocation]$In = [Microsoft.OpenApi.ParameterLocation]::Header, |
| | | 125 | | [Parameter()] |
| | | 126 | | [string]$ApiKeyName, |
| | | 127 | | [Parameter()] |
| | | 128 | | [string[]]$AdditionalHeaderNames, |
| | | 129 | | [Parameter()] |
| | | 130 | | [switch]$AllowQueryStringFallback, |
| | | 131 | | [Parameter()] |
| | | 132 | | [switch]$AllowInsecureHttp, |
| | | 133 | | [Parameter()] |
| | | 134 | | [switch]$EmitChallengeHeader, |
| | | 135 | | [Parameter()] |
| | | 136 | | [Kestrun.Authentication.ApiKeyChallengeFormat]$ChallengeHeaderFormat, |
| | | 137 | | |
| | | 138 | | [Parameter()] |
| | | 139 | | [Kestrun.Claims.ClaimPolicyConfig]$ClaimPolicyConfig, |
| | | 140 | | |
| | | 141 | | # Optional "issue claims" configuration (independent from validation mode) |
| | | 142 | | [scriptblock]$IssueClaimsScriptBlock, |
| | | 143 | | [string]$IssueClaimsCode, |
| | | 144 | | [Kestrun.Scripting.ScriptLanguage]$IssueClaimsCodeLanguage = [Kestrun.Scripting.ScriptLanguage]::CSharp, |
| | | 145 | | [string]$IssueClaimsCodeFilePath, |
| | | 146 | | |
| | | 147 | | [switch]$PassThru |
| | | 148 | | ) |
| | | 149 | | begin { |
| | | 150 | | # Ensure the server instance is resolved |
| | 0 | 151 | | $Server = Resolve-KestrunServer -Server $Server |
| | | 152 | | } |
| | | 153 | | process { |
| | | 154 | | # Build Options only when not provided directly |
| | 0 | 155 | | if ($PSCmdlet.ParameterSetName -ne 'Options') { |
| | 0 | 156 | | $Options = [Kestrun.Authentication.ApiKeyAuthenticationOptions]::new() |
| | | 157 | | # Set host reference |
| | 0 | 158 | | $Options.Host = $Server |
| | 0 | 159 | | $Options.ValidateCodeSettings = [Kestrun.Authentication.AuthenticationCodeSettings]::new() |
| | | 160 | | |
| | 0 | 161 | | switch ($PSCmdlet.ParameterSetName) { |
| | | 162 | | 'ScriptBlock' { |
| | 0 | 163 | | $Options.ValidateCodeSettings.Code = $ScriptBlock.ToString() |
| | 0 | 164 | | $Options.ValidateCodeSettings.Language = [Kestrun.Scripting.ScriptLanguage]::PowerShell |
| | | 165 | | } |
| | | 166 | | 'CodeInline' { |
| | 0 | 167 | | $Options.ValidateCodeSettings.Code = $Code |
| | 0 | 168 | | $Options.ValidateCodeSettings.Language = $CodeLanguage |
| | | 169 | | } |
| | | 170 | | 'CodeFile' { |
| | 0 | 171 | | if (-not (Test-Path -Path $CodeFilePath)) { |
| | 0 | 172 | | throw "The specified code file path does not exist: $CodeFilePath" |
| | | 173 | | } |
| | | 174 | | |
| | 0 | 175 | | $extension = [System.IO.Path]::GetExtension($CodeFilePath) |
| | | 176 | | |
| | 0 | 177 | | switch ($extension.ToLowerInvariant()) { |
| | | 178 | | '.ps1' { |
| | 0 | 179 | | $Options.ValidateCodeSettings.Language = [Kestrun.Scripting.ScriptLanguage]::PowerShell |
| | | 180 | | } |
| | | 181 | | '.cs' { |
| | 0 | 182 | | $Options.ValidateCodeSettings.Language = [Kestrun.Scripting.ScriptLanguage]::CSharp |
| | | 183 | | } |
| | | 184 | | '.vb' { |
| | 0 | 185 | | $Options.ValidateCodeSettings.Language = [Kestrun.Scripting.ScriptLanguage]::VisualBasic |
| | | 186 | | } |
| | | 187 | | default { |
| | 0 | 188 | | throw "Unsupported '$extension' code file extension for validation." |
| | | 189 | | } |
| | | 190 | | } |
| | | 191 | | |
| | 0 | 192 | | $Options.ValidateCodeSettings.Code = Get-Content -Path $CodeFilePath -Raw |
| | | 193 | | } |
| | | 194 | | 'StaticKey' { |
| | 0 | 195 | | $Options.StaticApiKey = $StaticApiKey |
| | | 196 | | } |
| | | 197 | | } |
| | | 198 | | |
| | | 199 | | # Common API key options |
| | 0 | 200 | | if ($PSBoundParameters.ContainsKey('ApiKeyName')) { |
| | 0 | 201 | | $Options.ApiKeyName = $ApiKeyName |
| | | 202 | | } |
| | | 203 | | |
| | | 204 | | # Set location of API key |
| | 0 | 205 | | $Options.In = $In |
| | | 206 | | |
| | 0 | 207 | | if ($PSBoundParameters.ContainsKey('AdditionalHeaderNames') -and $AdditionalHeaderNames.Count -gt 0) { |
| | 0 | 208 | | $Options.AdditionalHeaderNames = $AdditionalHeaderNames |
| | | 209 | | } |
| | | 210 | | |
| | 0 | 211 | | if ($AllowQueryStringFallback.IsPresent) { |
| | 0 | 212 | | $Options.AllowQueryStringFallback = $true |
| | | 213 | | } |
| | | 214 | | |
| | 0 | 215 | | $Options.AllowInsecureHttp = $AllowInsecureHttp.IsPresent |
| | | 216 | | |
| | 0 | 217 | | $Options.EmitChallengeHeader = $EmitChallengeHeader.IsPresent |
| | | 218 | | |
| | | 219 | | |
| | 0 | 220 | | if ($PSBoundParameters.ContainsKey('ChallengeHeaderFormat')) { |
| | 0 | 221 | | $Options.ChallengeHeaderFormat = $ChallengeHeaderFormat |
| | | 222 | | } |
| | | 223 | | |
| | 0 | 224 | | if ($PSBoundParameters.ContainsKey('ClaimPolicyConfig')) { |
| | 0 | 225 | | $Options.ClaimPolicyConfig = $ClaimPolicyConfig |
| | | 226 | | } |
| | | 227 | | |
| | 0 | 228 | | if (-not ([string]::IsNullOrWhiteSpace($Description))) { |
| | 0 | 229 | | $Options.Description = $Description |
| | | 230 | | } |
| | | 231 | | |
| | | 232 | | # Optional issue-claims settings (single-choice) |
| | 0 | 233 | | $issueModes = @() |
| | 0 | 234 | | if ($PSBoundParameters.ContainsKey('IssueClaimsScriptBlock')) { $issueModes += 'ScriptBlock' } |
| | 0 | 235 | | if ($PSBoundParameters.ContainsKey('IssueClaimsCode')) { $issueModes += 'Code' } |
| | 0 | 236 | | if ($PSBoundParameters.ContainsKey('IssueClaimsCodeFilePath')) { $issueModes += 'File' } |
| | | 237 | | |
| | 0 | 238 | | if ($issueModes.Count -gt 1) { |
| | 0 | 239 | | throw 'Specify only one of -IssueClaimsScriptBlock, -IssueClaimsCode, or -IssueClaimsCodeFilePath.' |
| | | 240 | | } |
| | | 241 | | |
| | 0 | 242 | | if ($issueModes.Count -eq 1) { |
| | 0 | 243 | | $Options.IssueClaimsCodeSettings = [Kestrun.Authentication.AuthenticationCodeSettings]::new() |
| | | 244 | | |
| | 0 | 245 | | switch ($issueModes[0]) { |
| | | 246 | | 'ScriptBlock' { |
| | 0 | 247 | | $Options.IssueClaimsCodeSettings.Code = $IssueClaimsScriptBlock.ToString() |
| | 0 | 248 | | $Options.IssueClaimsCodeSettings.Language = [Kestrun.Scripting.ScriptLanguage]::PowerShell |
| | | 249 | | } |
| | | 250 | | 'Code' { |
| | 0 | 251 | | $Options.IssueClaimsCodeSettings.Code = $IssueClaimsCode |
| | 0 | 252 | | $Options.IssueClaimsCodeSettings.Language = $IssueClaimsCodeLanguage |
| | | 253 | | } |
| | | 254 | | 'File' { |
| | 0 | 255 | | if (-not (Test-Path -Path $IssueClaimsCodeFilePath)) { |
| | 0 | 256 | | throw "The specified issue-claims code file path does not exist: $IssueClaimsCodeFilePath" |
| | | 257 | | } |
| | | 258 | | |
| | 0 | 259 | | $issueExt = [System.IO.Path]::GetExtension($IssueClaimsCodeFilePath) |
| | | 260 | | |
| | 0 | 261 | | switch ($issueExt.ToLowerInvariant()) { |
| | | 262 | | '.ps1' { |
| | 0 | 263 | | $Options.IssueClaimsCodeSettings.Language = [Kestrun.Scripting.ScriptLanguage]::PowerShe |
| | | 264 | | } |
| | | 265 | | '.cs' { |
| | 0 | 266 | | $Options.IssueClaimsCodeSettings.Language = [Kestrun.Scripting.ScriptLanguage]::CSharp |
| | | 267 | | } |
| | | 268 | | '.vb' { |
| | 0 | 269 | | $Options.IssueClaimsCodeSettings.Language = [Kestrun.Scripting.ScriptLanguage]::VisualBa |
| | | 270 | | } |
| | | 271 | | default { |
| | 0 | 272 | | throw "Unsupported '$issueExt' code file extension for issue-claims." |
| | | 273 | | } |
| | | 274 | | } |
| | | 275 | | |
| | 0 | 276 | | $Options.IssueClaimsCodeSettings.Code = Get-Content -Path $IssueClaimsCodeFilePath -Raw |
| | | 277 | | } |
| | | 278 | | } |
| | | 279 | | } |
| | | 280 | | |
| | | 281 | | # OpenAPI documentation IDs |
| | 0 | 282 | | $Options.DocumentationId = $DocId |
| | | 283 | | } |
| | | 284 | | |
| | | 285 | | # Add API key authentication to the server |
| | 0 | 286 | | [Kestrun.Hosting.KestrunHostAuthnExtensions]::AddApiKeyAuthentication( |
| | 0 | 287 | | $Server, $AuthenticationScheme, $DisplayName, $Options ) | Out-Null |
| | | 288 | | |
| | | 289 | | # Return the modified server instance if PassThru is specified |
| | 0 | 290 | | if ($PassThru.IsPresent) { |
| | 0 | 291 | | return $Server |
| | | 292 | | } |
| | | 293 | | } |
| | | 294 | | } |