feat(federation): add /users/by-home-id reverse lookup for stub backfill
HMAC-authenticated, rate-limited (60/min/peer) endpoint that resolves a homeUserId on this instance to its canonical username + profile snapshot. Native non-deleted users only. Mirrors /users/lookup's auth shape. Adds lookupRemoteUserByHomeId to federationLookup.ts as the client-side helper. Used by the upcoming stub-backfill worker on peers that hold legacy snowflake-named replicas of users now visible by their real handle.
This commit is contained in:
@@ -2336,6 +2336,94 @@ export async function federationRoutes(app: FastifyInstance): Promise<void> {
|
||||
},
|
||||
);
|
||||
|
||||
// ─── POST /api/federation/users/by-home-id ──────────────────────────────────
|
||||
// Server-to-server: reverse-lookup a homeUserId to its canonical username +
|
||||
// profile snapshot. Used by the stub-username backfill worker on peers that
|
||||
// hold legacy snowflake-named replicas of users now visible by their real
|
||||
// handle. Same auth+rate-limit shape as /users/lookup.
|
||||
app.post<{ Body: { homeUserId?: unknown } }>(
|
||||
'/api/federation/users/by-home-id',
|
||||
{ bodyLimit: 4 * 1024 },
|
||||
async (request, reply) => {
|
||||
const db = getDb();
|
||||
|
||||
// 1. Verify HMAC headers
|
||||
const fedHeaders = parseFederationHeaders(request.headers as Record<string, string | string[] | undefined>);
|
||||
if (!fedHeaders) {
|
||||
return reply.code(401).send({ error: 'Missing or malformed federation headers', statusCode: 401 });
|
||||
}
|
||||
|
||||
const peer = db
|
||||
.select()
|
||||
.from(schema.federationPeers)
|
||||
.where(eq(schema.federationPeers.origin, fedHeaders.origin))
|
||||
.get();
|
||||
|
||||
if (!peer || peer.status !== 'active') {
|
||||
return reply.code(403).send({ error: 'Unknown or inactive peer', statusCode: 403 });
|
||||
}
|
||||
|
||||
if (isLookupRateLimited(peer.origin)) {
|
||||
return reply.code(429).header('Retry-After', '60').send({ error: 'Rate limit exceeded', statusCode: 429 });
|
||||
}
|
||||
|
||||
const bodyString = JSON.stringify(request.body);
|
||||
if (!verifyPeerSignature(bodyString, fedHeaders.signature, fedHeaders.timestamp, fedHeaders.nonce, peer)) {
|
||||
return reply.code(401).send({ error: 'Invalid signature', statusCode: 401 });
|
||||
}
|
||||
|
||||
// Replay protection
|
||||
if (fedHeaders.nonce) {
|
||||
if (isNonceDuplicate(peer.origin, fedHeaders.nonce)) {
|
||||
return reply.code(409).send({ error: 'Duplicate nonce — possible replay', statusCode: 409 });
|
||||
}
|
||||
} else if (peer.nonceSupported) {
|
||||
return reply.code(401).send({ error: 'Nonce required — peer previously supported nonces', statusCode: 401 });
|
||||
}
|
||||
|
||||
// 2. Validate body
|
||||
const rawId = (request.body as { homeUserId?: unknown } | null)?.homeUserId;
|
||||
if (typeof rawId !== 'string' || rawId.trim().length === 0) {
|
||||
return reply.code(400).send({ error: 'homeUserId is required (string)', statusCode: 400 });
|
||||
}
|
||||
const homeUserId = rawId.trim();
|
||||
|
||||
// 3. Native-only lookup. Match by id (canonical native id) OR home_user_id
|
||||
// (backfilled column natives carry to satisfy tier-1 lookups). Excludes
|
||||
// tombstoned and replicated stubs.
|
||||
const user = db
|
||||
.select()
|
||||
.from(schema.users)
|
||||
.where(
|
||||
and(
|
||||
eq(schema.users.isDeleted, 0),
|
||||
isNull(schema.users.homeInstance),
|
||||
or(eq(schema.users.id, homeUserId), eq(schema.users.homeUserId, homeUserId)),
|
||||
),
|
||||
)
|
||||
.get();
|
||||
|
||||
if (!user) {
|
||||
return reply.code(200).send({ found: false });
|
||||
}
|
||||
|
||||
return reply.code(200).send({
|
||||
found: true,
|
||||
user: {
|
||||
homeUserId: user.homeUserId ?? user.id,
|
||||
username: user.username,
|
||||
profile: {
|
||||
displayName: user.displayName,
|
||||
avatar: user.avatar,
|
||||
avatarColor: user.avatarColor,
|
||||
banner: user.banner,
|
||||
bio: user.bio,
|
||||
},
|
||||
},
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
// ─── POST /api/federation/sync ──────────────────────────────────────────────
|
||||
// Server-to-server: checkpoint catch-up sync. A peer calls this after downtime
|
||||
// to retrieve missed DM mutations from the mutation log.
|
||||
|
||||
Reference in New Issue
Block a user