| | 1 | | <# |
| | 2 | | .SYNOPSIS |
| | 3 | | Protects a JWT token using a specified secret or certificate. |
| | 4 | | .DESCRIPTION |
| | 5 | | This function allows you to sign a JWT token with a secret or certificate, ensuring its integrity and authentici |
| | 6 | | .PARAMETER Builder |
| | 7 | | The JWT token builder to modify. |
| | 8 | | .PARAMETER Base64Url |
| | 9 | | The Base64Url encoded secret to use for signing the JWT token. |
| | 10 | | .PARAMETER HexadecimalKey |
| | 11 | | The hexadecimal key to use for signing the JWT token. |
| | 12 | | .PARAMETER Passphrase |
| | 13 | | The passphrase to use for signing the JWT token, provided as a secure string. |
| | 14 | | .PARAMETER PemPath |
| | 15 | | The path to a PEM file containing the RSA key to use for signing the JWT token. |
| | 16 | | .PARAMETER Certificate |
| | 17 | | The X509 certificate to use for signing the JWT token. |
| | 18 | | .PARAMETER Algorithm |
| | 19 | | The algorithm to use for signing the JWT token. |
| | 20 | | Defaults to 'Auto' which will determine the algorithm based on the provided secret or certificate. |
| | 21 | | .PARAMETER X509Certificate |
| | 22 | | The X509 certificate to use for signing the JWT token. |
| | 23 | | .OUTPUTS |
| | 24 | | [Kestrun.Jwt.JwtTokenBuilder] |
| | 25 | | The modified JWT token builder with the signing configuration applied. |
| | 26 | | .EXAMPLE |
| | 27 | | $builder = New-KrJWTTokenBuilder | Protect-KrJWT -Base64Url "your_base64_url_secret" |
| | 28 | | $builder | Protect-KrJWT -HexadecimalKey "a1b2c3d4e5f6" |
| | 29 | | $builder | Protect-KrJWT -Passphrase (ConvertTo-SecureString "mysecret" -AsPlainText -Force) |
| | 30 | | $builder | Protect-KrJWT -PemPath "C:\path\to\key.pem" |
| | 31 | | $builder | Protect-KrJWT -Certificate (Get-Item "C:\path\to\certificate.pfx") |
| | 32 | | This example demonstrates how to create a JWT token builder and apply various signing methods. |
| | 33 | | .NOTES |
| | 34 | | This function is part of the Kestrun.Jwt module and is used to build JWT tokens |
| | 35 | | Maps to JwtTokenBuilder.SignWithSecret, JwtTokenBuilder.SignWithSecretHex, JwtTokenBuilder.SignWithSecretPassphr |
| | 36 | | JwtTokenBuilder.SignWithRsaPem, and JwtTokenBuilder.SignWithCertificate methods. |
| | 37 | | .LINK |
| | 38 | | https://docs.microsoft.com/en-us/dotnet/api/system.identitymodel.tokens.jwt.jwtsecuritytoken |
| | 39 | | #> |
| | 40 | | function Protect-KrJWT { |
| | 41 | | [KestrunRuntimeApi('Everywhere')] |
| | 42 | | [CmdletBinding(DefaultParameterSetName = 'SecretBase64Url')] |
| | 43 | | [OutputType([Kestrun.Jwt.JwtTokenBuilder])] |
| | 44 | | param( |
| | 45 | | [Parameter(Mandatory = $true, ValueFromPipeline)] |
| | 46 | | [Kestrun.Jwt.JwtTokenBuilder] $Builder, |
| | 47 | | [Parameter(Mandatory = $true, ParameterSetName = 'SecretBase64Url')] |
| | 48 | | [string] $Base64Url, |
| | 49 | | [Parameter(Mandatory = $true, ParameterSetName = 'SecretHexadecimalKey')] |
| | 50 | | [string] $HexadecimalKey, |
| | 51 | | [Parameter(Mandatory = $true, ParameterSetName = 'SecretPassphrase')] |
| | 52 | | [securestring] $Passphrase, |
| | 53 | | [Parameter(Mandatory = $true, ParameterSetName = 'PemPath')] |
| | 54 | | [string] $PemPath, |
| | 55 | | [Parameter(Mandatory = $true, ParameterSetName = 'Certificate')] |
| | 56 | | [System.Security.Cryptography.X509Certificates.X509Certificate2] $X509Certificate, |
| | 57 | | [Parameter(Mandatory = $false)] |
| | 58 | | [ValidateSet('Auto', 'HS256', 'HS384', 'HS512', 'RS256', 'RS384', 'RS512', 'ES256', 'ES384', 'ES512')] |
| | 59 | | [string] $Algorithm = 'Auto' # Default to 'Auto' which will determine the algorithm based on the provided secret |
| | 60 | | ) |
| | 61 | |
|
| | 62 | | process { |
| 1 | 63 | | $algEnum = [Kestrun.Jwt.JwtAlgorithm]::$Algorithm |
| 1 | 64 | | switch ($PSCmdlet.ParameterSetName) { |
| | 65 | | 'SecretBase64Url' { |
| 0 | 66 | | $Builder.SignWithSecret($Base64Url, $algEnum) | Out-Null |
| | 67 | | break |
| | 68 | | } |
| | 69 | | 'SecretHexadecimalKey' { |
| 2 | 70 | | $Builder.SignWithSecretHex($HexadecimalKey, $algEnum) | Out-Null |
| | 71 | | break |
| | 72 | | } |
| | 73 | | 'SecretPassphrase' { |
| 0 | 74 | | $Builder.SignWithSecretPassphrase($Passphrase, $algEnum) | Out-Null |
| | 75 | | break |
| | 76 | | } |
| | 77 | | 'PemPath' { |
| 0 | 78 | | $resolvedPath = Resolve-KrPath -Path $PemPath -KestrunRoot |
| 0 | 79 | | $Builder.SignWithRsaPem($resolvedPath, $algEnum) | Out-Null |
| | 80 | | break |
| | 81 | | } |
| | 82 | | 'Certificate' { |
| 0 | 83 | | $Builder.SignWithCertificate($X509Certificate, $algEnum) | Out-Null |
| | 84 | | break |
| | 85 | | } |
| | 86 | | } |
| 1 | 87 | | return $Builder |
| | 88 | | } |
| | 89 | | } |
| | 90 | |
|