mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-26 22:57:32 +00:00
feat(notifications): Web Push delivers notifications to every device, even with TREK closed
A fourth built-in channel next to email, webhook and ntfy. Browsers register per device, the server encrypts per RFC 8291 and signs per RFC 8292 with node:crypto and sends only to known push services through the SSRF guard. The event matrix decides per category what arrives by push; admins switch the channel on under Notifications. VAPID keys come from the environment or are generated once, only while neither stored row exists, and kept encrypted. A pair that does not decrypt or match is never overwritten and never swapped for another one: push stays off with an error in the log and every device stays registered until the key is fixed. The subject is VAPID_SUBJECT, then an https APP_URL, then the project URL. Only chat messages collapse into one notification per trip, and a replacement still alerts. A 403 removes a device only after three failures in a row; 404 and 410 remove it at once. Password change, reset and an admin setting a password drop the user's devices with the other credentials. The request log redacts subscriptions and endpoints, boot errors print *** for every secret variable, and logout forgets the device with a plain fetch so an expired session cannot redirect midway. Addresses discussion #894.
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
import { Body, Controller, Delete, Get, Headers, HttpCode, HttpException, Post, UseGuards } from '@nestjs/common';
|
||||
import type { PushPublicKeyResult, PushSubscribeResult, PushUnsubscribeResult } from '@trek/shared';
|
||||
import type { User } from '../../../types';
|
||||
import { CurrentUser } from '../../auth/current-user.decorator';
|
||||
import { JwtAuthGuard } from '../../auth/jwt-auth.guard';
|
||||
import { PushSubscribeDto, PushUnsubscribeDto } from '../notifications.dto';
|
||||
import { checkPushSubscription } from './push-subscription.helpers';
|
||||
import { PushSubscriptionsService } from './push-subscriptions.service';
|
||||
import { PUSH_UNAVAILABLE_ERROR, PushUnavailableError, VapidKeysService } from './vapid-keys.service';
|
||||
|
||||
/**
|
||||
* /api/notifications/push: how a browser signs up for Web Push (#894).
|
||||
*
|
||||
* The browser fetches the server's public key, subscribes with its push
|
||||
* service, and hands the subscription over here; logging out or switching push
|
||||
* off in that browser deletes it again. Which events arrive is the ordinary
|
||||
* preference matrix ('push' column), and the test send is the generic
|
||||
* POST /api/notifications/test/push.
|
||||
*
|
||||
* No MCP tool mirrors these routes on purpose: a subscription is created by a
|
||||
* browser's push manager for that one browser, and there is nothing an MCP
|
||||
* client could subscribe with.
|
||||
*/
|
||||
@Controller('api/notifications/push')
|
||||
@UseGuards(JwtAuthGuard)
|
||||
export class PushController {
|
||||
constructor(
|
||||
private readonly keys: VapidKeysService,
|
||||
private readonly subscriptions: PushSubscriptionsService,
|
||||
) {}
|
||||
|
||||
@Get('public-key')
|
||||
publicKey(): PushPublicKeyResult {
|
||||
return { publicKey: this.currentPublicKey() };
|
||||
}
|
||||
|
||||
@Post('subscriptions')
|
||||
@HttpCode(200)
|
||||
subscribe(
|
||||
@CurrentUser() user: User,
|
||||
@Body() body: PushSubscribeDto,
|
||||
@Headers('user-agent') userAgent?: string,
|
||||
): PushSubscribeResult {
|
||||
const checked = checkPushSubscription(body.subscription);
|
||||
if ('error' in checked) throw new HttpException({ error: checked.error }, 400);
|
||||
const devices = this.subscriptions.upsert(user.id, checked.value, this.currentPublicKey(), userAgent);
|
||||
return { success: true, devices };
|
||||
}
|
||||
|
||||
/**
|
||||
* Idempotent: the push service may already have expired the row, and a
|
||||
* browser logging out must not get an error for that. Only the caller's own
|
||||
* endpoint can go; the same URL on someone else's account is left alone.
|
||||
* Works while push is unavailable too, so a device can always be forgotten.
|
||||
*/
|
||||
@Delete('subscriptions')
|
||||
@HttpCode(200)
|
||||
unsubscribe(@CurrentUser() user: User, @Body() body: PushUnsubscribeDto): PushUnsubscribeResult {
|
||||
this.subscriptions.removeForUser(user.id, body.endpoint);
|
||||
return { success: true };
|
||||
}
|
||||
|
||||
/**
|
||||
* The key browsers subscribe with. While the key pair cannot be used, stored
|
||||
* or from VAPID_* (the log says why), the answer is a 503: nothing could be
|
||||
* signed for a new subscription, and the devices already subscribed stay as
|
||||
* they are.
|
||||
*/
|
||||
private currentPublicKey(): string {
|
||||
try {
|
||||
return this.keys.getPublicKey();
|
||||
} catch (err) {
|
||||
if (err instanceof PushUnavailableError) throw new HttpException({ error: PUSH_UNAVAILABLE_ERROR }, 503);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user