No description
Find a file
2018-09-22 16:34:10 +02:00
blacklist.go added errors to blacklist 2018-09-22 16:34:10 +02:00
claims.go added benchmarks 2018-09-13 14:18:11 +02:00
go.mod added go.mod 2018-09-19 19:42:25 +02:00
hash.go added empty token check and copyright 2018-01-03 23:04:17 +01:00
jwt.go added errors to blacklist 2018-09-22 16:34:10 +02:00
jwt_test.go added benchmarks 2018-09-13 14:18:11 +02:00
LICENSE added empty token check and copyright 2018-01-03 23:04:17 +01:00
README.md fixed typos, added error prefix and added usage to README.md 2018-09-13 16:29:45 +02:00
time.go added utc time functions 2018-09-13 15:58:24 +02:00
token.go fixed typos, added error prefix and added usage to README.md 2018-09-13 16:29:45 +02:00

jwt - a simple JSON Web Token library written in Go

Usage

package main

import (
	"fmt"
	"git.giftfish.de/ston1th/jwt"
	"log"
	"time"
)

func main() {
	// create a new signing instance
	JWT, err := jwt.New(time.Hour, jwt.NewMemBlacklist(), nil)
	if err != nil {
		log.Fatal(err)
	}

	// create a new token
	t := jwt.NewToken(jwt.Claims{
		"username": "admin",
	}, nil)

	// sign the token
	err = JWT.Sign(t)
	if err != nil {
		log.Fatal(err)
	}

	// print the token string
	token := t.String()
	fmt.Println(token)

	// decode the token string
	newToken, err := jwt.DecodeToken(token)
	if err != nil {
		log.Fatal(err)
	}

	// verify the decoded token
	err = JWT.Verify(newToken)
	if err != nil {
		log.Fatal(err)
	}

	// add the token to the blacklist
	err = JWT.Invalidate(newToken)
	if err != nil {
		log.Fatal(err)
	}

	// try to verify the blacklisted token, it should fail
	err = JWT.Verify(newToken)
	if err != nil {
		log.Fatal(err)
	}
}