Skip to content

One admin search box: resolve an email or a principal id - #52

Merged
FlyOverCoderKY merged 2 commits into
mainfrom
claude/admin-smart-lookup
Jul 31, 2026
Merged

FlyOverCoderKY merged 2 commits into
mainfrom
claude/admin-smart-lookup

Conversation

@FlyOverCoderKY

Copy link
Copy Markdown
Contributor

The admin surface shipped with lookup-by-principal-id only. I told Nathan that was because @pegma/identity had no email lookup and we'd need an upstream addition — that was wrong: Identity.findUserByEmail(email): Promise<User | null> is already on the package's public interface. The gap was entirely host-side, in the deliberately narrow IdentityPort Pick that simply never listed it. No upstream change, no release cycle.

One box

classifyLookup decides server-side, on the one discriminator that is total here: a @ anywhere means email, anything else is a principal id. Identity's single normalization function requires exactly one @ in every address, and principal ids are opaque subjects that never contain one (the principal route's own charset excludes it). One rule, one tested home, and the operator types whatever they have.

The response reports matchedBy, and the UI says which interpretation won — so looking someone up by email also teaches you their principal id.

Two deliberate choices worth review

POST, not GET with ?q=. The query can be a real person's email address. A POST body keeps it out of URLs, referrers, and access logs, and it puts CSRF + same-origin in front of the one endpoint that can confirm whether an address exists. Identity's public enumeration resistance (identical answers for known/unknown emails on creation, sign-in, recovery) is untouched — this path requires an authenticated Admin holder.

A separate lookup limiter. pegma.admin.principal.lookup at 120/h per operator, distinct from the 30/h mutation budget: operators search far more than they grant, and the bound means a stolen admin session cannot harvest the directory. Added to the scheduled sweep alongside the other three — the gap Codex caught on #51.

The host never normalizes. It trims the text-box value and hands it to Identity, because exactly one normalization function exists and it lives there (AGENTS.md rule). Pinned by a test asserting findUserByEmail receives 'Member@Example.TEST' verbatim from a padded, mixed-case query.

Verification

208 node + 2 D1 tests green (6 new: classifier, id path, email path + verbatim pass-through, 404/400 matrix, Identity invalid_input → 400, Admin+CSRF gating). tsc and astro check clean. Widening the port also forced four test fakes to declare findUserByEmail — the drift guard working as designed. Every DOM selector in the rewritten client cross-checked against the page markup.

🤖 Generated with Claude Code

The admin surface accepted only exact principal ids because the host's
IdentityPort — a deliberately narrow Pick — never listed the lookup.
@pegma/identity has published findUserByEmail on its public Identity
interface all along, so this needs no upstream change: widen the port and
use it.

- classifyLookup decides email vs principal id server-side on the one
  discriminator that is total here (Identity requires exactly one @ in
  every address; principal ids never contain one), so the operator gets a
  single box and the rule has one tested home.
- POST /api/admin/lookup, not a GET with a query string: the query can be
  a real person's email, which then stays out of URLs, referrers, and
  logs, and CSRF plus same-origin gate the one endpoint that can confirm
  an address exists. Identity's public enumeration resistance is
  unchanged; this path needs an authenticated Admin holder.
- New pegma.admin.principal.lookup limiter (120/h), swept with the rest,
  so a stolen admin session cannot harvest the directory.
- The host never normalizes the email — it hands Identity the trimmed
  value, since exactly one normalization function exists and it lives
  there. Test-pinned.

208 node + 2 D1 tests green (6 new), tsc and astro check clean.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings July 31, 2026 02:39
@cursor

cursor Bot commented Jul 31, 2026

Copy link
Copy Markdown

Bugbot is not enabled for your account, so this pull request was not reviewed.

Enable Bugbot in the Cursor dashboard to get automatic reviews on future PRs.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR expands the pegma.dev admin role-management surface to support a single lookup box that resolves either an email address or a principal id (server-side classification), leveraging the already-public Identity.findUserByEmail API and adding a dedicated lookup rate limiter.

Changes:

  • Add POST /api/admin/lookup (CSRF + same-origin + Admin-gated) that classifies query as email vs principal id and returns principal details + roles.
  • Widen the host IdentityPort to include findUserByEmail, updating test fakes accordingly.
  • Update the /staff/roles UI and client script to use the new lookup endpoint and display which interpretation matched; add a separate durable lookup limiter and wire it into scheduled sweeping.

Reviewed changes

Copilot reviewed 11 out of 11 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
worker/src/support-desk.ts Adds a dedicated durable limiter for admin directory lookups (pegma.admin.principal.lookup).
worker/src/support-desk.d1.test.ts Updates IdentityPort test fake to include findUserByEmail.
worker/src/support-api.test.ts Extends test identity port mock with findUserByEmail behavior.
worker/src/index.ts Wires lookupLimiter into the admin API and adds it to the scheduled sweeper list.
worker/src/identity-contracts.ts Widens IdentityPort Pick to include findUserByEmail.
worker/src/identity-api.test.ts Updates IdentityPort test helper to include findUserByEmail.
worker/src/admin-api.ts Implements /api/admin/lookup, including classification, auth/CSRF gating, rate limiting, and error mapping.
worker/src/admin-api.test.ts Adds unit/integration coverage for classifier, lookup behavior, verbatim pass-through, and auth/CSRF gating.
src/pages/staff/roles.astro Updates form copy/fields for “Email or principal id” and adds UI space for matched-by messaging.
public/staff-roles.js Implements client-side lookup via POST, renders matched-by messaging, and refreshes principal after mutations.
docs/SUPPORT_COMPOSITION.md Documents the new admin lookup endpoint and lookup limiter alongside existing admin routes/limits.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread worker/src/admin-api.test.ts Resolved
Comment thread public/staff-roles.js Resolved

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: ca05b6d265

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread public/staff-roles.js Resolved
…omain

- A second lookup (or a post-mutation refresh) now invalidates any
  in-flight one: each load takes a ticket, and details plus history render
  together or not at all, so a slow response can never paint its
  principal's history under a newer principal's card.
- The comment no longer claims "one request" — the flow makes two.
- The classifier test used a real personal address; reserved
  example.test now, and the rest of the worker tests swept for others.

Verified by driving the shipped public/staff-roles.js under a DOM shim
(scratchpad, not committed — this repo has no client-JS harness and this
PR does not invent one): with the guard the newer principal renders; with
the guard stripped, the same harness reproduces the stale overwrite.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@FlyOverCoderKY
FlyOverCoderKY merged commit 40a68cf into main Jul 31, 2026
1 check passed
@FlyOverCoderKY
FlyOverCoderKY deleted the claude/admin-smart-lookup branch July 31, 2026 03:01
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.

2 participants