Skip to content
goloopPublic

About

Compact JSON Web Tokens for Go, deliberately limited to HS256 - standard library only.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Go Reference License Stay with Ukraine

jwt

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.

Install

go get github.com/goloop/jwt

Sign

token, 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.

Verify

claims, err := jwt.Verify(token, key,
	jwt.WithIssuer("api"),
	jwt.WithAudience("web"),
	jwt.WithLeeway(30*time.Second),
)

Verify:

  • requires alg=HS256 (rejects none, RS256, everything else);
  • requires a present exp;
  • verifies the signature in constant time before interpreting the payload;
  • checks exp/nbf/iat with optional leeway, and issuer/audience when set.

Startup validation

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.

Key rotation

claims, err := jwt.Verify(token, newKey, jwt.WithKey(oldKey))

Tokens are signed with the primary key; verification accepts any configured key.

Not supported (by design)

RS/ES/PS algorithms, none, JWE encryption, JWKS, and parsing without signature verification.

Documentation

License

MIT - see LICENSE.

Release v1.1.1

Decode null audiences correctly and avoid copying signed token input.

About

Compact JSON Web Tokens for Go, deliberately limited to HS256 - standard library only.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages