Phase 12 R2: Marketplace-Backend α + β — Authors + Deck-Init + Publish
Routes (additiv unter /api/v1/marketplace/*): - POST/GET /authors/me — eigenes Author-Profil anlegen/updaten/lesen - GET /authors/:slug — public Profile-Lookup (banned-reason gestrippt) - POST /decks — Deck-Init (Slug-Validation + Pflicht-Author-Profil + CHECK auf paid + Pro-License) - POST /decks/:slug/publish — Versions-Snapshot mit per-Karte cardContentHash aus @cards/domain, per-Version-Hash, AI-Mod-Stub-Log, atomarer latest_version_id-Bump in Drizzle-Transaction - PATCH /decks/:slug — Metadaten-Update (Owner-Only) - GET /decks/:slug — Public-Detail mit optional-auth-Middleware Geport aus cards-decommission-base:services/cards-server/, mit Greenfield-Anpassungen: - Hashing über @cards/domain.cardContentHash (gemeinsame SoT zwischen privatem cards.cards und marketplace.deck_cards), per- Version-Hash als SHA-256 über sortierte Karten-Hashes mit Ord-Prefix - AI-Moderation als R2-Stub (pass+rationale+model='stub'), echte mana-llm-Anbindung in späterer Welle - Auth-Middleware-Shape an Greenfield (userId/tier/authMode in c.get(...) statt user-Object), optional-auth als Schwester für anonymen Public-Read - Hono-typing: outer Marketplace-Decks-Router ist Partial<AuthVars> weil Public-GET kein JWT braucht; Auth-Subroute ist strict Lese-Referenz: - 3331 LOC altes cards-server-Code (routes, services, middleware, lib) unter docs/marketplace/archive/code/ archiviert. Read-only, nicht im Build-Path. Verifikation: - 16 neue Vitest-Tests (Slug + Version-Hash), 72 gesamt grün - type-check 0 errors - E2E-Smoke gegen lokale cards-api: Cardecky-Author + Deck r2-stoische-ethik mit 3 Karten v1.0.0 (basic + basic + cloze), per-Karten-Hashes geschrieben, ai_moderation_log-Row da, semver-409 + paid-422-Errors verifiziert. Smoke-Daten danach aufgeräumt. Verbleibend für R3+: Discovery (explore + search), Engagement (stars/ subscribe/fork), Smart-Merge mit FSRS-State-Erhalt; danach R4 PRs + Card-Discussions, R5 Frontend-Routes. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
9a7068dd19
commit
7dbbf63523
40 changed files with 4004 additions and 1 deletions
64
apps/api/src/middleware/marketplace/optional-auth.ts
Normal file
64
apps/api/src/middleware/marketplace/optional-auth.ts
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
import type { MiddlewareHandler } from 'hono';
|
||||
import { createRemoteJWKSet, jwtVerify } from 'jose';
|
||||
|
||||
import type { AuthVars, Tier } from '../auth.ts';
|
||||
|
||||
/**
|
||||
* Optional-Auth-Middleware für Marketplace-Public-Endpoints.
|
||||
*
|
||||
* Setzt `userId`/`tier`/`authMode` wenn ein gültiger Bearer-Token da
|
||||
* ist, lehnt aber **nie** ab. Public-Read (Explore, Deck-Detail, Author-
|
||||
* Profile) funktioniert anonym; signed-in User sehen zusätzlich ihren
|
||||
* Star/Subscribe/Fork-State.
|
||||
*
|
||||
* Schwester-Middleware zu `authMiddleware` (strict, in `../auth.ts`).
|
||||
* Beide nutzen denselben JWKS-Cache + dasselbe Algo, dieser ist nur
|
||||
* der Read-Path-Companion.
|
||||
*
|
||||
* Geschichte: 1:1 ported from `cards-decommission-base:services/cards-server/src/middleware/optional-auth.ts`,
|
||||
* mit Anpassung auf den Greenfield-`AuthVars`-Shape (`userId`/`tier`
|
||||
* statt `user`-Object).
|
||||
*/
|
||||
|
||||
const MANA_AUTH_URL = process.env.MANA_AUTH_URL ?? 'https://auth.mana.how';
|
||||
|
||||
let jwksCache: ReturnType<typeof createRemoteJWKSet> | null = null;
|
||||
function getJwks() {
|
||||
if (!jwksCache) {
|
||||
jwksCache = createRemoteJWKSet(new URL('/api/auth/jwks', MANA_AUTH_URL));
|
||||
}
|
||||
return jwksCache;
|
||||
}
|
||||
|
||||
function tierFromClaim(raw: unknown): Tier {
|
||||
if (typeof raw !== 'string') return 'public';
|
||||
if (raw === 'guest' || raw === 'public' || raw === 'beta' || raw === 'alpha' || raw === 'founder') {
|
||||
return raw;
|
||||
}
|
||||
return 'public';
|
||||
}
|
||||
|
||||
export const optionalAuthMiddleware: MiddlewareHandler<{ Variables: Partial<AuthVars> }> = async (
|
||||
c,
|
||||
next
|
||||
) => {
|
||||
const authHeader = c.req.header('Authorization');
|
||||
if (!authHeader?.startsWith('Bearer ')) {
|
||||
await next();
|
||||
return;
|
||||
}
|
||||
const token = authHeader.slice(7).trim();
|
||||
try {
|
||||
const { payload } = await jwtVerify(token, getJwks(), {});
|
||||
const sub = typeof payload.sub === 'string' ? payload.sub : null;
|
||||
if (sub) {
|
||||
c.set('userId', sub);
|
||||
c.set('tier', tierFromClaim(payload.tier));
|
||||
c.set('authMode', 'jwt');
|
||||
}
|
||||
} catch {
|
||||
// Bad Token = anonymous fortfahren. Strict-Middleware ist für
|
||||
// Mutation-Endpoints zuständig.
|
||||
}
|
||||
await next();
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue