Functions/Tokens/Get-JWTTokenInfo.ps1
function Get-JWTTokenInfo { [CmdletBinding()] Param ( # Full JWT Token to Decode [Parameter(Mandatory=$true)] [string] $Token ) Process { # Check for Token Validity if (!$token.Contains(".") -or !$token.StartsWith("eyJ")) { Write-Error "Invalid token" -ErrorAction Stop } # Deconstruct Token foreach ($i in 0..1) { $data = $token.Split('.')[$i].Replace('-', '+').Replace('_', '/') switch ($data.Length % 4) { 0 { break } 2 { $data += '==' } 3 { $data += '=' } } } $decodedToken = [System.Text.Encoding]::UTF8.GetString([convert]::FromBase64String($data)) | ConvertFrom-Json # Deconstruct Signature foreach ($i in 0..2) { $sig = $token.Split('.')[$i].Replace('-', '+').Replace('_', '/') switch ($sig.Length % 4) { 0 { break } 2 { $sig += '==' } 3 { $sig += '=' } } } # Attach Signature to Token Object $decodedToken | Add-Member -Type NoteProperty -Name "sig" -Value $sig # Convert Expiry time to PowerShell DateTime $orig = (Get-Date -Year 1970 -Month 1 -Day 1 -hour 0 -Minute 0 -Second 0 -Millisecond 0) $timeZone = Get-TimeZone $utcTime = $orig.AddSeconds($decodedToken.exp) $offset = $timeZone.GetUtcOffset($(Get-Date)).TotalMinutes #Daylight saving needs to be calculated $localTime = $utcTime.AddMinutes($offset) # Return local time, $timeToExpiry = ($localTime - (get-date)) $expired = if ($localTime -lt (get-date)){$true}else{$false} # Attach Expiry Time to Token Object $decodedToken | Add-Member -Type NoteProperty -Name "expiryDateTime" -Value $localTime $decodedToken | Add-Member -Type NoteProperty -Name "timeToExpiry" -Value $timeToExpiry $decodedToken | Add-Member -Type NoteProperty -Name "expired" -Value $expired # Output Token $decodedToken } } |