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:
1 parent
ee09c497de
commit
30409697f7
2 files changed
+62
-11
No files matched your search
@@ -28,10 +28,36 @@ type Session struct {
|
|||||||
UserID uuid.UUID
|
UserID uuid.UUID
|
||||||
OrgID uuid.UUID
|
OrgID uuid.UUID
|
||||||
Locale string
|
Locale string
|
||||||
|
Method Method
|
||||||
|
// SSOOrg is the organization whose identity provider authenticated the
|
||||||
|
// session, if Method is MethodSSO.
|
||||||
|
SSOOrg uuid.UUID
|
||||||
CSRF string
|
CSRF string
|
||||||
CreatedAt time.Time
|
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.
|
// CheckCSRF compares token with the session's CSRF token in constant time.
|
||||||
func (s *Session) CheckCSRF(token string) bool {
|
func (s *Session) CheckCSRF(token string) bool {
|
||||||
return token != "" && subtle.ConstantTimeCompare([]byte(token), []byte(s.CSRF)) == 1
|
return token != "" && subtle.ConstantTimeCompare([]byte(token), []byte(s.CSRF)) == 1
|
||||||
@@ -67,22 +93,27 @@ func RandomToken() string {
|
|||||||
return base64.RawURLEncoding.EncodeToString(b)
|
return base64.RawURLEncoding.EncodeToString(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create starts a session. org and locale may be empty.
|
// Create starts a session.
|
||||||
func (s *Sessions) Create(ctx context.Context, user, org uuid.UUID, locale string) (*Session, error) {
|
func (s *Sessions) Create(ctx context.Context, n NewSession) (*Session, error) {
|
||||||
sess := &Session{
|
sess := &Session{
|
||||||
Token: RandomToken(),
|
Token: RandomToken(),
|
||||||
UserID: user,
|
UserID: n.UserID,
|
||||||
OrgID: org,
|
OrgID: n.OrgID,
|
||||||
Locale: locale,
|
Locale: n.Locale,
|
||||||
|
Method: n.Method,
|
||||||
|
SSOOrg: n.SSOOrg,
|
||||||
CSRF: RandomToken(),
|
CSRF: RandomToken(),
|
||||||
CreatedAt: s.now(),
|
CreatedAt: s.now(),
|
||||||
}
|
}
|
||||||
|
user := n.UserID
|
||||||
k := key(sess.Token)
|
k := key(sess.Token)
|
||||||
_, err := s.rdb.TxPipelined(ctx, func(p redis.Pipeliner) error {
|
_, err := s.rdb.TxPipelined(ctx, func(p redis.Pipeliner) error {
|
||||||
p.HSet(ctx, k, map[string]any{
|
p.HSet(ctx, k, map[string]any{
|
||||||
"user": sess.UserID.String(),
|
"user": sess.UserID.String(),
|
||||||
"org": orgString(org),
|
"org": orgString(n.OrgID),
|
||||||
"locale": locale,
|
"locale": n.Locale,
|
||||||
|
"method": string(n.Method),
|
||||||
|
"sso_org": orgString(n.SSOOrg),
|
||||||
"csrf": sess.CSRF,
|
"csrf": sess.CSRF,
|
||||||
"created": sess.CreatedAt.Unix(),
|
"created": sess.CreatedAt.Unix(),
|
||||||
})
|
})
|
||||||
@@ -137,6 +168,16 @@ func (s *Sessions) SwitchOrg(ctx context.Context, sess *Session, org uuid.UUID)
|
|||||||
return nil
|
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.
|
// SetLocale changes the session language.
|
||||||
func (s *Sessions) SetLocale(ctx context.Context, sess *Session, locale string) error {
|
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 {
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
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 {
|
func orgString(id uuid.UUID) string {
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ func newTestSessions(t *testing.T) (*Sessions, *time.Time) {
|
|||||||
|
|
||||||
func mustCreate(t *testing.T, s *Sessions, user uuid.UUID) *Session {
|
func mustCreate(t *testing.T, s *Sessions, user uuid.UUID) *Session {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
sess, err := s.Create(context.Background(), user, uuid.Nil, "")
|
sess, err := s.Create(context.Background(), NewSession{UserID: user})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Create() failed: %v", err)
|
t.Fatalf("Create() failed: %v", err)
|
||||||
}
|
}
|
||||||
@@ -48,7 +48,7 @@ func TestSessionRoundTrip(t *testing.T) {
|
|||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
user, org := uuid.New(), uuid.New()
|
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 {
|
if err != nil {
|
||||||
t.Fatalf("Create() failed: %v", err)
|
t.Fatalf("Create() failed: %v", err)
|
||||||
}
|
}
|
||||||
@@ -56,7 +56,7 @@ func TestSessionRoundTrip(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Get() failed: %v", err)
|
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)
|
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} {
|
for token, want := range map[string]bool{created.CSRF: true, "": false, "x": false} {
|
||||||
|
|||||||
Reference in new issue
Block a user