An open-source, faster Devin alternative
TypeScript
Replace GET /api/me with POST /api/auth/token (identity JWT)
Igor Zalutski41ceefbb210b
diff --git a/internal/api/identity.go b/internal/api/identity.goindex 8bc9e242..32973351 100644--- a/internal/api/identity.go+++ b/internal/api/identity.go@@ -2,14 +2,18 @@ package apiimport ("net/http"+ "time""github.com/labstack/echo/v4""github.com/opensandbox/opensandbox/internal/auth")-// getMe returns the identity (org and optional user) associated with the-// current API key. Downstream services use this to resolve key → owner.-func (s *Server) getMe(c echo.Context) error {+const identityTokenTTL = 10 * time.Minute++// createAuthToken exchanges the current API key for a short-lived identity JWT.+// Downstream services (sessions-api) use this to resolve key → owner without+// calling back on every request.+func (s *Server) createAuthToken(c echo.Context) error {orgID, ok := auth.GetOrgID(c)if !ok {return c.JSON(http.StatusUnauthorized, map[string]string{@@ -17,13 +21,28 @@ func (s *Server) getMe(c echo.Context) error {})}- resp := map[string]interface{}{- "org_id": orgID.String(),+ if s.jwtIssuer == nil {+ return c.JSON(http.StatusServiceUnavailable, map[string]string{+ "error": "token issuance not configured",+ })}+ orgStr := orgID.String()+ var userStr *stringif userID := auth.GetUserID(c); userID != nil {- resp["user_id"] = userID.String()+ s := userID.String()+ userStr = &s+ }++ token, err := s.jwtIssuer.IssueIdentityToken(orgStr, userStr, identityTokenTTL)+ if err != nil {+ return c.JSON(http.StatusInternalServerError, map[string]string{+ "error": "failed to issue token",+ })}- return c.JSON(http.StatusOK, resp)+ return c.JSON(http.StatusOK, map[string]interface{}{+ "token": token,+ "expires_in": int(identityTokenTTL.Seconds()),+ })}diff --git a/internal/api/router.go b/internal/api/router.goindex 5591ea9b..aa93b918 100644--- a/internal/api/router.go+++ b/internal/api/router.go@@ -188,7 +188,7 @@ func NewServer(mgr sandbox.Manager, ptyMgr *sandbox.PTYManager, apiKey string, oapi.Use(auth.PGAPIKeyMiddleware(s.store, apiKey))// Identity- api.GET("/me", s.getMe)+ api.POST("/auth/token", s.createAuthToken)// Sandbox lifecycleapi.POST("/sandboxes", s.createSandbox)diff --git a/internal/auth/jwt.go b/internal/auth/jwt.goindex f1910173..9476662a 100644--- a/internal/auth/jwt.go+++ b/internal/auth/jwt.go@@ -45,6 +45,53 @@ func (j *JWTIssuer) IssueSandboxToken(orgID uuid.UUID, sandboxID, workerID strinreturn token.SignedString(j.secret)}+// IdentityClaims are the JWT claims for identity tokens issued to downstream+// services (e.g. sessions-api). They carry the caller's org and user identity+// so the downstream can authorize without calling back to the control plane.+type IdentityClaims struct {+ jwt.RegisteredClaims+ OrgID string `json:"org_id"`+ UserID *string `json:"user_id,omitempty"`+}++// IssueIdentityToken creates a short-lived JWT carrying the caller's identity.+func (j *JWTIssuer) IssueIdentityToken(orgID string, userID *string, ttl time.Duration) (string, error) {+ now := time.Now()+ claims := IdentityClaims{+ RegisteredClaims: jwt.RegisteredClaims{+ Subject: orgID,+ IssuedAt: jwt.NewNumericDate(now),+ ExpiresAt: jwt.NewNumericDate(now.Add(ttl)),+ Issuer: "opensandbox",+ },+ OrgID: orgID,+ UserID: userID,+ }++ token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)+ return token.SignedString(j.secret)+}++// ValidateIdentityToken parses and validates an identity JWT.+func (j *JWTIssuer) ValidateIdentityToken(tokenStr string) (*IdentityClaims, error) {+ token, err := jwt.ParseWithClaims(tokenStr, &IdentityClaims{}, func(t *jwt.Token) (interface{}, error) {+ if _, ok := t.Method.(*jwt.SigningMethodHMAC); !ok {+ return nil, fmt.Errorf("unexpected signing method: %v", t.Header["alg"])+ }+ return j.secret, nil+ })+ if err != nil {+ return nil, fmt.Errorf("invalid token: %w", err)+ }++ claims, ok := token.Claims.(*IdentityClaims)+ if !ok || !token.Valid {+ return nil, fmt.Errorf("invalid token claims")+ }++ return claims, nil+}+// SigningSecret returns the raw HMAC secret for use by other signing functions (e.g. signed URLs).func (j *JWTIssuer) SigningSecret() []byte { return j.secret }