token

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 24, 2023 License: Apache-2.0 Imports: 6 Imported by: 2

README

token

TXN2 JWT token middleware for gin-gonic.

gin-gonic Implementation

Includes:

import (
	"github.com/gin-gonic/gin"
	"github.com/txn2/token"
)

Add token middleware:

// gin router
r := gin.New()

// token middleware
jwt := token.NewJwt(token.JwtCfg{
    Exp:    10, // minutes
    EncKey: []byte("DZCAJZGByGb7pGc3zJKdQ9wGYKRDZpZETZN46tzTWFT"),
})

r.Use(jwt.GinHandler())

Example

Run Source:

go run ./example/server.go --key="n2r5u8x/A?D(G+KbPdX"

Tokenize a JSON object:

TOKEN=$(curl -X POST http://localhost:8080/tokenize?raw=true   -d '{
    "id": "sysop",
    "description": "Global system operator",
    "display_name": "System Operator",
    "active": true,
    "sysop": true,
    "password": "REDACTED",
    "sections_all": false,
    "sections": [],
    "accounts": [],
    "admin_accounts": []
}') && echo $TOKEN

Validate the token:

curl http://localhost:8080/validate -H "Authorization: Bearer $TOKEN"

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Jwt

type Jwt struct {
	Cfg JwtCfg
}

func NewJwt

func NewJwt(cfg JwtCfg) *Jwt

func (*Jwt) GetToken

func (jwt *Jwt) GetToken(v interface{}) (string, error)

GetToken generated a HS256 token from an object

func (*Jwt) GinHandler

func (jwt *Jwt) GinHandler() gin.HandlerFunc

GinHandler is a middleware for Gin-gonic

func (*Jwt) GinParse

func (jwt *Jwt) GinParse(c *gin.Context) (map[string]interface{}, error)

GinParse parses a gin.Context

type JwtCfg

type JwtCfg struct {
	EncKey []byte

	// Expiration minutes
	Exp int

	Claims interface{}
}

JwtCfg defines a token generator object

type Tok

type Tok struct {
	Claims jwtlib.MapClaims
	Valid  bool
	Err    error
}

Tok is a token

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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