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 ¶
Decode decodes the given token string to a Token by following steps:
- Decode the given token string as base64.
- Unmarshal it as a JSON object and populate it to a Toekn with Token.Source that has been given as a generic type.
Click to show internal directories.
Click to hide internal directories.