Documentation ¶
Index ¶
- Constants
- Variables
- func FromContext(ctx context.Context) (token jwt.Claims, ok bool)
- func GenerateSignature(message string, secret string) string
- func GenerateToken(key string, opts ...Option) (string, error)
- func GetCurrentUser(ctx context.Context) (string, error)
- func GetSigningMethod(method string) jwt.SigningMethod
- func NewAuthContext(ctx context.Context, claims AuthClaims) context.Context
- func NewContext(ctx context.Context, info jwt.Claims) context.Context
- func NewJwt() (*jwtMiddleware, error)
- func ParseJwtToken(jwtToken string, opts ...Option) (jwt.Claims, error)
- func VerifySignature(ak, sk, timeStr, sign string) bool
- type AuthClaims
- type JWTMiddleware
- type JwtConfig
- type Option
- type TokenClaims
- type TokenPair
Constants ¶
View Source
const ( TokenTypeAccess = "access" TokenTypeRefresh = "refresh" )
Variables ¶
View Source
var ( ErrMissingJwtToken = errors.New("缺少JWT令牌") ErrTokenInvalid = errors.New("令牌无效") ErrTokenExpired = errors.New("JWT令牌已过期") ErrTokenParseFail = errors.New("解析JWT令牌失败") ErrUnSupportSigningMethod = errors.New("不支持的签名方法") ErrInvalidTokenType = errors.New("非访问令牌") ErrNilConfig = errors.New("配置为空") )
Functions ¶
func FromContext ¶ added in v0.0.11
FromContext extract auth info from context
func GenerateSignature ¶
GenerateSignature 生成签名
func GenerateToken ¶ added in v0.0.11
func GetCurrentUser ¶ added in v0.1.1
GetCurrentUser 获取当前请求的用户名
func GetSigningMethod ¶ added in v0.1.1
func GetSigningMethod(method string) jwt.SigningMethod
GetSigningMethod 根据签名方法名获取对应的SigningMethod
func NewAuthContext ¶ added in v0.1.1
func NewAuthContext(ctx context.Context, claims AuthClaims) context.Context
NewAuthContext 将认证信息添加到请求的上下文中
func NewContext ¶ added in v0.0.11
NewContext put auth info into context
func ParseJwtToken ¶ added in v0.0.11
ParseJwtToken 解析和验证令牌
Types ¶
type AuthClaims ¶ added in v0.1.1
type AuthClaims interface {
jwt.Claims
GetUsername() string
}
AuthClaims 定义JWT Claims接口
func ClaimsFromContext ¶ added in v0.1.1
func ClaimsFromContext(ctx context.Context) (AuthClaims, bool)
ClaimsFromContext 从请求的上下文中获取认证信息
type JWTMiddleware ¶
type JWTMiddleware interface { Middleware(next http.Handler) http.Handler GenerateTokenPair(username string) (*TokenPair, error) RefreshToken(refreshToken string) (*TokenPair, error) }
JWTMiddleware 定义JWT中间件接口
type JwtConfig ¶
type JwtConfig struct { configs.TokenConfig SigningMethod jwt.SigningMethod ErrHandler func(w http.ResponseWriter, r *http.Request, err error) }
JwtConfig 定义JWT配置结构体
type Option ¶ added in v0.0.11
type Option func(*options)
Option is jwt option.
func WithClaims ¶ added in v0.0.11
func WithClaims(f func() jwt.Claims) Option
func WithKeyFunc ¶ added in v0.0.11
func WithKeyFunc(f jwt.Keyfunc) Option
func WithSigningMethod ¶ added in v0.0.11
func WithSigningMethod(method jwt.SigningMethod) Option
type TokenClaims ¶
type TokenClaims struct { ID string `json:"id"` Username string `json:"username"` TokenType string `json:"token_type"` Data interface{} jwt.RegisteredClaims }
TokenClaims 实现AuthClaims接口
func (*TokenClaims) GetUsername ¶ added in v0.1.1
func (tc *TokenClaims) GetUsername() string
GetUsername 获取Claims中的用户名
Click to show internal directories.
Click to hide internal directories.