jwtutils

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2025 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExtractTokenFromMetadata

func ExtractTokenFromMetadata(ctx context.Context) (string, error)

ExtractTokenFromMetadata extracts JWT token from gRPC metadata

func HasPermission

func HasPermission(ctx context.Context, permission string) (bool, error)

HasPermission checks if the user in the context has the specified permission

func RequirePermission

func RequirePermission(ctx context.Context, permission string) error

RequirePermission checks if the user has the required permission and returns an error if not

func SetUserInContext

func SetUserInContext(ctx context.Context, claims *JWTClaims) context.Context

SetUserInContext sets user claims in context

Types

type AuthMiddleware

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

AuthMiddleware provides JWT authentication for gRPC and HTTP requests

func InitAuthMiddleware

func InitAuthMiddleware(secretKey string, enabled bool) *AuthMiddleware

InitAuthMiddleware initializes auth middleware with config

func NewAuthMiddleware

func NewAuthMiddleware(jwtSecret string, enabled bool) *AuthMiddleware

NewAuthMiddleware creates a new auth middleware instance

func (*AuthMiddleware) HTTPMiddleware

func (a *AuthMiddleware) HTTPMiddleware(next http.Handler) http.Handler

HTTPMiddleware provides JWT authentication for HTTP requests

func (*AuthMiddleware) StreamServerInterceptor

func (a *AuthMiddleware) StreamServerInterceptor() grpc.StreamServerInterceptor

StreamServerInterceptor provides JWT authentication for streaming gRPC calls

func (*AuthMiddleware) UnaryServerInterceptor

func (a *AuthMiddleware) UnaryServerInterceptor() grpc.UnaryServerInterceptor

UnaryServerInterceptor provides JWT authentication for unary gRPC calls

type JWTClaims

type JWTClaims struct {
	UserID      int64    `json:"userId"`
	UserUUID    string   `json:"userUUID"`
	SessionUUID string   `json:"sessionUUID"`
	DeviceID    string   `json:"deviceId,omitempty"`
	Permissions []string `json:"permissions,omitempty"`
	jwt.RegisteredClaims
}

Token claims structure

func GetUserFromContext

func GetUserFromContext(ctx context.Context) (*JWTClaims, error)

GetUserFromContext extracts user claims from context

func ParseAndValidateToken

func ParseAndValidateToken(tokenString, jwtSecret string) (*JWTClaims, error)

ParseAndValidateToken parses and validates a JWT token

func (*JWTClaims) HasPermission

func (c *JWTClaims) HasPermission(permission string) bool

type JWT_CLAIM_KEY

type JWT_CLAIM_KEY string
const (
	JWT_CLAIM_KEY_USER JWT_CLAIM_KEY = "user_claims"
)

Jump to

Keyboard shortcuts

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