In my day-to-day work, I often find myself needing to decode JWT tokens and encode/decode base64 strings for debugging purposes. I’ve noticed that many people tend to rely on online tools for direct decoding. In this post, I want to introduce a simple Go utility I use locally to streamline these operations. The way I use it is quite straightforward: I copy either a base64 or JWT token, and in my terminal, I execute the sq command.

Here’s the code that powers this utility:

package main

import (
   "encoding/base64"
   "encoding/json"
   "flag"
   "fmt"
   "os/exec"
   "strings"
)

func main() {
   // Execute 'pbpaste' to copy text from the clipboard
   showHelp := flag.Bool("h", false, "Show help message")
   flag.Parse()

   if *showHelp {
       printHelp()
       return
   }
  // this is the core of the idea, using "pbpaste" using exec package. This many not work in other operating system. There are package that can work across 
  // operating systems.
   cmd := exec.Command("pbpaste")
   out, err := cmd.Output()
   if err != nil {
       fmt.Println("Error reading clipboard content:", err)
       return
   }

   clipboardText := string(out)

   if isJWT(clipboardText) {
       decodeJWT(clipboardText)
   } else {
       // If not a JWT, encode the content as base64
       encoded := base64.StdEncoding.EncodeToString([]byte(clipboardText))
       fmt.Println("Decoded Content:")
       fmt.Println(clipboardText)
       fmt.Println("\nBase64 Encoded Content:")
       fmt.Println(encoded)
   }
}

// Check if a string is a JWT by checking if it contains two dots (.) which is typical of JWT structure.
func isJWT(s string) bool {
   return strings.Count(s, ".") == 2
}

// Decode and pretty-print a JWT token's payload
func decodeJWT(jwtToken string) {
   parts := strings.Split(jwtToken, ".")
   if len(parts) != 3 {
       fmt.Println("Invalid JWT format")
       return
   }

   payloadJSON, err := base64.RawURLEncoding.DecodeString(parts[1])
   if err != nil {
       fmt.Println("Error decoding payload, Not valid:", err)
       return
   }

   var payload map[string]interface{}
   // takes the bytes and converts to payload type map
   if err := json.Unmarshal(payloadJSON, &payload); err != nil {
       fmt.Println("Error unmarshaling payload:", err)
       return
   }
   // Using MarshallIndent to pretty print
   prettyJSON, err := json.MarshalIndent(payload, "", "  ")
   if err != nil {
       fmt.Println("Error pretty-printing JSON:", err)
       return
   }
   fmt.Println("Decoded JWT Payload:")
   fmt.Println(string(prettyJSON))
}
func printHelp() {
   fmt.Println("sq is a Clipboard Tool")
   fmt.Println("To use, copy text or jwt on to clipboard and run sq in command line, sq dected and decode jwt if its jwt otherwise decodes what ever content is picked up from clipboard")
   fmt.Println("\nUsage:")
   fmt.Println("sq")
   fmt.Println("\nOptions:")
   fmt.Println("-h\t\tShow this help message")
}

You can extend the functionality by adding flags for encoding to base64. The provided code primarily focuses on decoding base64 and displaying payload information for JWT tokens. One potential downside is that, on occasion, you may accidentally copy incorrect content instead of the desired string, but this can easily be resolved.

This utility is designed to simplify your JWT and base64 operations, making it a valuable tool in daily work.