jwt

package
v0.0.1-0...-104a2d1 Latest Latest
Warning

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

Go to latest
Published: Jun 9, 2025 License: AGPL-3.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUnauthorized     = errors.New("unauthorized")
	ErrTokenExpired     = errors.New("token expired")
	ErrInvalidToken     = errors.New("invalid token")
	ErrNoTokenFound     = errors.New("no token found")
	ErrFailedTokenGen   = errors.New("failed to generate token")
	ErrFailedTokenStore = errors.New("failed to store token")
)

Functions

func GetUserID

func GetUserID(r *http.Request) (uuid.UUID, error)

Types

type AuthContextKey

type AuthContextKey string
var ContextKey AuthContextKey = "user"

Store user information in the request context

type Config

type Config struct {
	AccessTokenDuration  time.Duration
	RefreshTokenDuration time.Duration
	SigningKey           string
}

Config holds JWT configuration

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns a default configuration

type FullToken

type FullToken struct {
	ID         uuid.UUID
	LastUsedAt time.Time
}

type Middleware

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

func NewMiddleware

func NewMiddleware(service *Service) *Middleware

func (*Middleware) Verify

func (m *Middleware) Verify(next http.Handler) http.Handler

Verify authenticates requests using JWT tokens

type MockTokenRepository

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

MockTokenRepository implements TokenRepository for testing

func NewMockTokenRepository

func NewMockTokenRepository() *MockTokenRepository

NewMockTokenRepository creates a new mock repository

func (*MockTokenRepository) DeleteExpiredTokens

func (m *MockTokenRepository) DeleteExpiredTokens(ctx context.Context, userID uuid.UUID) error

DeleteExpiredTokens removes expired tokens from the mock repository

func (*MockTokenRepository) DeleteUserTokens

func (m *MockTokenRepository) DeleteUserTokens(ctx context.Context, userID uuid.UUID) error

DeleteUserTokens removes all tokens for a user

func (*MockTokenRepository) GetToken

func (m *MockTokenRepository) GetToken(ctx context.Context, userID uuid.UUID, refreshToken string) (TokenInfo, error)

GetToken retrieves a token from the mock repository

func (*MockTokenRepository) GetTokens

func (m *MockTokenRepository) GetTokens(ctx context.Context, userID uuid.UUID) ([]repository.GetSessionsRow, error)

func (*MockTokenRepository) RevokeToken

func (m *MockTokenRepository) RevokeToken(ctx context.Context, id uuid.UUID) error

func (*MockTokenRepository) SaveToken

func (m *MockTokenRepository) SaveToken(ctx context.Context, session SessionInfo, refreshToken string, expiresAt time.Time) error

SaveToken stores a token in the mock repository

func (*MockTokenRepository) UpdateTokenLastUsed

func (m *MockTokenRepository) UpdateTokenLastUsed(ctx context.Context, tokenID uuid.UUID) error

UpdateTokenLastUsed updates the last used timestamp of a token

type SQLCTokenRepository

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

SQLCTokenRepository adapts the sqlc-generated queries to implement the TokenRepository interface

func NewSQLCTokenRepository

func NewSQLCTokenRepository(queries *repository.Queries) *SQLCTokenRepository

NewSQLCTokenRepository creates a new repository adapter for sqlc-generated queries

func (*SQLCTokenRepository) DeleteExpiredTokens

func (r *SQLCTokenRepository) DeleteExpiredTokens(ctx context.Context, userID uuid.UUID) error

DeleteExpiredTokens removes expired tokens from the database

func (*SQLCTokenRepository) DeleteUserTokens

func (r *SQLCTokenRepository) DeleteUserTokens(ctx context.Context, userID uuid.UUID) error

DeleteUserTokens removes all tokens for a user

func (*SQLCTokenRepository) GetToken

func (r *SQLCTokenRepository) GetToken(ctx context.Context, userID uuid.UUID, refreshToken string) (TokenInfo, error)

GetToken retrieves a token from the database

func (*SQLCTokenRepository) GetTokens

func (r *SQLCTokenRepository) GetTokens(ctx context.Context, userID uuid.UUID) ([]repository.GetSessionsRow, error)

func (*SQLCTokenRepository) RevokeToken

func (r *SQLCTokenRepository) RevokeToken(ctx context.Context, id uuid.UUID) error

func (*SQLCTokenRepository) SaveToken

func (r *SQLCTokenRepository) SaveToken(ctx context.Context, session SessionInfo, refreshToken string, expiresAt time.Time) error

SaveToken stores a token in the database

func (*SQLCTokenRepository) UpdateTokenLastUsed

func (r *SQLCTokenRepository) UpdateTokenLastUsed(ctx context.Context, tokenID uuid.UUID) error

UpdateTokenLastUsed updates the last used timestamp of a token

type Service

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

Service manages JWT token operations

func NewService

func NewService(repo TokenRepository, config Config, logger *zerolog.Logger) *Service

NewService creates a new token service

func (*Service) GenerateTokenPair

func (s *Service) GenerateTokenPair(ctx context.Context, sessionInfo SessionInfo) (*TokenPair, error)

GenerateTokenPair creates new access and refresh tokens

func (*Service) GetSessions

func (s *Service) GetSessions(ctx context.Context, userID uuid.UUID) ([]repository.GetSessionsRow, error)

func (*Service) InvalidateTokens

func (s *Service) InvalidateTokens(ctx context.Context, userID uuid.UUID) error

func (*Service) RefreshAccessToken

func (s *Service) RefreshAccessToken(ctx context.Context, session SessionInfo, refreshToken string) (*TokenPair, error)

RefreshAccessToken validates a refresh token and issues a new token pair

func (*Service) RevokeSessions

func (s *Service) RevokeSessions(ctx context.Context, id uuid.UUID) error

func (*Service) StoreRefreshToken

func (s *Service) StoreRefreshToken(ctx context.Context, session SessionInfo, refreshToken string) error

storeRefreshToken saves the refresh token to the repository

func (*Service) VerifyAccessToken

func (s *Service) VerifyAccessToken(tokenString string) (jwt.MapClaims, error)

VerifyAccessToken validates an access token and returns its claims

type SessionInfo

type SessionInfo struct {
	UserID      uuid.UUID
	Roles       []string
	UserAgent   *string
	IpAddress   *string
	Location    *string
	BrowserName *string
	DeviceName  *string
	OsName      *string
}

type TokenInfo

type TokenInfo struct {
	ID           uuid.UUID
	UserID       uuid.UUID
	RefreshToken string
	ExpiresAt    time.Time
	LastUsedAt   time.Time
}

TokenInfo represents a stored token

type TokenPair

type TokenPair struct {
	AccessToken  string
	RefreshToken string
}

TokenPair contains access and refresh tokens

type TokenRepository

type TokenRepository interface {
	SaveToken(ctx context.Context, session SessionInfo, refreshToken string, expiresAt time.Time) error
	UpdateTokenLastUsed(ctx context.Context, tokenID uuid.UUID) error

	GetToken(ctx context.Context, userID uuid.UUID, refreshToken string) (TokenInfo, error)
	GetTokens(ctx context.Context, userID uuid.UUID) ([]repository.GetSessionsRow, error)

	DeleteExpiredTokens(ctx context.Context, userID uuid.UUID) error
	DeleteUserTokens(ctx context.Context, userID uuid.UUID) error
	RevokeToken(ctx context.Context, tokenID uuid.UUID) error
}

TokenRepository defines the interface for token storage

type TokenType

type TokenType string

TokenType represents different token types

const (
	AccessToken  TokenType = "access"
	RefreshToken TokenType = "refresh"
)

Jump to

Keyboard shortcuts

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