Skip to content

arcvats/godor-jwt

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

godor-jwt

JWT helpers and middleware(go fiber) for godor (got the door 😉)

Installation

go get -u github.com/arcvats/godor-jwt

Usage

godor-jwt provides helpers and middleware(fiber) for Encoding and Decoding a JWT token with HMAC methods

Config Struct

type Config struct {
    Algorithm string // default is HS256
    Secret    string // required
    Expiry    int64 // default is 60 minutes
}

JWT helpers

Encode

package main
import (
    godorjwt "github.com/arcvats/godor-jwt"
)

func main() {
	config := godorjwt.Config{
		Algorithm: "HS256",
		Secret: "secret",
		Expiry: 60,
    }
	payload := map[string]any{
        "sub": "1234567890",
        "name": "John Doe",
        "admin": true,
    }
	token, jti, expiry, error := godorjwt.Encode(payload, config)
}

Decode

package main
import (
    godorjwt "github.com/arcvats/godor-jwt"
)

func main() {
	config := godorjwt.Config{
		Secret: "secret",
    }
	tokenString := "some.jwt.token"
	decodedTokenClaims, error := godorjwt.Decode(tokenString, config)
}

JWT middleware(for go fiber)

package main
import (
    godorjwt "github.com/arcvats/godor-jwt"
	"github.com/gofiber/fiber/v2"
)

func main() {
	app := fiber.New()
	config := godorjwt.New("secret")
	// or
	//config := godorjwt.Config{
	//	Secret: "secret",
	//}
	app.Use(config.Decoder())
	/* 
	    Token is acquired from the Authorization header or jwt Cookie
        Now you can access the decoded token claims in the context
        claims := c.Locals("decodedToken").(map[string]any)
    */
}

License

MIT