Skip to content

feat(srv-sage): isolated IAS backend for SAGE auth (Part A — backend + MTA) - #3150

Merged
Thomas Jung (jung-thomas) merged 11 commits into
DEVfrom
feature/srv-sage-ias
Nov 20, 2026
Merged

Thomas Jung (jung-thomas) merged 11 commits into
DEVfrom
feature/srv-sage-ias

Conversation

@jung-thomas

Copy link
Copy Markdown
Contributor

Isolated IAS-authenticated backend for SAGE (srv-sage) — Part A

Restores SAGE IMS sign-in (broken by the 2.0.4 PKCE change) by giving SAGE its own backend + IAS public client, mirroring the MCP isolation pattern. Root cause: the XSUAA application plan cannot be a public client, so SAGE's secret-less PKCE got 401 invalid_client. XSUAA can't be fixed for this; IAS can (and already is, for MCP). Rather than bolt IAS trust onto the main tutorials-srv /author path (reverted once for breaking browser routes), SAGE gets an isolated app.

Design: docs/superpowers/specs/2026-11-19-sage-ias-isolated-backend-design.md · Plan: docs/superpowers/plans/2026-11-19-sage-ias-isolated-backend.md

What's in this PR (backend only — Part A)

  • srv-sage/ — new isolated CAP app (auth: kind:ias, xsuaa:true) serving SAGE's exact surface: MyOwnedTutorials, Tags, isSlugAvailable, reviewTutorial, snoozeTutorial, and POST /preview/render. Gated @requires:'authenticated-user'; per-user ownership enforced in-handler (ported verbatim from srv/author-service.js) — proven by an A-vs-B ownership test.
  • mta-sage.yaml + deploy/mta-sage-{dev,prod}.mtaext — standalone MTA adopting tutorials-hana / tutorials-sage-identity (new IAS) / tutorials-xsuaa as existing-service. Per-space XSUAA name handled via mtaext (xsuaa-imsprod on prod).
  • docs/developers/operations/sage-ias-setup.md — runbook for provisioning the dedicated tutorials-sage-identity public client (Tom-run).
  • Shared edits (scripts/bundle-shared.cjs, root package.json, .cdsrc.json, .gitignore) are append-only — srv/srv-mcp/srv-qa bundling verified unaffected.

Review

TDD ownership test passes; all 6 implementation tasks passed per-task review; a whole-branch opus review confirmed access control holds for all 5 ops, the IAS gate is fail-closed and byte-identical to the proven srv-mcp artifact, and the deploy descriptor is correct.

Required before this is usable (Tom-gated, by design — NOT in this PR)

  1. Provision tutorials-sage-identity on DEV per the runbook (dedicated IAS public client + vscode:// redirect URI).
  2. Deploy mta-sage.yaml to DEV (main MTA first; adopts its services).
  3. Live verification: IAS sign-in + all 5 ops + preview return real data. (A live IAS round-trip / cf-deploy boot can't run in CI — this is the designed gate.)

Follow-up (Part B, separate SAGE-repo PR)

Repoint the SAGE extension's auth to the IAS tenant/client and its API base to srv-sage, send the IAS id_token (per #2707), then cut SAGE 2.0.5 testing→stable.

- Add cf target to DEV block (symmetry with PROD, guards CF target drift)
- Note PROD XSUAA instance is xsuaa-imsprod, not tutorials-xsuaa; add
  mtaext override example to prevent silent service-not-found on PROD deploy
- Cite mta-mcp.yaml:64 and ias-setup.md M2M for authorization_code_pkce_s256
  grant name so reader can self-serve on invalid_grant_type errors
- Add transient-file warnings + manual-cleanup commands for sk-read (Step 3)
  and sk-verify (Step 6) in case the block exits early and keys dangle
- Reinforce sage-sk*.json transient / never-commit note in the record table
…uild task

Creates the srv-sage CAP app skeleton that IAS-authenticated SAGE author APIs
will be served from. Mirrors the srv-mcp isolation pattern: ESM server with
healthz endpoint and process-level error handlers; IAS+xsuaa auth config;
no MCP adapter, no author logic (added in later tasks). Adds the nodejs build
task to .cdsrc.json so `cds build --production` compiles srv-sage into gen/.
Exposes 5 operations SAGE actually uses: entities MyOwnedTutorials,
Tags (read-only); actions isSlugAvailable, reviewTutorial, snoozeTutorial.
Service-level @requires: 'authenticated-user' (IAS — no XSUAA scopes).
No AdminService dependency; @path: '/author' provisional pending Task 5.

(#task-3 — srv-sage author service model)
…sk 4)

Implements AuthorSageService handlers ported from srv/author-service.js:
- before('*') pinResolvedUser for tiered IAS/XSUAA/social identity resolution
- before('READ', MyOwnedTutorials) stamps WHERE userId = dbUser.uuid via provisionDbUser
- on('isSlugAvailable') case-insensitive LOWER(slug) match
- on('reviewTutorial') ownership-gated; delegates to srv-sage/lib/tutorial-review.js
- on('snoozeTutorial') ownership-gated; delegates to srv-sage/lib/tutorial-review.js

Adds srv-sage/lib/ bundle helpers (workspace-first, CF fallback):
- resolve-db-user.js: re-exports from @tutorials/core/resolve-db-user.js
- tutorial-review.js: self-contained review/snooze DB-write helpers
- _shared/ gitignored; built by scripts/bundle-shared.cjs (sageTargets block added)

TDD: test/unit/author-sage-ownership.test.js proves that ownership isolation
survives the authenticated-user gate — author-a cannot read author-b's
MyOwnedTutorials and cannot call reviewTutorial on author-b's tutorial (403
"Not the owner"), while author-a's own tutorial succeeds (200 + reviewedDate).
Part A — Action path confirmed (imsApiClient.ts analysis):
  SAGE extension POSTs bare paths: /author/isSlugAvailable (line 159),
  /author/reviewTutorial (line 188), /author/snoozeTutorial (line 195).
  CAP OData4 accepts both qualified and bare unbound-action paths — no @path
  alias needed. author-sage-service.cds updated with confirmed analysis in
  comments (removes provisional "task 5 confirms" note).

Part B — Preview renderer ported from srv-qa to srv-sage:
  - srv-sage/preview-renderer.js: adapted from srv-qa/preview-renderer.js;
    imports from ./lib/parsers.bundle.mjs (workspace-first, bundle fallback)
  - srv-sage/preview-semaphore.js: copied from srv-qa/preview-semaphore.js
  - srv-sage/server.js: adds POST /preview/render with IAS authenticated-user
    gate (req.user guard, fail-closed); no XSUAA scope check (IAS tokens carry
    no XSUAA scopes)
  - srv-sage/package.json: adds hugo-extended 0.147.8 (same pin as srv-qa)
  - package.json: prebuild:parsers-bundle now also builds srv-sage/lib/parsers.bundle.mjs
  - scripts/bundle-shared.cjs: comment updated; parsers.bundle built by
    prebuild:parsers-bundle (from scripts/parsers/index.ts), not here
  - srv-sage/lib/parsers.bundle.mjs: generated artifact (1.2 MB esbuild output)

Verified: cds compile clean; parsers bundle smoke test passes (composeTutorial,
renderHugoFrontmatter, normalizeVideo all present and functional).
…r import

Three review fixes on Task 5 output:

1. [Important] srv-sage/server.js — hoist IAS auth guard before express.json()
   so unauthenticated callers never trigger 50 MB body buffering. Guard is now
   a dedicated middleware in position 1 of the route chain (before body parse),
   matching srv-qa's pattern of placing requireAuthorScope before the parser.
   req.user?.id absent → 401 immediately, fail-closed.

2. [Minor] srv-sage/preview-renderer.js — fix stale header comment: was
   './lib/_shared/parsers.bundle.mjs', corrected to './lib/parsers.bundle.mjs'
   (the actual flat path used by both the import and prebuild:parsers-bundle).

3. [Minor] srv-sage/preview-renderer.js — replace dead try/catch workspace-
   first import with a direct static import of ./lib/parsers.bundle.mjs,
   matching srv-qa's pattern. @tutorials/parsers doesn't exist in packages/,
   so the try always threw; the static import is cleaner and honest.
New standalone MTA (tutorials-sage, ID tutorials-sage) packaging the
srv-sage module as tutorials-srv-sage on CF. Mirrors the mta-mcp.yaml
existing-service adoption pattern: adopts tutorials-hana, tutorials-xsuaa
(DEV) / xsuaa-imsprod (PROD via mtaext), and tutorials-sage-identity
(dedicated IAS public-PKCE instance provisioned per sage-ias-setup.md).

Before-all: npm install + bundle-shared.cjs (core.bundle.mjs) +
prebuild:parsers-bundle (parsers.bundle.mjs) + cds build --production.
Removes @tutorials/core from gen/srv-sage/package.json post-build to
prevent mbt workspace-symlink packaging failure (mirrors mta-mcp.yaml).

Adds deploy/mta-sage-{dev,prod}.mtaext:
- dev: no-op (tutorials-xsuaa is correct in dev space)
- prod: overrides tutorials-xsuaa → service-name: xsuaa-imsprod

Updates docs/developers/operations/mta-deployment.md: architecture
diagram updated to three MTAs; new SAGE MTA section; Key Files table.

Verified: mbt build -f mta-sage.yaml -t mta_archives succeeds; mtar
(tutorials-sage_1.0.0.mtar, 5.5MB) contains srv/lib/_shared/core.bundle.mjs
and srv/lib/parsers.bundle.mjs.
Generated build artifact (rebuilt by mta-sage.yaml before-all via
prebuild:parsers-bundle). srv-qa gitignores its equivalent; srv-sage was
mistakenly committing it — shipping a 1.2MB generated file into git and
tripping GitHub secret-scanning on a high-entropy string inside
sanitize-html's generated entity-decode trie (decodeTrieDict compressed
lookup data — a false positive, not a credential). Gitignored to match srv-qa.
@jung-thomas
Thomas Jung (jung-thomas) merged commit fb9f8cf into DEV Nov 20, 2026
5 of 6 checks passed
@jung-thomas
Thomas Jung (jung-thomas) deleted the feature/srv-sage-ias branch November 20, 2026 15:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant