Documentation
¶
Overview ¶
Package session owns the Issue and Validate primitives for opaque session tokens. Only the sha256 hash is persisted; the raw token is returned exactly once.
Index ¶
- Constants
- func DeviceLabel(ua string) string
- func IPPrefix(ip string) string
- func WithoutLimits(ctx context.Context) context.Context
- type AccountGate
- type AuthAppender
- type Cap
- type CredentialChecker
- type IssueOptions
- type Policy
- type Revoker
- type Service
- func (s *Service) Append(ctx context.Context, id models.ULID, at time.Time, methods ...string) ([]string, time.Time, error)
- func (s *Service) CapRoom(ctx context.Context, userID, exclude models.ULID) error
- func (s *Service) Check(ctx context.Context, token string) (*models.Session, *models.User, error)
- func (s *Service) CheckAccount(ctx context.Context, userID models.ULID) error
- func (s *Service) EnforceCap(ctx context.Context, userID, exclude models.ULID) error
- func (s *Service) Issue(ctx context.Context, user models.User, userAgent, ip string) (token string, sess models.Session, err error)
- func (s *Service) IssueWith(ctx context.Context, user models.User, opts IssueOptions) (token string, sess models.Session, err error)
- func (s *Service) Now() time.Time
- func (s *Service) SetAccountGate(g AccountGate)
- func (s *Service) SetCap(c Cap)
- func (s *Service) SetClock(now func() time.Time)
- func (s *Service) SetHooks(onIssue func(context.Context, models.Session), ...)
- func (s *Service) SetPolicy(p Policy)
- func (s *Service) Validate(ctx context.Context, token string) (*models.Session, *models.User, error)
- type SessionLister
- type SessionUserStorage
- type Storage
- type Toucher
Constants ¶
const ( // OverflowEvictOldest revokes the user's oldest sessions to make room. OverflowEvictOldest = "evict_oldest" // OverflowReject refuses the new session. OverflowReject = "reject" )
Overflow values for Cap.Mode.
Variables ¶
This section is empty.
Functions ¶
func DeviceLabel ¶
DeviceLabel derives a short "Browser on OS" label from a User-Agent string. It returns "Unknown device" when nothing recognizable is present.
Types ¶
type AccountGate ¶ added in v2.10.0
AccountGate vetoes issuing a session for a user, for example because the account is disabled or must change its password. It runs after the credential was verified, so its error may be specific.
type AuthAppender ¶ added in v2.11.0
type AuthAppender interface {
AppendSessionAuthMethods(ctx context.Context, id models.ULID, methods []string, at time.Time) ([]string, time.Time, error)
}
AuthAppender is the storage capability Append needs; it is satisfied by theauth.SessionAuthStorage.
type Cap ¶ added in v2.10.0
type Cap struct {
Max int
Mode string
Lister SessionLister
Revoker Revoker
}
Cap limits how many full sessions one user may hold at once. The zero value disables the cap. Enforcement is best effort under concurrency: two simultaneous sign-ins may both pass the check.
type CredentialChecker ¶
type CredentialChecker interface {
CheckCredential(ctx context.Context, credentialID string) error
}
CredentialChecker reports whether the upstream credential a session is tied to is still valid. It returns models.ErrCredentialRevoked when it is not; any other error is treated as transient and fails the validation closed.
type IssueOptions ¶
type IssueOptions struct {
UserAgent string
IP string
CredentialID string
// TTL overrides the service default when positive.
TTL time.Duration
}
IssueOptions customizes Service.IssueWith.
type Policy ¶
type Policy struct {
IdleTimeout time.Duration
TouchInterval time.Duration
Toucher Toucher
Revoker Revoker
Credentials CredentialChecker
}
Policy configures idle timeout, last-seen throttling and credential re-checks. The zero value keeps the original absolute-TTL-only behavior.
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service holds the dependencies needed to issue and validate sessions. Constructed once in theauth.New and reused for the lifetime of the process.
func (*Service) Append ¶ added in v2.11.0
func (s *Service) Append(ctx context.Context, id models.ULID, at time.Time, methods ...string) ([]string, time.Time, error)
Append atomically adds methods to the session's record and advances its AuthTime to at (now when zero). It returns the resulting record. When the storage lacks the capability it is a no-op returning nil, so third party storages keep working without a record.
func (*Service) CapRoom ¶ added in v2.10.0
CapRoom returns CodeSessionLimit when the cap is in reject mode and userID is already at it, without revoking anything. Callers use it before spending a one-time credential (a recovery code) so a refusal does not burn it.
func (*Service) Check ¶
Check is Validate without the last-seen update, for liveness probes such as long-lived stream re-checks that must not count as user activity.
func (*Service) CheckAccount ¶ added in v2.10.0
CheckAccount runs the account gate for userID. It returns nil when no gate is set.
func (*Service) EnforceCap ¶ added in v2.10.0
EnforceCap makes room for one more full session of userID, ignoring the session exclude (use the zero ULID for none). With OverflowEvictOldest it revokes the oldest sessions; with OverflowReject it returns CodeSessionLimit.
func (*Service) Issue ¶
func (s *Service) Issue(ctx context.Context, user models.User, userAgent, ip string) (token string, sess models.Session, err error)
Issue mints a fresh opaque token, stores its sha256 hash with a new Session row, and returns the raw token. The raw token is what the caller puts in a cookie / sends to the user; the hash is what's persisted.
func (*Service) IssueWith ¶
func (s *Service) IssueWith(ctx context.Context, user models.User, opts IssueOptions) (token string, sess models.Session, err error)
IssueWith is Issue with per-session options.
func (*Service) SetAccountGate ¶ added in v2.10.0
func (s *Service) SetAccountGate(g AccountGate)
SetAccountGate installs the gate consulted before every full session is issued. Call before the Service is shared.
func (*Service) SetCap ¶ added in v2.10.0
SetCap installs the concurrent session cap. Call before the Service is shared.
func (*Service) SetHooks ¶ added in v2.14.0
func (s *Service) SetHooks(onIssue func(context.Context, models.Session), onUse func(context.Context, *models.Session))
SetHooks installs callbacks run after a full session is issued and after a session authenticates a request. Call before the Service is shared.
func (*Service) Validate ¶
func (s *Service) Validate(ctx context.Context, token string) (*models.Session, *models.User, error)
Validate looks up a session by the hash of the supplied token, verifies it is not expired or revoked, and returns the session and its user. Returns models.ErrInvalidToken for missing/unknown tokens and models.ErrSessionExpired for expired or revoked sessions.
type SessionLister ¶ added in v2.10.0
type SessionLister interface {
ListUserSessions(ctx context.Context, userID models.ULID) ([]models.Session, error)
}
SessionLister lists a user's live sessions, newest first.
type SessionUserStorage ¶ added in v2.8.0
type SessionUserStorage interface {
SessionAndUserByTokenHash(ctx context.Context, hash []byte) (*models.Session, *models.User, error)
}
SessionUserStorage is an optional Storage extension that returns a session and its user together. Adapters that can do it in one round trip (a join) implement it; validate then skips the separate user lookup.
type Storage ¶
type Storage interface {
CreateSession(ctx context.Context, s models.Session) (models.Session, error)
SessionByTokenHash(ctx context.Context, hash []byte) (*models.Session, error)
UserByID(ctx context.Context, id models.ULID) (*models.User, error)
}
Storage is the minimal persistence subset this package needs. Declared here (not imported from root) so that internal/session does not import github.com/glincker/theauth-go/v2, which would form an import cycle with the root constructor.
Any type satisfying these three methods (notably the root theauth.Storage interface) is a valid argument to New. Duck-typed against the larger root contract by design.