jwt issues and verifies compact JSON Web Tokens, deliberately limited to
HS256. It follows the JWS compact serialization (RFC 7515) and the
registered claims (RFC 7519), but supports exactly one algorithm with strict
defaults - a smaller surface is a safer surface. Zero dependencies, standard
library only.
go get github.com/goloop/jwttoken, err := jwt.Sign(jwt.Claims{
Subject: "user-123",
Issuer: "api",
Audience: jwt.Audience{"web"},
ExpiresAt: time.Now().Add(time.Hour).Unix(),
IssuedAt: time.Now().Unix(),
Extra: map[string]any{"role": "admin"},
}, key)The header is always {"alg":"HS256","typ":"JWT"}. Custom claims go in
Claims.Extra.
claims, err := jwt.Verify(token, key,
jwt.WithIssuer("api"),
jwt.WithAudience("web"),
jwt.WithLeeway(30*time.Second),
)Verify:
- requires
alg=HS256(rejectsnone,RS256, everything else); - requires a present
exp; - verifies the signature in constant time before interpreting the payload;
- checks
exp/nbf/iatwith optional leeway, and issuer/audience when set.
if err := jwt.CheckKey(key); err != nil {
log.Fatal(err) // fail at process start, not at the first token
}CheckKey is the same check Sign and Verify run on every call: ErrNoKey
for an empty key, ErrWeakKey under 32 bytes.
claims, err := jwt.Verify(token, newKey, jwt.WithKey(oldKey))Tokens are signed with the primary key; verification accepts any configured key.
RS/ES/PS algorithms, none, JWE encryption, JWKS, and parsing without
signature verification.
MIT - see LICENSE.
Decode null audiences correctly and avoid copying signed token input.