jwt

package module
v0.0.0-...-a46fc82 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2023 License: MIT Imports: 3 Imported by: 2

README

Go JWT Library

JWT Library defines and implements an interface to create and validate JWT.

Features

  • Exports an interface to create and validate tokens. You can use this interface in your code to implement dependency injection.

  • Create tokens with an expiration time.

  • Support Generic Data for the token's payload.

Installation

Standard go get:

$ go get github.com/mig-elgt/jwt

Usage & Example

A quick code example is shown below:

package main

import (
	"fmt"
	"log"

	"github.com/mig-elgt/jwt"
)

func main() {
	// Create a Token with a Primitive Data Type
	t := jwt.New("secret_key")
	userID := 100
	token, err := t.Create(userID)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(token)
	// Output
	// eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkYXRhIjoxMDAsImV4cCI6MTY3NTg3NTcyNH0.CJVX6LQjTxQgiW7aUuNYcot6Re9Ba9DgW7XTm5G91lo

	// Validate Token
	// Token
	// eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkYXRhIjoxMDAsImV4cCI6MTY3NTg3NTcyNH0.CJVX6LQjTxQgiW7aUuNYcot6Re9Ba9DgW7XTm5G91lo
	data, err := t.Validate(token)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(data)
	// Output
	// 100

	// Create a Token with a custom Data Structure and an expiration time (in hours)
	t = jwt.NewWithExpiresAt("secret_key", 10)
	type payload struct {
		UserID int64 `json:"user_id"`
	}
	token, err = t.Create(&payload{UserID: 100})
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(token)
	// Output
	// eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkYXRhIjp7InVzZXJfaWQiOjEwMH0sImV4cCI6MTY3MzQ5NDAzNX0.BBDIZvq4xYLEkMl1G8pX_w7XgyF_RTD0OR1UB1eirVI

	// Validate Token
	// Token
	// eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkYXRhIjp7InVzZXJfaWQiOjEwMH0sImV4cCI6MTY3MzQ5NDAzNX0.BBDIZvq4xYLEkMl1G8pX_w7XgyF_RTD0OR1UB1eirVI
	data, err = t.Validate(token)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(data)
	// Output
	// map[user_id: 100]
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type TokenCreateValidator

type TokenCreateValidator interface {
	Create(payload interface{}) (string, error)
	Validate(string) (interface{}, error)
}

TokenCreateValidator describes the JWT operations.

func New

func New(secret string) TokenCreateValidator

New creates new JWT object given a secret value

func NewWithExpiresAt

func NewWithExpiresAt(secret string, expiresAt int) TokenCreateValidator

NewWithExpiresAt creates new JWT object with an expires time

Jump to

Keyboard shortcuts

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