| | | 1 | | <# |
| | | 2 | | .SYNOPSIS |
| | | 3 | | Adds a CORS policy to the server. |
| | | 4 | | .DESCRIPTION |
| | | 5 | | This cmdlet allows you to configure a CORS policy for the Kestrun server. |
| | | 6 | | It can be used to specify allowed origins, methods, headers, and other CORS settings. |
| | | 7 | | .PARAMETER Server |
| | | 8 | | The Kestrun server instance to which the CORS policy will be added. |
| | | 9 | | .PARAMETER Name |
| | | 10 | | The name of the CORS policy. |
| | | 11 | | .PARAMETER Builder |
| | | 12 | | The CORS policy builder to configure the CORS policy. |
| | | 13 | | .PARAMETER AllowAnyOrigin |
| | | 14 | | If specified, allows any origin to access the resources. |
| | | 15 | | .PARAMETER AllowAnyMethod |
| | | 16 | | If specified, allows any HTTP method to be used in requests. |
| | | 17 | | .PARAMETER AllowAnyHeader |
| | | 18 | | If specified, allows any header to be included in requests. |
| | | 19 | | If not specified, only headers explicitly allowed will be included. |
| | | 20 | | .PARAMETER AllowCredentials |
| | | 21 | | If specified, allows credentials (cookies, authorization headers, etc.) to be included in requests. |
| | | 22 | | .PARAMETER DisallowCredentials |
| | | 23 | | If specified, disallows credentials in requests. |
| | | 24 | | If not specified, credentials will be allowed. |
| | | 25 | | .PARAMETER AllowAll |
| | | 26 | | If specified, allows any origin, method, and header to access the resources. |
| | | 27 | | This is a shorthand for specifying AllowAnyOrigin, AllowAnyMethod, and AllowAnyHeader |
| | | 28 | | .PARAMETER PassThru |
| | | 29 | | If specified, returns the modified server instance after adding the CORS policy. |
| | | 30 | | .EXAMPLE |
| | | 31 | | $server | Add-KrCorsPolicyMiddleware -Name 'AllowAll' -AllowAnyOrigin -AllowAnyMethod -AllowAnyHeader |
| | | 32 | | This example adds a CORS policy named 'AllowAll' to the server, allowing any origin, method, and header. |
| | | 33 | | .EXAMPLE |
| | | 34 | | $server | Add-KrCorsPolicyMiddleware -Name 'CustomPolicy' -Builder $builder |
| | | 35 | | This example adds a CORS policy named 'CustomPolicy' to the server using the specified CORS policy builder. |
| | | 36 | | .EXAMPLE |
| | | 37 | | $server | Add-KrCorsPolicyMiddleware -Server $server -Name 'CustomPolicy' -AllowAnyOrigin -AllowAnyMethod -Allow |
| | | 38 | | This example adds a CORS policy named 'CustomPolicy' to the server, allowing any origin, method, and header. |
| | | 39 | | .NOTES |
| | | 40 | | This cmdlet is used to configure CORS policies for the Kestrun server, allowing you to control cross-origin requ |
| | | 41 | | .LINK |
| | | 42 | | https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.cors.infrastructure.corspolicybuilder?view=asp |
| | | 43 | | #> |
| | | 44 | | function Add-KrCorsPolicyMiddleware { |
| | | 45 | | [KestrunRuntimeApi('Definition')] |
| | | 46 | | [CmdletBinding(defaultParameterSetName = 'Items')] |
| | | 47 | | [OutputType([Kestrun.Hosting.KestrunHost])] |
| | | 48 | | param( |
| | | 49 | | [Parameter(Mandatory = $false, ValueFromPipeline = $true)] |
| | | 50 | | [Kestrun.Hosting.KestrunHost]$Server, |
| | | 51 | | |
| | | 52 | | [Parameter(Mandatory = $true)] |
| | | 53 | | [string]$Name, |
| | | 54 | | |
| | | 55 | | [Parameter(Mandatory = $true, ParameterSetName = 'Options')] |
| | | 56 | | [Microsoft.AspNetCore.Cors.Infrastructure.CorsPolicyBuilder]$Builder, |
| | | 57 | | |
| | | 58 | | [Parameter(ParameterSetName = 'Items')] |
| | | 59 | | [switch]$AllowAnyOrigin, |
| | | 60 | | |
| | | 61 | | [Parameter(ParameterSetName = 'Items')] |
| | | 62 | | [switch]$AllowAnyMethod, |
| | | 63 | | |
| | | 64 | | [Parameter(ParameterSetName = 'Items')] |
| | | 65 | | [switch]$AllowAnyHeader, |
| | | 66 | | |
| | | 67 | | [Parameter(ParameterSetName = 'Items')] |
| | | 68 | | [switch]$AllowCredentials, |
| | | 69 | | |
| | | 70 | | [Parameter(ParameterSetName = 'Items')] |
| | | 71 | | [switch]$DisallowCredentials, |
| | | 72 | | |
| | | 73 | | [Parameter(ParameterSetName = 'AllowAll')] |
| | | 74 | | [switch]$AllowAll, |
| | | 75 | | |
| | | 76 | | [Parameter()] |
| | | 77 | | [switch]$PassThru |
| | | 78 | | ) |
| | | 79 | | begin { |
| | | 80 | | # Ensure the server instance is resolved |
| | 0 | 81 | | $Server = Resolve-KestrunServer -Server $Server |
| | | 82 | | } |
| | | 83 | | process { |
| | 0 | 84 | | if ($PSCmdlet.ParameterSetName -eq 'Items') { |
| | | 85 | | |
| | 0 | 86 | | if ($AllowCredentials.IsPresent -and $DisallowCredentials.IsPresent) { |
| | 0 | 87 | | throw 'Cannot specify both AllowCredentials and DisallowCredentials.' |
| | | 88 | | } |
| | | 89 | | # Build the CORS policy based on the specified parameters |
| | 0 | 90 | | $Builder = [Microsoft.AspNetCore.Cors.Infrastructure.CorsPolicyBuilder]::new() |
| | 0 | 91 | | if ($AllowAnyOrigin.IsPresent) { |
| | 0 | 92 | | $Builder.AllowAnyOrigin() | Out-Null |
| | | 93 | | } |
| | 0 | 94 | | if ($AllowAnyMethod.IsPresent) { |
| | 0 | 95 | | $Builder.AllowAnyMethod() | Out-Null |
| | | 96 | | } |
| | 0 | 97 | | if ($AllowAnyHeader.IsPresent) { |
| | 0 | 98 | | $Builder.AllowAnyHeader() | Out-Null |
| | | 99 | | } |
| | 0 | 100 | | if ($AllowCredentials.IsPresent) { |
| | 0 | 101 | | $Builder.AllowCredentials() | Out-Null |
| | | 102 | | } |
| | 0 | 103 | | if ($DisallowCredentials.IsPresent) { |
| | 0 | 104 | | $Builder.DisallowCredentials() | Out-Null |
| | | 105 | | } |
| | 0 | 106 | | } elseif ( $AllowAll.IsPresent ) { |
| | | 107 | | # If AllowAll is specified, configure the builder to allow any origin, method, and header |
| | 0 | 108 | | $Builder = [Microsoft.AspNetCore.Cors.Infrastructure.CorsPolicyBuilder]::new() |
| | 0 | 109 | | $Builder.AllowAnyOrigin() | Out-Null |
| | 0 | 110 | | $Builder.AllowAnyMethod() | Out-Null |
| | 0 | 111 | | $Builder.AllowAnyHeader() | Out-Null |
| | | 112 | | } |
| | | 113 | | |
| | 0 | 114 | | [Kestrun.Hosting.KestrunSecurityMiddlewareExtensions]::AddCors($Server, $Name, $Builder) | Out-Null |
| | | 115 | | # Add the CORS policy to the server |
| | | 116 | | |
| | 0 | 117 | | if ($PassThru.IsPresent) { |
| | | 118 | | # if the PassThru switch is specified, return the modified server instance |
| | 0 | 119 | | return $Server |
| | | 120 | | } |
| | | 121 | | } |
| | | 122 | | } |
| | | 123 | | |