# ConvertFrom-Bearertoken # # Sample to convert Bearer/POP Tokens by decoding them [CmdletBinding()] param ( [Parameter(ValueFromPipeline = $true)] [string]$token="eyJhbGciOiJSUzI1NiIsImtpZCI6Inp4ZndEckpfZHQ3Ymg4a0xWek9WcE1aazFxeDBYay1VYTVZc2FNVzJGUjAiLCJ0eXAiOiJKV1QifQ.eyJhdCI6ImV5SjBlWEFpT2lKS1YxUWlMQ0p1YjI1alpTSTZJbWh4VDBSSVdsZHBiMVkxWDFWaFZVMVZTWHBDWmtoTFJVaG1VakEyZHpOalZsaHlUa05DTUROVmIyY2lMQ0poYkdjaU9pSlNVekkxTmlJc0luZzFkQ0k2SWt0Uk1uUkJZM0pGTjJ4Q1lWWldSMEp0WXpWR2IySm5aRXB2TkNJc0ltdHBaQ0k2SWt0Uk1uUkJZM0pGTjJ4Q1lWWldSMEp0WXpWR2IySm5aRXB2TkNKOS5leUpoZFdRaU9pSm9kSFJ3Y3pvdkwyOTFkR3h2YjJzdWIyWm1hV05sTG1OdmJTSXNJbWx6Y3lJNkltaDBkSEJ6T2k4dmMzUnpMbmRwYm1SdmQzTXVibVYwTDJZNFkyUmxaak14TFdFek1XVXROR0kwWVMwNU0yVTBMVFZtTlRjeFpUa3hNalUxWVM4aUxDSnBZWFFpT2pFM01qUTRPREExTVRZc0ltNWlaaUk2TVRjeU5EZzRNRFV4Tml3aVpYaHdJam94TnpJME9UWTNNakUyTENKaGFXOGlPaUpGTW1SbldWQnFjbUZYUVcxWlMxWXhVWFI2VjB0V1MyZFNSVWxLUVVFOVBTSXNJbUZ3Y0Y5a2FYTndiR0Y1Ym1GdFpTSTZJazFwWTNKdmMyOW1kQ0JVWldGdGN5QlRaWEoyYVdObGN5SXNJbUZ3Y0dsa0lqb2lZMk14Tldaa05UY3RNbU0yWXkwME1URTNMV0U0T0dNdE9ETmlNV1ExTm1JMFltSmxJaXdpWVhCd2FXUmhZM0lpT2lJeUlpd2lZMjVtSWpwN0ltcDNheUk2ZXlKcmRIa2l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c2swZ0pKUi1tdnRYZlFfdnk3ZVVfMmFVSUZ2S3Z1R0RMOFR3ZDFDb0pfT0lhcXNPVW53djFsRXNtVE16Q0lXTkJLZHBvLU1NV0N6TFZLY0pPQUM1aEt4b2R5ZlJkbjRfQ2lZQ3FKdWJFLWo5UXdDWTFWRkVEQTR3VDlWTzZ5anZvZnA4cnRYbDh0eWdvVHVsOXVQdnMyV0Q3V2tWdkZKNVZUNk1nRTJBLWhSVG4yNzBjaUdGSEpFcUg3TEZVbUNLTFkycGdEOTN3LWVPdXRMOG9JZTNPUW9uekRXMjNoRDVMS0EifQ.m-21MVnZRHOA2fNpPB5bY64SFldUnJOecO4BhlIZbh0B299gNxhM9fcM76qlcLqdhghm2mfECf7AaPHZkzDjPlf1dIW9-tCR4Uai1nCD5CtGTg2QmLbkFrjoFDU4tsBU1hP0r3tUMdCXK4G8lqvWJjvjsNew7nbDF4bFCZYCLZg0tovt886IlIxAXei2hDXnqZpgMyJbMdgD0MxLpV3e1Zo3mYn9pKpVATU09dJU6-xdZgx8ljtTdmZ1aMN72Si1tfVZ6kA5NXZSVFBmJAJvhG7IeyidqRpBZNhG3GV9hImh3g6WE3PlnuuVnkUnVQOK_9gd8kjwUO4i_sM7igMKtQ" ) begin{ Write-Verbose "ConvertFrom-Bearertoken: Start" } process{ Write-Verbose " Totals Token Length: $($token.length) Bytes" $tokens = $token.split(".") Write-Verbose "Found $($tokens.count) Tokens" $count=0 foreach ($currenttoken in $tokens) { $count++ Write-Verbose ("Decoding Token $count with Length $($currenttoken.length) Bytes") if (!$currenttoken.startswith("eyJ")) { Write-Verbose " Token does not start with ejJ and is not a Bearer Token" Continue } # replace "-" with "+", "_" with "/" and "%3d" with "=" $currenttoken = $currenttoken.replace("-","+").replace("_","/").replace("%3d","=") # Append = until length is a multiple of 4 if (($currenttoken.length % 4) -ne 0) { Write-Verbose " Token length is not a multiple of 4 - Adding padding" $currenttoken = $currenttoken + "".PadRight(4-($currenttoken.length % 4),"=") } # Decode the Base64 string tp byte array $bytes = [Convert]::FromBase64String($currenttoken) # Convert the byte array to a string $decodedString = [System.Text.Encoding]::ASCII.GetString($bytes) $decodedString | ConvertFrom-Json } Write-Verbose "Decode-Bearertoken: End" }