jwt

package module
v0.0.0-...-0c9901f Latest Latest
Warning

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

Go to latest
Published: May 3, 2021 License: MIT Imports: 7 Imported by: 0

README

go-sjwt

Simple Golang JWT

Installation 🚀

Activate GO111MODULE

go env -w GO111MODULE="on"

install package

go get -u github.com/agustrinaldokurniawan/go-sjwt

Usage 💻

import(
        ...
        sjwt "github.com/agustrinaldokurniawan/go-sjwt"
)
Set Algorithm

supported: SHA256 -> HS256

alg :=  "HS256"
Set Payload
payload := sjwt.Payload{}
payload.Iss = "login" // set issuer
payload.Aud = "www.domain.com" // set audience
payload.Exp = 3600 // set expired in second
payload.Sub = "user@email.com" // set subject
payload.Role = "admin" // set role
Get Token
secret := "mysecret" // change with your secret
token, err := sjwt.JWT(alg, payload, secret)
	if err != nil {
		return err
	}
  
return token
Verify Token
role, status, errVerfify := sjwt.VerifyJWT("token")
	if errVerfify != nil {
		fmt.Println(errVerfify)
	}

	if role == "admin" {
		fmt.Println("You are admin")
	}

	if status {
		fmt.Println("Authenticated")
		fmt.Println(token)
	}

Contributing ♥️

Pull request are welcome. I'm very happy if we can improve this code together 😊

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func JWT

func JWT(alg string, p Payload, secret string) (string, error)

func Signature

func Signature(h string, p string, secret string) (string, error)

func VerifyJWT

func VerifyJWT(v string) (string, bool, error)

Types

type Header struct {
	Typ string
	Alg string
}

type Payload

type Payload struct {
	Iss  string
	Iat  int64
	Exp  int64
	Aud  string
	Sub  string
	Role string
}

Jump to

Keyboard shortcuts

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