@oxy.so/core 1.12.1 → 1.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1934,7 +1934,17 @@
1934
1934
  "warning": "This action cannot be undone. Your account and all associated data will be permanently deleted.",
1935
1935
  "confirmLabel": "Type \"{{username}}\" to confirm",
1936
1936
  "confirm": "Delete Forever",
1937
- "error": "Failed to delete account"
1937
+ "error": "Failed to delete account",
1938
+ "handoff": {
1939
+ "commonsTitle": "Delete your account in Oxy Commons",
1940
+ "commonsMessage": "Your identity key is kept by Oxy Commons on this device, and deleting your account needs it. Commons will open on Settings > Delete account to finish.",
1941
+ "openCommons": "Open Oxy Commons",
1942
+ "commonsOpenFailed": "Couldn't open Oxy Commons. Open it yourself and go to Settings > Delete account.",
1943
+ "elsewhereTitle": "Delete your account where your identity is",
1944
+ "elsewhereMessage": "Deleting your account needs your identity key, and it isn't on this device. Open Oxy Commons on the device that holds your identity and go to Settings > Delete account, or open {{site}} in a browser that holds it.",
1945
+ "gotIt": "Got it",
1946
+ "identityUnreadable": "Couldn't read this device's identity storage. Unlock your device and try again."
1947
+ }
1938
1948
  },
1939
1949
  "manageAccount": {
1940
1950
  "title": "Manage your Oxy Account",
@@ -1934,7 +1934,17 @@
1934
1934
  "warning": "Esta acción no se puede deshacer. Tu cuenta y todos los datos asociados se eliminarán de forma permanente.",
1935
1935
  "confirmLabel": "Escribe \"{{username}}\" para confirmar",
1936
1936
  "confirm": "Eliminar para siempre",
1937
- "error": "No se pudo eliminar la cuenta"
1937
+ "error": "No se pudo eliminar la cuenta",
1938
+ "handoff": {
1939
+ "commonsTitle": "Elimina tu cuenta en Oxy Commons",
1940
+ "commonsMessage": "Tu clave de identidad la guarda Oxy Commons en este dispositivo, y eliminar tu cuenta la necesita. Commons se abrirá en Ajustes > Eliminar cuenta para terminar.",
1941
+ "openCommons": "Abrir Oxy Commons",
1942
+ "commonsOpenFailed": "No se pudo abrir Oxy Commons. Ábrelo tú y ve a Ajustes > Eliminar cuenta.",
1943
+ "elsewhereTitle": "Elimina tu cuenta donde está tu identidad",
1944
+ "elsewhereMessage": "Eliminar tu cuenta necesita tu clave de identidad, y no está en este dispositivo. Abre Oxy Commons en el dispositivo que guarda tu identidad y ve a Ajustes > Eliminar cuenta, o abre {{site}} en un navegador que la guarde.",
1945
+ "gotIt": "Entendido",
1946
+ "identityUnreadable": "No se pudo leer el almacén de identidad de este dispositivo. Desbloquea el dispositivo e inténtalo de nuevo."
1947
+ }
1938
1948
  },
1939
1949
  "manageAccount": {
1940
1950
  "title": "Gestiona tu cuenta de Oxy",
@@ -1934,7 +1934,17 @@
1934
1934
  "warning": "This action cannot be undone. Your account and all associated data will be permanently deleted.",
1935
1935
  "confirmLabel": "Type \"{{username}}\" to confirm",
1936
1936
  "confirm": "Delete Forever",
1937
- "error": "Failed to delete account"
1937
+ "error": "Failed to delete account",
1938
+ "handoff": {
1939
+ "commonsTitle": "Delete your account in Oxy Commons",
1940
+ "commonsMessage": "Your identity key is kept by Oxy Commons on this device, and deleting your account needs it. Commons will open on Settings > Delete account to finish.",
1941
+ "openCommons": "Open Oxy Commons",
1942
+ "commonsOpenFailed": "Couldn't open Oxy Commons. Open it yourself and go to Settings > Delete account.",
1943
+ "elsewhereTitle": "Delete your account where your identity is",
1944
+ "elsewhereMessage": "Deleting your account needs your identity key, and it isn't on this device. Open Oxy Commons on the device that holds your identity and go to Settings > Delete account, or open {{site}} in a browser that holds it.",
1945
+ "gotIt": "Got it",
1946
+ "identityUnreadable": "Couldn't read this device's identity storage. Unlock your device and try again."
1947
+ }
1938
1948
  },
1939
1949
  "manageAccount": {
1940
1950
  "title": "Manage your Oxy Account",
@@ -1934,7 +1934,17 @@
1934
1934
  "warning": "Esta acción no se puede deshacer. Tu cuenta y todos los datos asociados se eliminarán de forma permanente.",
1935
1935
  "confirmLabel": "Escribe \"{{username}}\" para confirmar",
1936
1936
  "confirm": "Eliminar para siempre",
1937
- "error": "No se pudo eliminar la cuenta"
1937
+ "error": "No se pudo eliminar la cuenta",
1938
+ "handoff": {
1939
+ "commonsTitle": "Elimina tu cuenta en Oxy Commons",
1940
+ "commonsMessage": "Tu clave de identidad la guarda Oxy Commons en este dispositivo, y eliminar tu cuenta la necesita. Commons se abrirá en Ajustes > Eliminar cuenta para terminar.",
1941
+ "openCommons": "Abrir Oxy Commons",
1942
+ "commonsOpenFailed": "No se pudo abrir Oxy Commons. Ábrelo tú y ve a Ajustes > Eliminar cuenta.",
1943
+ "elsewhereTitle": "Elimina tu cuenta donde está tu identidad",
1944
+ "elsewhereMessage": "Eliminar tu cuenta necesita tu clave de identidad, y no está en este dispositivo. Abre Oxy Commons en el dispositivo que guarda tu identidad y ve a Ajustes > Eliminar cuenta, o abre {{site}} en un navegador que la guarde.",
1945
+ "gotIt": "Entendido",
1946
+ "identityUnreadable": "No se pudo leer el almacén de identidad de este dispositivo. Desbloquea el dispositivo e inténtalo de nuevo."
1947
+ }
1938
1948
  },
1939
1949
  "manageAccount": {
1940
1950
  "title": "Gestiona tu cuenta de Oxy",
package/dist/esm/index.js CHANGED
@@ -34,6 +34,7 @@ export { getCommonsApprovalBlockingReason, parseCommonsApprovalExpiresAt, } from
34
34
  // Automatic "Sign in with Oxy" delivery selection — ONE pure decision that maps
35
35
  // the caller's facts onto exactly one primary route (open Commons / await push / QR).
36
36
  export { selectCommonsDelivery, pushTargetsFromDelivery, commonsDeliveryPlatform } from './utils/commonsDelivery.js';
37
+ export { OxyAccountEventError, OXY_ACCOUNT_DELETED_EVENT_URI } from './mixins/OxyServices.utility.js';
37
38
  export { OxyAppDataIdentifierError } from './mixins/OxyServices.appData.js';
38
39
  // ---------------------------------------------------------------------------
39
40
  // User identity and handles
@@ -7,6 +7,15 @@ import { jwtDecode } from 'jwt-decode';
7
7
  import { loadNodeCrypto } from '@oxy.so/protocol';
8
8
  import { logger } from '../logger/index.js';
9
9
  import { OXY_SERVICE_ENVIRONMENTS } from '../utils/oxyServiceEnvironment.js';
10
+ /** The SET `events` member Oxy uses for an account deletion. */
11
+ export const OXY_ACCOUNT_DELETED_EVENT_URI = 'https://oxy.so/events/account.deleted';
12
+ /** Why an account event token was refused. `message` never contains the token. */
13
+ export class OxyAccountEventError extends Error {
14
+ constructor(message) {
15
+ super(message);
16
+ this.name = 'OxyAccountEventError';
17
+ }
18
+ }
10
19
  /**
11
20
  * Expected JWT audience for tokens issued by the Oxy auth service.
12
21
  */
@@ -133,6 +142,135 @@ export function OxyServicesUtilityMixin(Base) {
133
142
  return null;
134
143
  }
135
144
  }
145
+ /**
146
+ * Verify an account event token from Oxy — the body of an account-event
147
+ * webhook, or an entry's `token` from `listAccountEvents` — and return the
148
+ * event. Throws {@link OxyAccountEventError} unless the token is an
149
+ * EdDSA-signed `secevent+jwt` from Oxy's published key set, issued by
150
+ * `oxy-auth`, addressed to this application, carrying exactly one known
151
+ * event. A service token (`typ: JWT`) is refused here, as this token is
152
+ * refused as a service token.
153
+ *
154
+ * Node only (it needs `node:crypto`). The caller must still be idempotent on
155
+ * `eventId`: Oxy delivers at least once, by push and by pull.
156
+ */
157
+ async verifyAccountEvent(token, options = {}) {
158
+ if (typeof token !== 'string' || token.length === 0 || token.length > 16 * 1024) {
159
+ throw new OxyAccountEventError('Account event token is missing or oversized');
160
+ }
161
+ const parts = token.split('.');
162
+ if (parts.length !== 3 || parts.some((part) => part.length === 0)) {
163
+ throw new OxyAccountEventError('Account event token is not a compact JWS');
164
+ }
165
+ const [headerB64, payloadB64, signatureB64] = parts;
166
+ let header;
167
+ let payload;
168
+ try {
169
+ header = parseJsonSegment(headerB64);
170
+ payload = parseJsonSegment(payloadB64);
171
+ }
172
+ catch {
173
+ throw new OxyAccountEventError('Account event token is malformed');
174
+ }
175
+ if (Object.keys(header).length !== 3
176
+ || header.alg !== 'EdDSA'
177
+ || header.typ !== 'secevent+jwt'
178
+ || typeof header.kid !== 'string'
179
+ || !/^[A-Za-z0-9._-]{1,128}$/.test(header.kid)) {
180
+ throw new OxyAccountEventError('Account event token header is not supported');
181
+ }
182
+ const nodeCrypto = await loadNodeCrypto();
183
+ let jwk;
184
+ try {
185
+ jwk = await resolveServiceTokenPublicKey(header.kid, options.jwksUrl ?? new URL('/.well-known/jwks.json', this.getBaseURL()).toString(), this._serviceTokenJwksCache);
186
+ }
187
+ catch (error) {
188
+ throw new OxyAccountEventError(error instanceof Error ? error.message.replace('Service token', 'Account event') : 'Signing key is unavailable');
189
+ }
190
+ const signature = Buffer.from(signatureB64, 'base64url');
191
+ if (signature.toString('base64url') !== signatureB64 || signature.length !== 64) {
192
+ throw new OxyAccountEventError('Account event token signature is malformed');
193
+ }
194
+ let verified = false;
195
+ try {
196
+ const publicKey = nodeCrypto.createPublicKey({ key: jwk, format: 'jwk' });
197
+ verified = nodeCrypto.verify(null, Buffer.from(`${headerB64}.${payloadB64}`), publicKey, signature);
198
+ }
199
+ catch {
200
+ verified = false;
201
+ }
202
+ if (!verified)
203
+ throw new OxyAccountEventError('Account event token signature is invalid');
204
+ if (payload.iss !== OXY_JWT_ISSUER) {
205
+ throw new OxyAccountEventError('Account event token issuer is not Oxy');
206
+ }
207
+ const audience = options.audience ?? await this._configuredServiceAppId();
208
+ if (typeof payload.aud !== 'string' || payload.aud !== audience) {
209
+ throw new OxyAccountEventError('Account event token is addressed to another application');
210
+ }
211
+ if (typeof payload.jti !== 'string' || payload.jti.length === 0 || payload.jti.length > 128) {
212
+ throw new OxyAccountEventError('Account event token has no event id');
213
+ }
214
+ if (typeof payload.iat !== 'number' || !Number.isFinite(payload.iat)) {
215
+ throw new OxyAccountEventError('Account event token has no issue time');
216
+ }
217
+ const events = payload.events;
218
+ if (typeof events !== 'object' || events === null || Array.isArray(events)) {
219
+ throw new OxyAccountEventError('Account event token carries no events');
220
+ }
221
+ const entries = Object.entries(events);
222
+ const deleted = events[OXY_ACCOUNT_DELETED_EVENT_URI];
223
+ if (entries.length !== 1 || typeof deleted !== 'object' || deleted === null || Array.isArray(deleted)) {
224
+ throw new OxyAccountEventError('Account event token carries an unknown event');
225
+ }
226
+ const body = deleted;
227
+ if (typeof body.userId !== 'string' || body.userId.length === 0 || body.userId.length > 128) {
228
+ throw new OxyAccountEventError('Account event names no account');
229
+ }
230
+ if (typeof body.occurredAt !== 'string' || Number.isNaN(Date.parse(body.occurredAt))) {
231
+ throw new OxyAccountEventError('Account event has no occurrence time');
232
+ }
233
+ if (body.username !== undefined && body.username !== null && typeof body.username !== 'string') {
234
+ throw new OxyAccountEventError('Account event username is malformed');
235
+ }
236
+ return {
237
+ eventId: payload.jti,
238
+ type: 'account.deleted',
239
+ userId: body.userId,
240
+ username: typeof body.username === 'string' ? body.username : null,
241
+ occurredAt: body.occurredAt,
242
+ retained: body.retained === true,
243
+ applicationId: payload.aud,
244
+ issuedAt: payload.iat,
245
+ };
246
+ }
247
+ /**
248
+ * One page of account events addressed to this application, oldest first,
249
+ * from Oxy's pull feed — the reconciliation path behind the webhook. Needs
250
+ * the service credential (`configureServiceAuth`). Verify each entry's
251
+ * `token` with {@link verifyAccountEvent} before acting on it.
252
+ */
253
+ async listAccountEvents(options = {}) {
254
+ const serviceToken = await this.getServiceToken();
255
+ const query = {};
256
+ if (options.after)
257
+ query.after = options.after;
258
+ if (options.limit !== undefined)
259
+ query.limit = String(options.limit);
260
+ return this.makeRequest('GET', '/account-events', query, {
261
+ cache: false,
262
+ headers: { Authorization: `Bearer ${serviceToken}` },
263
+ });
264
+ }
265
+ /** @internal The `appId` claim of this client's own service token. */
266
+ async _configuredServiceAppId() {
267
+ const serviceToken = await this.getServiceToken();
268
+ const appId = jwtDecode(serviceToken).appId;
269
+ if (typeof appId !== 'string' || appId.length === 0) {
270
+ throw new OxyAccountEventError('No audience given and the service credential names no application');
271
+ }
272
+ return appId;
273
+ }
136
274
  /**
137
275
  * Express.js authentication middleware
138
276
  *