nasermirzaei89 / jwt

JSON Web Token library

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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
}

About

JSON Web Token library

License:MIT License


Languages

Language:Go 100.0%