jwtservice

package module
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2024 License: CC0-1.0 Imports: 6 Imported by: 0

README

Go JWT Service

GitHub Release Build Status Go Reference codecov go.mod LICENSE Go Report Card

This is a GO JWT service that provides a standardized way to generate, verify, and authenticate JSON Web Tokens (JWT).

Create Service

You can initialize the service with just a secret in this case 123. By default the token expires in 24 hours

service := jwtservice.New("123")

You can also override options like expiration WithExpiration(exp time.Duration)

service := jwtservice.New("123", WithExpiration(12 * time.Hour))

Generate Token

service := jwtservice.New("123")
token, err := service.GenerateToken(jwtservice.Claims{"userId": 1})

Middleware Usage

service := jwtservice.New("123")

r := chi.NewRouter()

service.ApplyMiddleware(r)

Get Claims From Request Context

service := jwtservice.New("123")
token, err := service.GenerateToken(jwtservice.Claims{"userId": 1})

router := chi.NewRouter()

service.ApplyMiddleware(router)

router.Get("/admin", func(w http.ResponseWriter, r *http.Request) {
    claims, err := service.ClaimsFromRequest(r)
    
    fmt.Println(claims["userId"])
})

Credits

JWT Service uses mainly these packages:

Contributing

Feel free to create an issue or propose a pull request.

Follow the Code of Conduct.

Documentation

Overview

Package jwtservice provides a standardized way to generate, verify, and authenticate JWT.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Claims

type Claims map[string]any

Claims defines map for setting token claims

type Service

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

Service defines the JWT service structure

func New

func New(appKey string, opts ...ServiceOption) *Service

New create new JWT service

func (*Service) ApplyMiddleware

func (s *Service) ApplyMiddleware(router *chi.Mux)

ApplyMiddleware append Verifier and Authenticator middleware to the Mux middleware stack

func (*Service) Auth

func (s *Service) Auth() *jwtauth.JWTAuth

Auth returns jwtauth.JWTAuth

func (*Service) ClaimsFromRequest

func (s *Service) ClaimsFromRequest(r *http.Request) (map[string]any, error)

ClaimsFromRequest parses token claims from request token

func (*Service) GenerateToken

func (s *Service) GenerateToken(claims Claims) (string, error)

GenerateToken generate signed token

type ServiceOption

type ServiceOption func(*Service)

ServiceOption defines functions for configuring JWT service

func WithCustomAuth

func WithCustomAuth(auth *jwtauth.JWTAuth) ServiceOption

WithCustomAuth this allows overriding the default JWT auth instance

func WithExpiration

func WithExpiration(exp time.Duration) ServiceOption

WithExpiration is an option to set the token expiration using time duration (default: 24 * time.Hour)

Jump to

Keyboard shortcuts

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