jwt

package
v3.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2022 License: MIT Imports: 3 Imported by: 2

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:

func New

func New(opts ...Option) (JWT, error)

New create new JWT utils

type Option

type Option func(*jwtType) 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

Jump to

Keyboard shortcuts

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