Skip to content

nasermirzaei89/jwt

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JWT

JSON Web Token library

Build Status Go Report Card Codecov Go Reference License

Supported Algorithms

  • HS256
  • HS384
  • HS512
  • RS256
  • RS384
  • RS512
  • ES256
  • ES384
  • ES512
  • PS256
  • PS384
  • PS512

Usage

Sign

package main

import (
	"fmt"
	"log"

	"github.com/nasermirzaei89/jwt"
)

func main() {
	token := jwt.New(jwt.HS256)
	tokenStr, err := jwt.Sign(*token, []byte("secret_key"))
	if err != nil {
		log.Fatalln(err)
	}

	fmt.Println(tokenStr) // eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.e30.HUfJqC1q8JUPKD4jj8PZAYppSrQRL8tJHTljdcTfFCQ
}

Verify

package main

import (
	"log"

	"github.com/nasermirzaei89/jwt"
)

func main() {
	tokenStr := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.e30.HUfJqC1q8JUPKD4jj8PZAYppSrQRL8tJHTljdcTfFCQ"
	err := jwt.Verify(tokenStr, []byte("secret_key"))
	if err != nil {
		log.Fatalln(err)
	}
}

Sign With Claims

package main

import (
	"fmt"
	"log"
	"time"

	"github.com/nasermirzaei89/jwt"
)

func main() {
	token := jwt.New(jwt.HS256)
	token.SetIssuer("https://yourdomain.tld")
	token.SetExpirationTime(time.Now())
	tokenStr, err := jwt.Sign(*token, []byte("secret_key"))
	if err != nil {
		log.Fatalln(err)
	}

	fmt.Println(tokenStr) // variable
}