jwt

package
v0.14.0 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2024 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const (
	JwtHeaderType      = "typ"
	JwtHeaderAlgorithm = "alg"
	JwtHeaderKid       = "kid"
)
View Source
const CryptoKeysPropertiesPrefix = "security"

Variables

This section is empty.

Functions

func ParseJwtHeaders

func ParseJwtHeaders(jwtValue string) (map[string]interface{}, error)

ParseJwtHeaders extract JWT's headers without verifying the token

Types

type CryptoKeyProperties

type CryptoKeyProperties struct {
	Id        string `json:"id"`
	KeyFormat string `json:"format"`
	Location  string `json:"file"`
	Password  string `json:"password"`
}

func (CryptoKeyProperties) Format

type CryptoProperties

type CryptoProperties struct {
	Keys map[string]CryptoKeyProperties `json:"keys"`
	Jwt  JwtProperties                  `json:"jwt"`
}

func BindCryptoProperties

func BindCryptoProperties(ctx *bootstrap.ApplicationContext) CryptoProperties

BindCryptoProperties create and bind CryptoProperties, with a optional prefix

func NewCryptoProperties

func NewCryptoProperties() *CryptoProperties

CryptoProperties create a SessionProperties with default values

type FileJwkStore

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

FileJwkStore implements JwkStore and JwkRotator This store uses load key files for public and private keys. File locations and "kids" are read from properties. And rotate between pre-defined keys

func NewFileJwkStore

func NewFileJwkStore(props CryptoProperties) *FileJwkStore

func (*FileJwkStore) LoadAll

func (s *FileJwkStore) LoadAll(_ context.Context, names ...string) ([]Jwk, error)

func (*FileJwkStore) LoadByKid

func (s *FileJwkStore) LoadByKid(_ context.Context, kid string) (Jwk, error)

func (*FileJwkStore) LoadByName

func (s *FileJwkStore) LoadByName(_ context.Context, name string) (Jwk, error)

func (*FileJwkStore) Rotate

func (s *FileJwkStore) Rotate(_ context.Context, name string) error

type Jwk

type Jwk interface {
	Id() string
	Name() string
	Public() crypto.PublicKey
}

********************

	Abstraction
 ********************

type JwkRotator

type JwkRotator interface {
	JwkStore
	// Rotate change JWK of given name to next candicate
	Rotate(ctx context.Context, name string) error
}

type JwkStore

type JwkStore interface {
	// LoadByKid returns the JWK associated with given KID.
	// This method is usually used when decoding/verifiying JWT token
	LoadByKid(ctx context.Context, kid string) (Jwk, error)
	// LoadByKid returns the JWK associated with given name.
	// The method might return different JWK for same name, if the store is also support rotation
	// This method is usually used when encoding/encrypt JWT token
	LoadByName(ctx context.Context, name string) (Jwk, error)
	// LoadAll return all JWK with given names. If name is not provided, all JWK is returned
	LoadAll(ctx context.Context, names ...string) ([]Jwk, error)
}

type JwtDecoder

type JwtDecoder interface {
	Decode(ctx context.Context, token string) (oauth2.Claims, error)
	DecodeWithClaims(ctx context.Context, token string, claims interface{}) error
}

type JwtEncoder

type JwtEncoder interface {
	Encode(ctx context.Context, claims interface{}) (string, error)
}

type JwtProperties

type JwtProperties struct {
	KeyName string `json:"key-name"`
}

type KeyFormatType

type KeyFormatType string
const (
	KeyFileFormatPem KeyFormatType = "pem"
)

type PlaintextJwtDecoder

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

PlaintextJwtDecoder implements JwtEncoder

func NewPlaintextJwtDecoder

func NewPlaintextJwtDecoder() *PlaintextJwtDecoder

func (*PlaintextJwtDecoder) Decode

func (dec *PlaintextJwtDecoder) Decode(ctx context.Context, tokenString string) (oauth2.Claims, error)

func (*PlaintextJwtDecoder) DecodeWithClaims

func (dec *PlaintextJwtDecoder) DecodeWithClaims(_ context.Context, tokenString string, claims interface{}) (err error)

type PrivateJwk

type PrivateJwk interface {
	Jwk
	Private() crypto.PrivateKey
}

type RSJwtDecoder

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

RSJwtDecoder implements JwtEncoder

func NewRS256JwtDecoder

func NewRS256JwtDecoder(jwkStore JwkStore, defaultJwkName string) *RSJwtDecoder

func (*RSJwtDecoder) Decode

func (dec *RSJwtDecoder) Decode(ctx context.Context, tokenString string) (oauth2.Claims, error)

func (*RSJwtDecoder) DecodeWithClaims

func (dec *RSJwtDecoder) DecodeWithClaims(ctx context.Context, tokenString string, claims interface{}) (err error)

type RSJwtEncoder

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

RSJwtEncoder implements JwtEncoder

func NewRS256JwtEncoder

func NewRS256JwtEncoder(jwkStore JwkStore, jwkName string) *RSJwtEncoder

func (*RSJwtEncoder) Encode

func (enc *RSJwtEncoder) Encode(ctx context.Context, claims interface{}) (string, error)

type RsaKeyPair

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

********************

	Implements
 ********************

RsaKeyPair implements Jwk and PrivateJwk

func NewRsaPrivateJwk

func NewRsaPrivateJwk(kid string, name string, privateKey *rsa.PrivateKey) *RsaKeyPair

func (*RsaKeyPair) Id

func (k *RsaKeyPair) Id() string

func (*RsaKeyPair) Name

func (k *RsaKeyPair) Name() string

func (*RsaKeyPair) Private

func (k *RsaKeyPair) Private() crypto.PrivateKey

func (*RsaKeyPair) Public

func (k *RsaKeyPair) Public() crypto.PublicKey

type RsaPublicKey

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

RsaPublicKey implements Jwk

func NewRsaJwk

func NewRsaJwk(kid string, name string, publicKey *rsa.PublicKey) *RsaPublicKey

func (*RsaPublicKey) Id

func (k *RsaPublicKey) Id() string

func (*RsaPublicKey) Name

func (k *RsaPublicKey) Name() string

func (*RsaPublicKey) Public

func (k *RsaPublicKey) Public() crypto.PublicKey

type SingleJwkStore

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

SingleJwkStore implements JwkStore This store always returns single JWK if kid matches, return error if not This store is majorly for testing

func NewSingleJwkStore

func NewSingleJwkStore(kid string) *SingleJwkStore

func (*SingleJwkStore) LoadAll

func (s *SingleJwkStore) LoadAll(ctx context.Context, names ...string) ([]Jwk, error)

func (*SingleJwkStore) LoadByKid

func (s *SingleJwkStore) LoadByKid(_ context.Context, kid string) (Jwk, error)

func (*SingleJwkStore) LoadByName

func (s *SingleJwkStore) LoadByName(_ context.Context, name string) (Jwk, error)

type StaticJwkStore

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

StaticJwkStore implements JwkStore and JwkRotator This store uses "kid" as seed to generate PrivateJwk. For same "kid" the returned key is same this one is not thread safe

func NewStaticJwkStore

func NewStaticJwkStore(kids ...string) *StaticJwkStore

func (*StaticJwkStore) LoadAll

func (s *StaticJwkStore) LoadAll(ctx context.Context, names ...string) ([]Jwk, error)

func (*StaticJwkStore) LoadByKid

func (s *StaticJwkStore) LoadByKid(_ context.Context, kid string) (Jwk, error)

func (*StaticJwkStore) LoadByName

func (s *StaticJwkStore) LoadByName(_ context.Context, name string) (Jwk, error)

func (*StaticJwkStore) Rotate

func (s *StaticJwkStore) Rotate(ctx context.Context, name string) error

Jump to

Keyboard shortcuts

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