| | | 1 | | <# |
| | | 2 | | .SYNOPSIS |
| | | 3 | | Adds basic authentication to the Kestrun server. |
| | | 4 | | .DESCRIPTION |
| | | 5 | | Configures the Kestrun server to use basic authentication for incoming requests. |
| | | 6 | | .PARAMETER Server |
| | | 7 | | The Kestrun server instance to configure. |
| | | 8 | | .PARAMETER Name |
| | | 9 | | The name of the basic authentication scheme. |
| | | 10 | | .PARAMETER Options |
| | | 11 | | The options to configure the basic authentication. |
| | | 12 | | .PARAMETER ScriptBlock |
| | | 13 | | A script block that contains the logic for validating the username and password. |
| | | 14 | | .PARAMETER Code |
| | | 15 | | C# or VBNet code that contains the logic for validating the username and password. |
| | | 16 | | .PARAMETER CodeLanguage |
| | | 17 | | The scripting language of the code used for validating the username and password. |
| | | 18 | | .PARAMETER CodeFilePath |
| | | 19 | | Path to a file containing C# code that contains the logic for validating the username and password. |
| | | 20 | | .PARAMETER HeaderName |
| | | 21 | | The name of the header to look for the basic authentication credentials. |
| | | 22 | | .PARAMETER Base64Encoded |
| | | 23 | | If specified, the credentials are expected to be Base64 encoded. |
| | | 24 | | .PARAMETER SuppressWwwAuthenticate |
| | | 25 | | If specified, the server will not emit the WWW-Authenticate header in responses. |
| | | 26 | | .PARAMETER SeparatorRegex |
| | | 27 | | A regular expression to use for separating multiple credentials in the header. |
| | | 28 | | .PARAMETER Realm |
| | | 29 | | The realm for the basic authentication. |
| | | 30 | | .PARAMETER AllowInsecureHttp |
| | | 31 | | If specified, allows the basic authentication to be used over HTTP instead of HTTPS. |
| | | 32 | | .PARAMETER Logger |
| | | 33 | | A logger to use for logging authentication events. |
| | | 34 | | .PARAMETER ClaimPolicyConfig |
| | | 35 | | Configuration for claim policies to apply during authentication. |
| | | 36 | | .PARAMETER IssueClaimsScriptBlock |
| | | 37 | | A script block that contains the logic for issuing claims after successful authentication. |
| | | 38 | | .PARAMETER IssueClaimsCode |
| | | 39 | | C# or VBNet code that contains the logic for issuing claims after successful authentication. |
| | | 40 | | .PARAMETER IssueClaimsCodeLanguage |
| | | 41 | | The scripting language of the code used for issuing claims. |
| | | 42 | | .PARAMETER IssueClaimsCodeFilePath |
| | | 43 | | Path to a file containing the code that contains the logic for issuing claims after successful authentication |
| | | 44 | | .PARAMETER PassThru |
| | | 45 | | If specified, returns the modified server instance after adding the authentication. |
| | | 46 | | .EXAMPLE |
| | | 47 | | Add-KrBasicAuthentication -Server $server -Name "MyAuth" -Options $options -ScriptBlock $scriptBlock |
| | | 48 | | Configure Kestrun server to use basic authentication with the specified script block. |
| | | 49 | | .EXAMPLE |
| | | 50 | | Add-KrBasicAuthentication -Server $server -Name "MyAuth" -Options $options -Code $code -CodeLanguage $codeLangua |
| | | 51 | | Configure Kestrun server to use basic authentication with the specified code. |
| | | 52 | | .EXAMPLE |
| | | 53 | | Add-KrBasicAuthentication -Server $server -Name "MyAuth" -Options $options -CodeFilePath $codeFilePath |
| | | 54 | | Configure Kestrun server to use basic authentication with the specified code file. |
| | | 55 | | .NOTES |
| | | 56 | | This function is part of the Kestrun.Authentication module and is used to configure basic authentication for Kes |
| | | 57 | | Maps to Kestrun.Hosting.KestrunHostAuthnExtensions.AddBasicAuthentication |
| | | 58 | | #> |
| | | 59 | | function Add-KrBasicAuthentication { |
| | | 60 | | [KestrunRuntimeApi('Definition')] |
| | | 61 | | [CmdletBinding(defaultParameterSetName = 'v1')] |
| | | 62 | | [OutputType([Kestrun.Hosting.KestrunHost])] |
| | | 63 | | param( |
| | | 64 | | [Parameter(Mandatory = $false, ValueFromPipeline)] |
| | | 65 | | [Kestrun.Hosting.KestrunHost]$Server, |
| | | 66 | | |
| | | 67 | | [Parameter(Mandatory = $true)] |
| | | 68 | | [string]$Name, |
| | | 69 | | |
| | | 70 | | [Parameter(Mandatory = $true, ParameterSetName = 'Options')] |
| | | 71 | | [Kestrun.Authentication.BasicAuthenticationOptions]$Options, |
| | | 72 | | |
| | | 73 | | [Parameter(Mandatory = $true, ParameterSetName = 'v1')] |
| | | 74 | | [Parameter(Mandatory = $true, ParameterSetName = 'v1_i1')] |
| | | 75 | | [Parameter(Mandatory = $true, ParameterSetName = 'v1_i2')] |
| | | 76 | | [Parameter(Mandatory = $true, ParameterSetName = 'v1_i3')] |
| | | 77 | | [scriptblock]$ScriptBlock, |
| | | 78 | | |
| | | 79 | | [Parameter(Mandatory = $true, ParameterSetName = 'v2')] |
| | | 80 | | [Parameter(Mandatory = $true, ParameterSetName = 'v2_i1')] |
| | | 81 | | [Parameter(Mandatory = $true, ParameterSetName = 'v2_i2')] |
| | | 82 | | [Parameter(Mandatory = $true, ParameterSetName = 'v2_i3')] |
| | | 83 | | [string]$Code, |
| | | 84 | | |
| | | 85 | | [Parameter(ParameterSetName = 'v2')] |
| | | 86 | | [Parameter(ParameterSetName = 'v2_i1')] |
| | | 87 | | [Parameter(ParameterSetName = 'v2_i2')] |
| | | 88 | | [Parameter(ParameterSetName = 'v2_i3')] |
| | | 89 | | [Kestrun.Scripting.ScriptLanguage]$CodeLanguage = [Kestrun.Scripting.ScriptLanguage]::CSharp, |
| | | 90 | | |
| | | 91 | | [Parameter(Mandatory = $true, ParameterSetName = 'v3')] |
| | | 92 | | [Parameter(Mandatory = $true, ParameterSetName = 'v3_i1')] |
| | | 93 | | [Parameter(Mandatory = $true, ParameterSetName = 'v3_i2')] |
| | | 94 | | [Parameter(Mandatory = $true, ParameterSetName = 'v3_i3')] |
| | | 95 | | [string]$CodeFilePath, |
| | | 96 | | |
| | | 97 | | [Parameter(ParameterSetName = 'v1')] |
| | | 98 | | [Parameter(ParameterSetName = 'v1_i1')] |
| | | 99 | | [Parameter(ParameterSetName = 'v1_i2')] |
| | | 100 | | [Parameter(ParameterSetName = 'v1_i3')] |
| | | 101 | | [Parameter(ParameterSetName = 'v2')] |
| | | 102 | | [Parameter(ParameterSetName = 'v2_i1')] |
| | | 103 | | [Parameter(ParameterSetName = 'v2_i2')] |
| | | 104 | | [Parameter(ParameterSetName = 'v2_i3')] |
| | | 105 | | [Parameter(ParameterSetName = 'v3')] |
| | | 106 | | [Parameter(ParameterSetName = 'v3_i1')] |
| | | 107 | | [Parameter(ParameterSetName = 'v3_i2')] |
| | | 108 | | [Parameter(ParameterSetName = 'v3_i3')] |
| | | 109 | | [string]$HeaderName, |
| | | 110 | | |
| | | 111 | | [Parameter(ParameterSetName = 'v1')] |
| | | 112 | | [Parameter(ParameterSetName = 'v1_i1')] |
| | | 113 | | [Parameter(ParameterSetName = 'v1_i2')] |
| | | 114 | | [Parameter(ParameterSetName = 'v1_i3')] |
| | | 115 | | [Parameter(ParameterSetName = 'v2')] |
| | | 116 | | [Parameter(ParameterSetName = 'v2_i1')] |
| | | 117 | | [Parameter(ParameterSetName = 'v2_i2')] |
| | | 118 | | [Parameter(ParameterSetName = 'v2_i3')] |
| | | 119 | | [Parameter(ParameterSetName = 'v3')] |
| | | 120 | | [Parameter(ParameterSetName = 'v3_i1')] |
| | | 121 | | [Parameter(ParameterSetName = 'v3_i2')] |
| | | 122 | | [Parameter(ParameterSetName = 'v3_i3')] |
| | | 123 | | [switch]$Base64Encoded, |
| | | 124 | | |
| | | 125 | | [Parameter(ParameterSetName = 'v1')] |
| | | 126 | | [Parameter(ParameterSetName = 'v1_i1')] |
| | | 127 | | [Parameter(ParameterSetName = 'v1_i2')] |
| | | 128 | | [Parameter(ParameterSetName = 'v1_i3')] |
| | | 129 | | [Parameter(ParameterSetName = 'v2')] |
| | | 130 | | [Parameter(ParameterSetName = 'v2_i1')] |
| | | 131 | | [Parameter(ParameterSetName = 'v2_i2')] |
| | | 132 | | [Parameter(ParameterSetName = 'v2_i3')] |
| | | 133 | | [Parameter(ParameterSetName = 'v3')] |
| | | 134 | | [Parameter(ParameterSetName = 'v3_i1')] |
| | | 135 | | [Parameter(ParameterSetName = 'v3_i2')] |
| | | 136 | | [Parameter(ParameterSetName = 'v3_i3')] |
| | | 137 | | [switch]$SuppressWwwAuthenticate, |
| | | 138 | | |
| | | 139 | | [Parameter(ParameterSetName = 'v1')] |
| | | 140 | | [Parameter(ParameterSetName = 'v1_i1')] |
| | | 141 | | [Parameter(ParameterSetName = 'v1_i2')] |
| | | 142 | | [Parameter(ParameterSetName = 'v1_i3')] |
| | | 143 | | [Parameter(ParameterSetName = 'v2')] |
| | | 144 | | [Parameter(ParameterSetName = 'v2_i1')] |
| | | 145 | | [Parameter(ParameterSetName = 'v2_i2')] |
| | | 146 | | [Parameter(ParameterSetName = 'v2_i3')] |
| | | 147 | | [Parameter(ParameterSetName = 'v3')] |
| | | 148 | | [Parameter(ParameterSetName = 'v3_i1')] |
| | | 149 | | [Parameter(ParameterSetName = 'v3_i2')] |
| | | 150 | | [Parameter(ParameterSetName = 'v3_i3')] |
| | | 151 | | [Regex]$SeparatorRegex, |
| | | 152 | | |
| | | 153 | | [Parameter(ParameterSetName = 'v1')] |
| | | 154 | | [Parameter(ParameterSetName = 'v1_i1')] |
| | | 155 | | [Parameter(ParameterSetName = 'v1_i2')] |
| | | 156 | | [Parameter(ParameterSetName = 'v1_i3')] |
| | | 157 | | [Parameter(ParameterSetName = 'v2')] |
| | | 158 | | [Parameter(ParameterSetName = 'v2_i1')] |
| | | 159 | | [Parameter(ParameterSetName = 'v2_i2')] |
| | | 160 | | [Parameter(ParameterSetName = 'v2_i3')] |
| | | 161 | | [Parameter(ParameterSetName = 'v3')] |
| | | 162 | | [Parameter(ParameterSetName = 'v3_i1')] |
| | | 163 | | [Parameter(ParameterSetName = 'v3_i2')] |
| | | 164 | | [Parameter(ParameterSetName = 'v3_i3')] |
| | | 165 | | [string]$Realm, |
| | | 166 | | |
| | | 167 | | [Parameter(ParameterSetName = 'v1')] |
| | | 168 | | [Parameter(ParameterSetName = 'v1_i1')] |
| | | 169 | | [Parameter(ParameterSetName = 'v1_i2')] |
| | | 170 | | [Parameter(ParameterSetName = 'v1_i3')] |
| | | 171 | | [Parameter(ParameterSetName = 'v2')] |
| | | 172 | | [Parameter(ParameterSetName = 'v2_i1')] |
| | | 173 | | [Parameter(ParameterSetName = 'v2_i2')] |
| | | 174 | | [Parameter(ParameterSetName = 'v2_i3')] |
| | | 175 | | [Parameter(ParameterSetName = 'v3')] |
| | | 176 | | [Parameter(ParameterSetName = 'v3_i1')] |
| | | 177 | | [Parameter(ParameterSetName = 'v3_i2')] |
| | | 178 | | [Parameter(ParameterSetName = 'v3_i3')] |
| | | 179 | | [switch]$AllowInsecureHttp, |
| | | 180 | | |
| | | 181 | | [Parameter(ParameterSetName = 'v1')] |
| | | 182 | | [Parameter(ParameterSetName = 'v1_i1')] |
| | | 183 | | [Parameter(ParameterSetName = 'v1_i2')] |
| | | 184 | | [Parameter(ParameterSetName = 'v1_i3')] |
| | | 185 | | [Parameter(ParameterSetName = 'v2')] |
| | | 186 | | [Parameter(ParameterSetName = 'v2_i1')] |
| | | 187 | | [Parameter(ParameterSetName = 'v2_i2')] |
| | | 188 | | [Parameter(ParameterSetName = 'v2_i3')] |
| | | 189 | | [Parameter(ParameterSetName = 'v3')] |
| | | 190 | | [Parameter(ParameterSetName = 'v3_i1')] |
| | | 191 | | [Parameter(ParameterSetName = 'v3_i2')] |
| | | 192 | | [Parameter(ParameterSetName = 'v3_i3')] |
| | | 193 | | [Serilog.ILogger]$Logger, |
| | | 194 | | |
| | | 195 | | [Parameter(ParameterSetName = 'v1_i1')] |
| | | 196 | | [Parameter(ParameterSetName = 'v1_i2')] |
| | | 197 | | [Parameter(ParameterSetName = 'v1_i3')] |
| | | 198 | | [Parameter(ParameterSetName = 'v2_i1')] |
| | | 199 | | [Parameter(ParameterSetName = 'v2_i2')] |
| | | 200 | | [Parameter(ParameterSetName = 'v2_i3')] |
| | | 201 | | [Parameter(ParameterSetName = 'v3_i1')] |
| | | 202 | | [Parameter(ParameterSetName = 'v3_i2')] |
| | | 203 | | [Parameter(ParameterSetName = 'v3_i3')] |
| | | 204 | | [Kestrun.Claims.ClaimPolicyConfig]$ClaimPolicyConfig, |
| | | 205 | | |
| | | 206 | | [Parameter(Mandatory = $true, ParameterSetName = 'v1_i1')] |
| | | 207 | | [Parameter(Mandatory = $true, ParameterSetName = 'v2_i1')] |
| | | 208 | | [Parameter(Mandatory = $true, ParameterSetName = 'v3_i1')] |
| | | 209 | | [scriptblock]$IssueClaimsScriptBlock, |
| | | 210 | | |
| | | 211 | | [Parameter(Mandatory = $true, ParameterSetName = 'v3_i2')] |
| | | 212 | | [Parameter(Mandatory = $true, ParameterSetName = 'v2_i2')] |
| | | 213 | | [Parameter(Mandatory = $true, ParameterSetName = 'v1_i2')] |
| | | 214 | | [string]$IssueClaimsCode, |
| | | 215 | | |
| | | 216 | | [Parameter(ParameterSetName = 'v3_i2')] |
| | | 217 | | [Parameter(ParameterSetName = 'v2_i2')] |
| | | 218 | | [Parameter(ParameterSetName = 'v1_i2')] |
| | | 219 | | [Kestrun.Scripting.ScriptLanguage]$IssueClaimsCodeLanguage = [Kestrun.Scripting.ScriptLanguage]::CSharp, |
| | | 220 | | |
| | | 221 | | [Parameter(Mandatory = $true, ParameterSetName = 'v3_i3')] |
| | | 222 | | [Parameter(Mandatory = $true, ParameterSetName = 'v2_i3')] |
| | | 223 | | [Parameter(Mandatory = $true, ParameterSetName = 'v1_i3')] |
| | | 224 | | [string]$IssueClaimsCodeFilePath, |
| | | 225 | | |
| | | 226 | | [Parameter()] |
| | | 227 | | [switch]$PassThru |
| | | 228 | | ) |
| | | 229 | | process { |
| | 0 | 230 | | if ($PSCmdlet.ParameterSetName -ne 'Options') { |
| | 0 | 231 | | $Options = [Kestrun.Authentication.BasicAuthenticationOptions]::new() |
| | 0 | 232 | | $Options.ValidateCodeSettings = [Kestrun.Authentication.AuthenticationCodeSettings]::new() |
| | 0 | 233 | | if ($null -ne $ScriptBlock) { |
| | 0 | 234 | | $Options.ValidateCodeSettings.Code = $ScriptBlock.ToString() |
| | 0 | 235 | | $Options.ValidateCodeSettings.Language = [Kestrun.Scripting.ScriptLanguage]::PowerShell |
| | 0 | 236 | | } elseif (-not [string]::IsNullOrWhiteSpace($Code)) { |
| | 0 | 237 | | $Options.ValidateCodeSettings.Code = $Code |
| | 0 | 238 | | $Options.ValidateCodeSettings.Language = $CodeLanguage |
| | 0 | 239 | | } elseif (-not [string]::IsNullOrWhiteSpace($CodeFilePath)) { |
| | 0 | 240 | | if (-not (Test-Path -Path $CodeFilePath)) { |
| | 0 | 241 | | throw "The specified code file path does not exist: $CodeFilePath" |
| | | 242 | | } |
| | 0 | 243 | | $extension = Split-Path -Path $CodeFilePath -Extension |
| | 0 | 244 | | switch ($extension) { |
| | | 245 | | '.ps1' { |
| | 0 | 246 | | $Options.ValidateCodeSettings.Language = [Kestrun.Scripting.ScriptLanguage]::PowerShell |
| | | 247 | | } |
| | | 248 | | '.cs' { |
| | 0 | 249 | | $Options.ValidateCodeSettings.Language = [Kestrun.Scripting.ScriptLanguage]::CSharp |
| | | 250 | | } |
| | | 251 | | '.vb' { |
| | 0 | 252 | | $Options.ValidateCodeSettings.Language = [Kestrun.Scripting.ScriptLanguage]::VisualBasic |
| | | 253 | | } |
| | | 254 | | default { |
| | 0 | 255 | | throw "Unsupported '$extension' code file extension." |
| | | 256 | | } |
| | | 257 | | } |
| | 0 | 258 | | $Options.ValidateCodeSettings.Code = Get-Content -Path $CodeFilePath -Raw |
| | | 259 | | } |
| | | 260 | | |
| | 0 | 261 | | if (-not [string]::IsNullOrWhiteSpace($HeaderName)) { |
| | 0 | 262 | | $Options.HeaderName = $HeaderName |
| | | 263 | | } |
| | 0 | 264 | | if ($Base64Encoded.IsPresent) { |
| | 0 | 265 | | $Options.Base64Encoded = $Base64Encoded.IsPresent |
| | | 266 | | } |
| | 0 | 267 | | if ($SuppressWwwAuthenticate.IsPresent) { |
| | 0 | 268 | | $Options.SuppressWwwAuthenticate = $SuppressWwwAuthenticate.IsPresent |
| | | 269 | | } |
| | 0 | 270 | | if ($null -ne $SeparatorRegex) { |
| | 0 | 271 | | $Options.SeparatorRegex = $SeparatorRegex |
| | | 272 | | } |
| | 0 | 273 | | if (-not [string]::IsNullOrWhiteSpace($Realm)) { |
| | 0 | 274 | | $Options.Realm = $Realm |
| | | 275 | | } |
| | 0 | 276 | | if ($AllowInsecureHttp.IsPresent) { |
| | 0 | 277 | | $Options.RequireHttps = $false |
| | | 278 | | } else { |
| | 0 | 279 | | $Options.RequireHttps = $true |
| | | 280 | | } |
| | 0 | 281 | | if ($null -ne $ClaimPolicyConfig) { |
| | 0 | 282 | | $Options.ClaimPolicyConfig = $ClaimPolicyConfig |
| | | 283 | | } |
| | 0 | 284 | | if ($null -ne $Logger) { |
| | 0 | 285 | | $Options.Logger = $Logger |
| | | 286 | | } |
| | | 287 | | |
| | 0 | 288 | | if ($PSCmdlet.ParameterSetName.contains('_')) { |
| | | 289 | | |
| | 0 | 290 | | $Options.IssueClaimsCodeSettings = [Kestrun.Authentication.AuthenticationCodeSettings]::new() |
| | 0 | 291 | | if ($null -ne $IssueClaimsScriptBlock) { |
| | 0 | 292 | | $Options.IssueClaimsCodeSettings.Code = $IssueClaimsScriptBlock.ToString() |
| | 0 | 293 | | $Options.IssueClaimsCodeSettings.Language = [Kestrun.Scripting.ScriptLanguage]::PowerShell |
| | 0 | 294 | | } elseif (-not [string]::IsNullOrWhiteSpace($IssueClaimsCode)) { |
| | 0 | 295 | | $Options.IssueClaimsCodeSettings.Code = $IssueClaimsCode |
| | 0 | 296 | | $Options.IssueClaimsCodeSettings.Language = $IssueClaimsCodeLanguage |
| | 0 | 297 | | } elseif (-not [string]::IsNullOrWhiteSpace($IssueClaimsCodeFilePath)) { |
| | 0 | 298 | | if (-not (Test-Path -Path $IssueClaimsCodeFilePath)) { |
| | 0 | 299 | | throw "The specified code file path does not exist: $IssueClaimsCodeFilePath" |
| | | 300 | | } |
| | 0 | 301 | | $extension = Split-Path -Path $IssueClaimsCodeFilePath -Extension |
| | 0 | 302 | | switch ($extension) { |
| | | 303 | | '.ps1' { |
| | 0 | 304 | | $Options.IssueClaimsCodeSettings.Language = [Kestrun.Scripting.ScriptLanguage]::PowerShell |
| | | 305 | | } |
| | | 306 | | '.cs' { |
| | 0 | 307 | | $Options.IssueClaimsCodeSettings.Language = [Kestrun.Scripting.ScriptLanguage]::CSharp |
| | | 308 | | } |
| | | 309 | | '.vb' { |
| | 0 | 310 | | $Options.IssueClaimsCodeSettings.Language = [Kestrun.Scripting.ScriptLanguage]::VisualBasic |
| | | 311 | | } |
| | | 312 | | default { |
| | 0 | 313 | | throw "Unsupported '$extension' code file extension." |
| | | 314 | | } |
| | | 315 | | } |
| | 0 | 316 | | $Options.IssueClaimsCodeSettings.Code = Get-Content -Path $CodeFilePath -Raw |
| | | 317 | | } |
| | | 318 | | } |
| | | 319 | | } |
| | | 320 | | # Ensure the server instance is resolved |
| | 0 | 321 | | $Server = Resolve-KestrunServer -Server $Server |
| | | 322 | | |
| | 0 | 323 | | [Kestrun.Hosting.KestrunHostAuthnExtensions]::AddBasicAuthentication( |
| | | 324 | | $Server, |
| | | 325 | | $Name, |
| | | 326 | | $Options |
| | 0 | 327 | | ) | Out-Null |
| | 0 | 328 | | if ($PassThru.IsPresent) { |
| | | 329 | | # if the PassThru switch is specified, return the modified server instance |
| | 0 | 330 | | return $Server |
| | | 331 | | } |
| | | 332 | | } |
| | | 333 | | } |
| | | 334 | | |