mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-21 19:40:34 +00:00
feat(native): push inside the iOS app, through an encrypted relay
The app subscribes to Web Push like a browser, only the app makes the subscription: it asks for permission, registers with APNs and keeps its own P-256 key pair and auth secret in a keychain group a notification extension can share. The endpoint is liketrek's relay for this device's token, so the server encrypts exactly as for a browser and nobody on the way can read the notification. The settings card, the test push, sign-in resync and the logout teardown all take this path where the binary has nativePush; a tapped notification opens the place it points at, also from a cold start. The App ID has push enabled, the app carries the aps-environment and keychain-group entitlements.
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
import type { PushSubscriptionInput } from '@trek/shared';
|
||||
import { hasNativeFeature } from './capabilities';
|
||||
import { isNativeIos } from './platform';
|
||||
import { reportNativeFailure } from './reportFailure';
|
||||
import { TrekShell } from './trekShell';
|
||||
|
||||
/**
|
||||
* Push inside the app: the same Web Push subscription a browser makes, only
|
||||
* the app makes it (TrekShell.registerPush). Its endpoint is liketrek's relay
|
||||
* for this device's APNs token and its keys live on the device, so the server
|
||||
* encrypts exactly as for a browser and the relay hands on ciphertext that the
|
||||
* app's notification extension decrypts. push/webPush.ts takes this path
|
||||
* wherever the binary reports the nativePush feature; the settings card, the
|
||||
* test push and the logout teardown stay what they are.
|
||||
*
|
||||
* The endpoint is remembered on the device, the way a browser remembers its
|
||||
* subscription, so the card can say on or off and the logout can name it.
|
||||
*/
|
||||
const KEY = 'trek.nativePushEndpoint';
|
||||
|
||||
export function nativePushActive(): boolean {
|
||||
return isNativeIos() && hasNativeFeature('nativePush');
|
||||
}
|
||||
|
||||
export function nativePushEndpoint(): string | null {
|
||||
try {
|
||||
return window.localStorage.getItem(KEY);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function rememberEndpoint(endpoint: string | null): void {
|
||||
try {
|
||||
if (endpoint) window.localStorage.setItem(KEY, endpoint);
|
||||
else window.localStorage.removeItem(KEY);
|
||||
} catch {
|
||||
// Storage refused: the card then reads off and offers to switch it on again.
|
||||
}
|
||||
}
|
||||
|
||||
export class NativePushDenied extends Error {}
|
||||
|
||||
/** Permission, APNs and the device keys, as a subscription the server can store. */
|
||||
export async function registerNativePush(): Promise<PushSubscriptionInput> {
|
||||
try {
|
||||
const subscription = await TrekShell.registerPush();
|
||||
rememberEndpoint(subscription.endpoint);
|
||||
return subscription;
|
||||
} catch (err) {
|
||||
if ((err as { code?: string } | null)?.code === 'DENIED') throw new NativePushDenied();
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
export function forgetNativePush(): void {
|
||||
rememberEndpoint(null);
|
||||
}
|
||||
|
||||
/** Opens the place a tapped notification points at, inside the app's own routing. */
|
||||
export function openPushTarget(url: string): void {
|
||||
let target: URL;
|
||||
try {
|
||||
target = new URL(url, window.location.origin);
|
||||
} catch {
|
||||
return;
|
||||
}
|
||||
// Only this server's pages: a notification never navigates the app elsewhere.
|
||||
if (target.origin !== window.location.origin) return;
|
||||
window.history.pushState({}, '', `${target.pathname}${target.search}${target.hash}`);
|
||||
window.dispatchEvent(new PopStateEvent('popstate'));
|
||||
}
|
||||
|
||||
/** Follows taps on notifications, including the one the app was opened from. */
|
||||
export async function startNativePush(): Promise<void> {
|
||||
if (!nativePushActive()) return;
|
||||
void TrekShell.addListener('pushOpened', ({ url }) => openPushTarget(url));
|
||||
try {
|
||||
const { url } = await TrekShell.takeOpenedPush();
|
||||
if (url) openPushTarget(url);
|
||||
} catch (err) {
|
||||
reportNativeFailure(err);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user