crypt

package
v1.7.0 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2023 License: GPL-3.0 Imports: 4 Imported by: 0

README

Roki

The ultimate solution to sign and validate JSON Web Tokens (JWT) without breaking the ability to fully customize.

go get github.com/z3ntl3/roki/crypt

Examples
Signing
package main

import (
	"fmt"
	"log"
	"os"
	"time"

	"github.com/z3ntl3/roki/crypt"
)

type MyCustomClaims struct {
	Email string `json:"email"`
	Role  string `json:"role"`
	*crypt.StandardClaims
}

func (c *MyCustomClaims) Valid() error {
	return nil
}

func main() {
	os.Setenv("SECRET", "root")

	c := &crypt.JWT{}
	{
		c.SecretEnv = "SECRET"
	}

	myclaims := &MyCustomClaims{
		Email: "efdal@gmail.com",
		Role:  "hoi",
		StandardClaims: &crypt.StandardClaims{
			ExpiresAt: time.Now().Add(time.Hour * 24).Unix(),
		},
	}

	if err := c.Sign(myclaims, crypt.HMAC_HS512); err != nil {
		log.Fatal(err)
	}
	fmt.Println(c.TokenStr)
Validating
package main

import (
	"fmt"
	"log"
	"os"

	"github.com/z3ntl3/roki/crypt"
)

type MyCustomClaims struct {
	Email string `json:"email"`
	Role  string `json:"role"`
	*crypt.StandardClaims
}

func (c *MyCustomClaims) Valid() error {
	return nil
}

func main() {
	os.Setenv("SECRET", "root")

	c := &crypt.JWT{}
	{
		c.TokenStr = os.Getenv("token")
	}

	cl, err := c.Validate(&MyCustomClaims{}, func(t *crypt.Token) (interface{}, error) {
		return []byte(os.Getenv("SECRET")), nil
	})
	if err != nil {
		log.Fatal(err)
	}

	claims, ok := cl.(*MyCustomClaims)
	if !ok {
		log.Fatal("invalid token")
	}
	fmt.Println(claims.ExpiresAt, claims.Email, claims.Role)
}

Together
package main

import (
	"fmt"
	"log"
	"os"
	"time"

	"github.com/z3ntl3/roki/crypt"
)

type MyCustomClaims struct {
	Email string `json:"email"`
	Role  string `json:"role"`
	*crypt.StandardClaims
}

func (c *MyCustomClaims) Valid() error {
	return nil
}

func main() {
	os.Setenv("SECRET", "root")

	c := &crypt.JWT{}
	{
		c.SecretEnv = "SECRET"
	}

	myclaims := &MyCustomClaims{
		Email: "efdal@gmail.com",
		Role:  "hoi",
		StandardClaims: &crypt.StandardClaims{
			ExpiresAt: time.Now().Add(time.Hour * 24).Unix(),
		},
	}

	if err := c.Sign(myclaims, crypt.HMAC_HS512); err != nil {
		log.Fatal(err)
	}
	fmt.Println(c.TokenStr)

	cl, err := c.Validate(&MyCustomClaims{}, func(t *crypt.Token) (interface{}, error) {
		return []byte(os.Getenv("SECRET")), nil
	})
	if err != nil {
		log.Fatal(err)
	}

	claims, ok := cl.(*MyCustomClaims)
	if !ok {
		log.Fatal("invalid token")
	}
	fmt.Println(claims.ExpiresAt, claims.Email, claims.Role)
}

Documentation

Index

Constants

This section is empty.

Variables

Functions

This section is empty.

Types

type JWT

type JWT struct {
	SecretEnv string // your ENV key name for the secret | has to be set before calling 'Sign()' or 'Validate()'
	TokenStr  string /* populated after a successfull 'Sign()' call |
	when calling 'Validate()' as a standalone,
	you should set TokenStr manually to the token string you obtained from the client*/
}

func (*JWT) Sign

func (c *JWT) Sign(claims jwt.Claims, method jwt.SigningMethod) error

Signs JWT token with given custom claims and signing method

func (*JWT) Validate

func (c *JWT) Validate(claims jwt.Claims, keyfunc jwt.Keyfunc) (jwt.Claims, error)

Validates given token string with your custom claims type and keyfunc

type StandardClaims added in v1.5.0

type StandardClaims = jwt.StandardClaims

type Token added in v1.5.0

type Token = jwt.Token

Jump to

Keyboard shortcuts

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