package store import ( "errors" "strconv" "time" ) const DefaultAuthSessionMinutes = 24 * 60 const MaxAuthSessionMinutes = 30 * 24 * 60 // Absolute lifetime, fixed at issuance. Changing the policy cannot silently // revive, extend or invalidate existing sessions. App passwords are separate. func (s *Store) AuthSessionLifetimeMinutes(domain string) int { raw, err := s.GetConfigInDomain(domain, "auth_session_minutes") value, parseErr := strconv.Atoi(raw) if err != nil || parseErr != nil || value < 5 || value > MaxAuthSessionMinutes { return DefaultAuthSessionMinutes } return value } func (s *Store) SetAuthSessionLifetimeMinutes(domain string, minutes int) error { if minutes < 5 || minutes > MaxAuthSessionMinutes { return errors.New("session lifetime must be between 5 and 43200 minutes") } return s.SetConfigInDomain(domain, "auth_session_minutes", strconv.Itoa(minutes)) } func (s *Store) AuthSessionLifetime(domain string) time.Duration { return time.Duration(s.AuthSessionLifetimeMinutes(domain)) * time.Minute } // Opt-in persistent sessions have a separate, administrator-controlled ceiling. // Zero disables the option. Existing sessions retain their original expiry. func (s *Store) TrustedSessionDays(domain string) int { raw, err := s.GetConfigInDomain(domain, "auth_trusted_session_days") if err != nil { return 0 } if raw == "" { return 30 } days, err := strconv.Atoi(raw) if err != nil || days < 0 || days > 30 { return 0 } return days } func (s *Store) LoginSessionLifetime(domain string, remember bool) time.Duration { if days := s.TrustedSessionDays(domain); remember && days > 0 { return time.Duration(days) * 24 * time.Hour } return s.AuthSessionLifetime(domain) } func (s *Store) CreateLoginSession(domain, principal string, mfa int, browser, remember bool) (string, time.Duration, error) { lifetime := s.LoginSessionLifetime(domain, remember) prefix := "pw_api_" if browser { prefix = "pw_browser_" } token, err := s.createTypedAuthSessionForLifetime(domain, principal, mfa, prefix, lifetime) return token, lifetime, err }