Private/Authentication/ConvertFrom-JwtToken.ps1
function ConvertFrom-JwtToken { [CmdletBinding()] param( [Parameter(Mandatory)] [string]$Token ) if (-not $Token.Contains(".") -or -not $Token.StartsWith("eyJ")) { Write-ModuleLog -Message "Invalid token format detected" -Level Error -Component 'TokenProcessing' ` -ThrowError -ErrorOperation 'TokenDecoding' -ErrorMessage 'Invalid token format' } try { $tokenPayload = $Token.Split(".")[1].Replace('-', '+').Replace('_', '/') while ($tokenPayload.Length % 4) { $tokenPayload += "=" } $tokenBytes = [System.Convert]::FromBase64String($tokenPayload) $tokenJson = [System.Text.Encoding]::ASCII.GetString($tokenBytes) $tokenData = $tokenJson | ConvertFrom-Json -AsHashtable $tokenData["expirationDateTime"] = ([DateTime]('1970,1,1')).AddSeconds($tokenData["exp"]).ToLocalTime() $tokenData["access_token"] = $Token return [PSCustomObject]$tokenData } catch { Write-ModuleLog -Message "Unexpected error during token conversion" -Level Error -Component 'TokenProcessing' ` -ErrorRecord $_ -ThrowError -ErrorOperation 'TokenDecoding' -ErrorMessage 'Failed to decode JWT token' } } |