From ceb2a57febcc1da6d3b62957c187914c2176cf2b Mon Sep 17 00:00:00 2001 From: Richard van der Hoff <1389908+richvdh@users.noreply.github.com> Date: Thu, 11 May 2023 19:41:58 +0100 Subject: [PATCH] Rename and move `crypto.IBootstrapCrossSigningOpts` (#3352) * Define `UIAuthCallback` type and use in `IBootstrapCrossSigningOpts` * Move `IBootstrapCrossSigningOpts` to `crypto-api` and rename * Replace uses of `IBootstrapCrossSigningOpts` ... with `BootstrapCrossSigningOpts` * Update src/crypto-api.ts --- spec/unit/crypto/cross-signing.spec.ts | 5 +++-- src/client.ts | 5 ++--- src/crypto-api.ts | 15 +++++++++++++++ src/crypto/EncryptionSetup.ts | 5 +++-- src/crypto/index.ts | 17 +++++------------ src/interactive-auth.ts | 11 +++++++++++ 6 files changed, 39 insertions(+), 19 deletions(-) diff --git a/spec/unit/crypto/cross-signing.spec.ts b/spec/unit/crypto/cross-signing.spec.ts index eeee27566..276c2feaf 100644 --- a/spec/unit/crypto/cross-signing.spec.ts +++ b/spec/unit/crypto/cross-signing.spec.ts @@ -24,10 +24,11 @@ import * as olmlib from "../../../src/crypto/olmlib"; import { MatrixError } from "../../../src/http-api"; import { logger } from "../../../src/logger"; import { ICrossSigningKey, ICreateClientOpts, ISignedKey, MatrixClient } from "../../../src/client"; -import { CryptoEvent, IBootstrapCrossSigningOpts } from "../../../src/crypto"; +import { CryptoEvent } from "../../../src/crypto"; import { IDevice } from "../../../src/crypto/deviceinfo"; import { TestClient } from "../../TestClient"; import { resetCrossSigningKeys } from "./crypto-utils"; +import { BootstrapCrossSigningOpts } from "../../../src/crypto-api"; const PUSH_RULES_RESPONSE: Response = { method: "GET", @@ -146,7 +147,7 @@ describe("Cross Signing", function () { alice.uploadKeySignatures = async () => ({ failures: {} }); alice.setAccountData = async () => ({}); alice.getAccountDataFromServer = async (): Promise => ({} as T); - const authUploadDeviceSigningKeys: IBootstrapCrossSigningOpts["authUploadDeviceSigningKeys"] = async (func) => { + const authUploadDeviceSigningKeys: BootstrapCrossSigningOpts["authUploadDeviceSigningKeys"] = async (func) => { await func({}); }; diff --git a/src/client.ts b/src/client.ts index fe6e9d8c5..f1b90b850 100644 --- a/src/client.ts +++ b/src/client.ts @@ -74,7 +74,6 @@ import { CryptoEventHandlerMap, fixBackupKey, ICryptoCallbacks, - IBootstrapCrossSigningOpts, ICheckOwnCrossSigningTrustOpts, isCryptoAvailable, VerificationMethod, @@ -205,7 +204,7 @@ import { LocalNotificationSettings } from "./@types/local_notifications"; import { buildFeatureSupportMap, Feature, ServerSupport } from "./feature"; import { CryptoBackend } from "./common-crypto/CryptoBackend"; import { RUST_SDK_STORE_PREFIX } from "./rust-crypto/constants"; -import { CryptoApi } from "./crypto-api"; +import { BootstrapCrossSigningOpts, CryptoApi } from "./crypto-api"; import { DeviceInfoMap } from "./crypto/DeviceList"; import { AddSecretStorageKeyOpts, @@ -2751,7 +2750,7 @@ export class MatrixClient extends TypedEventEmitter { + public bootstrapCrossSigning(opts: BootstrapCrossSigningOpts): Promise { if (!this.crypto) { throw new Error("End-to-end encryption disabled"); } diff --git a/src/crypto-api.ts b/src/crypto-api.ts index bf3213f22..22d91cc12 100644 --- a/src/crypto-api.ts +++ b/src/crypto-api.ts @@ -17,6 +17,7 @@ limitations under the License. import type { IMegolmSessionData } from "./@types/crypto"; import { Room } from "./models/room"; import { DeviceMap } from "./models/device"; +import { UIAuthCallback } from "./interactive-auth"; /** * Public interface to the cryptography parts of the js-sdk @@ -123,6 +124,20 @@ export interface CryptoApi { getDeviceVerificationStatus(userId: string, deviceId: string): Promise; } +/** + * Options object for `CryptoApi.bootstrapCrossSigning`. + */ +export interface BootstrapCrossSigningOpts { + /** Optional. Reset the cross-signing keys even if keys already exist. */ + setupNewCrossSigning?: boolean; + + /** + * An application callback to collect the authentication data for uploading the keys. If not given, the keys + * will not be uploaded to the server (which seems like a bad thing?). + */ + authUploadDeviceSigningKeys?: UIAuthCallback; +} + export class DeviceVerificationStatus { /** * True if this device has been signed by its owner (and that signature verified). diff --git a/src/crypto/EncryptionSetup.ts b/src/crypto/EncryptionSetup.ts index b09df2619..b8f52fcdc 100644 --- a/src/crypto/EncryptionSetup.ts +++ b/src/crypto/EncryptionSetup.ts @@ -19,7 +19,7 @@ import { MatrixEvent } from "../models/event"; import { createCryptoStoreCacheCallbacks, ICacheCallbacks } from "./CrossSigning"; import { IndexedDBCryptoStore } from "./store/indexeddb-crypto-store"; import { Method, ClientPrefix } from "../http-api"; -import { Crypto, ICryptoCallbacks, IBootstrapCrossSigningOpts } from "./index"; +import { Crypto, ICryptoCallbacks } from "./index"; import { ClientEvent, ClientEventHandlerMap, @@ -31,9 +31,10 @@ import { import { IKeyBackupInfo } from "./keybackup"; import { TypedEventEmitter } from "../models/typed-event-emitter"; import { AccountDataClient, SecretStorageKeyDescription } from "../secret-storage"; +import { BootstrapCrossSigningOpts } from "../crypto-api"; interface ICrossSigningKeys { - authUpload: IBootstrapCrossSigningOpts["authUploadDeviceSigningKeys"]; + authUpload: BootstrapCrossSigningOpts["authUploadDeviceSigningKeys"]; keys: Record<"master" | "self_signing" | "user_signing", ICrossSigningKey>; } diff --git a/src/crypto/index.ts b/src/crypto/index.ts index 9554e6173..234bb2737 100644 --- a/src/crypto/index.ts +++ b/src/crypto/index.ts @@ -88,10 +88,13 @@ import { ServerSideSecretStorageImpl, } from "../secret-storage"; import { ISecretRequest } from "./SecretSharing"; -import { DeviceVerificationStatus } from "../crypto-api"; +import { BootstrapCrossSigningOpts, DeviceVerificationStatus } from "../crypto-api"; import { Device, DeviceMap } from "../models/device"; import { deviceInfoToDevice } from "./device-converter"; +/* re-exports for backwards compatibility */ +export type { BootstrapCrossSigningOpts as IBootstrapCrossSigningOpts } from "../crypto-api"; + const DeviceVerification = DeviceInfo.DeviceVerification; const defaultVerificationMethods = { @@ -127,16 +130,6 @@ interface IInitOpts { pickleKey?: string; } -export interface IBootstrapCrossSigningOpts { - /** Optional. Reset even if keys already exist. */ - setupNewCrossSigning?: boolean; - /** - * A function that makes the request requiring auth. Receives the auth data as an object. - * Can be called multiple times, first with an empty authDict, to obtain the flows. - */ - authUploadDeviceSigningKeys?(makeRequest: (authData: any) => Promise<{}>): Promise; -} - export interface ICryptoCallbacks extends SecretStorageCallbacks { getCrossSigningKey?: (keyType: string, pubKey: string) => Promise; saveCrossSigningKeys?: (keys: Record) => void; @@ -769,7 +762,7 @@ export class Crypto extends TypedEventEmitter { + }: BootstrapCrossSigningOpts = {}): Promise { logger.log("Bootstrapping cross-signing"); const delegateCryptoCallbacks = this.baseApis.cryptoCallbacks; diff --git a/src/interactive-auth.ts b/src/interactive-auth.ts index 7c78bd0f4..aac5ef53e 100644 --- a/src/interactive-auth.ts +++ b/src/interactive-auth.ts @@ -20,6 +20,7 @@ import { logger } from "./logger"; import { MatrixClient } from "./client"; import { defer, IDeferred } from "./utils"; import { MatrixError } from "./http-api"; +import { UIAResponse } from "./@types/uia"; const EMAIL_STAGE_TYPE = "m.login.email.identity"; const MSISDN_STAGE_TYPE = "m.login.msisdn"; @@ -118,6 +119,16 @@ export class NoAuthFlowFoundError extends Error { } } +/** + * The type of an application callback to perform the user-interactive bit of UIA. + * + * It is called with a single parameter, `makeRequest`, which is a function which takes the UIA parameters and + * makes the HTTP request. + * + * The generic parameter `T` is the type of the response of the endpoint, once it is eventually successful. + */ +export type UIAuthCallback = (makeRequest: (authData: IAuthDict) => Promise>) => Promise; + interface IOpts { /** * A matrix client to use for the auth process