@telecrypt-io/storage 0.3.0 → 0.5.7

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.
@@ -1,69 +1,63 @@
1
- import { type OidcClientConfig, type OidcRegistrationClientMetadata, type DeviceAuthorizationResponse, type DeviceAccessTokenResponse, type DeviceAccessTokenError, type BearerTokenResponse } from "matrix-js-sdk/lib/oidc/index.js";
1
+ import { type DeviceAuthorizationResponse, type DeviceAccessTokenResponse, type DeviceAccessTokenError, type BearerTokenResponse as OAuthBearerTokenResponse, type ValidatedAuthMetadata } from "matrix-js-sdk/lib/oauth/index.js";
2
2
  import type { AccessTokens, TokenRefreshFunction } from "matrix-js-sdk/lib/http-api/index.js";
3
- import type { IdTokenClaims } from "oidc-client-ts";
4
- export type { OidcClientConfig, DeviceAuthorizationResponse, DeviceAccessTokenResponse, DeviceAccessTokenError, BearerTokenResponse, AccessTokens, TokenRefreshFunction, };
5
- /**
6
- * The full result of a completed OIDC login (device-code or authorization
7
- * code) — enough for a caller to build a ready `TeleCryptIOStorage` via
8
- * `TeleCryptIOStorage.createFromOidc(...)` AND to persist for reuse/refresh
9
- * across restarts (CLI: profile file; UI: localStorage).
10
- */
11
- export interface OidcTokenSet {
12
- homeserverUrl: string;
13
- issuer: string;
14
- clientId: string;
15
- userId: string;
16
- deviceId: string;
17
- accessToken: string;
18
- refreshToken?: string;
19
- idToken: string;
20
- /** epoch ms, if the token response included an expiry. */
21
- expiresAt?: number;
3
+ /** Matrix 1.18 OAuth metadata returned by `/auth_metadata`. */
4
+ export type OidcClientConfig = Omit<ValidatedAuthMetadata, "revocation_endpoint"> & {
5
+ revocation_endpoint?: string;
6
+ };
7
+ export type { DeviceAuthorizationResponse, DeviceAccessTokenResponse, DeviceAccessTokenError, AccessTokens, TokenRefreshFunction };
8
+ /** The bearer response retained by the browser authorization-code API. */
9
+ export type BearerTokenResponse = OAuthBearerTokenResponse & {
10
+ scope: string;
11
+ };
12
+ /** The camel-case registration contract used by TeleCrypt web and CLI callers. */
13
+ export interface OidcRegistrationClientMetadata {
14
+ clientName?: string;
15
+ clientUri: string;
16
+ logoUri?: string;
17
+ applicationType?: "web" | "native";
18
+ redirectUris?: [string, ...string[]];
19
+ contacts?: string[];
20
+ tosUri?: string;
21
+ policyUri?: string;
22
22
  }
23
+ /** The issuer-bound endpoints accepted by the token refresh adapter. */
24
+ export type OidcTokenEndpointMetadata = Pick<OidcClientConfig, "issuer" | "token_endpoint" | "revocation_endpoint">;
23
25
  /**
24
26
  * Discovers the OIDC/MAS issuer and endpoints for a homeserver via the
25
- * Matrix client's authentication metadata endpoint.
26
- *
27
- * NODE CALLERS: this internally constructs oidc-client-ts state that
28
- * requires `window.sessionStorage`/`window.localStorage` to exist (a real
29
- * browser has both natively). A Node adapter may install those only around
30
- * this call: a permanent global `window` breaks rust-crypto WASM's own
31
- * environment detection. The functional-test-only adapter is kept in
32
- * `test/harness/oidcWindowShim.ts`, rather than in the production SDK.
27
+ * Matrix client's stable `/auth_metadata` endpoint. Matrix 42 performs this
28
+ * as a plain metadata fetch; discovery is safe for browser and Node callers.
33
29
  */
34
- export declare function discoverOidcIssuer(homeserverBaseUrl: string): Promise<OidcClientConfig>;
30
+ export declare function discoverOidcIssuer(homeserverBaseUrl: string, signal?: AbortSignal): Promise<OidcClientConfig>;
35
31
  /**
36
32
  * Dynamic client registration (DCR). `clientUri`/`redirectUris` must share a
37
33
  * host unless the issuer's policy allows a mismatch (our local dev/test MAS
38
34
  * does; a production issuer may not).
39
35
  */
40
- export declare function registerClient(authMetadata: OidcClientConfig, metadata: OidcRegistrationClientMetadata): Promise<string>;
36
+ export declare function registerClient(authMetadata: OidcClientConfig, metadata: OidcRegistrationClientMetadata, signal?: AbortSignal): Promise<string>;
41
37
  /**
42
38
  * Starts a device-code authorization session. `deviceId` is caller-chosen
43
39
  * (unlike the authorization-code flow, where the SDK picks a random one) so
44
40
  * it can be reused as this Matrix device's `device_id` throughout —
45
- * `generateScope(deviceId)` embeds it in the requested scope per MSC2967.
41
+ * `generateScope(deviceId)` embeds it in the requested stable Matrix OAuth
42
+ * scope.
46
43
  */
47
- export declare function startDeviceCodeLogin(authMetadata: OidcClientConfig, clientId: string, deviceId: string): Promise<DeviceAuthorizationResponse>;
44
+ export declare function startDeviceCodeLogin(authMetadata: OidcClientConfig, clientId: string, deviceId: string, signal?: AbortSignal): Promise<DeviceAuthorizationResponse>;
48
45
  /**
49
46
  * Polls the token endpoint until the device-code session is approved (or
50
47
  * denied/expired) — see matrix-js-sdk's `waitForDeviceAuthorization` for the
51
48
  * RFC 8628 polling semantics (honours `interval`/`expires_in`,
52
- * `authorization_pending`/`slow_down`).
49
+ * `authorization_pending`/`slow_down`). Sessions returned by
50
+ * startDeviceCodeLogin carry their binding in memory; reconstructed sessions
51
+ * must provide expectedDeviceId and are rejected before polling when no
52
+ * binding is available.
53
53
  */
54
- export declare function waitForDeviceCodeLogin(authMetadata: OidcClientConfig, clientId: string, session: DeviceAuthorizationResponse): Promise<DeviceAccessTokenResponse | DeviceAccessTokenError>;
54
+ export declare function waitForDeviceCodeLogin(authMetadata: OidcClientConfig, clientId: string, session: DeviceAuthorizationResponse, signal?: AbortSignal, expectedDeviceId?: string): Promise<DeviceAccessTokenResponse | DeviceAccessTokenError>;
55
55
  export declare function isDeviceAccessTokenError(result: DeviceAccessTokenResponse | DeviceAccessTokenError): result is DeviceAccessTokenError;
56
56
  /**
57
- * Builds the authorization URL to redirect the browser to. PKCE
58
- * verifier/state/nonce are generated and persisted to `window.sessionStorage`
59
- * internally by matrix-js-sdk/oidc-client-ts (`mx_oidc_`-prefixed keys) — the
60
- * caller does not need to manage them; `completeAuthorizationCodeFlow` reads
61
- * them back after the redirect.
62
- *
63
- * `deviceId` is optional: when supplied, it is embedded in the requested
64
- * scope (MSC2967) so MAS grants THAT device instead of minting a fresh one —
65
- * the web UI passes a stable per-browser device id so repeated logins reuse
66
- * the same Matrix device instead of accumulating one device per login.
57
+ * Builds the authorization URL to redirect the browser to. The Matrix 42
58
+ * OAuth2 helper owns PKCE challenge generation and URL construction; this
59
+ * SDK owns only the minimal context needed to recreate that helper after the
60
+ * redirect. The context is keyed by a one-time random state in sessionStorage.
67
61
  */
68
62
  export declare function beginAuthorizationCodeFlow(opts: {
69
63
  authMetadata: OidcClientConfig;
@@ -71,52 +65,36 @@ export declare function beginAuthorizationCodeFlow(opts: {
71
65
  homeserverUrl: string;
72
66
  redirectUri: string;
73
67
  deviceId?: string;
68
+ signal?: AbortSignal;
74
69
  }): Promise<string>;
75
70
  /** Completes the authorization-code exchange after the `?code&state` redirect. */
76
- export declare function completeAuthorizationCodeFlow(code: string, state: string): Promise<{
71
+ export declare function completeAuthorizationCodeFlow(code: string, state: string, signal?: AbortSignal): Promise<{
77
72
  tokenResponse: BearerTokenResponse;
78
73
  oidcClientSettings: {
79
74
  clientId: string;
80
75
  issuer: string;
81
76
  };
82
77
  homeserverUrl: string;
83
- idTokenClaims: IdTokenClaims;
84
78
  }>;
85
- /** The device_id granted is embedded in the token response's `scope`
86
- * (`urn:matrix:org.matrix.msc2967.client:device:<id>`), per MSC2967 — the
87
- * authorization-code flow doesn't return it any other way (the SDK
88
- * auto-generates it when the caller doesn't request a specific one). */
79
+ /** The device_id granted is embedded in the token response's stable Matrix
80
+ * OAuth scope. */
89
81
  export declare function extractDeviceIdFromScope(scope: string): string | null;
90
82
  /** Confirms {userId, deviceId} for a freshly-obtained access token via
91
83
  * `GET /_matrix/client/v3/account/whoami` — needed because neither the
92
84
  * device-code nor authorization-code token response includes the Matrix
93
85
  * user ID. */
94
- export declare function whoAmI(homeserverUrl: string, accessToken: string): Promise<{
86
+ export declare function whoAmI(homeserverUrl: string, accessToken: string, serverName: string, signal?: AbortSignal): Promise<{
95
87
  userId: string;
96
88
  deviceId: string | null;
97
89
  }>;
98
- /**
99
- * Refreshes an access token via a plain RFC 6749 `grant_type=refresh_token`
100
- * POST to the token endpoint — deliberately NOT matrix-js-sdk's
101
- * `OidcTokenRefresher`/oidc-client-ts, which unconditionally construct an
102
- * internal `OidcClient` requiring `window.sessionStorage`/`window.
103
- * localStorage` even though a plain refresh never actually reads or writes
104
- * them (confirmed by direct testing: `ReferenceError: window is not
105
- * defined` under plain Node; see test/functional/oidc.test.ts O.2). A public client (`token_endpoint_auth_method:
106
- * "none"`, what `registerClient` above registers) authenticates a refresh
107
- * with just `client_id` in the body, no secret — this is the whole request.
108
- * Works identically in Node and the browser (plain `fetch`), so both
109
- * adapters share it with zero platform-specific code.
110
- */
111
- export declare function refreshOidcToken(tokenEndpoint: string, clientId: string, refreshToken: string): Promise<AccessTokens>;
112
90
  /**
113
91
  * Builds the `tokenRefreshFunction` `TeleCryptIOStorage.createFromOidc`
114
- * expects: refreshes via `refreshOidcToken`, then calls `onPersist` with the
115
- * new tokens before returning them — so a caller's adapter-specific
92
+ * expects: refreshes via the internal OAuth request, then calls `onPersist`
93
+ * with the new tokens before returning them — so a caller's adapter-specific
116
94
  * persistence (CLI: profile file; UI: localStorage) always sees a refresh
117
95
  * that just happened, not just ones it initiated itself.
118
96
  */
119
- export declare function buildTokenRefreshFunction(tokenEndpoint: string, clientId: string, onPersist: (tokens: {
97
+ export declare function buildTokenRefreshFunction(metadata: OidcTokenEndpointMetadata, clientId: string, onPersist: (tokens: {
120
98
  accessToken: string;
121
99
  refreshToken?: string;
122
- }) => Promise<void>): TokenRefreshFunction;
100
+ }, signal?: AbortSignal) => Promise<void>, expectedDeviceId: string): TokenRefreshFunction & ((refreshToken: string, signal?: AbortSignal) => Promise<AccessTokens>);