| | | 1 | | |
| | | 2 | | <# |
| | | 3 | | .SYNOPSIS |
| | | 4 | | Adds a claim to the JWT token builder. |
| | | 5 | | .DESCRIPTION |
| | | 6 | | This function adds a claim to the JWT token builder, allowing for the specification of additional data. |
| | | 7 | | .PARAMETER Builder |
| | | 8 | | The JWT token builder to modify. |
| | | 9 | | .PARAMETER ClaimType |
| | | 10 | | The type of the claim to add. |
| | | 11 | | .PARAMETER UserClaimType |
| | | 12 | | The user-specific type of the claim to add, such as "Admin", "User", etc. |
| | | 13 | | This parameter is used when the claim type is based on user identity claims. |
| | | 14 | | .PARAMETER Value |
| | | 15 | | The value of the claim to add. |
| | | 16 | | .OUTPUTS |
| | | 17 | | [Kestrun.Jwt.JwtTokenBuilder] |
| | | 18 | | The modified JWT token builder. |
| | | 19 | | .EXAMPLE |
| | | 20 | | $builder = New-KrJWTTokenBuilder | Add-KrJWTClaim -Type "role" -Value "admin" |
| | | 21 | | This example creates a new JWT token builder and adds a claim to it. |
| | | 22 | | .NOTES |
| | | 23 | | This function is part of the Kestrun.Jwt module and is used to build JWT tokens |
| | | 24 | | Maps to JwtTokenBuilder.AddClaim |
| | | 25 | | .LINK |
| | | 26 | | https://docs.microsoft.com/en-us/dotnet/api/system.identitymodel.tokens.jwt.jwtsecuritytoken |
| | | 27 | | #> |
| | | 28 | | function Add-KrJWTClaim { |
| | | 29 | | [KestrunRuntimeApi('Everywhere')] |
| | | 30 | | [CmdletBinding(DefaultParameterSetName = 'ClaimType')] |
| | | 31 | | [OutputType([Kestrun.Jwt.JwtTokenBuilder])] |
| | | 32 | | param( |
| | | 33 | | [Parameter(Mandatory = $true, ValueFromPipeline)] |
| | | 34 | | [Kestrun.Jwt.JwtTokenBuilder] $Builder, |
| | | 35 | | [Parameter(Mandatory = $true, ParameterSetName = 'ClaimType')] |
| | | 36 | | [string] $ClaimType, |
| | | 37 | | [Parameter(Mandatory = $true, ParameterSetName = 'UserClaimType')] |
| | | 38 | | [Kestrun.Claims.UserIdentityClaim] $UserClaimType, |
| | | 39 | | [Parameter(Mandatory = $true)] |
| | | 40 | | [string] $Value |
| | | 41 | | ) |
| | | 42 | | process { |
| | | 43 | | # resolve ClaimType if the user chose the enum parameter-set |
| | 0 | 44 | | if ($UserClaimType) { |
| | 0 | 45 | | $ClaimType = [Kestrun.Claims.KestrunClaimExtensions]::ToClaimUri($UserClaimType) |
| | | 46 | | } |
| | | 47 | | |
| | 0 | 48 | | return $Builder.AddClaim($ClaimType, $Value) |
| | | 49 | | } |
| | | 50 | | } |
| | | 51 | | |