jwt

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 18, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Purpose: This file implements the GoStack JWT authentication extension. It provides claims structures, a manager to sign and verify tokens statelessly, a compliant contract.Guard wrapper, and standard HTTP middleware for request-scoped context authentication.

Philosophy: Stateless authentication should integrate seamlessly with GoStack's container (Citadel) and request lifecycle context cache. By writing a JWT manager and guard that satisfies the framework's contracts, developers get zero-overhead authentication without breaking downstream roles, permissions, or session checks.

Choice: We use `github.com/golang-jwt/jwt/v5` as a standard, secure base dependency to sign tokens, while presenting a highly simplified, GoStack-native interface containingHS256 defaults.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Middleware

func Middleware(manager *Manager, provider contract.UserProvider) httpFrame.Middleware

Middleware returns a GoStack-compliant http.Middleware interceptor.

Types

type Claims

type Claims struct {
	UserID string `json:"user_id"`
	Role   string `json:"role"`
	jwt.RegisteredClaims
}

Claims represents the JWT payload metadata.

func GetClaims

func GetClaims(ctx *httpFrame.Context) *Claims

GetClaims safely extracts validated JWT claims from the GoStack HTTP request Context.

type JWTGuard

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

JWTGuard implements contract.Guard statelessly using the JWT manager.

func NewJWTGuard

func NewJWTGuard(manager *Manager, provider contract.UserProvider) *JWTGuard

NewJWTGuard creates a new JWTGuard adapter instance.

func (*JWTGuard) Check

func (g *JWTGuard) Check(r *http.Request) bool

Check validates if the request context contains a valid JWT user signature.

func (*JWTGuard) Login

Login is a stateless no-op for JWTGuard.

func (*JWTGuard) Logout

func (g *JWTGuard) Logout(w http.ResponseWriter, r *http.Request) error

Logout is a stateless no-op for JWTGuard.

func (*JWTGuard) User

User extracts the JWT token, verifies it, hydrates the Authenticatable user entity, and caches it.

type Manager

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

Manager coordinates token generation and verification using configurable keys and algorithms.

func NewManager

func NewManager(secret string, algo string, ttl time.Duration) *Manager

NewManager initializes a JWT manager with configuration settings.

func (*Manager) Generate

func (m *Manager) Generate(userID string, role string) (string, error)

Generate signs and returns a new HS256 JWT containing the user's ID and role claims.

func (*Manager) Verify

func (m *Manager) Verify(tokenString string) (*Claims, error)

Verify parses, validates, and returns the claims from a JWT string.

type ServiceProvider

type ServiceProvider struct{}

ServiceProvider integrates JWT into the GoStack dependency container framework.

func NewServiceProvider

func NewServiceProvider() *ServiceProvider

func (*ServiceProvider) Boot

func (sp *ServiceProvider) Boot(c *foundation.Container)

Boot is a no-op lifecycle implementation for contract compliance.

func (*ServiceProvider) Register

func (sp *ServiceProvider) Register(c *foundation.Container)

Register binds the JWT Manager singleton into the container.

Jump to

Keyboard shortcuts

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