Skip to main content

How to Decode a JWT Without Verifying It in Go with ParseUnverified

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

golang-jwt's ParseUnverified reads a JSON Web Token's header and claims without the key. As its name says, it checks nothing, which makes it right for reading a token and wrong for trusting one.

Syntax​

Syntax
<claims> := jwt.MapClaims{}
<token>, <parts>, <err> := jwt.NewParser().ParseUnverified(<tokenString>, <claims>)
PartWhat it does
jwt.NewParser()Makes a parser with default settings.
ParseUnverified(<tokenString>, <claims>)Fills <claims> from the payload without checking the signature or any claim.
<token>The parsed token; <token>.Header holds the header, for example kid.
<parts>The token's three pieces as strings.
<err>An error when the token can't be read at all.

The golang-jwt source documents ParseUnverified: "ParseUnverified parses the token but does not validate the signature." And: "WARNING: Don't use this method unless you know what you're doing. It's only ever useful in cases where you know the signature is valid (since it has already been or will be checked elsewhere in the stack) and you want to extract values from it."

The Example​

The example reads a token that another service signed with a key this program doesn't have. The token expired on 1 December 2025, and its header names the key that signed it, key-2025-12, in a kid.

Prerequisites​

  • Go. This guide uses Go 1.27.4.

Step 1: Create the Module​

Terminal
mkdir demo
cd demo
go mod init example.com/demo
go get github.com/golang-jwt/jwt/v5

go mod init creates the module, and go get adds golang-jwt, version 5.3.1 here.

Step 2: Write the Code​

Create main.go in the demo folder:

main.go, which holds a token from another service that expired in December 2025, parses it with jwt.NewParser().ParseUnverified into MapClaims, and prints the kid header, the sub and role claims, and the expiry in UTC.

main.go
package main

import (
"fmt"

"github.com/golang-jwt/jwt/v5"
)

func main() {
// A token from another service, signed with a key this program doesn't have.
// It expired on 1 December 2025.
tokenString := "eyJhbGciOiJIUzI1NiIsImtpZCI6ImtleS0yMDI1LTEyIiwidHlwIjoiSldUIn0.eyJzdWIiOiJhZGEiLCJyb2xlIjoicmVhZGVyIiwiaWF0IjoxNzY0NTQ3MjAwLCJleHAiOjE3NjQ1NTA4MDB9.2EjUXD-_uc78GAdKpsWk7GOlMQztlsdWKQkT-RdKFKE"

claims := jwt.MapClaims{}
token, _, err := jwt.NewParser().ParseUnverified(tokenString, claims)
if err != nil {
fmt.Println("Could not read the token:", err)
return
}
fmt.Println(token.Header["kid"])
fmt.Println(claims["sub"], claims["role"])

exp, _ := claims.GetExpirationTime()
fmt.Println(exp.UTC())
}

Step 3: Run It​

Terminal
go run .
Output
key-2025-12
ada reader
2025-12-01 01:00:00 +0000 UTC

A zsh terminal where go run prints key-2025-12, then ada reader, then 2025-12-01 01:00:00 +0000 UTC.

ParseUnverified read the header and every claim without the key, and returned no error even though the token had expired months before.

Decoding Is Not Verifying​

Anyone can write a token with any claims, so these claims prove nothing. Use them to read, never to decide who someone is or what they may do. To trust a token, verify it with the key and the algorithm you expect:

main.go
token, err := jwt.ParseWithClaims(tokenString, claims, func(*jwt.Token) (any, error) {
return key, nil
}, jwt.WithValidMethods([]string{"HS256"}))

ParseWithClaims checks the signature and the expiry, and returns an error when either fails.

Reading the header first is the one common server-side use: a service with several keys reads kid and picks that key from its own list. The header is just as forgeable as the claims, so look it up only among keys you already trust.

Common Mistakes​

MistakeWhat happensDo this instead
Trusting claims from ParseUnverifiedAnyone can send a token with any claimsUse ParseWithClaims with the key
Expecting ParseUnverified to reject an expired tokenIt returned the claims of an expired token with no errorVerify; that checks exp
Passing the whole Authorization header valuetoken is malformed: could not base64 decode header: illegal base64 data at input byte 6Remove Bearer first

Troubleshooting​

SymptomLikely causeFix
token is malformed: token contains an invalid number of segmentsThe string isn't three pieces separated by dotsCheck what you passed
go: downloading github.com/golang-jwt/jwt v3.2.2+incompatible during go getGo also looks up the module path without /v5Nothing to fix; only v5 is added to go.mod

FAQ​

Why MapClaims and not my own struct?​

Either works. jwt.MapClaims takes any claims without a type, which suits reading an unknown token. With a struct that embeds jwt.RegisteredClaims, you get typed fields instead.

What does GetExpirationTime return?​

The exp claim as a *jwt.NumericDate, which holds a time.Time, plus an error if the claim has the wrong type.

Conclusion​

jwt.NewParser().ParseUnverified(tokenString, claims) reads a token's header and claims without a key and without any checks. Use it to look inside a token; use ParseWithClaims with the key before you trust it.

Additional Resources​