| | | 1 | | <# |
| | | 2 | | .SYNOPSIS |
| | | 3 | | Adds OpenID Connect (Authorization Code) authentication to the Kestrun server. |
| | | 4 | | .DESCRIPTION |
| | | 5 | | Convenience wrapper around the C# extension AddOpenIdConnectAuthentication. Registers three schemes: |
| | | 6 | | <Name>, <Name>.Cookies, <Name>.Policy |
| | | 7 | | Enables PKCE and token persistence by default; supports custom scopes and callback path. |
| | | 8 | | .PARAMETER Server |
| | | 9 | | The Kestrun server instance. If omitted, uses the current active server. |
| | | 10 | | .PARAMETER AuthenticationScheme |
| | | 11 | | Base scheme name (default 'Oidc'). |
| | | 12 | | .PARAMETER DisplayName |
| | | 13 | | The display name for the authentication scheme (default is the OpenID Connect default display name). |
| | | 14 | | .PARAMETER Description |
| | | 15 | | A description of the OpenID Connect authentication scheme. |
| | | 16 | | .PARAMETER Authority |
| | | 17 | | The OpenID Connect authority URL. |
| | | 18 | | .PARAMETER ClientId |
| | | 19 | | The OpenID Connect client ID. |
| | | 20 | | .PARAMETER ClientSecret |
| | | 21 | | The OpenID Connect client secret. |
| | | 22 | | .PARAMETER AuthorizationEndpoint |
| | | 23 | | The OpenID Connect authorization endpoint URL. |
| | | 24 | | .PARAMETER TokenEndpoint |
| | | 25 | | The OpenID Connect token endpoint URL. |
| | | 26 | | .PARAMETER ResponseType |
| | | 27 | | The OpenID Connect response type (default is 'Code'). |
| | | 28 | | .PARAMETER CallbackPath |
| | | 29 | | The callback path for OpenID Connect responses. |
| | | 30 | | .PARAMETER SignedOutCallbackPath |
| | | 31 | | The callback path for sign-out responses. |
| | | 32 | | .PARAMETER SaveTokens |
| | | 33 | | If specified, saves the OpenID Connect tokens in the authentication properties. |
| | | 34 | | .PARAMETER UsePkce |
| | | 35 | | If specified, enables Proof Key for Code Exchange (PKCE) for enhanced security. |
| | | 36 | | .PARAMETER GetClaimsFromUserInfoEndpoint |
| | | 37 | | If specified, retrieves additional claims from the UserInfo endpoint. |
| | | 38 | | .PARAMETER ClaimPolicy |
| | | 39 | | An optional Kestrun.Claims.ClaimPolicyConfig to apply claim policies during authentication. |
| | | 40 | | .PARAMETER Options |
| | | 41 | | An instance of Kestrun.Authentication.OidcOptions containing the OIDC configuration. |
| | | 42 | | .PARAMETER PassThru |
| | | 43 | | Return the modified server object. |
| | | 44 | | .EXAMPLE |
| | | 45 | | Add-KrOpenIdConnectAuthentication -Authority 'https://example.com' -ClientId $id -ClientSecret $secret |
| | | 46 | | .EXAMPLE |
| | | 47 | | Add-KrOpenIdConnectAuthentication -AuthenticationScheme 'AzureAD' -Authority $authority -ClientId $id -ClientSecret |
| | | 48 | | #> |
| | | 49 | | function Add-KrOpenIdConnectAuthentication { |
| | | 50 | | [KestrunRuntimeApi('Definition')] |
| | | 51 | | [CmdletBinding()] |
| | | 52 | | [OutputType([Kestrun.Hosting.KestrunHost])] |
| | | 53 | | param( |
| | | 54 | | [Parameter(ValueFromPipeline = $true)] |
| | | 55 | | [Kestrun.Hosting.KestrunHost]$Server, |
| | | 56 | | |
| | | 57 | | [Parameter(Mandatory = $false)] |
| | | 58 | | [string]$AuthenticationScheme = [Kestrun.Authentication.AuthenticationDefaults]::OidcSchemeName, |
| | | 59 | | |
| | | 60 | | [Parameter(Mandatory = $false)] |
| | | 61 | | [string]$DisplayName = [Kestrun.Authentication.AuthenticationDefaults]::OidcDisplayName, |
| | | 62 | | |
| | | 63 | | [Parameter(Mandatory = $false)] |
| | | 64 | | [string]$Description, |
| | | 65 | | |
| | | 66 | | [Parameter(Mandatory = $false)] |
| | | 67 | | [string]$Authority, |
| | | 68 | | |
| | | 69 | | [Parameter(Mandatory = $false)] |
| | | 70 | | [string]$ClientId, |
| | | 71 | | |
| | | 72 | | [Parameter(Mandatory = $false)] |
| | | 73 | | [string]$ClientSecret, |
| | | 74 | | |
| | | 75 | | [Parameter(Mandatory = $false)] |
| | | 76 | | [string]$AuthorizationEndpoint, |
| | | 77 | | |
| | | 78 | | [Parameter(Mandatory = $false)] |
| | | 79 | | [string]$TokenEndpoint, |
| | | 80 | | |
| | | 81 | | [Parameter(Mandatory = $false)] |
| | | 82 | | [string]$CallbackPath, |
| | | 83 | | |
| | | 84 | | [Parameter(Mandatory = $false)] |
| | | 85 | | [string]$SignedOutCallbackPath, |
| | | 86 | | |
| | | 87 | | [Parameter(Mandatory = $false)] |
| | | 88 | | [Microsoft.IdentityModel.Protocols.OpenIdConnect.OpenIdConnectResponseType]$ResponseType, |
| | | 89 | | |
| | | 90 | | [Parameter(Mandatory = $false)] |
| | | 91 | | [switch]$SaveTokens, |
| | | 92 | | |
| | | 93 | | [Parameter(Mandatory = $false)] |
| | | 94 | | [switch]$UsePkce, |
| | | 95 | | |
| | | 96 | | [Parameter(Mandatory = $false)] |
| | | 97 | | [switch]$GetClaimsFromUserInfoEndpoint, |
| | | 98 | | |
| | | 99 | | [Parameter(Mandatory = $false)] |
| | | 100 | | [Kestrun.Claims.ClaimPolicyConfig]$ClaimPolicy, |
| | | 101 | | |
| | | 102 | | [Parameter(Mandatory = $false)] |
| | | 103 | | [Kestrun.Authentication.OidcOptions]$Options, |
| | | 104 | | |
| | | 105 | | [Parameter(Mandatory = $false)] |
| | | 106 | | [switch]$PassThru |
| | | 107 | | ) |
| | | 108 | | begin { |
| | | 109 | | # Ensure the server instance is resolved |
| | 0 | 110 | | $Server = Resolve-KestrunServer -Server $Server |
| | | 111 | | } |
| | | 112 | | process { |
| | 0 | 113 | | if ( $null -eq $Options ) { |
| | | 114 | | # Build options from individual parameters if not provided |
| | 0 | 115 | | $Options = [Kestrun.Authentication.OidcOptions]::new() |
| | | 116 | | } |
| | 0 | 117 | | if ($Authority) { $Options.Authority = $Authority } |
| | 0 | 118 | | if ($ClientId) { $Options.ClientId = $ClientId } |
| | 0 | 119 | | if ($ClientSecret) { $Options.ClientSecret = $ClientSecret } |
| | 0 | 120 | | if ($AuthorizationEndpoint) { $Options.AuthorizationEndpoint = $AuthorizationEndpoint } |
| | 0 | 121 | | if ($TokenEndpoint) { $Options.TokenEndpoint = $TokenEndpoint } |
| | 0 | 122 | | if ($CallbackPath) { $Options.CallbackPath = $CallbackPath } |
| | 0 | 123 | | if ($SignedOutCallbackPath) { $Options.SignedOutCallbackPath = $SignedOutCallbackPath } |
| | 0 | 124 | | if ($ClaimPolicy) { $Options.ClaimPolicy = $ClaimPolicy } |
| | 0 | 125 | | if ($ResponseType) { $Options.ResponseType = $ResponseType } |
| | 0 | 126 | | if ($Description) { $Options.Description = $Description } |
| | 0 | 127 | | $Options.SaveTokens = $SaveTokens.IsPresent |
| | 0 | 128 | | $Options.UsePkce = $UsePkce.IsPresent |
| | 0 | 129 | | $Options.GetClaimsFromUserInfoEndpoint = $GetClaimsFromUserInfoEndpoint.IsPresent |
| | | 130 | | # Call C# extension with optional claim policy |
| | 0 | 131 | | [Kestrun.Hosting.KestrunHostAuthnExtensions]::AddOpenIdConnectAuthentication( |
| | | 132 | | $Server, |
| | | 133 | | $AuthenticationScheme, |
| | | 134 | | $DisplayName, |
| | | 135 | | $Options |
| | 0 | 136 | | ) | Out-Null |
| | | 137 | | |
| | 0 | 138 | | if ($PassThru.IsPresent) { |
| | | 139 | | # if the PassThru switch is specified, return the modified server instance |
| | 0 | 140 | | return $Server |
| | | 141 | | } |
| | | 142 | | } |
| | | 143 | | } |