feat(auth): record how a session was authenticated

Refs #41, #45

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:49:35 +01:00
1 parent ee09c497de
commit 30409697f7
2 files changed
+66 -15

No files matched your search

+63 -12
View File
@@ -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 {
+3 -3
View File
@@ -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} {