Documentation ¶
Index ¶
- Constants
- type Auth
- func (a *Auth) FromCtx(ctx *gear.Context) (josejwt.Claims, error)
- func (a *Auth) JWT() *jwt.JWT
- func (a *Auth) New(ctx *gear.Context) (val interface{}, err error)
- func (a *Auth) Serve(ctx *gear.Context) error
- func (a *Auth) SetJWT(j *jwt.JWT)
- func (a *Auth) SetSkipper(fn func(*gear.Context) bool) *Auth
- func (a *Auth) SetTokenParser(ex TokenExtractor)
- type TokenExtractor
Constants ¶
const Version = "1.6.0"
Version ...
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Auth ¶ added in v1.3.0
type Auth struct {
// contains filtered or unexported fields
}
Auth is helper type. It combine JWT and Crypto object, and some useful mothod for JWT. You can use it as a gear middleware.
func New ¶ added in v1.3.0
func New(keys ...interface{}) *Auth
New returns a Auth instance.
auther := auth.New([]byte("my key"))
func (*Auth) FromCtx ¶ added in v1.3.0
FromCtx will parse and validate token from the ctx, and return it as jwt.Claims. If token not exists or validate failure, a error and a empty jwt.Claims instance returned.
claims, err := auther.FromCtx(ctx) fmt.Println(claims, err)
func (*Auth) New ¶ added in v1.3.0
New implements gear.Any interface, then we can use it with ctx.Any:
any, err := ctx.Any(auther) if err != nil { return err } claims := any.(jwt.Claims)
that is auth.FromCtx doing for us.
func (*Auth) Serve ¶ added in v1.3.0
Serve implements gear.Handler interface. We can use it as middleware. It will parse and validate token from the ctx, if succeed, gear's middleware process will go on, otherwise process ended and a 401 error will be to respond to client.
app := gear.New() auther := auth.New() app.UseHandler(auther) // or app.Use(auther.Serve)
func (*Auth) SetSkipper ¶ added in v1.5.1
SetSkipper set a skip function to auth. If skip function return true, the auth middleware process will be skipped.
func (*Auth) SetTokenParser ¶ added in v1.3.0
func (a *Auth) SetTokenParser(ex TokenExtractor)
SetTokenParser set a custom tokenExtractor to auth.
type TokenExtractor ¶
TokenExtractor is a function that takes a gear.Context as input and returns either a string token or an empty string. Default to:
func(ctx *gear.Context) (token string) { if val := ctx.Get("Authorization"); strings.HasPrefix(val, "BEARER ") { token = val[7:] } else { token = ctx.Param("access_token") } return }