jwt

package
v0.0.0-...-1bf96ee Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2022 License: AGPL-3.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DEFAULT_TOKEN string = "zihaotoken"
)
View Source
const (
	//DefaultContextKey jwt
	DefaultContextKey = "iris-jwt"
)

Variables

This section is empty.

Functions

func InitJWT

func InitJWT()

Types

type Claims

type Claims struct {
	UserId   string `json:"userId"`
	RealName string `json:"realName"`
	Phone    string `json:"phone"`
	Enable   bool   `json:"enable"`
	TenantId string `json:"tenantId"`
	TokenId  string `json:"tokenId"`
	jwt.StandardClaims
}

type Config

type Config struct {
	// The function that will return the Key to validate the JWT.
	// It can be either a shared secret or a public key.
	// Default value: nil
	ValidationKeyGetter jwt.Keyfunc
	// The name of the property in the request where the user (&token) information
	// from the JWT will be stored.
	// Default value: "jwt"
	ContextKey string
	// The function that will be called when there's an error validating the token
	// Default value:
	ErrorHandler func(context.Context, constants.Code)
	// A boolean indicating if the credentials are required or not
	// Default value: false
	CredentialsOptional bool
	// A function that extracts the token from the request
	// Default: FromAuthHeader (i.e., from Authorization header as bearer token)
	Extractor TokenExtractor
	// Debug flag turns on debugging output
	// Default: false
	Debug bool
	// When set, all requests with the OPTIONS method will use authentication
	// if you enable this option you should register your route with iris.Options(...) also
	// Default: false
	EnableAuthOnOptions bool
	// When set, the middelware verifies that tokens are signed with the specific signing algorithm
	// If the signing method is not constant the ValidationKeyGetter callback can be used to implement additional checks
	// Important to avoid security issues described here: https://auth0.com/blog/2015/03/31/critical-vulnerabilities-in-json-web-token-libraries/
	// Default: nil
	SigningMethod jwt.SigningMethod
	// When set, the expiration time of token will be check every time
	// if the token was expired, expiration error will be returned
	// Default: false
	Expiration bool
}

Config is a struct for specifying configuration options for the jwt middleware.

type IJwt

type IJwt struct {
	Config Config
}

Middleware the middleware for JSON Web tokens authentication method

func (*IJwt) Check

func (j *IJwt) Check(ctx context.Context) (*jwt.Token, error)

CheckJWT the main functionality, checks for token

func (*IJwt) CheckTokenString

func (j *IJwt) CheckTokenString(token string) (*jwt.Token, constants.Code, error)

func (*IJwt) GetToken

func (j *IJwt) GetToken(ctx context.Context) *jwt.Token

Get returns the user (&token) information for this client/request

func (*IJwt) InitIJwtConfig

func (j *IJwt) InitIJwtConfig() *IJwt

---------------------------------------------------------

type JWT

type JWT struct {
	*IJwt
}
var G_JWT *JWT

func (*JWT) GenerateToken

func (j *JWT) GenerateToken(user *user.UserDto) (string, error)

在登录成功的时候生成token

func (*JWT) ServeHTTP

func (j *JWT) ServeHTTP(ctx *context.Context) (err error)

Serve the middleware's action

func (*JWT) ServeWebsocket

func (j *JWT) ServeWebsocket(ctx context.Context)

Serve the middleware's action

func (*JWT) Token2Model

func (j *JWT) Token2Model(token *jwt.Token) (*user.UserDto, error)

解析token的信息为用户

func (*JWT) TokenString2Model

func (j *JWT) TokenString2Model(tokenString string) (user *user.UserDto, err error)

type TokenExtractor

type TokenExtractor func(context.Context) (string, error)

TokenExtractor is a function that takes a context as input and returns either a token or an error. An error should only be returned if an attempt to specify a token was found, but the information was somehow incorrectly formed. In the case where a token is simply not present, this should not be treated as an error. An empty string should be returned in that case.

Jump to

Keyboard shortcuts

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