Skip to content

An open-source, faster Devin alternative

TypeScript

Wire user_id from API key through to sandbox sessions (#116)

Mohamed Habib2492df786bce
diff --git a/internal/api/sandbox.go b/internal/api/sandbox.go
index fb14e280..91df180f 100644
--- a/internal/api/sandbox.go
+++ b/internal/api/sandbox.go
@@ -183,7 +183,7 @@ func (s *Server) createSandbox(c echo.Context) error {
if template == "" {
template = "default"
}
- _, _ = s.store.CreateSandboxSession(ctx, sb.ID, orgID, nil, template, region, workerID, cfgJSON, metadataJSON)
+ _, _ = s.store.CreateSandboxSession(ctx, sb.ID, orgID, auth.GetUserID(c), template, region, workerID, cfgJSON, metadataJSON)
}
return c.JSON(http.StatusCreated, sb)
@@ -476,7 +476,7 @@ func (s *Server) createSandboxRemote(c echo.Context, ctx context.Context, cfg ty
}
cfgJSON, _ := json.Marshal(cfgForPersistence(cfg))
metadataJSON, _ := json.Marshal(cfg.Metadata)
- _, _ = s.store.CreateSandboxSession(ctx, grpcResp.SandboxId, orgID, nil, template, region, worker.ID, cfgJSON, metadataJSON)
+ _, _ = s.store.CreateSandboxSession(ctx, grpcResp.SandboxId, orgID, auth.GetUserID(c), template, region, worker.ID, cfgJSON, metadataJSON)
if templateID != nil {
_ = s.store.UpdateSandboxSessionTemplate(ctx, grpcResp.SandboxId, *templateID)
}
@@ -1700,7 +1700,7 @@ func (s *Server) createFromCheckpointCore(c echo.Context, userEnvs map[string]st
}
cfgJSON, _ := json.Marshal(cfgForPersistence(originalCfg))
metadataJSON, _ := json.Marshal(originalCfg.Metadata)
- _, _ = s.store.CreateSandboxSession(ctx, sandboxID, orgID, nil, template, region, workerID, cfgJSON, metadataJSON)
+ _, _ = s.store.CreateSandboxSession(ctx, sandboxID, orgID, auth.GetUserID(c), template, region, workerID, cfgJSON, metadataJSON)
// Track checkpoint lineage for patch system
_ = s.store.SetSandboxCheckpointID(ctx, sandboxID, checkpointID)
}
diff --git a/internal/auth/middleware.go b/internal/auth/middleware.go
index 1d79e0c4..f98730b7 100644
--- a/internal/auth/middleware.go
+++ b/internal/auth/middleware.go
@@ -14,6 +14,8 @@ type contextKey string
const (
// ContextKeyOrgID is the echo context key for the authenticated org ID.
ContextKeyOrgID contextKey = "org_id"
+ // ContextKeyUserID is the echo context key for the authenticated user ID.
+ ContextKeyUserID contextKey = "user_id"
)
// SetOrgID stores the org ID in the echo context.
@@ -31,6 +33,24 @@ func GetOrgID(c echo.Context) (uuid.UUID, bool) {
return id, ok
}
+// SetUserID stores the user ID in the echo context.
+func SetUserID(c echo.Context, userID uuid.UUID) {
+ c.Set(string(ContextKeyUserID), userID)
+}
+
+// GetUserID retrieves the user ID from the echo context. Returns nil if not set.
+func GetUserID(c echo.Context) *uuid.UUID {
+ v := c.Get(string(ContextKeyUserID))
+ if v == nil {
+ return nil
+ }
+ id, ok := v.(uuid.UUID)
+ if !ok {
+ return nil
+ }
+ return &id
+}
+
// PGAPIKeyMiddleware validates API keys against PostgreSQL.
// Falls back to static API key comparison if store is nil (combined/dev mode).
func PGAPIKeyMiddleware(store *db.Store, staticKey string) echo.MiddlewareFunc {
@@ -60,13 +80,16 @@ func PGAPIKeyMiddleware(store *db.Store, staticKey string) echo.MiddlewareFunc {
// Validate against PG if store is available
if store != nil {
- orgID, err := store.ValidateAPIKey(c.Request().Context(), key)
+ orgID, userID, err := store.ValidateAPIKey(c.Request().Context(), key)
if err != nil {
return c.JSON(http.StatusForbidden, map[string]string{
"error": "invalid API key",
})
}
SetOrgID(c, orgID)
+ if userID != nil {
+ SetUserID(c, *userID)
+ }
return next(c)
}
diff --git a/internal/controlplane/server.go b/internal/controlplane/server.go
index 3731cb3d..f17fda65 100644
--- a/internal/controlplane/server.go
+++ b/internal/controlplane/server.go
@@ -209,7 +209,7 @@ func (s *Server) createSandbox(c echo.Context) error {
}
cfgJSON, _ := json.Marshal(req)
metadataJSON, _ := json.Marshal(req.Metadata)
- _, _ = s.store.CreateSandboxSession(ctx, grpcResp.SandboxId, orgID, nil, template, region, worker.ID, cfgJSON, metadataJSON)
+ _, _ = s.store.CreateSandboxSession(ctx, grpcResp.SandboxId, orgID, auth.GetUserID(c), template, region, worker.ID, cfgJSON, metadataJSON)
// Issue sandbox-scoped JWT (24h TTL — independent of sandbox idle timeout)
token, err := s.jwtIssuer.IssueSandboxToken(orgID, grpcResp.SandboxId, worker.ID, 24*time.Hour)
diff --git a/internal/db/store.go b/internal/db/store.go
index 8b3f3f27..326b8638 100644
--- a/internal/db/store.go
+++ b/internal/db/store.go
@@ -378,23 +378,25 @@ func (s *Store) CreateAPIKey(ctx context.Context, orgID uuid.UUID, createdBy *uu
return apiKey, nil
}
-// ValidateAPIKey looks up an API key by hash and returns the associated org ID.
-func (s *Store) ValidateAPIKey(ctx context.Context, keyPlaintext string) (uuid.UUID, error) {
+// ValidateAPIKey looks up an API key by hash and returns the associated org ID
+// and the user ID of the key's creator (nil for keys with no creator).
+func (s *Store) ValidateAPIKey(ctx context.Context, keyPlaintext string) (uuid.UUID, *uuid.UUID, error) {
hash := HashAPIKey(keyPlaintext)
var orgID uuid.UUID
+ var createdBy *uuid.UUID
var expiresAt *time.Time
err := s.pool.QueryRow(ctx,
- `SELECT org_id, expires_at FROM api_keys WHERE key_hash = $1`, hash,
- ).Scan(&orgID, &expiresAt)
+ `SELECT org_id, created_by, expires_at FROM api_keys WHERE key_hash = $1`, hash,
+ ).Scan(&orgID, &createdBy, &expiresAt)
if err != nil {
- return uuid.Nil, fmt.Errorf("invalid API key")
+ return uuid.Nil, nil, fmt.Errorf("invalid API key")
}
if expiresAt != nil && expiresAt.Before(time.Now()) {
- return uuid.Nil, fmt.Errorf("API key expired")
+ return uuid.Nil, nil, fmt.Errorf("API key expired")
}
// Update last_used
_, _ = s.pool.Exec(ctx, `UPDATE api_keys SET last_used = now() WHERE key_hash = $1`, hash)
- return orgID, nil
+ return orgID, createdBy, nil
}
func (s *Store) ListAPIKeys(ctx context.Context, orgID uuid.UUID) ([]APIKey, error) {