Documentation
¶
Index ¶
- Variables
- func GetUserID(r *http.Request) (uuid.UUID, error)
- type AuthContextKey
- type Config
- type FullToken
- type Middleware
- type MockTokenRepository
- func (m *MockTokenRepository) DeleteExpiredTokens(ctx context.Context, userID uuid.UUID) error
- func (m *MockTokenRepository) DeleteUserTokens(ctx context.Context, userID uuid.UUID) error
- func (m *MockTokenRepository) GetToken(ctx context.Context, userID uuid.UUID, refreshToken string) (TokenInfo, error)
- func (m *MockTokenRepository) GetTokens(ctx context.Context, userID uuid.UUID) ([]repository.GetSessionsRow, error)
- func (m *MockTokenRepository) RevokeToken(ctx context.Context, id uuid.UUID) error
- func (m *MockTokenRepository) SaveToken(ctx context.Context, session SessionInfo, refreshToken string, ...) error
- func (m *MockTokenRepository) UpdateTokenLastUsed(ctx context.Context, tokenID uuid.UUID) error
- type SQLCTokenRepository
- func (r *SQLCTokenRepository) DeleteExpiredTokens(ctx context.Context, userID uuid.UUID) error
- func (r *SQLCTokenRepository) DeleteUserTokens(ctx context.Context, userID uuid.UUID) error
- func (r *SQLCTokenRepository) GetToken(ctx context.Context, userID uuid.UUID, refreshToken string) (TokenInfo, error)
- func (r *SQLCTokenRepository) GetTokens(ctx context.Context, userID uuid.UUID) ([]repository.GetSessionsRow, error)
- func (r *SQLCTokenRepository) RevokeToken(ctx context.Context, id uuid.UUID) error
- func (r *SQLCTokenRepository) SaveToken(ctx context.Context, session SessionInfo, refreshToken string, ...) error
- func (r *SQLCTokenRepository) UpdateTokenLastUsed(ctx context.Context, tokenID uuid.UUID) error
- type Service
- func (s *Service) GenerateTokenPair(ctx context.Context, sessionInfo SessionInfo) (*TokenPair, error)
- func (s *Service) GetSessions(ctx context.Context, userID uuid.UUID) ([]repository.GetSessionsRow, error)
- func (s *Service) InvalidateTokens(ctx context.Context, userID uuid.UUID) error
- func (s *Service) RefreshAccessToken(ctx context.Context, session SessionInfo, refreshToken string) (*TokenPair, error)
- func (s *Service) RevokeSessions(ctx context.Context, id uuid.UUID) error
- func (s *Service) StoreRefreshToken(ctx context.Context, session SessionInfo, refreshToken string) error
- func (s *Service) VerifyAccessToken(tokenString string) (jwt.MapClaims, error)
- type SessionInfo
- type TokenInfo
- type TokenPair
- type TokenRepository
- type TokenType
Constants ¶
This section is empty.
Variables ¶
Functions ¶
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
type Middleware ¶
type Middleware struct {
// contains filtered or unexported fields
}
func NewMiddleware ¶
func NewMiddleware(service *Service) *Middleware
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 ¶
DeleteExpiredTokens removes expired tokens from the mock repository
func (*MockTokenRepository) DeleteUserTokens ¶
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 (*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 ¶
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 ¶
DeleteExpiredTokens removes expired tokens from the database
func (*SQLCTokenRepository) DeleteUserTokens ¶
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 (*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 ¶
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 (*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 (*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 ¶
VerifyAccessToken validates an access token and returns its claims
type SessionInfo ¶
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 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