jwt

package module
v0.0.0-...-54095ba Latest Latest
Warning

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

Go to latest
Published: Jul 15, 2018 License: MIT Imports: 8 Imported by: 2

README

Gin JWT middleware

Very simple but useful implementation of JWT for gin framework as middleware.

Key option of this implementation - you can use access-token to refresh it within RefreshTimeout time

Using github.com/robbert229/jwt for token manipulation

Little code from github.com/appleboy/gin-jwt was used

Usage

see example directory

Minimal requirements:

  • Implement Authenticator handler
  • Implement IRefreshTokenStorage

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExtractPayload

func ExtractPayload(c *gin.Context) map[string]interface{}

Helper: Extract payload from gic.Context

Types

type GinMiddleware

type GinMiddleware struct {
	// secret key
	SecretKey string
	// header name in request that contains our token
	Header string
	// algorithm (see github.com/robbert229/jwt for details)
	Algorithm *jwt.Algorithm
	// access token ttl
	Timeout time.Duration
	// refresh token ttl
	RefreshTimeout time.Duration
	// func for handle unauthorized responses
	Unauthorized func(*gin.Context, int, string)
	// authentication function
	Authenticator func(userID string, password string, c *gin.Context) (interface{}, bool)
	// authorization function
	Authorizator func(payload interface{}, c *gin.Context) bool
	// refresh token storage instance
	RefreshTokenStorage IRefreshTokenStorage
}

func (*GinMiddleware) Init

func (m *GinMiddleware) Init() error

Initialize middleware struct

func (*GinMiddleware) IssueAccessToken

func (m *GinMiddleware) IssueAccessToken(refreshToken string, payload map[string]interface{}, c *gin.Context)

Generate new access token and send it to client

func (*GinMiddleware) LoginHandler

func (m *GinMiddleware) LoginHandler(c *gin.Context)

Handler for login action

func (*GinMiddleware) LogoutHandler

func (m *GinMiddleware) LogoutHandler(c *gin.Context)

Handler for logout action

func (*GinMiddleware) MiddlewareFunc

func (m *GinMiddleware) MiddlewareFunc() gin.HandlerFunc

MiddlewareFunc makes GinMiddleware implement the Gin Middleware interface.

func (*GinMiddleware) RefreshHandler

func (m *GinMiddleware) RefreshHandler(c *gin.Context)

Handler for token refresh action

type IRefreshTokenStorage

type IRefreshTokenStorage interface {
	Issue() (string, error)
	// Call to check is refresh token already expired
	IsExpired(token string) bool
	// Add/Update refresh token in
	Update(token string, timeout time.Duration, payload map[string]interface{}, c *gin.Context) error
	// Delete refresh token from storage
	Delete(token string) error
	// Revoke refresh token
	Revoke(token string, accessTokenTimeout time.Duration) error
	// Check is token was revoked
	IsRevoked(token string) bool
}

Refresh token storage interface

type Login

type Login struct {
	Username string `form:"username" json:"username" binding:"required"`
	Password string `form:"password" json:"password" binding:"required"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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