JWT Debugger — Swift source
Decode a JSON Web Token and inspect its header and payload as pretty JSON, with exp/iat claim awareness and an expiry badge. Runs entirely in your browser.
This is the Swift implementation — the same logic the interactive tool runs, in a shareable, citable form.
// JWT base64url decoding helper — Swift port of the jwt tool.
import Foundation
enum B64urlError: Error { case invalidInput }
enum Jwt {
// Decode a base64url segment (JWT header/payload) to text. Data's base64
// decoder is strict: after the URL-safe mapping it returns nil for input
// outside the standard alphabet, mapped here to the TS throw analogue.
static func b64urlDecode(_ str: String) throws -> String {
// 1. URL-safe alphabet -> standard base64 alphabet.
var s = ""
for c in str.unicodeScalars {
switch c {
case "-": s.unicodeScalars.append("+")
case "_": s.unicodeScalars.append("/")
default: s.unicodeScalars.append(c)
}
}
// 2. Restore stripped '=' padding so the length is a multiple of 4.
if s.unicodeScalars.count % 4 == 1 { throw B64urlError.invalidInput }
s += String(repeating: "=", count: (4 - s.unicodeScalars.count % 4) % 4)
// 3. Strict standard base64 -> bytes.
guard let data = Data(base64Encoded: s) else { throw B64urlError.invalidInput }
// 4. Bytes -> text, byte-faithful: each byte becomes the Unicode
// scalar of the same value (Latin-1), mirroring JS atob so the
// full 0x00-0xFF byte range round-trips (UTF-8 would drop bytes).
return data.map { String(UnicodeScalar($0)) }.joined()
}
}
// eyJhbGciOiJIUzI1NiJ9 decodes to {"alg":"HS256"} (main.swift: top-level try)
print(try Jwt.b64urlDecode("eyJhbGciOiJIUzI1NiJ9"))
Also available in 13 other languages
Every CosmoDev tool ships its pure logic in TypeScript (web) and Go (CLI), with authored implementations in a dozen-plus languages — the same contract, ported. Compare all languages side by side →