jwt

package module
v0.0.0-...-108a331 Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2023 License: MIT Imports: 9 Imported by: 1

README

Jwt

Build Status

A simple jwt

⚙ Installation

go get -u github.com/eininst/go-jwt

Create a token

package main

import (
    "fmt"
    "github.com/eininst/go-jwt"
    "time"
)

func main() {
    j := jwt.New("m9561kcc59534db1203e6572f7d5dq1x")

    data := map[string]interface{}{
        "id":   123,
        "name": "wzq",
    }

    token := j.CreateToken(data, time.Second*10)
    fmt.Println("Create a token:", token)
}

Parse a token

func main() {
    var ps map[string]interface{}
    
    er := j.ParseToken(token, &ps)
    
    if errors.Is(er, jwt.Expired) {
        fmt.Println("my is expired")
        return
    }
    
    fmt.Println("Parse a token:", ps)
}

Resullt:

Create a token: FXLRoc+EhSJLLm0ZrhxRwJilwynP3pohKTS4NsKyxIfabXw8qN7auU+WZzDdcplPUGrMyggOmlN9f5EwDj0ZsqiAtTRlMQpeQ78azCDxCq0=
Parse a token: map[id:123 name:wzq]

See examples

License

MIT

Documentation

Index

Constants

View Source
const Expired = JwtError("jwt: token is expired") // nolint:errname

Variables

This section is empty.

Functions

func AesDecrypt

func AesDecrypt(crypted, key []byte) ([]byte, error)

func AesEncrypt

func AesEncrypt(origData, key []byte) ([]byte, error)

func CreateToken

func CreateToken(data interface{}, expire time.Duration) string

func PKCS5Padding

func PKCS5Padding(ciphertext []byte, blockSize int) []byte

func PKCS5UnPadding

func PKCS5UnPadding(origData []byte) []byte

func ParseToken

func ParseToken(token string, v interface{}) error

func SetDefault

func SetDefault(secretKey string)

Types

type Jwt

type Jwt struct {
	SecretKey string
}

func New

func New(secretKey string) *Jwt

func (*Jwt) CreateToken

func (j *Jwt) CreateToken(data interface{}, expire time.Duration) string

func (*Jwt) ParseToken

func (j *Jwt) ParseToken(token string, v interface{}) error

type JwtError

type JwtError string

func (JwtError) Error

func (e JwtError) Error() string

func (JwtError) RedisError

func (JwtError) RedisError()

type Token

type Token struct {
	Data string `json:"data"`
	Exp  int64  `json:"exp"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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