File size: 9,194 Bytes
17c0464 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 | /**
* @fileoverview The admin portal's miniapp registry (`/api/admin/registry`, `docs/registry.md`), mounted by
* `admin.api.ts` behind `organization.registry.administer`, which only an Organization Admin holds: no operator key
* carries it.
*
* GET /entries?q=&status=&verification=&cursor= every entry, newest first, 50 to a page
* GET /export every entry as CSV, for App Review
* POST /register {urls} registers 1 to 50 URLs as the Organization Admin, verified as a
* phone's registration would be
* GET /coverage?kind=&value= what archiving a rule would cover, asked before archiving
* POST /archive {rule: {kind, value}, reason?} archives by URL, package, domain or account
* POST /unarchive {rule: {kind, value}}
* GET /rules?cursor= the archive rules, newest first
*
* Archiving, unarchiving, each URL registered and each export are recorded in the admin audit log
* (`admin_action_audit_logs`, the admin portal's Audit log page) with the Organization Admin's email.
* A malformed request is `400 invalid_request`.
*/
import {
ADMIN_REGISTRY_QUERY_MAX_LENGTH,
ADMIN_REGISTRY_REASON_MAX_LENGTH,
ADMIN_REGISTRY_REGISTER_MAX_URLS,
ADMIN_REGISTRY_RULE_KINDS,
ADMIN_REGISTRY_STATUSES,
ADMIN_REGISTRY_VERIFICATIONS,
type AdminRegistryArchiveResponse,
type AdminRegistryRegisterResponse,
type AdminRegistryUnarchiveResponse,
} from "@mentra/workspace-contract"
import {Hono} from "hono"
import {bodyLimit} from "hono/body-limit"
import {z} from "zod"
import {AdminActionAuditLogModel} from "../../models/admin-action-audit-log.model"
import {
distinctUrls,
listArchiveRules,
listRegistryEntries,
registerUrlsAsAdmin,
registryCoverage,
registryCsv,
REGISTRY_CURSOR_PATTERN,
requireArchiveRule,
} from "../../services/registry/registry-admin.service"
import {archive, RegistryRequestError, unarchive, type RegistryArchiveRule} from "../../services/registry/registry.service"
import type {AppContext, AppEnv} from "../../types/hono.types"
import {InvalidRequest} from "../../types/oauth.types"
import {principalLabel} from "../middleware/principal.middleware"
import {readJsonObject} from "../workspaces/request"
/** A list of 50 URLs, each up to 2,048 characters, fits with room to spare. */
const BODY_LIMIT_BYTES = 256 * 1024
const MAX_URL_LENGTH = 2048
/** The longest a rule's value can be: a URL. */
const MAX_RULE_VALUE_LENGTH = MAX_URL_LENGTH
const cursorSchema = z.string().regex(REGISTRY_CURSOR_PATTERN).optional()
const listQuerySchema = z.object({
q: z.string().max(ADMIN_REGISTRY_QUERY_MAX_LENGTH).optional(),
status: z.enum(ADMIN_REGISTRY_STATUSES).optional(),
verification: z.enum(ADMIN_REGISTRY_VERIFICATIONS).optional(),
cursor: cursorSchema,
})
const ruleSchema = z.object({
kind: z.enum(ADMIN_REGISTRY_RULE_KINDS),
value: z.string().min(1).max(MAX_RULE_VALUE_LENGTH),
})
const archiveSchema = z.object({rule: ruleSchema, reason: z.string().max(ADMIN_REGISTRY_REASON_MAX_LENGTH).optional()})
const unarchiveSchema = z.object({rule: ruleSchema})
/** Blank lines are allowed and dropped; the body limit bounds how many there can be. */
const registerSchema = z.object({urls: z.array(z.string().max(MAX_URL_LENGTH))})
const app = new Hono<AppEnv>()
app.use(
"*",
bodyLimit({
maxSize: BODY_LIMIT_BYTES,
onError: c => c.json({error: "payload_too_large", error_description: "request body too large"}, 413),
}),
)
/** `schema`'s reading of `value`, or a 400 saying what was expected. */
function parse<T extends z.ZodTypeAny>(schema: T, value: unknown, expected: string): z.infer<T> {
const parsed = schema.safeParse(value)
if (!parsed.success) throw new InvalidRequest(expected)
return parsed.data
}
/** A rule Core can keep, in its form; a 400 for one that can't name any entry. */
function validRule(rule: RegistryArchiveRule): RegistryArchiveRule {
try {
return requireArchiveRule(rule)
} catch (error) {
if (error instanceof RegistryRequestError) throw new InvalidRequest(error.message)
throw error
}
}
/** Who did it, for the audit log and a rule's author: the Organization Admin's email. */
function actor(c: AppContext): string {
return principalLabel(c.get("principal")!)
}
function audit(
c: AppContext,
event: {action: string; targetType: string; targetId: string; reason?: string | null; metadata?: Record<string, unknown>},
) {
return {
adminId: actor(c),
action: event.action,
targetType: event.targetType,
targetId: event.targetId,
reason: event.reason ?? null,
metadata: event.metadata ?? null,
}
}
const ruleTarget = (rule: RegistryArchiveRule) => `${rule.kind}:${rule.value}`
app.get("/entries", async c => {
const query = parse(
listQuerySchema,
{
q: c.req.query("q"),
status: c.req.query("status") || undefined,
verification: c.req.query("verification") || undefined,
cursor: c.req.query("cursor") || undefined,
},
`q is at most ${ADMIN_REGISTRY_QUERY_MAX_LENGTH} characters, status is active or archived, verification is verified or phone_reported, and cursor is a page's next`,
)
c.header("cache-control", "no-store")
return c.json(await listRegistryEntries(query))
})
app.get("/export", async c => {
await AdminActionAuditLogModel.create(audit(c, {action: "miniapp_registry.export", targetType: "miniapp_registry", targetId: "entries"}))
const lines = registryCsv()
const encoder = new TextEncoder()
const body = new ReadableStream<Uint8Array>({
async pull(controller) {
try {
const {value, done} = await lines.next()
if (done) controller.close()
else controller.enqueue(encoder.encode(value))
} catch (error) {
controller.error(error)
}
},
async cancel() {
await lines.return(undefined)
},
})
return new Response(body, {
headers: {
"content-type": "text/csv; charset=utf-8",
"content-disposition": 'attachment; filename="miniapp-registry.csv"',
"cache-control": "no-store",
},
})
})
app.post("/register", async c => {
const principal = c.get("principal")!
if (principal.kind !== "user") return c.json({error: "forbidden", error_description: "a signed-in person is required"}, 403)
const body = parse(registerSchema, await readJsonObject(c), "body must be {urls: [miniapp URLs]}")
const urls = distinctUrls(body.urls)
if (urls.length === 0 || urls.length > ADMIN_REGISTRY_REGISTER_MAX_URLS) {
throw new InvalidRequest(`urls must name 1 to ${ADMIN_REGISTRY_REGISTER_MAX_URLS} miniapp URLs`)
}
const results = await registerUrlsAsAdmin(urls, {mentraUserId: principal.mentraUserId, tenantId: principal.tenantId})
await AdminActionAuditLogModel.insertMany(
results.map((result, index) =>
audit(c, {
action: "miniapp_registry.register",
targetType: "miniapp_url",
targetId: urls[index]!.key,
metadata: {result: result.result},
}),
),
)
const response: AdminRegistryRegisterResponse = {results}
c.header("cache-control", "no-store")
return c.json(response)
})
app.get("/coverage", async c => {
const rule = parse(ruleSchema, {kind: c.req.query("kind"), value: c.req.query("value")}, "kind and value must name an archive rule")
c.header("cache-control", "no-store")
return c.json(await registryCoverage(validRule(rule)))
})
app.post("/archive", async c => {
const body = parse(archiveSchema, await readJsonObject(c), "body must be {rule: {kind, value}, reason?}")
const rule = validRule(body.rule)
const reason = body.reason?.trim() ?? ""
const result: AdminRegistryArchiveResponse = await archive(rule, {reason, createdBy: actor(c)})
await AdminActionAuditLogModel.create(
audit(c, {
action: "miniapp_registry.archive",
targetType: "miniapp_registry_rule",
targetId: ruleTarget(result.rule),
reason: reason || null,
metadata: {created: result.created, entriesArchived: result.entriesArchived},
}),
)
return c.json(result)
})
app.post("/unarchive", async c => {
const body = parse(unarchiveSchema, await readJsonObject(c), "body must be {rule: {kind, value}}")
const result: AdminRegistryUnarchiveResponse = await unarchive(validRule(body.rule))
await AdminActionAuditLogModel.create(
audit(c, {
action: "miniapp_registry.unarchive",
targetType: "miniapp_registry_rule",
targetId: ruleTarget(result.rule),
metadata: {removed: result.removed, entriesRestored: result.entriesRestored},
}),
)
return c.json(result)
})
app.get("/rules", async c => {
const cursor = parse(cursorSchema, c.req.query("cursor") || undefined, "cursor is a page's next")
c.header("cache-control", "no-store")
return c.json(await listArchiveRules({cursor}))
})
export default app
|