mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-19 08:12:52 +00:00
feat(custom-metrics): accept the measurement token on value logging
A script or a home-automation rule holding a measurement token can now
post a value to POST /api/custom-metrics/{id}/entries for a custom metric
its owner already created. The route names MEASUREMENTS_WRITE_SCOPE on
the POST only; the value list, the metric list and every create, edit,
delete and restore leg still name no scope and refuse the token 403.
The token stays on its owner's record: requireAuth refuses any account
selector, and an id that is unknown, deleted or another account's answers
the same 404. The entry has no source column, so the audit row names the
token that wrote it.
The bearer-scope guard now freezes the scope's reach at three files, with
the custom-metric file naming it once and only on the POST. Integration
cases cover the admitted write, idempotent retry, the foreign and deleted
ids, five other narrow scopes, every other custom-metric leg, and a shared
record. The OpenAPI descriptions and the token card text in every locale
say what the token can do and where a metric's id is found.
Refs #1377
This commit is contained in:
1 parent
deb7db2ffa
commit
2167e251c1
17 files changed
+422
-41
No files matched your search
@@ -1,6 +1,11 @@
|
||||
import { NextRequest } from "next/server";
|
||||
|
||||
import { apiHandler, requireAuth, requireRecordAuth } from "@/lib/api-handler";
|
||||
import {
|
||||
apiHandler,
|
||||
isScopedCredential,
|
||||
requireAuth,
|
||||
requireRecordAuth,
|
||||
} from "@/lib/api-handler";
|
||||
import {
|
||||
apiError,
|
||||
apiSuccess,
|
||||
@@ -16,6 +21,7 @@ import { encryptNote } from "@/lib/crypto/note-cipher";
|
||||
import { prisma } from "@/lib/db";
|
||||
import { withIdempotency } from "@/lib/idempotency";
|
||||
import { annotate } from "@/lib/logging/context";
|
||||
import { MEASUREMENTS_WRITE_SCOPE } from "@/lib/measurements/scopes";
|
||||
import {
|
||||
createCustomMetricEntrySchema,
|
||||
listCustomMetricEntriesSchema,
|
||||
@@ -28,8 +34,9 @@ import {
|
||||
* GET lists the metric's values with offset pagination (the chart + history
|
||||
* feed). POST records a value, snapshotting the metric's current `unit` onto
|
||||
* the entry at write time (historical truth). `userId` is always narrowed from
|
||||
* the session and the parent metric is verified owner-scoped (a forged / foreign
|
||||
* id is a 404) before any value is read or written.
|
||||
* the session (or, on POST, the measurement-ingest token) and the parent metric
|
||||
* is verified owner-scoped (a forged / foreign id is a 404) before any value is
|
||||
* read or written.
|
||||
*/
|
||||
|
||||
type RouteParams = { params: Promise<{ id: string }> };
|
||||
@@ -105,7 +112,27 @@ async function postCustomMetricEntry(
|
||||
// product did not offer. Whoever builds a delegate-reachable way to log a
|
||||
// tracked value adds it back in the same diff: the argument for admitting it
|
||||
// was never wrong, and the GET arm above stays delegable meanwhile.
|
||||
const { user } = await requireAuth();
|
||||
//
|
||||
// It does accept the measurement-ingest token (#1377): a custom metric's
|
||||
// value is a reading the person defined themselves, and the bridge that
|
||||
// pushes their weight is the one that would push it. What that admits is
|
||||
// this POST and nothing beside it. The GET above names no scope, nor do the
|
||||
// metric's own create / edit / delete routes, so the token can add a value
|
||||
// to a metric that already exists and cannot list the metrics, read one
|
||||
// back, or make a new one. The id it writes to comes from the person, who
|
||||
// reads it off the metric's page; a token that has only ever been pasted
|
||||
// into a scale's uploader never learns one.
|
||||
//
|
||||
// `requireAuth` rather than the record resolver, so the selector refusal is
|
||||
// the undeclared-mode one every credential meets here: the token is on its
|
||||
// owner's record or nowhere. The ownership query below then turns any id
|
||||
// that is not that owner's live metric into the same 404 an unknown id gets.
|
||||
const auth = await requireAuth(MEASUREMENTS_WRITE_SCOPE);
|
||||
const { user } = auth;
|
||||
// A custom-metric entry has no source column, so the row cannot say a
|
||||
// bridge wrote it the way an `EXTERNAL` measurement does. The audit row
|
||||
// can, and it names the token, so revoking the right one is possible later.
|
||||
const ingestTokenId = isScopedCredential(auth) ? auth.session.id : null;
|
||||
|
||||
const { id } = await params;
|
||||
|
||||
@@ -156,12 +183,20 @@ async function postCustomMetricEntry(
|
||||
await auditLog("customMetricEntry.create", {
|
||||
userId: user.id,
|
||||
ipAddress: getClientIp(request),
|
||||
details: { customMetricId: id, entryId: created.id },
|
||||
details: {
|
||||
customMetricId: id,
|
||||
entryId: created.id,
|
||||
...(ingestTokenId ? { ingestTokenId } : {}),
|
||||
},
|
||||
});
|
||||
|
||||
annotate({
|
||||
action: { name: "custom-metric.entry.create" },
|
||||
meta: { customMetricId: id, entryId: created.id },
|
||||
meta: {
|
||||
customMetricId: id,
|
||||
entryId: created.id,
|
||||
via_ingest_scope: ingestTokenId !== null,
|
||||
},
|
||||
});
|
||||
invalidateUserCorrelationPatterns(user.id);
|
||||
|
||||
|
||||
Reference in new issue
Block a user