Documentation
¶
Index ¶
- Variables
- func HashToken(token string) string
- func RefreshTokenNotFoundError(id shared.ID) error
- func SessionNotFoundError(id shared.ID) error
- type RefreshToken
- func NewRefreshToken(userID shared.ID, sessionID shared.ID, token string, duration time.Duration) (*RefreshToken, error)
- func NewRefreshTokenInFamily(userID shared.ID, sessionID shared.ID, token string, family shared.ID, ...) (*RefreshToken, error)
- func ReconstituteRefreshToken(id shared.ID, userID shared.ID, sessionID shared.ID, tokenHash string, ...) *RefreshToken
- func (rt *RefreshToken) CreatedAt() time.Time
- func (rt *RefreshToken) ExpiresAt() time.Time
- func (rt *RefreshToken) Family() shared.ID
- func (rt *RefreshToken) ID() shared.ID
- func (rt *RefreshToken) IsExpired() bool
- func (rt *RefreshToken) IsRevoked() bool
- func (rt *RefreshToken) IsUsed() bool
- func (rt *RefreshToken) IsValid() bool
- func (rt *RefreshToken) MarkUsed() error
- func (rt *RefreshToken) Revoke() error
- func (rt *RefreshToken) RevokedAt() *time.Time
- func (rt *RefreshToken) SessionID() shared.ID
- func (rt *RefreshToken) TokenHash() string
- func (rt *RefreshToken) UsedAt() *time.Time
- func (rt *RefreshToken) UserID() shared.ID
- func (rt *RefreshToken) VerifyToken(token string) bool
- type RefreshTokenRepository
- type Repository
- type Session
- func New(userID shared.ID, accessToken string, ipAddress string, userAgent string, ...) (*Session, error)
- func NewWithID(id shared.ID, userID shared.ID, accessToken string, ipAddress string, ...) (*Session, error)
- func Reconstitute(id shared.ID, userID shared.ID, accessTokenHash string, ipAddress string, ...) *Session
- func (s *Session) AccessTokenHash() string
- func (s *Session) CreatedAt() time.Time
- func (s *Session) DeviceFingerprint() string
- func (s *Session) Expire() error
- func (s *Session) ExpiresAt() time.Time
- func (s *Session) ID() shared.ID
- func (s *Session) IPAddress() string
- func (s *Session) IsActive() bool
- func (s *Session) IsExpired() bool
- func (s *Session) LastActivityAt() time.Time
- func (s *Session) Revoke() error
- func (s *Session) SetDeviceFingerprint(fingerprint string)
- func (s *Session) Status() Status
- func (s *Session) UpdateActivity()
- func (s *Session) UpdatedAt() time.Time
- func (s *Session) UserAgent() string
- func (s *Session) UserID() shared.ID
- func (s *Session) VerifyToken(token string) bool
- type Status
Constants ¶
This section is empty.
Variables ¶
var ( ErrSessionNotFound = errors.New("session not found") ErrSessionExpired = errors.New("session has expired") ErrSessionRevoked = errors.New("session has been revoked") ErrRefreshTokenNotFound = errors.New("refresh token not found") ErrRefreshTokenExpired = errors.New("refresh token has expired") ErrRefreshTokenUsed = errors.New("refresh token has already been used") ErrRefreshTokenRevoked = errors.New("refresh token has been revoked") ErrTokenFamilyMismatch = errors.New("refresh token family mismatch (possible replay attack)") ErrMaxSessionsReached = errors.New("maximum number of active sessions reached") ErrInvalidToken = errors.New("invalid token") )
Domain errors for session operations.
Functions ¶
func RefreshTokenNotFoundError ¶
RefreshTokenNotFoundError returns a refresh token not found error.
func SessionNotFoundError ¶
SessionNotFoundError returns a session not found error with ID.
Types ¶
type RefreshToken ¶
type RefreshToken struct {
// contains filtered or unexported fields
}
RefreshToken represents a refresh token for session renewal. Implements token rotation with family tracking for replay attack detection.
func NewRefreshToken ¶
func NewRefreshToken( userID shared.ID, sessionID shared.ID, token string, duration time.Duration, ) (*RefreshToken, error)
NewRefreshToken creates a new refresh token.
func NewRefreshTokenInFamily ¶
func NewRefreshTokenInFamily( userID shared.ID, sessionID shared.ID, token string, family shared.ID, duration time.Duration, ) (*RefreshToken, error)
NewRefreshTokenInFamily creates a new refresh token in an existing family (rotation).
func ReconstituteRefreshToken ¶
func ReconstituteRefreshToken( id shared.ID, userID shared.ID, sessionID shared.ID, tokenHash string, family shared.ID, expiresAt time.Time, usedAt *time.Time, revokedAt *time.Time, createdAt time.Time, ) *RefreshToken
ReconstituteRefreshToken creates a refresh token from persisted data.
func (*RefreshToken) CreatedAt ¶
func (rt *RefreshToken) CreatedAt() time.Time
CreatedAt returns when the token was created.
func (*RefreshToken) ExpiresAt ¶
func (rt *RefreshToken) ExpiresAt() time.Time
ExpiresAt returns when the token expires.
func (*RefreshToken) Family ¶
func (rt *RefreshToken) Family() shared.ID
Family returns the token family ID.
func (*RefreshToken) IsExpired ¶
func (rt *RefreshToken) IsExpired() bool
IsExpired returns true if the token has expired.
func (*RefreshToken) IsRevoked ¶
func (rt *RefreshToken) IsRevoked() bool
IsRevoked returns true if the token has been revoked.
func (*RefreshToken) IsUsed ¶
func (rt *RefreshToken) IsUsed() bool
IsUsed returns true if the token has been used.
func (*RefreshToken) IsValid ¶
func (rt *RefreshToken) IsValid() bool
IsValid returns true if the token is valid (not expired, used, or revoked).
func (*RefreshToken) MarkUsed ¶
func (rt *RefreshToken) MarkUsed() error
MarkUsed marks the token as used.
func (*RefreshToken) Revoke ¶
func (rt *RefreshToken) Revoke() error
Revoke marks the token as revoked.
func (*RefreshToken) RevokedAt ¶
func (rt *RefreshToken) RevokedAt() *time.Time
RevokedAt returns when the token was revoked.
func (*RefreshToken) SessionID ¶
func (rt *RefreshToken) SessionID() shared.ID
SessionID returns the associated session ID.
func (*RefreshToken) TokenHash ¶
func (rt *RefreshToken) TokenHash() string
TokenHash returns the hash of the token.
func (*RefreshToken) UsedAt ¶
func (rt *RefreshToken) UsedAt() *time.Time
UsedAt returns when the token was used.
func (*RefreshToken) UserID ¶
func (rt *RefreshToken) UserID() shared.ID
UserID returns the user ID.
func (*RefreshToken) VerifyToken ¶
func (rt *RefreshToken) VerifyToken(token string) bool
VerifyToken verifies if the provided token matches this refresh token.
type RefreshTokenRepository ¶
type RefreshTokenRepository interface {
// Create creates a new refresh token.
Create(ctx context.Context, token *RefreshToken) error
// GetByID retrieves a refresh token by its ID.
GetByID(ctx context.Context, id shared.ID) (*RefreshToken, error)
// GetByTokenHash retrieves a refresh token by its hash.
GetByTokenHash(ctx context.Context, hash string) (*RefreshToken, error)
// GetByFamily retrieves all refresh tokens in a family.
GetByFamily(ctx context.Context, family shared.ID) ([]*RefreshToken, error)
// Update updates a refresh token.
Update(ctx context.Context, token *RefreshToken) error
// Delete deletes a refresh token.
Delete(ctx context.Context, id shared.ID) error
// RevokeByFamily revokes all tokens in a family (for replay attack detection).
RevokeByFamily(ctx context.Context, family shared.ID) error
// RevokeBySessionID revokes all tokens for a session.
RevokeBySessionID(ctx context.Context, sessionID shared.ID) error
// RevokeByUserID revokes all tokens for a user.
RevokeByUserID(ctx context.Context, userID shared.ID) error
// DeleteExpired deletes all expired tokens (for cleanup job).
DeleteExpired(ctx context.Context) (int64, error)
}
RefreshTokenRepository defines the interface for refresh token persistence.
type Repository ¶
type Repository interface {
// Create creates a new session.
Create(ctx context.Context, session *Session) error
// GetByID retrieves a session by its ID.
GetByID(ctx context.Context, id shared.ID) (*Session, error)
// GetByAccessTokenHash retrieves a session by access token hash.
GetByAccessTokenHash(ctx context.Context, hash string) (*Session, error)
// GetActiveByUserID retrieves all active sessions for a user.
GetActiveByUserID(ctx context.Context, userID shared.ID) ([]*Session, error)
// Update updates an existing session.
Update(ctx context.Context, session *Session) error
// Delete deletes a session.
Delete(ctx context.Context, id shared.ID) error
// RevokeAllByUserID revokes all sessions for a user.
RevokeAllByUserID(ctx context.Context, userID shared.ID) error
// RevokeAllByUserIDExcept revokes all sessions for a user except the specified session.
RevokeAllByUserIDExcept(ctx context.Context, userID shared.ID, exceptSessionID shared.ID) error
// CountActiveByUserID counts active sessions for a user.
CountActiveByUserID(ctx context.Context, userID shared.ID) (int, error)
// GetOldestActiveByUserID retrieves the oldest active session for a user.
// Returns nil if no active sessions exist.
GetOldestActiveByUserID(ctx context.Context, userID shared.ID) (*Session, error)
// DeleteExpired deletes all expired sessions (for cleanup job).
DeleteExpired(ctx context.Context) (int64, error)
}
Repository defines the interface for session persistence.
type Session ¶
type Session struct {
// contains filtered or unexported fields
}
Session represents an authentication session.
func New ¶
func New( userID shared.ID, accessToken string, ipAddress string, userAgent string, sessionDuration time.Duration, ) (*Session, error)
New creates a new session.
func NewWithID ¶
func NewWithID( id shared.ID, userID shared.ID, accessToken string, ipAddress string, userAgent string, sessionDuration time.Duration, ) (*Session, error)
NewWithID creates a new session entity with a pre-generated ID. Use this when you need the session ID before creating the session (e.g., for JWT).
func Reconstitute ¶
func Reconstitute( id shared.ID, userID shared.ID, accessTokenHash string, ipAddress string, userAgent string, deviceFingerprint string, expiresAt time.Time, lastActivityAt time.Time, status Status, createdAt time.Time, updatedAt time.Time, ) *Session
Reconstitute creates a session from persisted data.
func (*Session) AccessTokenHash ¶
AccessTokenHash returns the hash of the access token.
func (*Session) DeviceFingerprint ¶
DeviceFingerprint returns the device fingerprint.
func (*Session) LastActivityAt ¶
LastActivityAt returns the last activity time.
func (*Session) SetDeviceFingerprint ¶
SetDeviceFingerprint sets the device fingerprint.
func (*Session) UpdateActivity ¶
func (s *Session) UpdateActivity()
UpdateActivity updates the last activity time.
func (*Session) VerifyToken ¶
VerifyToken verifies if the provided token matches this session.