From 30409697f7dd9f41b28c70ab67884e9c528e69ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Untersch=C3=BCtz?= Date: Sun, 22 Nov 2026 00:49:35 +0100 Subject: [PATCH] feat(auth): record how a session was authenticated Refs #41, #45 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3 --- internal/auth/session.go | 75 +++++++++++++++++++++++++++++------ internal/auth/session_test.go | 6 +-- 2 files changed, 66 insertions(+), 15 deletions(-) diff --git a/internal/auth/session.go b/internal/auth/session.go index 5e5a6a7..f283914 100644 --- a/internal/auth/session.go +++ b/internal/auth/session.go @@ -24,14 +24,40 @@ 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 - OrgID uuid.UUID - Locale string + Token string + UserID uuid.UUID + OrgID uuid.UUID + Locale string + Method Method + // SSOOrg is the organization whose identity provider authenticated the + // session, if Method is MethodSSO. + SSOOrg uuid.UUID CSRF string CreatedAt time.Time } +// Method records how a session was authenticated. Organization policies +// that require TOTP or SSO are checked against it. +type Method string + +// Authentication methods. Sessions from before methods were recorded have +// the empty Method and count as the weakest. +const ( + MethodPassword Method = "password" + MethodPasswordTOTP Method = "password+totp" + MethodSSO Method = "sso" +) + +// NewSession describes a session to create. OrgID, Locale and SSOOrg may be +// empty. +type NewSession struct { + UserID uuid.UUID + OrgID uuid.UUID + Locale string + Method Method + SSOOrg uuid.UUID +} + // 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 @@ -67,22 +93,27 @@ func RandomToken() string { 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) { +// Create starts a session. +func (s *Sessions) Create(ctx context.Context, n NewSession) (*Session, error) { sess := &Session{ Token: RandomToken(), - UserID: user, - OrgID: org, - Locale: locale, + UserID: n.UserID, + OrgID: n.OrgID, + Locale: n.Locale, + Method: n.Method, + SSOOrg: n.SSOOrg, CSRF: RandomToken(), CreatedAt: s.now(), } + user := n.UserID k := key(sess.Token) _, err := s.rdb.TxPipelined(ctx, func(p redis.Pipeliner) error { p.HSet(ctx, k, map[string]any{ "user": sess.UserID.String(), - "org": orgString(org), - "locale": locale, + "org": orgString(n.OrgID), + "locale": n.Locale, + "method": string(n.Method), + "sso_org": orgString(n.SSOOrg), "csrf": sess.CSRF, "created": sess.CreatedAt.Unix(), }) @@ -137,6 +168,16 @@ func (s *Sessions) SwitchOrg(ctx context.Context, sess *Session, org uuid.UUID) return nil } +// SetMethod records a stronger authentication, e.g. after the user proved +// possession of a TOTP device in this session. +func (s *Sessions) SetMethod(ctx context.Context, sess *Session, m Method) error { + if err := s.rdb.HSet(ctx, key(sess.Token), "method", string(m)).Err(); err != nil { + return fmt.Errorf("auth: set method: %w", err) + } + sess.Method = m + 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 { @@ -190,7 +231,17 @@ func decode(token string, v map[string]string) (*Session, error) { if err != nil { return nil, err } - return &Session{Token: token, UserID: user, OrgID: org, Locale: v["locale"], CSRF: v["csrf"], CreatedAt: time.Unix(created, 0)}, nil + var ssoOrg uuid.UUID + if v["sso_org"] != "" { + if ssoOrg, err = uuid.Parse(v["sso_org"]); err != nil { + return nil, err + } + } + return &Session{ + Token: token, UserID: user, OrgID: org, Locale: v["locale"], + Method: Method(v["method"]), SSOOrg: ssoOrg, + CSRF: v["csrf"], CreatedAt: time.Unix(created, 0), + }, nil } func orgString(id uuid.UUID) string { diff --git a/internal/auth/session_test.go b/internal/auth/session_test.go index 148b36b..7e81d68 100644 --- a/internal/auth/session_test.go +++ b/internal/auth/session_test.go @@ -36,7 +36,7 @@ func newTestSessions(t *testing.T) (*Sessions, *time.Time) { func mustCreate(t *testing.T, s *Sessions, user uuid.UUID) *Session { t.Helper() - sess, err := s.Create(context.Background(), user, uuid.Nil, "") + sess, err := s.Create(context.Background(), NewSession{UserID: user}) if err != nil { t.Fatalf("Create() failed: %v", err) } @@ -48,7 +48,7 @@ func TestSessionRoundTrip(t *testing.T) { ctx := context.Background() user, org := uuid.New(), uuid.New() - created, err := s.Create(ctx, user, org, "de") + created, err := s.Create(ctx, NewSession{UserID: user, OrgID: org, Locale: "de", Method: MethodSSO, SSOOrg: org}) if err != nil { t.Fatalf("Create() failed: %v", err) } @@ -56,7 +56,7 @@ func TestSessionRoundTrip(t *testing.T) { if err != nil { t.Fatalf("Get() failed: %v", err) } - if got.UserID != user || got.OrgID != org || got.Locale != "de" || got.CSRF != created.CSRF { + if got.UserID != user || got.OrgID != org || got.Locale != "de" || got.Method != MethodSSO || got.SSOOrg != org || got.CSRF != created.CSRF { t.Errorf("Get() = %+v, want user %v org %v locale de csrf %q", got, user, org, created.CSRF) } for token, want := range map[string]bool{created.CSRF: true, "": false, "x": false} {