jwt

package
v0.0.0-...-0ae65ae Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2019 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrTokenUnauthorized   = errors.New("token unauthorized")
	ErrTokenExpired        = errors.New("token expired")
	ErrInvalidAccessToken  = errors.New("invalid access token")
	ErrInvalidRefreshToken = errors.New("invalid refresh token")
)

The list of jwt token errors presented to the end user.

Functions

func AuthenticateRefreshJWT

func AuthenticateRefreshJWT(next http.Handler) http.Handler

AuthenticateRefreshJWT checks validity of refresh tokens and is only used for access token refresh and logout requests. It responds with 401 Unauthorized for invalid or expired refresh tokens.

func Authenticator

func Authenticator(next http.Handler) http.Handler

Authenticator is a default authentication middleware to enforce access from the Verifier middleware request context values. The Authenticator sends a 401 Unauthorized response for any unverified tokens and passes the good ones through.

func ErrUnauthorized

func ErrUnauthorized(err error) render.Renderer

ErrUnauthorized renders status 401 Unauthorized with custom error message.

func RefreshTokenFromCtx

func RefreshTokenFromCtx(ctx context.Context) string

RefreshTokenFromCtx retrieves the parsed refresh token from context.

Types

type AppClaims

type AppClaims struct {
	ID    int
	Sub   string
	Roles []string
}

AppClaims represent the claims parsed from JWT access token.

func ClaimsFromCtx

func ClaimsFromCtx(ctx context.Context) AppClaims

ClaimsFromCtx retrieves the parsed AppClaims from request context.

func (*AppClaims) ParseClaims

func (c *AppClaims) ParseClaims(claims jwtauth.Claims) error

ParseClaims parses JWT claims into AppClaims.

type ErrResponse

type ErrResponse struct {
	Err            error `json:"-"` // low-level runtime error
	HTTPStatusCode int   `json:"-"` // http response status code

	StatusText string `json:"status"`          // user-level status message
	AppCode    int64  `json:"code,omitempty"`  // application-specific error code
	ErrorText  string `json:"error,omitempty"` // application-level error message, for debugging
}

ErrResponse renderer type for handling all sorts of errors.

func (*ErrResponse) Render

func (e *ErrResponse) Render(w http.ResponseWriter, r *http.Request) error

Render sets the application-specific error code in AppCode.

type RefreshClaims

type RefreshClaims struct {
	Token string
}

RefreshClaims represent the claims parsed from JWT refresh token.

func (*RefreshClaims) ParseClaims

func (c *RefreshClaims) ParseClaims(claims jwtauth.Claims) error

ParseClaims parses the JWT claims into RefreshClaims.

type Token

type Token struct {
	ID        int       `json:"id,omitempty"`
	CreatedAt time.Time `json:"created_at,omitempty"`
	UpdatedAt time.Time `json:"updated_at,omitempty"`
	AccountID int       `json:"-"`

	Token      string    `json:"-"`
	Expiry     time.Time `json:"-"`
	Mobile     bool      `sql:",notnull" json:"mobile"`
	Identifier string    `json:"identifier,omitempty"`
}

Token holds refresh jwt information.

func (*Token) BeforeInsert

func (t *Token) BeforeInsert(db orm.DB) error

BeforeInsert hook executed before database insert operation.

func (*Token) BeforeUpdate

func (t *Token) BeforeUpdate(db orm.DB) error

BeforeUpdate hook executed before database update operation.

func (*Token) Claims

func (t *Token) Claims() jwtauth.Claims

Claims returns the token claims to be signed

type TokenAuth

type TokenAuth struct {
	JwtAuth          *jwtauth.JWTAuth
	JwtExpiry        time.Duration
	JwtRefreshExpiry time.Duration
}

TokenAuth implements JWT authentication flow.

func NewTokenAuth

func NewTokenAuth() (*TokenAuth, error)

NewTokenAuth configures and returns a JWT authentication instance.

func (*TokenAuth) CreateJWT

func (a *TokenAuth) CreateJWT(c jwtauth.Claims) (string, error)

CreateJWT returns an access token for provided account claims.

func (*TokenAuth) CreateRefreshJWT

func (a *TokenAuth) CreateRefreshJWT(c jwtauth.Claims) (string, error)

CreateRefreshJWT returns a refresh token for provided token Claims.

func (*TokenAuth) GenTokenPair

func (a *TokenAuth) GenTokenPair(ca jwtauth.Claims, cr jwtauth.Claims) (string, string, error)

GenTokenPair returns both an access token and a refresh token.

func (*TokenAuth) Verifier

func (a *TokenAuth) Verifier() func(http.Handler) http.Handler

Verifier http middleware will verify a jwt string from a http request.

Jump to

Keyboard shortcuts

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