jwt

package
v0.0.0-...-8be7e5c Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2023 License: MIT Imports: 12 Imported by: 5

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 ParseStructToMap

func ParseStructToMap(c interface{}) (map[string]interface{}, error)

func RefreshTokenFromCtx

func RefreshTokenFromCtx(ctx context.Context) string

RefreshTokenFromCtx retrieves the parsed refresh token from context.

Types

type AppClaims

type AppClaims struct {
	ID    int      `json:"id,omitempty"`
	Sub   string   `json:"sub,omitempty"`
	Roles []string `json:"roles,omitempty"`
	CommonClaims
}

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 map[string]interface{}) error

ParseClaims parses JWT claims into AppClaims.

type CommonClaims

type CommonClaims struct {
	ExpiresAt int64 `json:"exp,omitempty"`
	IssuedAt  int64 `json:"iat,omitempty"`
}

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 {
	ID    int    `json:"id,omitempty"`
	Token string `json:"token,omitempty"`
	CommonClaims
}

RefreshClaims represents the claims parsed from JWT refresh token.

func (*RefreshClaims) ParseClaims

func (c *RefreshClaims) ParseClaims(claims map[string]interface{}) 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() RefreshClaims

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 AppClaims) (string, error)

CreateJWT returns an access token for provided account claims.

func (*TokenAuth) CreateRefreshJWT

func (a *TokenAuth) CreateRefreshJWT(c RefreshClaims) (string, error)

CreateRefreshJWT returns a refresh token for provided token Claims.

func (*TokenAuth) GenTokenPair

func (a *TokenAuth) GenTokenPair(accessClaims AppClaims, refreshClaims RefreshClaims) (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