bjt

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: May 24, 2023 License: MIT Imports: 4 Imported by: 0

README

bjt

bjt (stannds for Base64 JSON Token) is a Go library that provides a simple way to encode and decode tokens using a base64 encoded JSON format.

Supported Go version

1.20 or higher.

Usage

package main

import (
	"fmt"
	"os"

	"github.com/kauche/bjt"
)

type myTokenSource struct {
	ID     string `json:"id"`
	Number int    `json:"number"`
}

func main() {
	source := &myTokenSource{
		ID:     "124b473d-6079-46c4-b8bd-3824c93cef32",
		Number: 123,
	}

	token := bjt.NewToken(source)

	tokenStr, err := token.Encode()
	if err != nil {
		fmt.Fprintf(os.Stderr, "failed to encode: %s\n", err)
		os.Exit(1)
	}

	// This prints `eyJpZCI6IjEyNGI0NzNkLTYwNzktNDZjNC1iOGJkLTM4MjRjOTNjZWYzMiIsIm51bWJlciI6MTIzfQ==`
	// that is the base64 encoded myTokenSource JSON object: `{"id":"124b473d-6079-46c4-b8bd-3824c93cef32","number":123}`
	fmt.Println(tokenStr)

	decodedToken, err := bjt.Decode[myTokenSource](tokenStr)
	if err != nil {
		fmt.Fprintf(os.Stderr, "failed to decode: %s\n", err)
		os.Exit(1)
	}

	// This prints `ID:124b473d-6079-46c4-b8bd-3824c93cef32 Number:123`
	fmt.Printf("ID:%s Number:%d\n", decodedToken.Source.ID, decodedToken.Source.Number)
}

See https://pkg.go.dev/github.com/kauche/bjt for more details.

Documentation

Overview

`bjt` (stannds for Base64 JSON Token) is a Go library that provides a simple way to encode and decode tokens using a base64 encoded JSON format.

Example
package main

import (
	"fmt"
	"os"

	"github.com/kauche/bjt"
)

type myTokenSource struct {
	ID     string `json:"id"`
	Number int    `json:"number"`
}

func main() {
	source := &myTokenSource{
		ID:     "124b473d-6079-46c4-b8bd-3824c93cef32",
		Number: 123,
	}

	token := bjt.NewToken(source)

	tokenStr, err := token.Encode()
	if err != nil {
		fmt.Fprintf(os.Stderr, "failed to encode: %s\n", err)
		return
	}

	// This prints `eyJpZCI6IjEyNGI0NzNkLTYwNzktNDZjNC1iOGJkLTM4MjRjOTNjZWYzMiIsIm51bWJlciI6MTIzfQ==`
	// that is the base64 encoded myTokenSource JSON object: `{"id":"124b473d-6079-46c4-b8bd-3824c93cef32","number":123}`
	fmt.Println(tokenStr)

	decodedToken, err := bjt.Decode[myTokenSource](tokenStr)
	if err != nil {
		fmt.Fprintf(os.Stderr, "failed to decode: %s\n", err)
		return
	}

	// This prints `ID:124b473d-6079-46c4-b8bd-3824c93cef32 Number:123`
	fmt.Printf("ID:%s Number:%d\n", decodedToken.Source.ID, decodedToken.Source.Number)
}

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrInvalidToken = errors.New("invalid token")

Functions

This section is empty.

Types

type Token

type Token[T any] struct {
	Source *T
}

Token is a token that is encoded to and decoded from a base64 JSON string.

func Decode

func Decode[T any](str string) (*Token[T], error)

Decode decodes the given token string to a Token by following steps:

  1. Decode the given token string as base64.
  2. Unmarshal it as a JSON object and populate it to a Toekn with Token.Source that has been given as a generic type.

func NewToken

func NewToken[T any](source *T) *Token[T]

NewToken creates a new Token with the given source.

func (*Token[T]) Encode

func (p *Token[T]) Encode() (string, error)

Encode encodes the Token to a base64 string by following steps:

  1. Marshal the Token.Source to a JSON object.
  2. Encode it as base64.

Jump to

Keyboard shortcuts

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