mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-05 02:14:05 +00:00
feat(api): separate API keys from MCP tokens, add travellers
An MCP token drives every assistant tool; an API key reads trips over HTTP. Handing the same credential to a third-party integration gives it the whole MCP surface, so the two are now separate kinds on `mcp_tokens` and the kind sits in the WHERE clause — a token of the wrong kind is indistinguishable from one that does not exist. Also adds `travellers` to the trip payload: display name and an owner flag, no ids and no email addresses. An integration key reads its owner's itinerary; it is not a way to enumerate the people around them. Documents the surface in the wiki, since integrators need something to read that is not the source.
This commit is contained in:
@@ -5,11 +5,15 @@ import { TokenService } from '../tokens/token.service';
|
||||
/**
|
||||
* Authenticates a caller of the public API with a long-lived `trek_…` token.
|
||||
*
|
||||
* The tokens are the ones users already manage under Settings → Integrations; this
|
||||
* guard adds no new credential type and no new table. It reuses
|
||||
* `TokenService.verifyMcpToken`, which hashes the presented token with SHA-256 and
|
||||
* looks up the hash — the raw token is never stored and never compared in the
|
||||
* application, so a database read cannot yield a usable credential.
|
||||
* Accepts an **API key** — the kind users mint under Settings → Integrations for
|
||||
* third-party software. An MCP token does not resolve here, and an API key does not
|
||||
* open the MCP surface: the two are separate credentials with separate blast radii,
|
||||
* and `verifyApiToken` puts the kind in the WHERE clause so a token of the wrong
|
||||
* kind is indistinguishable from one that does not exist.
|
||||
*
|
||||
* The lookup hashes the presented key with SHA-256 and matches the hash — the raw
|
||||
* key is never stored and never compared in the application, so a database read
|
||||
* cannot yield a usable credential.
|
||||
*
|
||||
* Deliberately narrower than the MCP transport's `verifyToken`, which also accepts
|
||||
* OAuth bearer tokens and a plain web-session JWT:
|
||||
@@ -38,7 +42,7 @@ export class ApiTokenGuard implements CanActivate {
|
||||
401,
|
||||
);
|
||||
}
|
||||
const user = this.tokens.verifyMcpToken(token);
|
||||
const user = this.tokens.verifyApiToken(token);
|
||||
if (!user) {
|
||||
throw new HttpException(
|
||||
{ error: 'Invalid API token', code: 'API_TOKEN_INVALID' },
|
||||
|
||||
@@ -6,6 +6,7 @@ import type {
|
||||
PublicApiInclude,
|
||||
PublicApiPlace,
|
||||
PublicApiReservation,
|
||||
PublicApiTraveller,
|
||||
PublicApiTrip,
|
||||
PublicApiTripSummary,
|
||||
} from '@trek/shared';
|
||||
@@ -75,6 +76,9 @@ export class PublicApiService {
|
||||
if (include.includes('accommodations')) {
|
||||
trip.accommodations = this.buildAccommodations(tripId);
|
||||
}
|
||||
if (include.includes('travellers')) {
|
||||
trip.travellers = this.buildTravellers(tripId);
|
||||
}
|
||||
return trip;
|
||||
}
|
||||
|
||||
@@ -216,6 +220,29 @@ export class PublicApiService {
|
||||
notes: r.notes ?? null,
|
||||
}));
|
||||
}
|
||||
|
||||
/**
|
||||
* Who is on the trip: the owner first, then members in join order.
|
||||
*
|
||||
* Names only. The query selects `username` and nothing else — no ids, no email
|
||||
* addresses — because an integration key is a credential for reading its owner's
|
||||
* itinerary, not for enumerating the people around them. What it returns is
|
||||
* exactly what those people already see on the trip in TREK.
|
||||
*/
|
||||
private buildTravellers(tripId: number): PublicApiTraveller[] {
|
||||
const rows = this.db.all<TravellerRow>(
|
||||
`SELECT u.username, 1 AS is_owner
|
||||
FROM trips t JOIN users u ON u.id = t.user_id
|
||||
WHERE t.id = ?
|
||||
UNION ALL
|
||||
SELECT u.username, 0 AS is_owner
|
||||
FROM trip_members m JOIN users u ON u.id = m.user_id
|
||||
WHERE m.trip_id = ?
|
||||
ORDER BY is_owner DESC`,
|
||||
tripId, tripId,
|
||||
);
|
||||
return rows.map((r) => ({ name: r.username, owner: r.is_owner === 1 }));
|
||||
}
|
||||
}
|
||||
|
||||
function toTripSummary(row: TripRow): PublicApiTripSummary {
|
||||
@@ -296,6 +323,11 @@ interface ReservationRow {
|
||||
notes: string | null;
|
||||
}
|
||||
|
||||
interface TravellerRow {
|
||||
username: string;
|
||||
is_owner: number;
|
||||
}
|
||||
|
||||
interface AccommodationRow {
|
||||
name: string | null;
|
||||
address: string | null;
|
||||
|
||||
Reference in New Issue
Block a user