From 321775b800cd3ad0a04269c2d75add87fffd8214 Mon Sep 17 00:00:00 2001 From: RMidhunSuresh Date: Fri, 24 Mar 2023 19:18:31 +0530 Subject: [PATCH] Rename CancelTypes -> CancelReason --- .../verification/SAS/SASVerification.ts | 4 +-- .../verification/SAS/channel/Channel.ts | 34 +++++++++---------- .../verification/SAS/channel/MockChannel.ts | 4 +-- src/matrix/verification/SAS/channel/types.ts | 2 +- .../SAS/stages/CalculateSASStage.ts | 6 ++-- .../stages/SelectVerificationMethodStage.ts | 4 +-- .../SAS/stages/SendAcceptVerificationStage.ts | 4 +-- .../verification/SAS/stages/VerifyMacStage.ts | 6 ++-- 8 files changed, 32 insertions(+), 32 deletions(-) diff --git a/src/matrix/verification/SAS/SASVerification.ts b/src/matrix/verification/SAS/SASVerification.ts index f249118a..355acd26 100644 --- a/src/matrix/verification/SAS/SASVerification.ts +++ b/src/matrix/verification/SAS/SASVerification.ts @@ -21,7 +21,7 @@ import type {DeviceTracker} from "../../e2ee/DeviceTracker.js"; import type * as OlmNamespace from "@matrix-org/olm"; import {IChannel} from "./channel/Channel"; import {HomeServerApi} from "../../net/HomeServerApi"; -import {CancelTypes, VerificationEventTypes} from "./channel/types"; +import {CancelReason, VerificationEventTypes} from "./channel/types"; import {SendReadyStage} from "./stages/SendReadyStage"; import {SelectVerificationMethodStage} from "./stages/SelectVerificationMethodStage"; import {VerificationCancelledError} from "./VerificationCancelledError"; @@ -78,7 +78,7 @@ export class SASVerification { const tenMinutes = 10 * 60 * 1000; this.timeout = clock.createTimeout(tenMinutes); await this.timeout.elapsed(); - await this.channel.cancelVerification(CancelTypes.TimedOut); + await this.channel.cancelVerification(CancelReason.TimedOut); } catch { // Ignore errors diff --git a/src/matrix/verification/SAS/channel/Channel.ts b/src/matrix/verification/SAS/channel/Channel.ts index c874586d..0fb2cb7c 100644 --- a/src/matrix/verification/SAS/channel/Channel.ts +++ b/src/matrix/verification/SAS/channel/Channel.ts @@ -20,22 +20,22 @@ import type {ILogItem} from "../../../../logging/types"; import type {Clock} from "../../../../platform/web/dom/Clock.js"; import type {DeviceMessageHandler} from "../../../DeviceMessageHandler.js"; import {makeTxnId} from "../../../common.js"; -import {CancelTypes, VerificationEventTypes} from "./types"; +import {CancelReason, VerificationEventTypes} from "./types"; import {Disposables} from "../../../../utils/Disposables"; import {VerificationCancelledError} from "../VerificationCancelledError"; const messageFromErrorType = { - [CancelTypes.UserCancelled]: "User declined", - [CancelTypes.InvalidMessage]: "Invalid Message.", - [CancelTypes.KeyMismatch]: "Key Mismatch.", - [CancelTypes.OtherDeviceAccepted]: "Another device has accepted this request.", - [CancelTypes.TimedOut]: "Timed Out", - [CancelTypes.UnexpectedMessage]: "Unexpected Message.", - [CancelTypes.UnknownMethod]: "Unknown method.", - [CancelTypes.UnknownTransaction]: "Unknown Transaction.", - [CancelTypes.UserMismatch]: "User Mismatch", - [CancelTypes.MismatchedCommitment]: "Hash commitment does not match.", - [CancelTypes.MismatchedSAS]: "Emoji/decimal does not match.", + [CancelReason.UserCancelled]: "User declined", + [CancelReason.InvalidMessage]: "Invalid Message.", + [CancelReason.KeyMismatch]: "Key Mismatch.", + [CancelReason.OtherDeviceAccepted]: "Another device has accepted this request.", + [CancelReason.TimedOut]: "Timed Out", + [CancelReason.UnexpectedMessage]: "Unexpected Message.", + [CancelReason.UnknownMethod]: "Unknown method.", + [CancelReason.UnknownTransaction]: "Unknown Transaction.", + [CancelReason.UserMismatch]: "User Mismatch", + [CancelReason.MismatchedCommitment]: "Hash commitment does not match.", + [CancelReason.MismatchedSAS]: "Emoji/decimal does not match.", } export interface IChannel { @@ -45,7 +45,7 @@ export interface IChannel { getReceivedMessage(event: VerificationEventTypes): any; setStartMessage(content: any): void; setOurDeviceId(id: string): void; - cancelVerification(cancellationType: CancelTypes): Promise; + cancelVerification(cancellationType: CancelReason): Promise; acceptMessage: any; startMessage: any; initiatedByUs: boolean; @@ -185,7 +185,7 @@ export class ToDeviceChannel extends Disposables implements IChannel { * This does not apply for inbound m.key.verification.start or m.key.verification.cancel messages. */ console.log("Received event with unknown transaction id: ", event); - await this.cancelVerification(CancelTypes.UnknownTransaction); + await this.cancelVerification(CancelReason.UnknownTransaction); return; } console.log("event", event); @@ -211,8 +211,8 @@ export class ToDeviceChannel extends Disposables implements IChannel { const devices = await this.deviceTracker.devicesForUsers([this.otherUserId], this.hsApi, log); const otherDevices = devices.filter(device => device.deviceId !== fromDevice && device.deviceId !== this.ourDeviceId); const cancelMessage = { - code: CancelTypes.OtherDeviceAccepted, - reason: messageFromErrorType[CancelTypes.OtherDeviceAccepted], + code: CancelReason.OtherDeviceAccepted, + reason: messageFromErrorType[CancelReason.OtherDeviceAccepted], transaction_id: this.id, }; const deviceMessages = otherDevices.reduce((acc, device) => { acc[device.deviceId] = cancelMessage; return acc; }, {}); @@ -224,7 +224,7 @@ export class ToDeviceChannel extends Disposables implements IChannel { await this.hsApi.sendToDevice(VerificationEventTypes.Cancel, payload, makeTxnId(), { log }).response(); } - async cancelVerification(cancellationType: CancelTypes) { + async cancelVerification(cancellationType: CancelReason) { await this.log.wrap("Channel.cancelVerification", async log => { if (this.isCancelled) { throw new VerificationCancelledError(); diff --git a/src/matrix/verification/SAS/channel/MockChannel.ts b/src/matrix/verification/SAS/channel/MockChannel.ts index b2de0f3f..a9cc4dea 100644 --- a/src/matrix/verification/SAS/channel/MockChannel.ts +++ b/src/matrix/verification/SAS/channel/MockChannel.ts @@ -2,7 +2,7 @@ import type {ILogItem} from "../../../../lib"; import {createCalculateMAC} from "../mac"; import {VerificationCancelledError} from "../VerificationCancelledError"; import {IChannel} from "./Channel"; -import {CancelTypes, VerificationEventTypes} from "./types"; +import {CancelReason, VerificationEventTypes} from "./types"; import anotherjson from "another-json"; interface ITestChannel extends IChannel { @@ -115,7 +115,7 @@ export class MockChannel implements ITestChannel { this.ourUserDeviceId = id; } - async cancelVerification(_: CancelTypes): Promise { + async cancelVerification(_: CancelReason): Promise { console.log("MockChannel.cancelVerification()"); this.isCancelled = true; } diff --git a/src/matrix/verification/SAS/channel/types.ts b/src/matrix/verification/SAS/channel/types.ts index de6a999b..400404b7 100644 --- a/src/matrix/verification/SAS/channel/types.ts +++ b/src/matrix/verification/SAS/channel/types.ts @@ -9,7 +9,7 @@ export const enum VerificationEventTypes { Done = "m.key.verification.done", } -export const enum CancelTypes { +export const enum CancelReason { UserCancelled = "m.user", TimedOut = "m.timeout", UnknownTransaction = "m.unknown_transaction", diff --git a/src/matrix/verification/SAS/stages/CalculateSASStage.ts b/src/matrix/verification/SAS/stages/CalculateSASStage.ts index daa744d4..fc509aea 100644 --- a/src/matrix/verification/SAS/stages/CalculateSASStage.ts +++ b/src/matrix/verification/SAS/stages/CalculateSASStage.ts @@ -15,7 +15,7 @@ limitations under the License. */ import anotherjson from "another-json"; import {BaseSASVerificationStage} from "./BaseSASVerificationStage"; -import {CancelTypes, VerificationEventTypes} from "../channel/types"; +import {CancelReason, VerificationEventTypes} from "../channel/types"; import {generateEmojiSas} from "../generator"; import {ILogItem} from "../../../../logging/types"; import {SendMacStage} from "./SendMacStage"; @@ -89,7 +89,7 @@ export class CalculateSASStage extends BaseSASVerificationStage { const hash = this.olmUtil.sha256(commitmentStr); if (hash !== receivedCommitment) { log.log({l: "Commitment mismatched!", received: receivedCommitment, calculated: hash}); - await this.channel.cancelVerification(CancelTypes.MismatchedCommitment); + await this.channel.cancelVerification(CancelReason.MismatchedCommitment); return false; } return true; @@ -130,7 +130,7 @@ export class CalculateSASStage extends BaseSASVerificationStage { this.resolve(); } else { - await this.channel.cancelVerification(CancelTypes.MismatchedSAS); + await this.channel.cancelVerification(CancelReason.MismatchedSAS); this.reject(new VerificationCancelledError()); } } diff --git a/src/matrix/verification/SAS/stages/SelectVerificationMethodStage.ts b/src/matrix/verification/SAS/stages/SelectVerificationMethodStage.ts index 9108ca71..78f6aaf9 100644 --- a/src/matrix/verification/SAS/stages/SelectVerificationMethodStage.ts +++ b/src/matrix/verification/SAS/stages/SelectVerificationMethodStage.ts @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ import {BaseSASVerificationStage} from "./BaseSASVerificationStage"; -import {CancelTypes, VerificationEventTypes} from "../channel/types"; +import {CancelReason, VerificationEventTypes} from "../channel/types"; import {KEY_AGREEMENT_LIST, HASHES_LIST, MAC_LIST, SAS_LIST} from "./constants"; import {SendAcceptVerificationStage} from "./SendAcceptVerificationStage"; import {SendKeyStage} from "./SendKeyStage"; @@ -69,7 +69,7 @@ export class SelectVerificationMethodStage extends BaseSASVerificationStage { received: receivedStartMessage.content.method, sent: sentStartMessage.content.method, }); - await this.channel.cancelVerification(CancelTypes.UnexpectedMessage); + await this.channel.cancelVerification(CancelReason.UnexpectedMessage); return; } // In the case of conflict, the lexicographically smaller id wins diff --git a/src/matrix/verification/SAS/stages/SendAcceptVerificationStage.ts b/src/matrix/verification/SAS/stages/SendAcceptVerificationStage.ts index b921a6a8..c69d41df 100644 --- a/src/matrix/verification/SAS/stages/SendAcceptVerificationStage.ts +++ b/src/matrix/verification/SAS/stages/SendAcceptVerificationStage.ts @@ -16,7 +16,7 @@ limitations under the License. import anotherjson from "another-json"; import {BaseSASVerificationStage} from "./BaseSASVerificationStage"; import {HASHES_LIST, MAC_LIST, SAS_SET, KEY_AGREEMENT_LIST} from "./constants"; -import {CancelTypes, VerificationEventTypes} from "../channel/types"; +import {CancelReason, VerificationEventTypes} from "../channel/types"; import {SendKeyStage} from "./SendKeyStage"; // from element-web @@ -33,7 +33,7 @@ export class SendAcceptVerificationStage extends BaseSASVerificationStage { const macMethod = intersection(MAC_LIST, new Set(startMessage.message_authentication_codes))[0]; const sasMethod = intersection(startMessage.short_authentication_string, SAS_SET); if (!keyAgreement || !hashMethod || !macMethod || !sasMethod.length) { - await this.channel.cancelVerification(CancelTypes.UnknownMethod); + await this.channel.cancelVerification(CancelReason.UnknownMethod); return; } const ourPubKey = this.olmSAS.get_pubkey(); diff --git a/src/matrix/verification/SAS/stages/VerifyMacStage.ts b/src/matrix/verification/SAS/stages/VerifyMacStage.ts index 55cb80e3..8366abfc 100644 --- a/src/matrix/verification/SAS/stages/VerifyMacStage.ts +++ b/src/matrix/verification/SAS/stages/VerifyMacStage.ts @@ -15,7 +15,7 @@ limitations under the License. */ import {BaseSASVerificationStage} from "./BaseSASVerificationStage"; import {ILogItem} from "../../../../logging/types"; -import {CancelTypes, VerificationEventTypes} from "../channel/types"; +import {CancelReason, VerificationEventTypes} from "../channel/types"; import {createCalculateMAC} from "../mac"; import {SendDoneStage} from "./SendDoneStage"; @@ -46,7 +46,7 @@ export class VerifyMacStage extends BaseSASVerificationStage { const calculatedMAC = calculateMAC(Object.keys(content.mac).sort().join(","), baseInfo + "KEY_IDS"); if (content.keys !== calculatedMAC) { log.log({ l: "MAC verification failed for keys field", keys: content.keys, calculated: calculatedMAC }); - this.channel.cancelVerification(CancelTypes.KeyMismatch); + this.channel.cancelVerification(CancelReason.KeyMismatch); return; } @@ -54,7 +54,7 @@ export class VerifyMacStage extends BaseSASVerificationStage { const calculatedMAC = calculateMAC(key, baseInfo + keyId); if (keyInfo !== calculatedMAC) { log.log({ l: "Mac verification failed for key", keyMac: keyInfo, calculatedMAC, keyId, key }); - this.channel.cancelVerification(CancelTypes.KeyMismatch); + this.channel.cancelVerification(CancelReason.KeyMismatch); return; } }, log);