| | | 1 | | <# |
| | | 2 | | .SYNOPSIS |
| | | 3 | | Adds a claim to the current user. |
| | | 4 | | This function allows you to add a new claim to the current user's identity. |
| | | 5 | | The claim can be of any type, and you must specify the claim type and value. |
| | | 6 | | The function will return the updated claims collection. |
| | | 7 | | .DESCRIPTION |
| | | 8 | | This function is designed to be used in the context of Kestrun for managing user claims. |
| | | 9 | | It supports both string-based claim types and user identity claims. |
| | | 10 | | Claims can be added to the user's identity using this function. |
| | | 11 | | This function is part of the Kestrun.Claims module and is used to manage user claims. |
| | | 12 | | It maps to ClaimCollection.Add method. |
| | | 13 | | .PARAMETER Claims |
| | | 14 | | The claims to add to the current user's identity. This can be a single claim or an array of claims. |
| | | 15 | | If this parameter is specified, the ClaimType and Value parameters are ignored. |
| | | 16 | | If this parameter is not specified, you must provide the ClaimType and Value parameters. |
| | | 17 | | .PARAMETER ClaimType |
| | | 18 | | The type of claim to add to the user's identity. This is required if the Claims parameter is not specified. |
| | | 19 | | It can be a string representing the claim type or a Kestrun.Claims.UserIdentityClaim enum value. |
| | | 20 | | .PARAMETER UserClaimType |
| | | 21 | | The user identity claim type to use when adding the claim. This is required if the Claims parameter is not speci |
| | | 22 | | It must be a valid Kestrun.Claims.UserIdentityClaim enum value. |
| | | 23 | | .PARAMETER Value |
| | | 24 | | The value of the claim to add to the user's identity. This is required if the Claims parameter is not specified. |
| | | 25 | | It can be a string or a Kestrun.Claims.UserIdentityClaim enum value. |
| | | 26 | | If the Claims parameter is specified, this parameter is ignored. |
| | | 27 | | |
| | | 28 | | .EXAMPLE |
| | | 29 | | Adds a claim to the current user's identity. |
| | | 30 | | This example demonstrates how to add a claim using the ClaimType and Value parameters. |
| | | 31 | | Add-KrUserClaim -ClaimType "customClaimType" -Value "customClaimValue" |
| | | 32 | | .EXAMPLE |
| | | 33 | | Adds a claim to the current user's identity. |
| | | 34 | | This example demonstrates how to add a claim using the UserClaimType and Value parameters. |
| | | 35 | | Add-KrUserClaim -UserClaimType "Email" -Value "user@example.com" |
| | | 36 | | .NOTES |
| | | 37 | | This function is part of the Kestrun.Claims module and is used to manage user claims. |
| | | 38 | | It maps to ClaimCollection.Add method. |
| | | 39 | | #> |
| | | 40 | | function Add-KrUserClaim { |
| | | 41 | | [KestrunRuntimeApi('Route')] |
| | | 42 | | [CmdletBinding(DefaultParameterSetName = 'ClaimType')] |
| | | 43 | | [OutputType([System.Security.Claims.Claim[]])] |
| | | 44 | | [OutputType([System.Array])] |
| | | 45 | | param( |
| | | 46 | | [Parameter(ValueFromPipeline)] |
| | | 47 | | [System.Security.Claims.Claim[]] $Claims, |
| | | 48 | | [Parameter(Mandatory = $true, ParameterSetName = 'ClaimType')] |
| | | 49 | | [string] $ClaimType, |
| | | 50 | | [Parameter(Mandatory = $true, ParameterSetName = 'UserClaimType')] |
| | | 51 | | [Kestrun.Claims.UserIdentityClaim] $UserClaimType, |
| | | 52 | | [Parameter(Mandatory = $true)] |
| | | 53 | | [string] $Value |
| | | 54 | | ) |
| | | 55 | | |
| | 0 | 56 | | begin { $bag = [System.Collections.Generic.List[System.Security.Claims.Claim]]::new() } |
| | | 57 | | |
| | 0 | 58 | | process { if ($null -ne $Claims) { $bag.AddRange($Claims) } } |
| | | 59 | | |
| | | 60 | | end { |
| | | 61 | | # resolve ClaimType if the user chose the enum parameter-set |
| | 0 | 62 | | if ($UserClaimType) { |
| | 0 | 63 | | $ClaimType = [Kestrun.Claims.KestrunClaimExtensions]::ToClaimUri($UserClaimType) |
| | | 64 | | } |
| | | 65 | | |
| | 0 | 66 | | $bag.Add([System.Security.Claims.Claim]::new($ClaimType, $Value)) |
| | | 67 | | |
| | | 68 | | # OUTPUT: one strongly-typed array, not enumerated |
| | 0 | 69 | | , ([System.Security.Claims.Claim[]] $bag.ToArray()) |
| | | 70 | | } |
| | | 71 | | } |
| | | 72 | | |