mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-06 18:54:17 +00:00
feat(journey): protect a public journey link with a password and an expiry
A journey's share link can now ask readers for a password, stop working on a date its owner picks, and count how often it was opened (#2553, #1702, no password, no expiry and no views. - journey_share_tokens gains password_hash, password_version, expires_at, view_count and last_viewed_at (append-only migration, SQLite and Postgres, added only where missing). - The password is hashed with bcrypt at the account cost. POST /api/public/journey/:token/unlock checks it, rate limited per address and link (only refusals count), and sets an httpOnly cookie scoped to the link's API path holding an HMAC over the token and the password version. Secure follows the request, so plain-HTTP installs keep working. Changing or removing the password raises the version and locks every old cookie out. - Every public route, the photo proxies included, first checks the link: 404 unknown, 410 expired (JOURNEY_LINK_EXPIRED), 401 locked (JOURNEY_PASSWORD_REQUIRED). - Saving the share switches keeps the password and the expiry; the owner's read returns has_password, expires_at, view_count and last_viewed_at, never the hash, and none of it reaches the public payload. - A reader is counted once a day per address and browser, in an in-memory store behind an injectable port; a restart may count them again. - create_journey_share_link takes password and expires_at like the REST route; get_journey_share_link reports has_password.
This commit is contained in:
@@ -18,6 +18,11 @@ export class JourneyShareTokens {
|
||||
newest_first: number & Opt = 0;
|
||||
share_plan: number & Opt = 0;
|
||||
share_recordings: number & Opt = 0;
|
||||
password_hash?: string | null;
|
||||
password_version: number & Opt = 0;
|
||||
expires_at?: string | null;
|
||||
view_count: number & Opt = 0;
|
||||
last_viewed_at?: string | null;
|
||||
createdByRef!: Ref<Users>;
|
||||
}
|
||||
|
||||
@@ -38,6 +43,11 @@ export const JourneyShareTokensSchema = defineEntity({
|
||||
newest_first: p.integer().default(0),
|
||||
share_plan: p.integer().default(0),
|
||||
share_recordings: p.integer().default(0),
|
||||
password_hash: p.text().nullable(),
|
||||
password_version: p.integer().default(0),
|
||||
expires_at: p.type(DbTimestampType).nullable(),
|
||||
view_count: p.integer().default(0),
|
||||
last_viewed_at: p.type(DbTimestampType).nullable(),
|
||||
createdByRef: () => p.manyToOne(Users).ref().joinColumn('created_by').hidden().index('idx_journey_share_tokens_created_by'),
|
||||
},
|
||||
});
|
||||
|
||||
@@ -14,4 +14,9 @@ export interface JourneyShareTokensTable {
|
||||
newest_first: InsertOptional<number>;
|
||||
share_plan: InsertOptional<number>;
|
||||
share_recordings: InsertOptional<number>;
|
||||
password_hash: InsertOptional<string | null>;
|
||||
password_version: InsertOptional<number>;
|
||||
expires_at: InsertOptional<string | null>;
|
||||
view_count: InsertOptional<number>;
|
||||
last_viewed_at: InsertOptional<string | null>;
|
||||
}
|
||||
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
import { addColumnIfMissing } from '../migration-utils';
|
||||
import { Migration } from '@mikro-orm/migrations';
|
||||
|
||||
/**
|
||||
* A journey's public link can ask a reader for a password, stop working on a
|
||||
* date its owner picks, and count how often it was opened (#2553, #1702, #2959).
|
||||
*
|
||||
* - `password_hash`: the bcrypt hash of the link's password, null for a link
|
||||
* anyone with the address may read.
|
||||
* - `password_version`: raised whenever the password is set, changed or taken
|
||||
* off. A reader's unlock cookie is signed over it, so a changed password
|
||||
* locks out everybody who unlocked the old one.
|
||||
* - `expires_at`: when the link stops working, null for never.
|
||||
* - `view_count` / `last_viewed_at`: how many readers opened it, each counted
|
||||
* once a day, and when the last one did. The owner sees them, a reader never.
|
||||
*
|
||||
* Every existing link keeps working exactly as before: no password, no expiry,
|
||||
* nothing counted yet.
|
||||
*/
|
||||
export class Migration20200101046620_a_journey_link_can_ask_for_a_password_and_run_out extends Migration {
|
||||
override name = 'Migration20200101046620_a_journey_link_can_ask_for_a_password_and_run_out';
|
||||
|
||||
override async up(): Promise<void> {
|
||||
await addColumnIfMissing(this, 'journey_share_tokens', 'password_hash', {
|
||||
sqlite: 'password_hash TEXT',
|
||||
postgres: '"password_hash" text null',
|
||||
});
|
||||
await addColumnIfMissing(this, 'journey_share_tokens', 'password_version', {
|
||||
sqlite: 'password_version INTEGER NOT NULL DEFAULT 0',
|
||||
postgres: '"password_version" bigint not null default 0',
|
||||
});
|
||||
await addColumnIfMissing(this, 'journey_share_tokens', 'expires_at', {
|
||||
sqlite: 'expires_at DATETIME',
|
||||
postgres: '"expires_at" text null',
|
||||
});
|
||||
await addColumnIfMissing(this, 'journey_share_tokens', 'view_count', {
|
||||
sqlite: 'view_count INTEGER NOT NULL DEFAULT 0',
|
||||
postgres: '"view_count" bigint not null default 0',
|
||||
});
|
||||
await addColumnIfMissing(this, 'journey_share_tokens', 'last_viewed_at', {
|
||||
sqlite: 'last_viewed_at DATETIME',
|
||||
postgres: '"last_viewed_at" text null',
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,10 @@
|
||||
import { currentTimestampKysely } from '../dialect/kysely-functions';
|
||||
import type { JourneyShareTokens } from '../entities/JourneyShareTokens.entity';
|
||||
import type { DB } from '../kysely/db';
|
||||
import { TrekRepository } from './_shared/trek-repository';
|
||||
|
||||
type ShareTokensDB = Pick<DB, 'journey_share_tokens'>;
|
||||
|
||||
/** JS1's narrow projection — `createOrUpdateJourneyShareLink`'s existing-link read. */
|
||||
export interface JourneyShareLinkFlagsRow {
|
||||
token: string;
|
||||
@@ -10,6 +14,9 @@ export interface JourneyShareLinkFlagsRow {
|
||||
newest_first: number;
|
||||
share_plan: number;
|
||||
share_recordings: number;
|
||||
password_hash: string | null;
|
||||
password_version: number;
|
||||
expires_at: string | null;
|
||||
}
|
||||
|
||||
/** JS4/JS11's full-row shape (`getJourneyShareLink`, `getPublicJourney`'s entrypoint read). */
|
||||
@@ -25,6 +32,21 @@ export interface JourneyShareTokenRow {
|
||||
newest_first: number;
|
||||
share_plan: number;
|
||||
share_recordings: number;
|
||||
/** The bcrypt hash of the link's password, null when anyone with the address may read. */
|
||||
password_hash: string | null;
|
||||
/** Raised on every password change; the unlock cookie is signed over it. */
|
||||
password_version: number;
|
||||
/** When the link stops working (canonical DATETIME text), null for never. */
|
||||
expires_at: string | null;
|
||||
view_count: number;
|
||||
last_viewed_at: string | null;
|
||||
}
|
||||
|
||||
/** A link's password and expiry, as stored. */
|
||||
export interface JourneyShareProtection {
|
||||
password_hash: string | null;
|
||||
password_version: number;
|
||||
expires_at: string | null;
|
||||
}
|
||||
|
||||
/** Every flag of a link, as stored (0/1). */
|
||||
@@ -104,6 +126,9 @@ export class JourneyShareTokensRepository extends TrekRepository<JourneyShareTok
|
||||
'jst.newest_first',
|
||||
'jst.share_plan',
|
||||
'jst.share_recordings',
|
||||
'jst.password_hash',
|
||||
'jst.password_version',
|
||||
'jst.expires_at',
|
||||
])
|
||||
.where({ journey: journeyId })
|
||||
.execute<JourneyShareLinkFlagsRow | undefined>('get', false);
|
||||
@@ -121,9 +146,36 @@ export class JourneyShareTokensRepository extends TrekRepository<JourneyShareTok
|
||||
await this.nativeUpdate({ journey: journeyId }, flags);
|
||||
}
|
||||
|
||||
/**
|
||||
* The password and expiry of a link, written as the service resolved them
|
||||
* (the same "keep what was not given" merge as the flags above).
|
||||
*/
|
||||
async updateProtection(journeyId: number, protection: JourneyShareProtection): Promise<void> {
|
||||
await this.nativeUpdate({ journey: journeyId }, protection);
|
||||
}
|
||||
|
||||
/** JS3 — the create branch: `INSERT INTO journey_share_tokens (journey_id, token, created_by, share_timeline, share_gallery, share_map, newest_first) VALUES (?,?,?,?,?,?,?)`. */
|
||||
async insertLink(journeyId: number, token: string, createdBy: number, flags: JourneyShareFlags): Promise<void> {
|
||||
await this.insert({ journey: journeyId, token, createdByRef: createdBy, ...flags });
|
||||
async insertLink(
|
||||
journeyId: number,
|
||||
token: string,
|
||||
createdBy: number,
|
||||
flags: JourneyShareFlags,
|
||||
protection: JourneyShareProtection = { password_hash: null, password_version: 0, expires_at: null },
|
||||
): Promise<void> {
|
||||
await this.insert({ journey: journeyId, token, createdByRef: createdBy, ...flags, ...protection });
|
||||
}
|
||||
|
||||
/**
|
||||
* One more reader opened the link: count it and remember when. A single
|
||||
* statement, so two readers at once both count.
|
||||
*/
|
||||
async recordView(id: number): Promise<void> {
|
||||
const now = currentTimestampKysely(this.getEntityManager().getPlatform());
|
||||
await this.kysely<ShareTokensDB>()
|
||||
.updateTable('journey_share_tokens')
|
||||
.set((eb) => ({ view_count: eb('view_count', '+', 1), last_viewed_at: now }))
|
||||
.where('id', '=', id)
|
||||
.execute();
|
||||
}
|
||||
|
||||
/** JS4 — `getJourneyShareLink`: `SELECT * FROM journey_share_tokens WHERE journey_id = ?`. */
|
||||
|
||||
Reference in New Issue
Block a user