Skip to content
/ xjwt Public

library provides an API for working with JSON Web Token, using jwt-go

License

Notifications You must be signed in to change notification settings

mkbeh/xjwt

Repository files navigation

JWT Library

This library provides an API for working with JSON Web Token, using jwt-go.

What the heck is a JWT?

JWT.io has a great introduction to JSON Web Tokens.

In short, it's a signed JSON object that does something useful (for example, authentication). It's commonly used for Bearer tokens in Oauth 2. A token is made of three parts, separated by .'s. The first two parts are JSON objects, that have been base64url encoded. The last part is the signature, encoded the same way.

The first part is called the header. It contains the necessary information for verifying the last part, the signature. For example, which encryption method was used for signing and what key was used.

The part in the middle is the interesting bit. It's called the Claims and contains the actual stuff you care about. Refer to RFC 7519 for information about reserved keys and the proper way to add your own.

Installation Guidelines

  1. To install the jwt package, you first need to have Go installed, then you can use the command below to add jwt-go as a dependency in your Go program.
go get -u github.com/mkbeh/xjwt
  1. Import it in your code:
import "github.com/mkbeh/xjwt"

Usage

A detailed usage guide, including how to sign and verify tokens can be found in examples.

package main

import (
	"fmt"

	"github.com/golang-jwt/jwt/v5"
	tm "github.com/mkbeh/xjwt"
)

func main() {
	tokenManager, err := tm.New(tm.WithSecretKey([]byte("secret")))
	if err != nil {
		panic(err)
	}

	token, err := tokenManager.CreateWithClaims(jwt.MapClaims{"sub": "123"})
	if err != nil {
		panic(err)
	}

	fmt.Printf("token: %s", token)

	claims := jwt.MapClaims{}
	if err := tokenManager.ParseWithClaims(token, claims); err != nil {
		panic(err)
	}

	fmt.Printf("claims: %+v", claims)

}

About

library provides an API for working with JSON Web Token, using jwt-go

Topics

Resources

License

Stars

Watchers

Forks

Languages