Jwt

package module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Jun 6, 2021 License: MIT Imports: 7 Imported by: 0

README

go-jwt

Implementation of Jwt in golang.

Installation

go get github.com/MangoMilk/go-jwt

QuickStart

import (
    "strconv"
    "github.com/MangoMilk/go-jwt"
)

const SECRET = "secret"

func main() {
    // generate 
    jwt := Jwt.NewJwt()

    jwt.SetStandardPayload(Jwt.StandardPayload{
        Iss: "Dwarf",
        Iat: strconv.Itoa(int(time.Now().Unix())),
    })
    
    additionPayload := make(map[string]interface{})
    additionPayload["username"] = "dwarf"
    jwt.SetAdditionPayload(additionPayload)
    
    setHeaderErr :=jwt.SetHeader(Header{Alg: Jwt.HS256})
    if setHeaderErr != nil {
        panic(setHeaderErr)
    }
 
    var token string
    signErr := jwt.Signature(SECRET)
    if signErr != nil {
        panic(signErr)
    }

    token = jwt.Generate()
    fmt.Printf("token: ", token)

    // verify
    jwt2 := Jwt.NewJwt()
    resolveErr := jwt2.Resolve(token)
    if resolveErr == nil {
        panic(resolveErr)
    }

    fmt.Println(jwt2.VerifySign(SECRET) == nil)
    //fmt.Println("header: ", jwt2.Header))
    //fmt.Println("payload: ", jwt2.Payload))
}

Documentation

Index

Constants

View Source
const (
	HS256 = "HS256"
	HS1   = "HS1"
)

Variables

This section is empty.

Functions

func NewJwt

func NewJwt() *jwt

Types

type Header struct {
	Typ string `json:"typ"`
	Alg string `json:"alg"`
}

Header

func (*Header) SetAlg

func (h *Header) SetAlg(alg string) error

func (*Header) SetTyp

func (h *Header) SetTyp(typ string)

func (*Header) ToString added in v0.0.3

func (h *Header) ToString() string

type Payload

type Payload struct {
	StandardPayload
	Addition map[string]interface{}
}

func (*Payload) SetAud

func (p *Payload) SetAud(aud string)

func (*Payload) SetExp

func (p *Payload) SetExp(exp string)

func (*Payload) SetIat

func (p *Payload) SetIat(iat string)

func (*Payload) SetIss

func (p *Payload) SetIss(iss string)

func (*Payload) SetJti

func (p *Payload) SetJti(jti string)

func (*Payload) SetNbf

func (p *Payload) SetNbf(nbf string)

func (*Payload) SetSub

func (p *Payload) SetSub(sub string)

func (*Payload) ToString added in v0.0.3

func (p *Payload) ToString() string

type StandardPayload

type StandardPayload struct {
	Iss string `json:"iss"` // 签发者
	Iat string `json:"iat"` // 签发时间
	Exp string `json:"exp"` // 过期时间
	Aud string `json:"aud"` // 接收jwt的一方
	Sub string `json:"sub"` // jwt面向的用户
	Nbf string `json:"nbf"` // 定义在什么时间之前,该jwt都是不可用的
	Jti string `json:"jti"` // jwt的唯一身份标识,主要用来作为一次性token,从而回避重放攻击
}

Payload

Jump to

Keyboard shortcuts

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