Processing Ajax...

Title
Close Dialog

Message

Confirm
Close Dialog

Confirm
Close Dialog

Confirm
Close Dialog

JWT Base64 Decode

Description
This function converts JWT token's payload into JSON. I find it a lot more convenient than visiting jwt.io or running an echo + base64 -d in WSL.
Language
C#.net
Minimum Version
Created By
István Hegedűs59983
Contributors
-
Date Created
Mar 9, 2023
Date Last Modified
Mar 9, 2023

Macro Code

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
public static class ClipboardFusionHelper
{
	private static Regex jwtTokenPattern = new Regex("^ey[A-Z0-9a-z\\+/]+?\\.(ey[A-Z0-9a-z\\+/]+?)\\.", RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.Singleline);
	public static string ProcessText(string text)
	{
		if (!text.StartsWith("ey") || text.IndexOf(".ey") <= 0)
			return text;

		var match = jwtTokenPattern.Match(text);
		if (!match.Success)
			return text;
		try
		{
			var token = match.Groups[1].Value;
			switch (token.Length % 4)
			{
				case 1:
					token += "===";
					break;
				case 2:
					token += "==";
					break;
				case 3:
					token += "=";
					break;
			}

			return System.Text.Encoding.UTF8.GetString(System.Convert.FromBase64String(token));
		}
		catch
		{
			return text;
		}
	}
}