jwt

package module
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2025 License: MIT Imports: 9 Imported by: 0

README

go-jwt

Simple, minimal and zero dependency Go JWT library.

Go Version Go Reference JWT Logo

Installation

go get -u github.com/ghostsama2503/go-jwt

Building and signing a token

package main

import (
	"fmt"
	"time"

	"github.com/ghostsama2503/go-jwt"
)

func main() {
	secretKey := []byte("secret")
	currentTime := time.Now().UTC()

	claims := &jwt.CommonClaims{
		Subject:        "test",
		ExpirationTime: currentTime.AddDate(0, 1, 0).Unix(),
		IssuedAtTime:   currentTime.Unix(),
	}

	token := jwt.New(claims)

	signedToken, err := token.Signed(secretKey)
	if err != nil {
		panic(err)
	}

	fmt.Println(signedToken)
}

Parsing and validating a token

package main

import (
	"fmt"
	"os"

	"github.com/ghostsama2503/go-jwt"
)

func main() {
	secretKey := []byte("secret")
	tokenString := os.Getenv("TOKEN")

	token, err := jwt.Parse(tokenString)
	if err != nil {
		panic(err)
	}

	if err := token.Validate(secretKey); err != nil {
		panic(err)
	}

	fmt.Println("valid token")
}

Disclaimer

It's provided as-is, with no guarantees or warranties. It's a lightweight and easy-to-use solution for basic needs. For more robust solutions, please consider alternatives like golang-jwt/jwt.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrJwtError           = errors.New("jwt error")
	ErrInvalidTokenFormat = fmt.Errorf("%w: invalid token format", ErrJwtError)
	ErrExpiredToken       = fmt.Errorf("%w: expired token", ErrJwtError)
	ErrInvalidSignature   = fmt.Errorf("%w: invalid signature", ErrJwtError)
)

Functions

func Sign

func Sign(encodedToken string, secretKey []byte) ([]byte, error)

Types

type Claims

type Claims interface {
	GetIssuer() string
	GetSubject() string
	GetExpirationTime() int64
	GetIssuedAtTime() int64
}

type CommonClaims

type CommonClaims struct {
	Issuer         string `json:"iss"`
	Subject        string `json:"sub"`
	ExpirationTime int64  `json:"exp"`
	IssuedAtTime   int64  `json:"iat"`
}

func (*CommonClaims) GetExpirationTime

func (claims *CommonClaims) GetExpirationTime() int64

func (*CommonClaims) GetIssuedAtTime

func (claims *CommonClaims) GetIssuedAtTime() int64

func (*CommonClaims) GetIssuer

func (claims *CommonClaims) GetIssuer() string

func (*CommonClaims) GetSubject

func (claims *CommonClaims) GetSubject() string
type Header struct {
	Type      string `json:"typ"`
	Algorithm string `json:"alg"`
}

type Token

type Token struct {
	Header    *Header
	Payload   Claims
	Signature []byte
}

func New

func New(claims Claims) *Token

func Parse

func Parse(tokenString string) (*Token, error)

func ParseWithClaims

func ParseWithClaims(tokenString string, claims Claims) (*Token, error)

func (*Token) Encoded

func (token *Token) Encoded() (string, error)

func (*Token) Signed

func (token *Token) Signed(secretKey []byte) (string, error)

func (*Token) Validate

func (token *Token) Validate(secretKey []byte) error

Jump to

Keyboard shortcuts

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