Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/api/gitsheets/agent-grants.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ root = "agent-grants"
# Public on purpose: which programs may act, with which scopes, granted by
# whom, until when. The token and its hash are never here; the hash lives in
# the site's private store. The only writer is the site.
implements = ["codeforphilly.org/agent-grants/v2"]
implements = ["codeforphilly.org/agent-grants/v3"]
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"$id" = "https://codeforphilly.org/agent-grants/v2"
"$id" = "https://codeforphilly.org/agent-grants/v3"
"$schema" = "http://json-schema.org/draft-07/schema#"
required = ["createdAt", "createdBySlug", "expiresAt", "id", "label", "personSlug", "scopes"]
title = "Agent grant"
Expand Down Expand Up @@ -42,5 +42,5 @@ minItems = 1
type = "array"

[properties.scopes.items]
enum = ["moderation:read", "moderation:vote", "moderation:purge", "slack:link", "projects:applications:read", "projects:applications:decide"]
enum = ["moderation:read", "moderation:vote", "moderation:purge", "slack:link", "projects:applications:read", "projects:applications:decide", "account-claims:read"]
type = "string"
4 changes: 4 additions & 0 deletions apps/api/gitsheets/contracts/sources.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ source = "https://raw.githubusercontent.com/CodeForPhilly/codeforphilly-ng/71bf9
adopted = 2026-11-30T00:52:16.731Z
source = "https://raw.githubusercontent.com/CodeForPhilly/codeforphilly-ng/842bcb2c91140a1327c52ae85a55d40277404d04/contracts/codeforphilly.org/agent-grants/v2.json"

["codeforphilly.org/agent-grants/v3"]
adopted = 2026-12-01T04:49:54.108Z
source = "https://raw.githubusercontent.com/CodeForPhilly/codeforphilly-ng/ac5a8fab7807c041c8741e1e85f6514656067423/contracts/codeforphilly.org/agent-grants/v3.json"

["codeforphilly.org/blog-posts/v1"]
adopted = 2026-10-14T03:31:18.13Z
source = "https://raw.githubusercontent.com/CodeForPhilly/codeforphilly-ng/7fe6b81a0bbd05b7a4e6896d55e181fdb61cfe42/contracts/codeforphilly.org/blog-posts/v1.json"
Expand Down
2 changes: 2 additions & 0 deletions apps/api/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ import { moderationRoutes } from './routes/moderation.js';
import { agentRoutes } from './routes/agents.js';
import { slackLinkRoutes } from './routes/slack-link.js';
import { agentApplicationRoutes } from './routes/agent-applications.js';
import { agentAccountClaimRoutes } from './routes/agent-account-claims.js';
import { webhookRoutes } from './routes/webhooks.js';
import { samlRoutes } from './routes/saml.js';
import { internalRoutes } from './routes/internal.js';
Expand Down Expand Up @@ -227,6 +228,7 @@ export async function buildApp(opts: BuildAppOptions = {}): Promise<FastifyInsta
await fastify.register(agentRoutes);
await fastify.register(slackLinkRoutes);
await fastify.register(agentApplicationRoutes);
await fastify.register(agentAccountClaimRoutes);
await fastify.register(webhookRoutes);
await fastify.register(samlRoutes);
await fastify.register(internalRoutes);
Expand Down
14 changes: 1 addition & 13 deletions apps/api/src/routes/account-claim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -437,19 +437,7 @@ export async function accountClaimRoutes(fastify: FastifyInstance): Promise<void
},
async (request) => {
requireAuth(request, ['staff']);
const items = await fastify.services.accountClaim.staffQueue();
return ok(
items.map((r) => ({
requestId: r.id,
type: r.type,
claimedSlug: r.claimedSlug,
claimedPersonId: r.claimedPersonId,
requesterGithubLogin: r.requesterGithubLogin,
requesterPersonId: r.requesterPersonId,
evidence: r.evidence,
submittedAt: r.submittedAt,
})),
);
return ok(await fastify.services.accountClaim.staffQueueView());
},
);

Expand Down
34 changes: 34 additions & 0 deletions apps/api/src/routes/agent-account-claims.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Agent account-claim feed — specs/api/account-claim.md → GET /api/agent/account-claims.
*
* GET /api/agent/account-claims — agent:account-claims:read
*
* The pending claim requests, reduced to what an alert needs: which member,
* which GitHub login, when. Never the evidence, the staff note, an address,
* or anything from a private profile. As for /api/agent/events, the scope is
* checked before anything else, and signed-in people and staff are anonymous
* here (401), like any caller without the grant.
*/
import type { FastifyInstance } from 'fastify';

import { agentScopeCaller } from '../auth/caller.js';

export async function agentAccountClaimRoutes(fastify: FastifyInstance): Promise<void> {
fastify.get(
'/api/agent/account-claims',
{
preValidation: async (request) => {
agentScopeCaller(request, 'account-claims:read');
},
schema: {
tags: ['account-claim'],
summary: 'Pending account-claim requests, without their evidence (agent:account-claims:read)',
},
},
async (request) => {
agentScopeCaller(request, 'account-claims:read');
const data = await fastify.services.accountClaim.agentQueueView();
return { success: true, data, metadata: { timestamp: new Date().toISOString(), count: data.length } };
},
);
}
109 changes: 109 additions & 0 deletions apps/api/src/services/account-claim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,51 @@ export interface StaffDenyResult {
readonly request: AccountClaimRequest;
}

/** The claimed member as a reviewer sees it (specs/api/account-claim.md → staff queue). */
export interface ClaimedPersonFacts {
readonly slug: string;
readonly fullName: string;
readonly createdAt: string;
/** Carried over from the old site (`legacyId` set). */
readonly imported: boolean;
/** The GitHub login the member is already linked to, or null. */
readonly githubLogin: string | null;
readonly memberOfCount: number;
readonly isAgent: boolean;
}

/** One row of GET /api/staff/account-claim/queue. Staff only: carries the evidence. */
export interface StaffQueueItem {
readonly requestId: string;
readonly type: AccountClaimRequest['type'];
readonly claimedSlug: string;
readonly claimedPersonId: string | null;
readonly claimedPerson: ClaimedPersonFacts | null;
readonly requesterGithubLogin: string;
readonly requesterPersonId: string | null;
readonly requesterPerson: { slug: string; fullName: string; createdAt: string } | null;
readonly evidence: string;
readonly submittedAt: string;
}

/**
* One row of GET /api/agent/account-claims: what an alert needs and nothing
* private. Built field by field; never spread from a request record.
*/
export interface AgentClaimItem {
readonly requestId: string;
readonly type: AccountClaimRequest['type'];
readonly submittedAt: string;
/** The typed text, only when it has the shape of a username. */
readonly claimedText: string | null;
readonly resolves: boolean;
readonly claimedPersonSlug: string | null;
readonly requesterGithubLogin: string;
}

/** Username shape (specs/behaviors/slug-handles.md), any case: what may leave the site as typed. */
const USERNAME_SHAPE = /^[a-z0-9][a-z0-9-]{1,49}$/i;

/** Reconstruct a ResolvedGitHubIdentity from the claim-pending JWT claims. */
export function ghIdentityFromClaim(claims: ClaimPendingClaims): ResolvedGitHubIdentity {
const id = Number(claims.sub);
Expand Down Expand Up @@ -414,6 +459,70 @@ export class AccountClaimService {
return all.slice().sort((a, b) => a.submittedAt.localeCompare(b.submittedAt));
}

/**
* The member a request would act on, as it stands now: `claimedPersonId`
* to a person that exists and is not deactivated. Null exactly when
* staffApprove would refuse with `no_claimed_person`.
*/
resolveClaimed(r: AccountClaimRequest): Person | null {
if (!r.claimedPersonId) return null;
const p = this.#state.people.get(r.claimedPersonId);
return p && !p.deletedAt ? p : null;
}

/** The staff queue with what a reviewer needs (specs/api/account-claim.md). */
async staffQueueView(): Promise<StaffQueueItem[]> {
return (await this.staffQueue()).map((r) => {
const claimed = this.resolveClaimed(r);
const requester = r.requesterPersonId ? this.#state.people.get(r.requesterPersonId) : undefined;
return {
requestId: r.id,
type: r.type,
claimedSlug: r.claimedSlug,
claimedPersonId: r.claimedPersonId,
claimedPerson: claimed
? {
slug: claimed.slug,
fullName: claimed.fullName,
createdAt: claimed.createdAt,
imported: claimed.legacyId != null,
githubLogin: claimed.githubUserId ? (claimed.githubLogin ?? null) : null,
memberOfCount: membershipsOfPerson(this.#state, claimed.slug).filter((m) => m.state === 'member').length,
isAgent: isAgentAccount(this.#state, claimed.slug),
}
: null,
requesterGithubLogin: r.requesterGithubLogin,
requesterPersonId: r.requesterPersonId,
requesterPerson:
requester && !requester.deletedAt
? { slug: requester.slug, fullName: requester.fullName, createdAt: requester.createdAt }
: null,
evidence: r.evidence,
submittedAt: r.submittedAt,
};
});
}

/**
* The pending requests reduced for an agent's alert: no evidence, no staff
* note, no ids beyond the request's, nothing from a private profile.
*/
async agentQueueView(): Promise<AgentClaimItem[]> {
return (await this.staffQueue()).map((r) => {
const claimed = this.resolveClaimed(r);
const typed = r.claimedSlug.trim();
return {
requestId: r.id,
type: r.type,
submittedAt: r.submittedAt,
claimedText: USERNAME_SHAPE.test(typed) ? typed : null,
resolves: claimed !== null,
claimedPersonSlug: claimed?.slug ?? null,
requesterGithubLogin: r.requesterGithubLogin,
};
});
}

/** Mark a request denied. */
async staffDeny(
tx: DualStoreTx,
Expand Down
Loading