Documentation ¶
Overview ¶
Package jwt all in one JWT sdk
Index ¶
- Variables
- func ParseTokenWithoutValidate(token string, payload jwt.Claims) (err error)
- type DivideOption
- type JWT
- type Option
- type Type
- func (e *Type) ParseClaims(token string, claimsPtr jwt.Claims, opts ...DivideOption) error
- func (e *Type) ParseClaimsByES256(token string, claimsPtr jwt.Claims, opts ...DivideOption) error
- func (e *Type) ParseClaimsByHS256(token string, claimsPtr jwt.Claims, opts ...DivideOption) error
- func (e *Type) ParseClaimsByRS256(token string, claimsPtr jwt.Claims, opts ...DivideOption) error
- func (e *Type) Sign(claims jwt.Claims, opts ...DivideOption) (string, error)
- func (e *Type) SignByES256(claims jwt.Claims, opts ...DivideOption) (string, error)
- func (e *Type) SignByHS256(claims jwt.Claims, opts ...DivideOption) (string, error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
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 ¶
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 ¶
Option options to setup JWT
func WithPriKeyByte ¶
WithPriKeyByte set jwt asymmetrical private key
func WithPubKeyByte ¶
WithPubKeyByte set jwt asymmetrical public key
func WithSecretByte ¶
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 (*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