jwt

package
v4.5.0 Latest Latest
Warning

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

Go to latest
Published: Aug 24, 2023 License: MIT Imports: 3 Imported by: 1

Documentation

Overview

Package jwt all in one JWT sdk

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// SignMethodHS256 use HS256 for jwt
	SignMethodHS256 = jwt.SigningMethodHS256
	// SignMethodES256 use ES256 for jwt
	SignMethodES256 = jwt.SigningMethodES256
	// SignMethodRS256 use RSA-256 for jwt
	SignMethodRS256 = jwt.SigningMethodRS256
)

Functions

func ParseTokenWithoutValidate

func ParseTokenWithoutValidate(token string, payload jwt.Claims) (err error)

ParseTokenWithoutValidate parse and get payload without validate jwt token

Types

type DivideOption

type DivideOption func(*divideOpt) error

DivideOption options to use separate secret for every user in parsing/signing

func WithDividePriKey

func WithDividePriKey(priKey []byte) DivideOption

WithDividePriKey set asymmetrical private key for each signning/verify

func WithDividePubKey

func WithDividePubKey(pubKey []byte) DivideOption

WithDividePubKey set asymmetrical public key for each signning/verify

func WithDivideSecret

func WithDivideSecret(secret []byte) DivideOption

WithDivideSecret set symmetric key for each signning/verify

type JWT

type JWT interface {
	Sign(claims jwt.Claims, opts ...DivideOption) (string, error)
	SignByHS256(claims jwt.Claims, opts ...DivideOption) (string, error)
	SignByES256(claims jwt.Claims, opts ...DivideOption) (string, error)
	ParseClaims(token string, claimsPtr jwt.Claims, opts ...DivideOption) error
	ParseClaimsByHS256(token string, claimsPtr jwt.Claims, opts ...DivideOption) error
	ParseClaimsByES256(token string, claimsPtr jwt.Claims, opts ...DivideOption) error
	ParseClaimsByRS256(token string, claimsPtr jwt.Claims, opts ...DivideOption) error
}

JWT jwt tool to sign & parse(with/without verify) token

Example
secret = []byte("4738947328rh3ru23f32hf238f238fh28f")
j, err := New(
	WithSignMethod(SignMethodHS256),
	WithSecretByte(secret),
)
if err != nil {
	log.Shared.Panic("new jwt", zap.Error(err))
}

type jwtClaims struct {
	jwt.RegisteredClaims
}

claims := &jwtClaims{
	jwt.RegisteredClaims{
		Subject: "laisky",
	},
}

// signing
token, err := j.Sign(claims)
if err != nil {
	log.Shared.Panic("sign jwt", zap.Error(err))
}

// verify
claims = &jwtClaims{}
if err := j.ParseClaims(token, claims); err != nil {
	log.Shared.Panic("sign jwt", zap.Error(err))
}
Output:

type Option

type Option func(*Type) error

Option options to setup JWT

func WithPriKeyByte

func WithPriKeyByte(prikey []byte) Option

WithPriKeyByte set jwt asymmetrical private key

func WithPubKeyByte

func WithPubKeyByte(pubkey []byte) Option

WithPubKeyByte set jwt asymmetrical public key

func WithSecretByte

func WithSecretByte(secret []byte) Option

WithSecretByte set jwt symmetric signning key

func WithSignMethod

func WithSignMethod(method jwt.SigningMethod) Option

WithSignMethod set jwt signing method

type Type

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

Type is token utils that support HS256/ES256

func New

func New(opts ...Option) (*Type, error)

New create new JWT utils

func (*Type) ParseClaims

func (e *Type) ParseClaims(token string, claimsPtr jwt.Claims, opts ...DivideOption) error

ParseClaims parse token to claims

func (*Type) ParseClaimsByES256

func (e *Type) ParseClaimsByES256(token string, claimsPtr jwt.Claims, opts ...DivideOption) error

ParseClaimsByES256 parse token to claims by ES256

func (*Type) ParseClaimsByHS256

func (e *Type) ParseClaimsByHS256(token string, claimsPtr jwt.Claims, opts ...DivideOption) error

ParseClaimsByHS256 parse token to claims by HS256

func (*Type) ParseClaimsByRS256

func (e *Type) ParseClaimsByRS256(token string, claimsPtr jwt.Claims, opts ...DivideOption) error

ParseClaimsByRS256 parse token to claims by rs256

func (*Type) Sign

func (e *Type) Sign(claims jwt.Claims, opts ...DivideOption) (string, error)

Sign sign claims to token

func (*Type) SignByES256

func (e *Type) SignByES256(claims jwt.Claims, opts ...DivideOption) (string, error)

SignByES256 signing claims by ES256

func (*Type) SignByHS256

func (e *Type) SignByHS256(claims jwt.Claims, opts ...DivideOption) (string, error)

SignByHS256 signing claims by HS256

Jump to

Keyboard shortcuts

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