An open-source, faster Devin alternative
TypeScript
identity: add email + workos_user_id to identity JWT
Igor Zalutski9710c52b68a2
diff --git a/internal/api/identity.go b/internal/api/identity.goindex 32973351..f862e28b 100644--- a/internal/api/identity.go+++ b/internal/api/identity.go@@ -1,6 +1,7 @@package apiimport (+ "log""net/http""time"@@ -12,7 +13,8 @@ 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.+// calling back on every request. The token carries email + workos_user_id so+// downstreams can emit analytics events consistently with opencomputer.func (s *Server) createAuthToken(c echo.Context) error {orgID, ok := auth.GetOrgID(c)if !ok {@@ -27,14 +29,31 @@ func (s *Server) createAuthToken(c echo.Context) error {})}- orgStr := orgID.String()- var userStr *string+ in := auth.IdentityTokenInput{OrgID: orgID.String()}if userID := auth.GetUserID(c); userID != nil {- s := userID.String()- userStr = &s+ userStr := userID.String()+ in.UserID = &userStr++ // Look up email + workos_user_id for downstream analytics identity.+ // Soft failure: if the lookup fails, still issue a token (without+ // those fields) rather than block the caller.+ if s.store != nil {+ user, err := s.store.GetUserByID(c.Request().Context(), *userID)+ if err != nil {+ log.Printf("identity: failed to load user %s: %v", userStr, err)+ } else {+ if user.Email != "" {+ email := user.Email+ in.Email = &email+ }+ if user.WorkOSUserID != nil && *user.WorkOSUserID != "" {+ in.WorkOSUserID = user.WorkOSUserID+ }+ }+ }}- token, err := s.jwtIssuer.IssueIdentityToken(orgStr, userStr, identityTokenTTL)+ token, err := s.jwtIssuer.IssueIdentityToken(in, identityTokenTTL)if err != nil {return c.JSON(http.StatusInternalServerError, map[string]string{"error": "failed to issue token",diff --git a/internal/auth/jwt.go b/internal/auth/jwt.goindex 9476662a..7e8d26d1 100644--- a/internal/auth/jwt.go+++ b/internal/auth/jwt.go@@ -47,25 +47,39 @@ func (j *JWTIssuer) IssueSandboxToken(orgID uuid.UUID, sandboxID, workerID strin// 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.+// so the downstream can authorize and attribute analytics events without+// calling back to the control plane.type IdentityClaims struct {jwt.RegisteredClaims- OrgID string `json:"org_id"`- UserID *string `json:"user_id,omitempty"`+ OrgID string `json:"org_id"`+ UserID *string `json:"user_id,omitempty"`+ Email *string `json:"email,omitempty"`+ WorkOSUserID *string `json:"workos_user_id,omitempty"`+}++// IdentityTokenInput bundles the identity fields embedded in an identity JWT.+// Nil pointers are omitted from the resulting token.+type IdentityTokenInput struct {+ OrgID string+ UserID *string+ Email *string+ WorkOSUserID *string}// IssueIdentityToken creates a short-lived JWT carrying the caller's identity.-func (j *JWTIssuer) IssueIdentityToken(orgID string, userID *string, ttl time.Duration) (string, error) {+func (j *JWTIssuer) IssueIdentityToken(in IdentityTokenInput, ttl time.Duration) (string, error) {now := time.Now()claims := IdentityClaims{RegisteredClaims: jwt.RegisteredClaims{- Subject: orgID,+ Subject: in.OrgID,IssuedAt: jwt.NewNumericDate(now),ExpiresAt: jwt.NewNumericDate(now.Add(ttl)),Issuer: "opensandbox",},- OrgID: orgID,- UserID: userID,+ OrgID: in.OrgID,+ UserID: in.UserID,+ Email: in.Email,+ WorkOSUserID: in.WorkOSUserID,}token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)diff --git a/internal/db/store.go b/internal/db/store.goindex 324c3655..81adca52 100644--- a/internal/db/store.go+++ b/internal/db/store.go@@ -372,6 +372,16 @@ func (s *Store) GetUserByEmail(ctx context.Context, email string) (*User, error)return user, nil}+func (s *Store) GetUserByID(ctx context.Context, userID uuid.UUID) (*User, error) {+ user, err := scanUser(s.pool.QueryRow(ctx,+ `SELECT `+userColumns+` FROM users WHERE id = $1`, userID,+ ))+ if err != nil {+ return nil, fmt.Errorf("user not found: %w", err)+ }+ return user, nil+}+// --- API Key operations ---type APIKey struct {