jwtverify

package module
v0.0.0-...-6d63f94 Latest Latest
Warning

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

Go to latest
Published: May 14, 2024 License: GPL-3.0 Imports: 5 Imported by: 0

README

JWT Verify Module

Use Case

I am gonna use this multiple times in my projects and I dont want to reinvent the logic every time.

License

Project signed under GPLv3 license.

Installation

go get -u github.com/marios-pz/jwtverify

How to use it

Example with chi router

// main program
package main

import (
	"log"
	"net/http"
	"github.com/marios-pz/jwtverify"
	"github.com/go-chi/chi"
	"github.com/go-chi/chi/middleware"
	"github.com/go-chi/cors"
)

func main() {
	token := jwtverify.NewJWTTokenManager("SECRET_KEY")
	r := chi.NewRouter()
    // ... handle chi router

	r.Route("/", func(r chi.Router) {
		r.Group(func(r chi.Router) {
			r.Use(token.JWTHandler)
            // private endpoints
		})

        // public endpoints
        // login handler
	})

	err = http.ListenAndServe(":3000", r)
	if err != nil {
		log.Println("There was an error listening on port :3000", err)
	}
}

// login handler
token, err := token.GenerateToken(token.MakeClaim(user_id, lifespan))
if err != nil {
    helpers.RespondWithError(w, http.StatusInternalServerError, "could not create jwt token")
    return
}

cookie := http.Cookie{
    Name:     "<token-name>",
    Value:    token,
    Expires:  time.Now().Add(20 * time.Minute), // 20 minutes lifespan
    HttpOnly: true,
    SameSite: http.SameSiteStrictMode,
    Path:     "/",
    Secure:   true,
}

http.SetCookie(w, &cookie)

helpers.RespondWithSuccess(w, http.StatusCreated, "user created. here's a cookie!")

Documentation

Overview

Package jwtverify provides utilities for working with JSON Web Tokens (JWT) in Go. A helper package for JWT operations.

Index

Constants

This section is empty.

Variables

View Source
var JWTSigningMethod = jwt.SigningMethodHS256

JWTSigningMethod specifies the signing method used for JWT tokens.

Functions

This section is empty.

Types

type JWTTokenManager

type JWTTokenManager struct {
	// contains filtered or unexported fields
}

TokenManager represents a JWT token manager.

func NewJWTTokenManager

func NewJWTTokenManager(secretKey string) *JWTTokenManager

NewTokenManager creates a new TokenManager with the provided secret key.

func (*JWTTokenManager) GenerateToken

func (tm *JWTTokenManager) GenerateToken(claims jwt.Claims) (string, error)

GenerateToken generates a new JWT token with the given claims.

func (*JWTTokenManager) JWTHandler

func (tm *JWTTokenManager) JWTHandler(next http.Handler) http.Handler

JWTHandler returns an http.Handler middleware that verifies JWT tokens for incoming requests.

func (*JWTTokenManager) MakeClaim

func (*JWTTokenManager) MakeClaim(userID int32, minutes int64) jwt.MapClaims

MakeClaim creates and returns JWT claims for the given user ID and expiration time.

func (*JWTTokenManager) VerifyJWT

func (tm *JWTTokenManager) VerifyJWT(endpointHandler func(w http.ResponseWriter, r *http.Request)) http.HandlerFunc

VerifyJWT returns an http.HandlerFunc that verifies JWT tokens before passing the request to the endpointHandler.

func (*JWTTokenManager) VerifyToken

func (tm *JWTTokenManager) VerifyToken(tokenString string) (jwt.Claims, error)

VerifyToken verifies the given JWT token string and returns the claims if valid.

Jump to

Keyboard shortcuts

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