refactor: follow the google go style guide

Document every package and exported identifier, return the log level
from NewLogger instead of keeping it in a package variable, and stop
tests from mutating the argon2 parameters.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
Sebastian UnterschützandClaude Opus 5.5 committed 2026-11-22 00:18:02 +01:00
1 parent 048e114fd9
commit d20225982a
21 files changed
+147 -36

No files matched your search

+16
View File
@@ -1,3 +1,5 @@
// Package auth implements sessions, password and TOTP verification, rate
// limiting and CSRF protection.
package auth
import (
@@ -16,8 +18,11 @@ import (
"github.com/redis/go-redis/v9"
)
// ErrNoSession is returned for unknown, expired or malformed sessions.
var ErrNoSession = errors.New("auth: no session")
// Session is a signed-in browser. Token is the cookie value and never
// stored server side.
type Session struct {
Token string
UserID uuid.UUID
@@ -27,10 +32,12 @@ type Session struct {
CreatedAt time.Time
}
// CheckCSRF compares token with the session's CSRF token in constant time.
func (s *Session) CheckCSRF(token string) bool {
return token != "" && subtle.ConstantTimeCompare([]byte(token), []byte(s.CSRF)) == 1
}
// Sessions stores sessions in Redis so any API replica can serve any request.
type Sessions struct {
rdb redis.UniversalClient
idle time.Duration
@@ -38,6 +45,8 @@ type Sessions struct {
now func() time.Time
}
// NewSessions returns a store whose sessions expire after idle without use
// and after absolute regardless of use.
func NewSessions(rdb redis.UniversalClient, idle, absolute time.Duration) *Sessions {
return &Sessions{rdb: rdb, idle: idle, absolute: absolute, now: time.Now}
}
@@ -51,12 +60,14 @@ func key(token string) string {
func userIndex(id uuid.UUID) string { return "user_sessions:" + id.String() }
// RandomToken returns 256 bits of randomness, URL-safe encoded.
func RandomToken() string {
b := make([]byte, 32)
_, _ = rand.Read(b)
return base64.RawURLEncoding.EncodeToString(b)
}
// Create starts a session. org and locale may be empty.
func (s *Sessions) Create(ctx context.Context, user, org uuid.UUID, locale string) (*Session, error) {
sess := &Session{
Token: RandomToken(),
@@ -86,6 +97,7 @@ func (s *Sessions) Create(ctx context.Context, user, org uuid.UUID, locale strin
return sess, nil
}
// Get loads the session for token and extends its idle expiry.
func (s *Sessions) Get(ctx context.Context, token string) (*Session, error) {
if token == "" {
return nil, ErrNoSession
@@ -115,6 +127,8 @@ func (s *Sessions) Get(ctx context.Context, token string) (*Session, error) {
return sess, nil
}
// SwitchOrg changes the active organization. Membership must be checked
// by the caller.
func (s *Sessions) SwitchOrg(ctx context.Context, sess *Session, org uuid.UUID) error {
if err := s.rdb.HSet(ctx, key(sess.Token), "org", orgString(org)).Err(); err != nil {
return fmt.Errorf("auth: switch org: %w", err)
@@ -123,6 +137,7 @@ func (s *Sessions) SwitchOrg(ctx context.Context, sess *Session, org uuid.UUID)
return nil
}
// SetLocale changes the session language.
func (s *Sessions) SetLocale(ctx context.Context, sess *Session, locale string) error {
if err := s.rdb.HSet(ctx, key(sess.Token), "locale", locale).Err(); err != nil {
return fmt.Errorf("auth: set locale: %w", err)
@@ -131,6 +146,7 @@ func (s *Sessions) SetLocale(ctx context.Context, sess *Session, locale string)
return nil
}
// Delete ends sess.
func (s *Sessions) Delete(ctx context.Context, sess *Session) error {
k := key(sess.Token)
_, err := s.rdb.TxPipelined(ctx, func(p redis.Pipeliner) error {