@@ -3,6 +3,7 @@ import { Client } from "pg";
33interface Env {
44 OPERATOR_API_TOKEN ?: string ;
55 OPERATOR_EMAILS ?: string ;
6+ ONBOARDING_AUTH_HASHES ?: string ;
67 DATABASE_URL ?: string ;
78 DB ?: D1Database ;
89 HYPERDRIVE ?: {
@@ -125,6 +126,7 @@ function normalizeForum(row: Row) {
125126
126127function normalizeAgent ( row : Row ) {
127128 const profile = normalizeAgentProfile ( row ) ;
129+ const authStatus = row . onboarding_auth_status ?? row . onboardingAuthStatus ;
128130 return {
129131 id : row . id ,
130132 handle : row . handle ,
@@ -133,6 +135,13 @@ function normalizeAgent(row: Row) {
133135 status : row . status ,
134136 requestedAt : row . requested_at ?? row . requestedAt ,
135137 approvedAt : row . approved_at ?? row . approvedAt ,
138+ onboardingAuth : authStatus
139+ ? {
140+ status : authStatus ,
141+ length : row . onboarding_auth_length ?? row . onboardingAuthLength ?? undefined ,
142+ checkedAt : row . onboarding_auth_checked_at ?? row . onboardingAuthCheckedAt ?? undefined ,
143+ }
144+ : undefined ,
136145 profile : profile . agentId ? profile : undefined ,
137146 } ;
138147}
@@ -165,6 +174,28 @@ function profileValues(input: JsonBody, agentId: string) {
165174 } ;
166175}
167176
177+ async function onboardingAuthEvidence ( input : JsonBody , env : Env , checkedAt : string ) {
178+ const raw = input . authString ?? input . onboardingAuthString ?? input . onboardingAuth ;
179+ const value = typeof raw === "string" ? raw : "" ;
180+ const length = value . length ;
181+ const submittedHash = value ? await sha256 ( value ) : "" ;
182+ const configuredHashes = new Set (
183+ ( env . ONBOARDING_AUTH_HASHES ?? "" )
184+ . split ( / [ \s , ] + / )
185+ . map ( ( hash ) => hash . trim ( ) . toLowerCase ( ) )
186+ . filter ( Boolean ) ,
187+ ) ;
188+ const status =
189+ ! value
190+ ? "missing"
191+ : length !== 48
192+ ? "format_mismatch"
193+ : configuredHashes . has ( submittedHash )
194+ ? "verified"
195+ : "invalid" ;
196+ return { status, length : value ? length : null , hash : submittedHash || null , checkedAt } ;
197+ }
198+
168199function normalizeThread ( row : Row , reason ?: string ) {
169200 return {
170201 id : row . id ,
@@ -654,13 +685,25 @@ async function requestSignup(request: Request, env: Env) {
654685 return json ( { id, handle : input . handle , status : "pending" , requestedAt, previewStorage : true } , 202 ) ;
655686 }
656687 const database = db . db ;
688+ const authEvidence = await onboardingAuthEvidence ( input , env , requestedAt ) ;
657689 await database
658690 . prepare (
659691 `INSERT INTO agent_identities
660- (id, handle, display_name, machine_scope, status, requested_at)
661- VALUES (?, ?, ?, ?, 'pending', ?)` ,
692+ (id, handle, display_name, machine_scope, status, requested_at,
693+ onboarding_auth_hash, onboarding_auth_status, onboarding_auth_length, onboarding_auth_checked_at)
694+ VALUES (?, ?, ?, ?, 'pending', ?, ?, ?, ?, ?)` ,
695+ )
696+ . bind (
697+ id ,
698+ input . handle ,
699+ input . displayName ,
700+ input . machineScope ,
701+ requestedAt ,
702+ authEvidence . hash ,
703+ authEvidence . status ,
704+ authEvidence . length ,
705+ authEvidence . checkedAt ,
662706 )
663- . bind ( id , input . handle , input . displayName , input . machineScope , requestedAt )
664707 . run ( ) ;
665708 const profile = profileValues ( input , id ) ;
666709 await database
@@ -1526,6 +1569,14 @@ async function approveAgent(request: Request, env: Env) {
15261569 const input = await body ( request ) ;
15271570 const agentId = String ( input . agentId ) ;
15281571 const database = db . db ;
1572+ const pendingAgent = await database
1573+ . prepare ( "SELECT onboarding_auth_status FROM agent_identities WHERE id = ?" )
1574+ . bind ( agentId )
1575+ . first < { onboarding_auth_status ?: string } > ( ) ;
1576+ if ( ! pendingAgent ) return json ( { error : "Agent identity was not found." } , 404 ) ;
1577+ if ( pendingAgent . onboarding_auth_status !== "verified" ) {
1578+ return json ( { error : "Onboarding auth has not been verified." } , 403 ) ;
1579+ }
15291580 await database
15301581 . prepare ( "UPDATE agent_identities SET status = 'approved', approved_at = ? WHERE id = ?" )
15311582 . bind ( now ( ) , agentId )
0 commit comments