Documentation
¶
Overview ¶
Package session provides the business logic for managing user sessions in happyDomain. It exposes a Service that handles the full session lifecycle: creation, retrieval, update, and deletion, as well as bulk operations such as closing all sessions for a given user.
The package defines the SessionStorage interface that any persistence backend must implement. A concrete implementation is injected at construction time via NewService, keeping this layer free of storage concerns.
Session identifiers are randomly generated, base32-encoded strings (see NewSessionID). Sessions carry an expiry timestamp and are automatically bound to a single user — cross-user access is rejected at the use-case level.
Typical usage:
svc := session.NewService(myStorageBackend)
sess, err := svc.CreateUserSession(user, "browser login")
// … store sess.Id in a cookie …
sess, err = svc.GetUserSession(user, sessionID)
err = svc.UpdateUserSession(user, sessionID, func(s *happydns.Session) {
s.Description = "renamed"
})
err = svc.DeleteUserSession(user, sessionID)
err = svc.CloseUserSessions(user) // invalidate all sessions at once
Index ¶
- Constants
- func NewSessionID() string
- type Service
- func (s *Service) ByID(userID happydns.Identifier) error
- func (s *Service) CloseAll(user happydns.UserInfo) error
- func (s *Service) CloseInteractive(user happydns.UserInfo) error
- func (s *Service) CloseUserSessions(user *happydns.User) error
- func (s *Service) CreateUserSession(user *happydns.User, description string) (*happydns.Session, error)
- func (s *Service) DeleteUserSession(user *happydns.User, sessionID string) error
- func (s *Service) GetUserSession(user *happydns.User, sessionID string) (*happydns.Session, error)
- func (s *Service) ListUserSessions(user *happydns.User) ([]*happydns.Session, error)
- func (s *Service) UpdateUserSession(user *happydns.User, sessionID string, updateFunc func(sess *happydns.Session)) error
- type SessionStorage
Constants ¶
SESSION_MAX_DURATION is the lifetime assigned to every newly created session.
SESSION_RENEWAL_THRESHOLD is the remaining lifetime below which a session is automatically renewed to SESSION_MAX_DURATION on the next request.
const SessionIDLen = (sessionIDKeyLen*8 + 4) / 5
SessionIDLen is the length of a session ID string (base32, no padding).
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service handles all session-related operations for happyDomain users. It relies on a SessionStorage backend for persistence and enforces ownership checks so that one user can never read or modify another user's sessions.
func NewService ¶ added in v0.5.0
func NewService(store SessionStorage) *Service
NewService creates a new session Service backed by the given store.
func (*Service) ByID ¶ added in v0.5.0
ByID deletes all sessions for the user identified by userID. It satisfies the SessionCloserUsecase interface, allowing callers that only have a user identifier to invalidate all of that user's sessions without constructing a full [happydns.User] value.
func (*Service) CloseAll ¶ added in v0.5.0
CloseAll deletes all sessions for user. It satisfies the SessionCloserUsecase interface and accepts the broader [happydns.UserInfo] type so callers are not required to hold a full [happydns.User] value.
func (*Service) CloseInteractive ¶ added in v0.7.2
CloseInteractive deletes the sessions user opened through the web interface, keeping the machine sessions, created through the API to be used as long lived tokens, alive. It satisfies the SessionCloserUsecase interface.
func (*Service) CloseUserSessions ¶
CloseUserSessions deletes all sessions belonging to user. Errors from individual deletions are collected and returned as a combined error so that a single failure does not prevent the remaining sessions from being removed.
func (*Service) CreateUserSession ¶
func (s *Service) CreateUserSession(user *happydns.User, description string) (*happydns.Session, error)
CreateUserSession creates and persists a new session for user. The session is assigned a random identifier, the current time as its issue date, and an expiry of SESSION_MAX_DURATION from now. description is a human-readable label that the user can use to identify the session (e.g. "my backup script").
Sessions created here are machine sessions: they are meant to be used as long lived tokens by scripts and third party tools, and are therefore not revoked when the user changes their password. Interactive sessions are created by the session store during login instead.
func (*Service) DeleteUserSession ¶
DeleteUserSession removes the session identified by sessionID. The session must belong to user; an attempt to delete another user's session returns an error and leaves the session untouched.
func (*Service) GetUserSession ¶
GetUserSession retrieves the session identified by sessionID and verifies that it belongs to user. Returns [happydns.ErrSessionNotFound] if the session does not exist or belongs to a different user.
func (*Service) ListUserSessions ¶
ListUserSessions returns all active sessions belonging to user.
func (*Service) UpdateUserSession ¶
func (s *Service) UpdateUserSession( user *happydns.User, sessionID string, updateFunc func(sess *happydns.Session), ) error
UpdateUserSession applies updateFunc to the session identified by sessionID and persists the result. The session must belong to user; otherwise an error is returned. The function sets ModifiedOn automatically. Attempting to change the session ID inside updateFunc is rejected with an error.
type SessionStorage ¶
type SessionStorage interface {
// ListAllSessions retrieves the list of known Sessions.
ListAllSessions() (happydns.Iterator[happydns.Session], error)
// GetSession retrieves the Session with the given identifier.
GetSession(sessionid string) (*happydns.Session, error)
// ListAuthUserSessions retrieves all Session for the given AuthUser.
ListAuthUserSessions(user *happydns.UserAuth) ([]*happydns.Session, error)
// ListUserSessions retrieves all Session for the given User.
ListUserSessions(userid happydns.Identifier) ([]*happydns.Session, error)
// UpdateSession updates the fields of the given Session.
UpdateSession(session *happydns.Session) error
// DeleteSession removes the given Session from the database.
DeleteSession(sessionid string) error
// ClearSessions deletes all Sessions present in the database.
ClearSessions() error
}
SessionStorage is the persistence interface required by Service. Any storage backend that implements these methods can be injected via NewService.
Source Files
¶
- doc.go
- session.go
- storage.go