@telecrypt-io/storage 0.2.6 → 0.2.9

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.
@@ -363,13 +363,20 @@ export class TeleCryptIOStorage {
363
363
  return event_id;
364
364
  }
365
365
  async downloadFile(branch) {
366
- const { info } = await branch.getFileInfo();
367
- // If the underlying room event couldn't be decrypted (e.g. this device
368
- // never received/recovered the megolm session for it), matrix-js-sdk
369
- // hands back a placeholder with no usable `info` rather than throwing —
370
- // so without this check, callers would hit an opaque
371
- // "Cannot read properties of undefined" a few lines down instead of a
372
- // clear, actionable error.
366
+ let info;
367
+ try {
368
+ ({ info } = await branch.getFileInfo());
369
+ }
370
+ catch {
371
+ // matrix-js-sdk's MSC3089Branch.getFileInfo() reads `file["url"]` off
372
+ // the raw event content; when the event is undecryptable on this
373
+ // device it hands back a placeholder with no `file` block, so that
374
+ // read throws an opaque "Cannot read properties of undefined" instead
375
+ // of a useful error. Surface the real cause.
376
+ throw new Error(`downloadFile: could not read file info from the event — it is likely undecryptable on this device (missing megolm session; try restoring from a Recovery Key)`);
377
+ }
378
+ // Belt-and-braces: if a future matrix-js-sdk version returns a
379
+ // placeholder without throwing, still fail with the clear message.
373
380
  if (!info || typeof info.url !== "string") {
374
381
  throw new Error("downloadFile: could not read file info from the event — it is likely undecryptable on this device (missing megolm session; try restoring from a Recovery Key)");
375
382
  }
@@ -61,12 +61,18 @@ export declare function isDeviceAccessTokenError(result: DeviceAccessTokenRespon
61
61
  * internally by matrix-js-sdk/oidc-client-ts (`mx_oidc_`-prefixed keys) — the
62
62
  * caller does not need to manage them; `completeAuthorizationCodeFlow` reads
63
63
  * them back after the redirect.
64
+ *
65
+ * `deviceId` is optional: when supplied, it is embedded in the requested
66
+ * scope (MSC2967) so MAS grants THAT device instead of minting a fresh one —
67
+ * the web UI passes a stable per-browser device id so repeated logins reuse
68
+ * the same Matrix device instead of accumulating one device per login.
64
69
  */
65
70
  export declare function beginAuthorizationCodeFlow(opts: {
66
71
  authMetadata: OidcClientConfig;
67
72
  clientId: string;
68
73
  homeserverUrl: string;
69
74
  redirectUri: string;
75
+ deviceId?: string;
70
76
  }): Promise<string>;
71
77
  /** Completes the authorization-code exchange after the `?code&state` redirect. */
72
78
  export declare function completeAuthorizationCodeFlow(code: string, state: string): Promise<{
package/dist/core/oidc.js CHANGED
@@ -10,6 +10,7 @@
10
10
  */
11
11
  import { createClient } from "matrix-js-sdk";
12
12
  import { registerOidcClient, generateScope, generateOidcAuthorizationUrl, completeAuthorizationCodeGrant, startDeviceAuthorization, waitForDeviceAuthorization, } from "matrix-js-sdk/lib/oidc/index.js";
13
+ import { OidcClient, WebStorageStateStore } from "oidc-client-ts";
13
14
  import { CliError } from "./errors.js";
14
15
  /**
15
16
  * Discovers the OIDC/MAS issuer + endpoints for a homeserver, via
@@ -84,9 +85,41 @@ export function isDeviceAccessTokenError(result) {
84
85
  * internally by matrix-js-sdk/oidc-client-ts (`mx_oidc_`-prefixed keys) — the
85
86
  * caller does not need to manage them; `completeAuthorizationCodeFlow` reads
86
87
  * them back after the redirect.
88
+ *
89
+ * `deviceId` is optional: when supplied, it is embedded in the requested
90
+ * scope (MSC2967) so MAS grants THAT device instead of minting a fresh one —
91
+ * the web UI passes a stable per-browser device id so repeated logins reuse
92
+ * the same Matrix device instead of accumulating one device per login.
87
93
  */
88
94
  export async function beginAuthorizationCodeFlow(opts) {
89
95
  const nonce = crypto.randomUUID();
96
+ if (opts.deviceId) {
97
+ // matrix-js-sdk's generateOidcAuthorizationUrl hardcodes
98
+ // generateScope() with no device id, so every login would mint a fresh
99
+ // Matrix device. Build the URL ourselves with the stable device id in
100
+ // the requested scope (MSC2967) so MAS grants THAT device — mirroring
101
+ // matrix-js-sdk's construction exactly (same state store, same user
102
+ // state shape) so completeAuthorizationCodeFlow still works unchanged.
103
+ const scope = generateScope(opts.deviceId);
104
+ const oidcClient = new OidcClient({
105
+ ...opts.authMetadata,
106
+ signingKeys: opts.authMetadata.signingKeys ?? undefined,
107
+ metadata: opts.authMetadata,
108
+ client_id: opts.clientId,
109
+ redirect_uri: opts.redirectUri,
110
+ authority: opts.authMetadata.issuer,
111
+ response_mode: "query",
112
+ response_type: "code",
113
+ scope,
114
+ stateStore: new WebStorageStateStore({ prefix: "mx_oidc_", store: window.sessionStorage }),
115
+ });
116
+ const userState = { homeserverUrl: opts.homeserverUrl, nonce };
117
+ const request = await oidcClient.createSigninRequest({
118
+ state: userState,
119
+ nonce,
120
+ });
121
+ return request.url;
122
+ }
90
123
  return generateOidcAuthorizationUrl({
91
124
  metadata: opts.authMetadata,
92
125
  clientId: opts.clientId,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@telecrypt-io/storage",
3
- "version": "0.2.6",
3
+ "version": "0.2.9",
4
4
  "license": "BUSL-1.1",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",