Skip to content

Commit

Permalink
Add new enum for verification methods. (#4129)
Browse files Browse the repository at this point in the history
* Define constants for the verification methods.

* Remove some confusing references to the *old* `VerificationMethod`
  • Loading branch information
richvdh authored Mar 22, 2024
1 parent d1259b2 commit d5a35f8
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 20 deletions.
13 changes: 5 additions & 8 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ import {
ICryptoCallbacks,
IRoomKeyRequestBody,
isCryptoAvailable,
VerificationMethod,
} from "./crypto";
import { DeviceInfo } from "./crypto/deviceinfo";
import { decodeRecoveryKey } from "./crypto/recoverykey";
Expand Down Expand Up @@ -378,8 +377,10 @@ export interface ICreateClientOpts {
* Verification methods we should offer to the other side when performing an interactive verification.
* If unset, we will offer all known methods. Currently these are: showing a QR code, scanning a QR code, and SAS
* (aka "emojis").
*
* See {@link types.VerificationMethod} for a set of useful constants for this parameter.
*/
verificationMethods?: Array<VerificationMethod>;
verificationMethods?: Array<string>;

/**
* Whether relaying calls through a TURN server should be forced. Default false.
Expand Down Expand Up @@ -1271,7 +1272,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
protected ongoingScrollbacks: { [roomId: string]: { promise?: Promise<Room>; errorTs?: number } } = {};
protected notifTimelineSet: EventTimelineSet | null = null;
protected cryptoStore?: CryptoStore;
protected verificationMethods?: VerificationMethod[];
protected verificationMethods?: string[];
protected fallbackICEServerAllowed = false;
protected syncApi?: SlidingSyncSdk | SyncApi;
public roomNameGenerator?: ICreateClientOpts["roomNameGenerator"];
Expand Down Expand Up @@ -2777,11 +2778,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
}

// deprecated: use requestVerification instead
public legacyDeviceVerification(
userId: string,
deviceId: string,
method: VerificationMethod,
): Promise<VerificationRequest> {
public legacyDeviceVerification(userId: string, deviceId: string, method: string): Promise<VerificationRequest> {
if (!this.crypto) {
throw new Error("End-to-end encryption disabled");
}
Expand Down
5 changes: 3 additions & 2 deletions src/crypto/verification/QRCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ import { MatrixClient } from "../../client";
import { IVerificationChannel } from "./request/Channel";
import { MatrixEvent } from "../../models/event";
import { ShowQrCodeCallbacks, VerifierEvent } from "../../crypto-api/verification";
import { VerificationMethod } from "../../types";

export const SHOW_QR_CODE_METHOD = "m.qr_code.show.v1";
export const SCAN_QR_CODE_METHOD = "m.qr_code.scan.v1";
export const SHOW_QR_CODE_METHOD = VerificationMethod.ShowQrCode;
export const SCAN_QR_CODE_METHOD = VerificationMethod.ScanQrCode;

/** @deprecated use VerifierEvent */
export type QrCodeEvent = VerifierEvent;
Expand Down
3 changes: 2 additions & 1 deletion src/crypto/verification/SAS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { IContent, MatrixEvent } from "../../models/event";
import { generateDecimalSas } from "./SASDecimal";
import { EventType } from "../../@types/event";
import { EmojiMapping, GeneratedSas, ShowSasCallbacks, VerifierEvent } from "../../crypto-api/verification";
import { VerificationMethod } from "../../types";

// backwards-compatibility exports
export type {
Expand Down Expand Up @@ -233,7 +234,7 @@ export class SAS extends Base {

// eslint-disable-next-line @typescript-eslint/naming-convention
public static get NAME(): string {
return "m.sas.v1";
return VerificationMethod.Sas;
}

public get events(): string[] {
Expand Down
10 changes: 8 additions & 2 deletions src/rust-crypto/rust-crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,14 @@ import { ISignatures } from "../@types/signed";
import { encodeBase64 } from "../base64";
import { OutgoingRequestsManager } from "./OutgoingRequestsManager";
import { PerSessionKeyBackupDownloader } from "./PerSessionKeyBackupDownloader";

const ALL_VERIFICATION_METHODS = ["m.sas.v1", "m.qr_code.scan.v1", "m.qr_code.show.v1", "m.reciprocate.v1"];
import { VerificationMethod } from "../types";

const ALL_VERIFICATION_METHODS = [
VerificationMethod.Sas,
VerificationMethod.ScanQrCode,
VerificationMethod.ShowQrCode,
VerificationMethod.Reciprocate,
];

interface ISignableObject {
signatures?: ISignatures;
Expand Down
15 changes: 8 additions & 7 deletions src/rust-crypto/verification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { TypedReEmitter } from "../ReEmitter";
import { MatrixEvent } from "../models/event";
import { EventType, MsgType } from "../@types/event";
import { defer, IDeferred } from "../utils";
import { VerificationMethod } from "../types";

/**
* An incoming, or outgoing, request to verify a user or a device via cross-signing.
Expand Down Expand Up @@ -230,9 +231,9 @@ export class RustVerificationRequest

const verification: RustSdkCryptoJs.Qr | RustSdkCryptoJs.Sas | undefined = this.inner.getVerification();
if (verification instanceof RustSdkCryptoJs.Sas) {
return "m.sas.v1";
return VerificationMethod.Sas;
} else if (verification instanceof RustSdkCryptoJs.Qr) {
return "m.reciprocate.v1";
return VerificationMethod.Reciprocate;
} else {
return null;
}
Expand Down Expand Up @@ -336,7 +337,7 @@ export class RustVerificationRequest
* @param method - the name of the verification method to use.
*/
public async startVerification(method: string): Promise<Verifier> {
if (method !== "m.sas.v1") {
if (method !== VerificationMethod.Sas) {
throw new Error(`Unsupported verification method ${method}`);
}

Expand Down Expand Up @@ -756,10 +757,10 @@ export class RustSASVerifier extends BaseRustVerifer<RustSdkCryptoJs.Sas> implem

/** For each specced verification method, the rust-side `VerificationMethod` corresponding to it */
const verificationMethodsByIdentifier: Record<string, RustSdkCryptoJs.VerificationMethod> = {
"m.sas.v1": RustSdkCryptoJs.VerificationMethod.SasV1,
"m.qr_code.scan.v1": RustSdkCryptoJs.VerificationMethod.QrCodeScanV1,
"m.qr_code.show.v1": RustSdkCryptoJs.VerificationMethod.QrCodeShowV1,
"m.reciprocate.v1": RustSdkCryptoJs.VerificationMethod.ReciprocateV1,
[VerificationMethod.Sas]: RustSdkCryptoJs.VerificationMethod.SasV1,
[VerificationMethod.ScanQrCode]: RustSdkCryptoJs.VerificationMethod.QrCodeScanV1,
[VerificationMethod.ShowQrCode]: RustSdkCryptoJs.VerificationMethod.QrCodeShowV1,
[VerificationMethod.Reciprocate]: RustSdkCryptoJs.VerificationMethod.ReciprocateV1,
};

/**
Expand Down
30 changes: 30 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,33 @@ export type * from "./@types/media";
export * from "./@types/membership";
export type * from "./@types/event";
export type * from "./@types/state_events";

/** The different methods for device and user verification */
export enum VerificationMethod {
/** Short authentication string (emoji or decimals).
*
* @see https://spec.matrix.org/v1.9/client-server-api/#short-authentication-string-sas-verification
*/
Sas = "m.sas.v1",

/**
* Verification by showing a QR code which is scanned by the other device.
*
* @see https://spec.matrix.org/v1.9/client-server-api/#qr-codes
*/
ShowQrCode = "m.qr_code.show.v1",

/**
* Verification by scanning a QR code that is shown by the other device.
*
* @see https://spec.matrix.org/v1.9/client-server-api/#qr-codes
*/
ScanQrCode = "m.qr_code.scan.v1",

/**
* Verification by confirming that we have scanned a QR code.
*
* @see https://spec.matrix.org/v1.9/client-server-api/#qr-codes
*/
Reciprocate = "m.reciprocate.v1",
}

0 comments on commit d5a35f8

Please sign in to comment.