jwt

package
v1.2.7 Latest Latest
Warning

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

Go to latest
Published: May 27, 2021 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const TokenHeaderKeyPrefix = "BEARER "

TokenHeaderKeyPrefix is a token prefix regarding RFCXXX.

Variables

View Source
var (
	// ErrWrongSignatureAlgorithm is for unsupported signature algorithm.
	ErrWrongSignatureAlgorithm = errors.New("Unsupported signature algorithm")
	// ErrEmptyToken is when token is empty.
	ErrEmptyToken = errors.New("Token is empty")
	// ErrTokenInvalid is when token is invalid.
	ErrTokenInvalid = errors.New("Token is invalid")
)

StrToTokenSignAlg maps string token service algorithm names to values.

View Source
var TimeFunc = time.Now

TimeFunc provides the current time when parsing token to validate "exp" claim (expiration time). You can override it to use another time value. This is useful for testing or if your server uses a time zone different from your tokens'.

Functions

func ExtractTokenFromBearerHeader

func ExtractTokenFromBearerHeader(token string) []byte

ExtractTokenFromBearerHeader extracts token from the Bearer token header value.

func LoadPrivateKeyFromPEM

func LoadPrivateKeyFromPEM(file string, alg TokenSignatureAlgorithm) (interface{}, error)

LoadPrivateKeyFromPEM loads private key from PEM file.

func LoadPublicKeyFromPEM

func LoadPublicKeyFromPEM(file string, alg TokenSignatureAlgorithm) (interface{}, error)

LoadPublicKeyFromPEM loads public key from PEM file.

func LoadPublicKeyFromString

func LoadPublicKeyFromString(s string, alg TokenSignatureAlgorithm) (interface{}, error)

LoadPublicKeyFromString loads public key from string.

func NewTokenWithClaims

func NewTokenWithClaims(method jwt.SigningMethod, kid string, claims jwt.Claims) *jwt.Token

NewTokenWithClaims generates new JWT token with claims and keyID.

Types

type Claims

type Claims struct {
	Payload map[string]interface{} `json:"payload,omitempty"`
	Scopes  string                 `json:"scopes,omitempty"`
	Type    string                 `json:"type,omitempty"`
	KeyID   string                 `json:"kid,omitempty"` // optional keyID
	jwt.StandardClaims
}

Claims is an extended claims structure.

type JWToken

type JWToken struct {
	JWT *jwt.Token
	New bool
}

JWToken represents JWT token.

func (*JWToken) Audience added in v1.1.11

func (t *JWToken) Audience() []string

Audience standard token claim

func (*JWToken) ExpiresAt added in v1.1.11

func (t *JWToken) ExpiresAt() time.Time

ExpiresAt standard token claim

func (*JWToken) ID added in v1.1.11

func (t *JWToken) ID() string

ID standard token claim

func (*JWToken) IssuedAt added in v1.1.11

func (t *JWToken) IssuedAt() time.Time

IssuedAt standard token claim

func (*JWToken) Issuer added in v1.1.11

func (t *JWToken) Issuer() string

Issuer standard token claim

func (*JWToken) NotBefore added in v1.1.11

func (t *JWToken) NotBefore() time.Time

NotBefore standard token claim

func (*JWToken) Payload

func (t *JWToken) Payload() map[string]interface{}

Payload returns token payload.

func (*JWToken) Scopes added in v1.2.2

func (t *JWToken) Scopes() string

Scopes standard token claim

func (*JWToken) Subject added in v1.1.11

func (t *JWToken) Subject() string

Subject standard token claim

func (*JWToken) Type

func (t *JWToken) Type() string

Type returns token type.

func (*JWToken) UserID

func (t *JWToken) UserID() string

UserID returns user ID.

func (*JWToken) Validate

func (t *JWToken) Validate() error

Validate validates token data. Returns nil if all data is valid.

type StandardTokenClaims added in v1.1.11

type StandardTokenClaims interface {
	Audience() []string
	ExpiresAt() time.Time
	ID() string
	IssuedAt() time.Time
	Issuer() string
	NotBefore() time.Time
	Subject() string
}

StandardTokenClaims structured version of Claims Section, as referenced at https://tools.ietf.org/html/rfc7519#section-4.1

type Token

type Token interface {
	StandardTokenClaims
	Validate() error
	UserID() string
	Type() string
	Scopes() string
	Payload() map[string]interface{}
}

Token is an abstract application token.

func ParseTokenWithPublicKey

func ParseTokenWithPublicKey(t string, publicKey interface{}) (Token, error)

ParseTokenWithPublicKey parses token with provided public key.

type TokenSignatureAlgorithm

type TokenSignatureAlgorithm int

TokenSignatureAlgorithm is a signing algorithm used by the token service. For now, we only support ES256 and RS256.

const (
	// TokenSignatureAlgorithmES256 is a ES256 signature.
	TokenSignatureAlgorithmES256 TokenSignatureAlgorithm = iota + 1
	// TokenSignatureAlgorithmRS256 is a RS256 signature.
	TokenSignatureAlgorithmRS256
	// TokenSignatureAlgorithmAuto tries to detect algorithm on the fly.
	TokenSignatureAlgorithmAuto
)

func LoadPublicKeyFromPEMAuto added in v0.0.10

func LoadPublicKeyFromPEMAuto(file string) (interface{}, TokenSignatureAlgorithm, error)

LoadPublicKeyFromPEMAuto loads keys from pem file with key algorithm auto detection

func LoadPublicKeyFromStringAuto added in v0.0.10

func LoadPublicKeyFromStringAuto(s string) (interface{}, TokenSignatureAlgorithm, error)

LoadPublicKeyFromStringAuto loads keys from string with key algorithm auto detection

func (TokenSignatureAlgorithm) MarshalJSON

func (alg TokenSignatureAlgorithm) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaller.

func (TokenSignatureAlgorithm) MarshalYAML

func (alg TokenSignatureAlgorithm) MarshalYAML() (interface{}, error)

MarshalYAML implements yaml.Marshaller.

func (TokenSignatureAlgorithm) String

func (alg TokenSignatureAlgorithm) String() string

String implements Stringer.

func (*TokenSignatureAlgorithm) UnmarshalJSON

func (alg *TokenSignatureAlgorithm) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaller.

func (*TokenSignatureAlgorithm) UnmarshalYAML

func (alg *TokenSignatureAlgorithm) UnmarshalYAML(unmarshal func(interface{}) error) error

UnmarshalYAML implements yaml.Unmarshaller.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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