| | | 1 | | <# |
| | | 2 | | .SYNOPSIS |
| | | 3 | | Sets the validity period for the JWT token. |
| | | 4 | | .DESCRIPTION |
| | | 5 | | This function sets the validity period for the JWT token, specifying how long the token will be valid. |
| | | 6 | | The period can be provided as a [TimeSpan] object (-Lifetime) or directly as -Hours, -Minutes, or -Seconds. |
| | | 7 | | .PARAMETER Builder |
| | | 8 | | The JWT token builder to modify. |
| | | 9 | | .PARAMETER Lifetime |
| | | 10 | | The duration for which the JWT token will be valid. |
| | | 11 | | .PARAMETER Hours |
| | | 12 | | The number of hours for which the JWT token will be valid. |
| | | 13 | | .PARAMETER Minutes |
| | | 14 | | The number of minutes for which the JWT token will be valid. |
| | | 15 | | .PARAMETER Seconds |
| | | 16 | | The number of seconds for which the JWT token will be valid. |
| | | 17 | | .OUTPUTS |
| | | 18 | | [Kestrun.Jwt.JwtTokenBuilder] |
| | | 19 | | The modified JWT token builder. |
| | | 20 | | .EXAMPLE |
| | | 21 | | $builder = New-KrJWTTokenBuilder | Limit-KrJWTValidity -Hours 1 |
| | | 22 | | Creates a JWT token builder and sets its validity period to 1 hour. |
| | | 23 | | .EXAMPLE |
| | | 24 | | $builder = New-KrJWTTokenBuilder | Limit-KrJWTValidity -Lifetime (New-TimeSpan -Hours 2 -Minutes 30) |
| | | 25 | | Creates a JWT token builder and sets its validity period to 2 hours and 30 minutes. |
| | | 26 | | .NOTES |
| | | 27 | | This function is part of the Kestrun.Jwt module and is used to build JWT tokens. |
| | | 28 | | Maps to JwtTokenBuilder.ValidFor(TimeSpan). |
| | | 29 | | .LINK |
| | | 30 | | https://docs.microsoft.com/en-us/dotnet/api/system.identitymodel.tokens.jwt.jwtsecuritytoken |
| | | 31 | | #> |
| | | 32 | | function Limit-KrJWTValidity { |
| | | 33 | | [KestrunRuntimeApi('Everywhere')] |
| | | 34 | | [CmdletBinding(DefaultParameterSetName = 'TimeSpan')] |
| | | 35 | | [OutputType([Kestrun.Jwt.JwtTokenBuilder])] |
| | | 36 | | param( |
| | | 37 | | [Parameter(Mandatory, ValueFromPipeline)] |
| | | 38 | | [Kestrun.Jwt.JwtTokenBuilder] $Builder, |
| | | 39 | | |
| | | 40 | | [Parameter(Mandatory, ParameterSetName = 'TimeSpan')] |
| | | 41 | | [TimeSpan] $Lifetime, |
| | | 42 | | |
| | | 43 | | [Parameter(ParameterSetName = 'Discrete')] |
| | | 44 | | [ValidateRange(0, [double]::MaxValue)] |
| | | 45 | | [double] $Hours = 0, |
| | | 46 | | |
| | | 47 | | [Parameter(ParameterSetName = 'Discrete')] |
| | | 48 | | [ValidateRange(0, [double]::MaxValue)] |
| | | 49 | | [double] $Minutes = 0, |
| | | 50 | | |
| | | 51 | | [Parameter(ParameterSetName = 'Discrete')] |
| | | 52 | | [ValidateRange(0, [double]::MaxValue)] |
| | | 53 | | [double] $Seconds = 0 |
| | | 54 | | ) |
| | | 55 | | process { |
| | 0 | 56 | | $ts = (($PSCmdlet.ParameterSetName -eq 'TimeSpan') ? |
| | 0 | 57 | | $Lifetime : [TimeSpan]::FromSeconds(($Hours * 3600) + ($Minutes * 60) + $Seconds)) |
| | | 58 | | |
| | 0 | 59 | | return $Builder.ValidFor($ts) |
| | | 60 | | } |
| | | 61 | | } |