jwt

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 29, 2024 License: MIT Imports: 9 Imported by: 1

Documentation

Index

Constants

View Source
const (
	TokenID     = "jti" // TokenID is a unique identifier for this token.
	Issuer      = "iss" // Issuer is the principal that issued the token
	Audience    = "aud" // Audience identifies the recipents the token is intended for
	Subject     = "sub" // Subject is the user/account /etc that this token authorizes access to
	IssuedAt    = "iat" // IssuedAt is a Unix timestamp for when the token was issued
	ExpiresAt   = "exp" // ExpiresAt is a Unix timestamp for when the token should expire
	NotBeforeAt = "nbf" // NotBeforeAt is a timestamp this token should not be accepted until

	Alg   = "alg" // Alg is a header field identifying the signing algorithm
	Typ   = "typ" // Typ is a header field that must be set to "JWT"
	KeyID = "kid" // KeyID is a header field, an opaque string identifying the key used

	Forever time.Duration = 1<<63 - 1 // Forever is validity duration of tokens that do not expire

	HS256 Algorithm = "HS256"

	MinHS256KeyLen = 32
	MaxHS256KeyLen = 64 // anything longer is hashed to 32 bytes
)

Variables

View Source
var (
	ErrCorrupted          = errors.New("token corrupted")
	ErrAlg                = errors.New("token uses a wrong algorithm")
	ErrExpired            = errors.New("token expired")
	ErrNotYetValid        = errors.New("token not valid yet")
	ErrTooLong            = errors.New("token too long")
	ErrSignature          = errors.New("token signature invalid")
	ErrSignatureCorrupted = errors.New("token signature corrupted")

	MaxTokenLen        = 8000 // MaxTokenLen is the safety limit to avoid decoding very long data
	ExpectedClaimCount = 10   // ExpectedClaimCount is a starting size for the claims map
)

Functions

func SignHS256

func SignHS256(claims, headerClaims Claims, key []byte, buf []byte) []byte

SignHS256 produces a signed JWT token from the given claims.

func SignHS256Raw

func SignHS256Raw(claims, header []byte, key []byte, buf []byte) []byte

SignHS256Raw produces a signed JWT token from the given raw claims.

func SignHS256String

func SignHS256String(claims, headerClaims Claims, key []byte) string

Types

type Algorithm

type Algorithm string

type Claims

type Claims map[string]any

func DecodeHS256String

func DecodeHS256String(rawToken string, tolerance time.Duration, key []byte) (Claims, error)

func DecodeHS256StringAt

func DecodeHS256StringAt(rawToken string, key []byte, tolerance time.Duration, now time.Time) (Claims, error)

func New

func New(subject string, validity time.Duration) Claims

func NewAt

func NewAt(subject string, validity time.Duration, now time.Time) Claims

func (Claims) ExpiresAt

func (c Claims) ExpiresAt() time.Time

func (Claims) Int64

func (c Claims) Int64(key string) (int64, bool)

func (Claims) Issuer

func (c Claims) Issuer() string

func (Claims) String

func (c Claims) String(key string) string

func (Claims) Subject

func (c Claims) Subject() string

func (Claims) Time

func (c Claims) Time(key string) time.Time

func (Claims) TokenID

func (c Claims) TokenID() string

func (Claims) ValidateTime

func (c Claims) ValidateTime(tolerance time.Duration) error

func (Claims) ValidateTimeAt

func (c Claims) ValidateTimeAt(tolerance time.Duration, now time.Time) error

type Token

type Token struct {
	// contains filtered or unexported fields
}

Token is a result of parsing a JWT token.

func Parse

func Parse(rawToken []byte) (Token, error)

Parse decodes JWT parts of a token.

func ParseString

func ParseString(rawToken string) (Token, error)

ParseString decodes JWT parts of a token.

func (*Token) Alg

func (t *Token) Alg() Algorithm

func (*Token) Claims

func (t *Token) Claims() Claims

func (*Token) KeyID

func (t *Token) KeyID() string

func (*Token) Parse

func (token *Token) Parse(rawToken []byte) error

Parse decodes JWT parts of a token.

func (*Token) ParseString

func (token *Token) ParseString(rawToken string) error

ParseString decodes JWT parts of a token.

func (*Token) Validate

func (token *Token) Validate(alg Algorithm, key []byte) error

func (*Token) ValidateHS256

func (token *Token) ValidateHS256(key []byte) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL