jwt

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2024 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrTokenExpired     = errors.New("token is expired")
	ErrTokenNotValidYet = errors.New("token not active yet")
	ErrTokenMalformed   = errors.New("that's not even a token")
	ErrTokenInvalid     = errors.New("couldn't handle this token")
)
View Source
var (
	Concurrency_Control = &singleflight.Group{}
)

Functions

func GetClaimsInt64Ptr

func GetClaimsInt64Ptr(claims jwt.MapClaims, key string) *int64

func GetClaimsIssuerAndExpiresAt

func GetClaimsIssuerAndExpiresAt(claims jwt.Claims) (issuer *string, expiresAt *jwt.NumericDate)

func GetClaimsIssuerAndExpiresAt2

func GetClaimsIssuerAndExpiresAt2(claims jwt.MapClaims) (issuer *string, expiresAt *jwt.NumericDate)

func GetClaimsString

func GetClaimsString(claims jwt.MapClaims, key string) string

func GetClaimsStringPtr

func GetClaimsStringPtr(claims jwt.MapClaims, key string) *string

func ParseJwtToken

func ParseJwtToken(jwtToken *string, secret string) (jwt.Claims, error)

Types

type BaseClaims

type BaseClaims struct {
	UserId      string `json:"user_id"`                  //
	Username    string `json:"username"`                 //
	Roles       string `json:"roles,omitempty"`          //
	Groups      string `json:"groups,omitempty"`         //
	TenantId    string `json:"tenant_id,omitempty"`      // our's
	From        uint8  `json:"from,omitempty"`           // login from
	UserIdInIdp string `json:"user_id_in_idp,omitempty"` // authing use sub as user id,
	Idp         string `json:"idp,omitempty"`            //
	Unionid     string `json:"unionid,omitempty"`        // only used for parsing third jwt string
}

if put user info to redis/cache, then we can use less props, more props can get from cache/db if didn't use cache, then we can add more props here more props means the jwt string will become large

type CustomClaims

type CustomClaims struct {
	jwt.RegisteredClaims
	BaseClaims
	BufferTime int64 `json:"buffer_time,omitempty"` //
}

Custom claims structure

func ParseJwtTokenWithClaims

func ParseJwtTokenWithClaims(jwtToken *string, secret string) (*CustomClaims, error)

self jwt token, no need to check activated

type JWTConfig

type JWTConfig struct {
	SigningKey  string `yaml:"signingKey" json:"signing-key"`   // jwt签名
	ExpiresTime string `yaml:"expiresTime" json:"expires-time"` // 过期时间
	BufferTime  string `yaml:"bufferTime" json:"buffer-time"`   // 缓冲时间
	Issuer      string `yaml:"issuer" json:"issuer"`            // 签发者, domain format
}

func (*JWTConfig) GetBufferTime

func (j *JWTConfig) GetBufferTime() time.Duration

func (*JWTConfig) GetExpiresTime

func (j *JWTConfig) GetExpiresTime() time.Duration

type JwtUtil

type JwtUtil interface {
	CreateClaims(baseClaims *BaseClaims) *CustomClaims
	CreateToken(claims *CustomClaims) (string, error)    // check the claims first and create token
	CreateTokenRaw(claims *CustomClaims) (string, error) // create token with provide claims, did not check the claims
	CreateTokenByOldToken(oldToken string, claims *CustomClaims) (string, error)
	ParseToken(jwtToken string) (*CustomClaims, error)
	// contains filtered or unexported methods
}

func New

func New(config *JWTConfig) JwtUtil

Jump to

Keyboard shortcuts

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