Documentation
¶
Overview ¶
Package jwt provides JWT token generation and validation for the application.
The module covers:
- token generation with HS256 algorithm,
- token validation and claims extraction,
- 24-hour token expiry,
- integration with the users domain.
Index ¶
- Variables
- func Module() fx.Option
- type Claims
- type Config
- type Repository
- func (r *Repository) Create(ctx context.Context, userID int64, tokenHash string, expiresAt time.Time) error
- func (r *Repository) FindByHash(ctx context.Context, tokenHash string) (*Token, error)
- func (r *Repository) RevokeAllForUser(ctx context.Context, userID int64) error
- func (r *Repository) RevokeByHash(ctx context.Context, tokenHash string) error
- type Service
- func (s *Service) GenerateRefreshToken(ctx context.Context, userID int64) (string, error)
- func (s *Service) GenerateTokenPair(ctx context.Context, user *users.User) (string, string, error)
- func (s *Service) RevokeRefreshToken(ctx context.Context, rawToken string) error
- func (s *Service) RotateRefreshToken(ctx context.Context, oldRawToken string, userID int64) (string, error)
- func (s *Service) RotateTokenPair(ctx context.Context, oldRawToken string, user *users.User) (string, string, error)
- func (s *Service) ValidateRefreshToken(ctx context.Context, rawToken string) (int64, error)
- func (s *Service) ValidateToken(tokenString string) (*Claims, error)
- type Token
Constants ¶
This section is empty.
Variables ¶
Functions ¶
Types ¶
type Claims ¶
type Claims struct {
jwt.RegisteredClaims
UserID int64 `json:"user_id"`
Role users.Role `json:"role"`
Status users.Status `json:"status"`
}
Claims represents the JWT token claims.
type Repository ¶ added in v0.1.0
type Repository struct {
// contains filtered or unexported fields
}
func NewRepository ¶ added in v0.1.0
func NewRepository(db *bun.DB) *Repository
func (*Repository) FindByHash ¶ added in v0.1.0
func (*Repository) RevokeAllForUser ¶ added in v0.1.0
func (r *Repository) RevokeAllForUser(ctx context.Context, userID int64) error
func (*Repository) RevokeByHash ¶ added in v0.1.0
func (r *Repository) RevokeByHash(ctx context.Context, tokenHash string) error
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service handles JWT token operations.
func NewService ¶
func NewService(config Config, refreshRepo *Repository) *Service
NewService creates and initializes a new JWT service.
func (*Service) GenerateRefreshToken ¶ added in v0.1.0
GenerateRefreshToken creates an opaque random refresh token, stores its hash in the database, and returns the raw token string.
func (*Service) GenerateTokenPair ¶ added in v0.1.0
GenerateTokenPair creates a new access token and refresh token pair.
func (*Service) RevokeRefreshToken ¶ added in v0.1.0
RevokeRefreshToken revokes a raw refresh token.
func (*Service) RotateRefreshToken ¶ added in v0.1.0
func (s *Service) RotateRefreshToken(ctx context.Context, oldRawToken string, userID int64) (string, error)
RotateRefreshToken revokes the old token and generates a new one. Returns the new raw refresh token.
func (*Service) RotateTokenPair ¶ added in v0.1.0
func (s *Service) RotateTokenPair(ctx context.Context, oldRawToken string, user *users.User) (string, string, error)
RotateTokenPair revokes the old refresh token and generates a new access token + refresh token pair.
func (*Service) ValidateRefreshToken ¶ added in v0.1.0
ValidateRefreshToken validates a raw refresh token. Returns the user ID if valid.