| | | 1 | | <# |
| | | 2 | | .SYNOPSIS |
| | | 3 | | Adds OAuth 2.0 (Authorization Code) authentication to the Kestrun server. |
| | | 4 | | .DESCRIPTION |
| | | 5 | | Configures the Kestrun server to use a generic OAuth 2.0 authorization-code flow. |
| | | 6 | | You can pass a prebuilt OAuthOptions object, or specify individual items (authority, paths, client, etc.). |
| | | 7 | | .PARAMETER Server |
| | | 8 | | The Kestrun server instance to configure. If not specified, the current server instance is used. |
| | | 9 | | .PARAMETER AuthenticationScheme |
| | | 10 | | The name of the OAuth authentication scheme (e.g., 'MyOAuth'). |
| | | 11 | | .PARAMETER DisplayName |
| | | 12 | | The display name for the authentication scheme (e.g., 'GitHub Login'). |
| | | 13 | | .PARAMETER Description |
| | | 14 | | A description of the OAuth authentication scheme. |
| | | 15 | | .PARAMETER ClientId |
| | | 16 | | The OAuth client ID. |
| | | 17 | | .PARAMETER ClientSecret |
| | | 18 | | The OAuth client secret. |
| | | 19 | | .PARAMETER AuthorizationEndpoint |
| | | 20 | | The OAuth authorization endpoint URL. |
| | | 21 | | .PARAMETER TokenEndpoint |
| | | 22 | | The OAuth token endpoint URL. |
| | | 23 | | .PARAMETER CallbackPath |
| | | 24 | | The callback path for OAuth responses. |
| | | 25 | | .PARAMETER SaveTokens |
| | | 26 | | If specified, saves the OAuth tokens in the authentication properties. |
| | | 27 | | .PARAMETER UsePkce |
| | | 28 | | If specified, enables Proof Key for Code Exchange (PKCE) for enhanced security. |
| | | 29 | | .PARAMETER ClaimPolicy |
| | | 30 | | An optional Kestrun.Claims.ClaimPolicyConfig to apply claim policies during authentication. |
| | | 31 | | .PARAMETER Options |
| | | 32 | | An instance of Kestrun.Authentication.OAuth2Options containing the OAuth configuration. |
| | | 33 | | .PARAMETER PassThru |
| | | 34 | | If specified, returns the modified Kestrun server object. |
| | | 35 | | .EXAMPLE |
| | | 36 | | Add-KrOAuth2Authentication -AuthenticationScheme 'MyOAuth' -Options $oauthOptions |
| | | 37 | | Adds an OAuth2 authentication scheme named 'MyOAuth' using the provided options. |
| | | 38 | | .NOTES |
| | | 39 | | This is a convenience wrapper around the C# extension AddOAuth2Authentication. |
| | | 40 | | #> |
| | | 41 | | function Add-KrOAuth2Authentication { |
| | | 42 | | [KestrunRuntimeApi('Definition')] |
| | | 43 | | [CmdletBinding()] |
| | | 44 | | [OutputType([Kestrun.Hosting.KestrunHost])] |
| | | 45 | | param( |
| | | 46 | | [Parameter(Mandatory = $false, ValueFromPipeline = $true)] |
| | | 47 | | [Kestrun.Hosting.KestrunHost]$Server, |
| | | 48 | | |
| | | 49 | | [Parameter(Mandatory = $false)] |
| | | 50 | | [string]$AuthenticationScheme = [Kestrun.Authentication.AuthenticationDefaults]::OAuth2SchemeName, |
| | | 51 | | |
| | | 52 | | [Parameter(Mandatory = $false)] |
| | | 53 | | [string]$DisplayName = [Kestrun.Authentication.AuthenticationDefaults]::OAuth2DisplayName, |
| | | 54 | | |
| | | 55 | | [Parameter(Mandatory = $false)] |
| | | 56 | | [string]$Description, |
| | | 57 | | |
| | | 58 | | [Parameter(Mandatory = $false)] |
| | | 59 | | [string]$ClientId, |
| | | 60 | | |
| | | 61 | | [Parameter(Mandatory = $false)] |
| | | 62 | | [string]$ClientSecret, |
| | | 63 | | |
| | | 64 | | [Parameter(Mandatory = $false)] |
| | | 65 | | [string]$AuthorizationEndpoint, |
| | | 66 | | |
| | | 67 | | [Parameter(Mandatory = $false)] |
| | | 68 | | [string]$TokenEndpoint, |
| | | 69 | | |
| | | 70 | | [Parameter(Mandatory = $false)] |
| | | 71 | | [string]$CallbackPath, |
| | | 72 | | |
| | | 73 | | [Parameter(Mandatory = $false)] |
| | | 74 | | [switch]$SaveTokens, |
| | | 75 | | |
| | | 76 | | [Parameter(Mandatory = $false)] |
| | | 77 | | [switch]$UsePkce, |
| | | 78 | | |
| | | 79 | | [Parameter(Mandatory = $false)] |
| | | 80 | | [Kestrun.Claims.ClaimPolicyConfig]$ClaimPolicy, |
| | | 81 | | |
| | | 82 | | [Parameter(Mandatory = $false)] |
| | | 83 | | [Kestrun.Authentication.OAuth2Options]$Options, |
| | | 84 | | |
| | | 85 | | [Parameter(Mandatory = $false)] |
| | | 86 | | [switch]$PassThru |
| | | 87 | | ) |
| | | 88 | | begin { |
| | | 89 | | # Ensure the server instance is resolved |
| | 0 | 90 | | $Server = Resolve-KestrunServer -Server $Server |
| | | 91 | | } |
| | | 92 | | process { |
| | 0 | 93 | | if ($null -eq $Options) { |
| | | 94 | | # Build options from individual parameters if not provided |
| | 0 | 95 | | $Options = [Kestrun.Authentication.OAuth2Options]::new() |
| | | 96 | | } |
| | | 97 | | |
| | 0 | 98 | | if ($ClientId) { $Options.ClientId = $ClientId } |
| | 0 | 99 | | if ($ClientSecret) { $Options.ClientSecret = $ClientSecret } |
| | 0 | 100 | | if ($AuthorizationEndpoint) { $Options.AuthorizationEndpoint = $AuthorizationEndpoint } |
| | 0 | 101 | | if ($TokenEndpoint) { $Options.TokenEndpoint = $TokenEndpoint } |
| | 0 | 102 | | if ($CallbackPath) { $Options.CallbackPath = $CallbackPath } |
| | 0 | 103 | | if ($ClaimPolicy) { $Options.ClaimPolicy = $ClaimPolicy } |
| | 0 | 104 | | if ($Description) { $Options.Description = $Description } |
| | | 105 | | |
| | 0 | 106 | | $Options.SaveTokens = $SaveTokens.IsPresent |
| | 0 | 107 | | $Options.UsePkce = $UsePkce.IsPresent |
| | | 108 | | |
| | | 109 | | # Bridge to your C# extension (parallel to AddCookieAuthentication) |
| | 0 | 110 | | [Kestrun.Hosting.KestrunHostAuthnExtensions]::AddOAuth2Authentication( |
| | 0 | 111 | | $Server, $AuthenticationScheme, $DisplayName, $Options) | Out-Null |
| | 0 | 112 | | if ($PassThru.IsPresent) { |
| | 0 | 113 | | return $Server |
| | | 114 | | } |
| | | 115 | | } |
| | | 116 | | } |