| | 1 | | <# |
| | 2 | | .SYNOPSIS |
| | 3 | | Updates an existing JWT token. |
| | 4 | | .DESCRIPTION |
| | 5 | | This function updates an existing JWT token by renewing it with a new lifetime. |
| | 6 | | It can either take a token directly or extract it from the current HTTP context. |
| | 7 | | .PARAMETER Builder |
| | 8 | | The JWT token builder used to renew the token. |
| | 9 | | .PARAMETER Lifetime |
| | 10 | | The new duration for which the JWT token will be valid. |
| | 11 | | .PARAMETER Token |
| | 12 | | The existing JWT token to update. |
| | 13 | | .PARAMETER FromContext |
| | 14 | | Indicates whether to extract the token from the HTTP context. |
| | 15 | | .OUTPUTS |
| | 16 | | [string] |
| | 17 | | The updated JWT token. |
| | 18 | | .EXAMPLE |
| | 19 | | Update-KrJWT -Builder $jwtBuilder -Token $existingToken -Lifetime (New-TimeSpan -Minutes 30) |
| | 20 | | This updates the existing JWT token with a new lifetime of 30 minutes. |
| | 21 | | .EXAMPLE |
| | 22 | | Update-KrJWT -Builder $jwtBuilder -FromContext -Lifetime (New-TimeSpan -Minutes 30) |
| | 23 | | This updates the existing JWT token extracted from the HTTP context with a new lifetime of 30 minutes. |
| | 24 | | .NOTES |
| | 25 | | This function is part of the Kestrun.Jwt module and is used to manage JWT tokens. |
| | 26 | | Maps to JwtBuilderResult.Renew |
| | 27 | | .LINK |
| | 28 | | https://docs.microsoft.com/en-us/dotnet/api/system.identitymodel.tokens.jwt.jwtsecuritytoken |
| | 29 | | #> |
| | 30 | | function Update-KrJWT { |
| | 31 | | [KestrunRuntimeApi('Everywhere')] |
| | 32 | | [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '')] |
| | 33 | | [CmdletBinding(defaultParameterSetName = 'Token')] |
| | 34 | | [OutputType([string])] |
| | 35 | | param( |
| | 36 | | [Parameter(Mandatory = $true, ValueFromPipeline)] |
| | 37 | | [Kestrun.Jwt.JwtTokenBuilder] $Builder, |
| | 38 | | [Parameter(Mandatory = $true, ParameterSetName = 'Token')] |
| | 39 | | [string]$Token, |
| | 40 | | [Parameter(Mandatory = $true, ParameterSetName = 'Context')] |
| | 41 | | [switch] $FromContext, |
| | 42 | | [Parameter()] |
| | 43 | | [TimeSpan] $Lifetime |
| | 44 | | ) |
| | 45 | | process { |
| 0 | 46 | | if ($PSCmdlet.ParameterSetName -eq 'Token') { |
| 0 | 47 | | return $Builder.RenewJwt($Token, $Lifetime) |
| | 48 | | } |
| 0 | 49 | | if ($FromContext.IsPresent -and $null -ne $Context.Request) { |
| 0 | 50 | | return $Builder.RenewJwt($Context, $Lifetime) |
| | 51 | | } |
| | 52 | | } |
| | 53 | | } |
| | 54 | |
|