| | | 1 | | <# |
| | | 2 | | .SYNOPSIS |
| | | 3 | | Adds a CORS policy to the Kestrun server. |
| | | 4 | | .DESCRIPTION |
| | | 5 | | This function adds a CORS policy to the Kestrun server. It can be used to configure CORS policies for the Kestrun se |
| | | 6 | | .PARAMETER Builder |
| | | 7 | | The CORS policy builder to be used to create the CORS policy. |
| | | 8 | | .PARAMETER Server |
| | | 9 | | The Kestrun server to which the CORS policy will be added. |
| | | 10 | | .PARAMETER Name |
| | | 11 | | The name of the CORS policy to be added. |
| | | 12 | | .PARAMETER Default |
| | | 13 | | Specifies that the CORS policy is the default policy. |
| | | 14 | | .PARAMETER AllowAll |
| | | 15 | | Specifies that the CORS policy should allow all origins, methods, and headers. |
| | | 16 | | .PARAMETER PassThru |
| | | 17 | | Specifies that the CORS policy builder should be passed through the pipeline. |
| | | 18 | | .EXAMPLE |
| | | 19 | | New-KrCorsPolicyBuilder | Set-KrCorsMethod -Any | Set-KrCorsHeader -Any | Add-KrCorsPolicy -Server $server -Name 'My |
| | | 20 | | .EXAMPLE |
| | | 21 | | New-KrCorsPolicyBuilder | Set-KrCorsMethod -Any | Set-KrCorsHeader -Any | Add-KrCorsPolicy -Server $server -Default |
| | | 22 | | .EXAMPLE |
| | | 23 | | Add-KrCorsPolicy -Server $server -Default -AllowAll |
| | | 24 | | .EXAMPLE |
| | | 25 | | New-KrCorsPolicyBuilder | Set-KrCorsExposedHeader -Headers 'X-Total-Count','X-Page-Number'| |
| | | 26 | | Add-KrCorsPolicy -Server $server -Name 'MyCORSPolicy' |
| | | 27 | | .NOTES |
| | | 28 | | This function is part of the Kestrun runtime API and is used to configure CORS policies for the Kestrun server. |
| | | 29 | | #> |
| | | 30 | | function Add-KrCorsPolicy { |
| | | 31 | | [KestrunRuntimeApi('Definition')] |
| | | 32 | | [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSReviewUnusedParameter', '')] |
| | | 33 | | [CmdletBinding(DefaultParameterSetName = 'Named')] |
| | | 34 | | [OutputType([Microsoft.AspNetCore.Cors.Infrastructure.CorsPolicyBuilder])] |
| | | 35 | | param( |
| | | 36 | | # Builder path (pipeline-friendly) |
| | | 37 | | [Parameter(Mandatory = $true, ValueFromPipeline = $true, ParameterSetName = 'Named')] |
| | | 38 | | [Parameter(Mandatory = $true, ValueFromPipeline = $true, ParameterSetName = 'Default')] |
| | | 39 | | [Microsoft.AspNetCore.Cors.Infrastructure.CorsPolicyBuilder]$Builder, |
| | | 40 | | |
| | | 41 | | # Server |
| | | 42 | | [Parameter()] |
| | | 43 | | [Kestrun.Hosting.KestrunHost]$Server, |
| | | 44 | | |
| | | 45 | | # Named policy |
| | | 46 | | [Parameter(Mandatory = $true, ParameterSetName = 'AllowAll-Named')] |
| | | 47 | | [Parameter(Mandatory = $true, ParameterSetName = 'Named')] |
| | | 48 | | [ValidateNotNullOrEmpty()] |
| | | 49 | | [string]$Name, |
| | | 50 | | |
| | | 51 | | # Default policy |
| | | 52 | | [Parameter(Mandatory = $true, ParameterSetName = 'Default')] |
| | | 53 | | [Parameter(Mandatory = $true, ParameterSetName = 'AllowAll-Default')] |
| | | 54 | | [switch]$Default, |
| | | 55 | | |
| | | 56 | | # Convenience: allow everything (no builder required) |
| | | 57 | | [Parameter(Mandatory = $true, ParameterSetName = 'AllowAll-Named')] |
| | | 58 | | [Parameter(Mandatory = $true, ParameterSetName = 'AllowAll-Default')] |
| | | 59 | | [switch]$AllowAll, |
| | | 60 | | |
| | | 61 | | # Pipeline continuation |
| | | 62 | | [Parameter(ValueFromPipeline = $true, ParameterSetName = 'Named')] |
| | | 63 | | [Parameter(ValueFromPipeline = $true, ParameterSetName = 'Default')] |
| | | 64 | | [switch]$PassThru |
| | | 65 | | ) |
| | | 66 | | |
| | | 67 | | begin { |
| | 0 | 68 | | $Server = Resolve-KestrunServer -Server $Server |
| | | 69 | | } |
| | | 70 | | |
| | | 71 | | process { |
| | 0 | 72 | | if ($AllowAll.IsPresent -and $PSBoundParameters.ContainsKey('Builder')) { |
| | 0 | 73 | | throw 'Do not pipe a builder when using -AllowAll. Use -AllowAll without a builder.' |
| | | 74 | | } |
| | | 75 | | |
| | 0 | 76 | | if ($AllowAll.IsPresent) { |
| | | 77 | | # Allow all origins, methods, and headers |
| | 0 | 78 | | if ([string]::IsNullOrEmpty($Name)) { |
| | 0 | 79 | | [Kestrun.Hosting.KestrunSecurityMiddlewareExtensions]::AddCorsDefaultPolicyAllowAll($Server) | Out-Null |
| | | 80 | | } else { |
| | 0 | 81 | | [Kestrun.Hosting.KestrunSecurityMiddlewareExtensions]::AddCorsPolicyAllowAll($Server, $Name) | Out-Null |
| | | 82 | | } |
| | | 83 | | } else { |
| | | 84 | | # Use the provided builder to add the CORS policy |
| | 0 | 85 | | if ([string]::IsNullOrEmpty($Name)) { |
| | 0 | 86 | | [Kestrun.Hosting.KestrunSecurityMiddlewareExtensions]::AddCorsDefaultPolicy($Server, $Builder) | Out-Nul |
| | | 87 | | } else { |
| | 0 | 88 | | [Kestrun.Hosting.KestrunSecurityMiddlewareExtensions]::AddCorsPolicy($Server, $Name, $Builder) | Out-Nul |
| | | 89 | | } |
| | | 90 | | } |
| | | 91 | | # Pass the builder through the pipeline if requested |
| | 0 | 92 | | if ($PassThru.IsPresent) { |
| | 0 | 93 | | return $Builder |
| | | 94 | | } |
| | | 95 | | } |
| | | 96 | | } |