Rename CancelTypes -> CancelReason

This commit is contained in:
RMidhunSuresh 2023-03-24 19:18:31 +05:30
parent e2ae5e716e
commit 321775b800
8 changed files with 32 additions and 32 deletions

View File

@ -21,7 +21,7 @@ import type {DeviceTracker} from "../../e2ee/DeviceTracker.js";
import type * as OlmNamespace from "@matrix-org/olm"; import type * as OlmNamespace from "@matrix-org/olm";
import {IChannel} from "./channel/Channel"; import {IChannel} from "./channel/Channel";
import {HomeServerApi} from "../../net/HomeServerApi"; import {HomeServerApi} from "../../net/HomeServerApi";
import {CancelTypes, VerificationEventTypes} from "./channel/types"; import {CancelReason, VerificationEventTypes} from "./channel/types";
import {SendReadyStage} from "./stages/SendReadyStage"; import {SendReadyStage} from "./stages/SendReadyStage";
import {SelectVerificationMethodStage} from "./stages/SelectVerificationMethodStage"; import {SelectVerificationMethodStage} from "./stages/SelectVerificationMethodStage";
import {VerificationCancelledError} from "./VerificationCancelledError"; import {VerificationCancelledError} from "./VerificationCancelledError";
@ -78,7 +78,7 @@ export class SASVerification {
const tenMinutes = 10 * 60 * 1000; const tenMinutes = 10 * 60 * 1000;
this.timeout = clock.createTimeout(tenMinutes); this.timeout = clock.createTimeout(tenMinutes);
await this.timeout.elapsed(); await this.timeout.elapsed();
await this.channel.cancelVerification(CancelTypes.TimedOut); await this.channel.cancelVerification(CancelReason.TimedOut);
} }
catch { catch {
// Ignore errors // Ignore errors

View File

@ -20,22 +20,22 @@ import type {ILogItem} from "../../../../logging/types";
import type {Clock} from "../../../../platform/web/dom/Clock.js"; import type {Clock} from "../../../../platform/web/dom/Clock.js";
import type {DeviceMessageHandler} from "../../../DeviceMessageHandler.js"; import type {DeviceMessageHandler} from "../../../DeviceMessageHandler.js";
import {makeTxnId} from "../../../common.js"; import {makeTxnId} from "../../../common.js";
import {CancelTypes, VerificationEventTypes} from "./types"; import {CancelReason, VerificationEventTypes} from "./types";
import {Disposables} from "../../../../utils/Disposables"; import {Disposables} from "../../../../utils/Disposables";
import {VerificationCancelledError} from "../VerificationCancelledError"; import {VerificationCancelledError} from "../VerificationCancelledError";
const messageFromErrorType = { const messageFromErrorType = {
[CancelTypes.UserCancelled]: "User declined", [CancelReason.UserCancelled]: "User declined",
[CancelTypes.InvalidMessage]: "Invalid Message.", [CancelReason.InvalidMessage]: "Invalid Message.",
[CancelTypes.KeyMismatch]: "Key Mismatch.", [CancelReason.KeyMismatch]: "Key Mismatch.",
[CancelTypes.OtherDeviceAccepted]: "Another device has accepted this request.", [CancelReason.OtherDeviceAccepted]: "Another device has accepted this request.",
[CancelTypes.TimedOut]: "Timed Out", [CancelReason.TimedOut]: "Timed Out",
[CancelTypes.UnexpectedMessage]: "Unexpected Message.", [CancelReason.UnexpectedMessage]: "Unexpected Message.",
[CancelTypes.UnknownMethod]: "Unknown method.", [CancelReason.UnknownMethod]: "Unknown method.",
[CancelTypes.UnknownTransaction]: "Unknown Transaction.", [CancelReason.UnknownTransaction]: "Unknown Transaction.",
[CancelTypes.UserMismatch]: "User Mismatch", [CancelReason.UserMismatch]: "User Mismatch",
[CancelTypes.MismatchedCommitment]: "Hash commitment does not match.", [CancelReason.MismatchedCommitment]: "Hash commitment does not match.",
[CancelTypes.MismatchedSAS]: "Emoji/decimal does not match.", [CancelReason.MismatchedSAS]: "Emoji/decimal does not match.",
} }
export interface IChannel { export interface IChannel {
@ -45,7 +45,7 @@ export interface IChannel {
getReceivedMessage(event: VerificationEventTypes): any; getReceivedMessage(event: VerificationEventTypes): any;
setStartMessage(content: any): void; setStartMessage(content: any): void;
setOurDeviceId(id: string): void; setOurDeviceId(id: string): void;
cancelVerification(cancellationType: CancelTypes): Promise<void>; cancelVerification(cancellationType: CancelReason): Promise<void>;
acceptMessage: any; acceptMessage: any;
startMessage: any; startMessage: any;
initiatedByUs: boolean; 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. * 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); console.log("Received event with unknown transaction id: ", event);
await this.cancelVerification(CancelTypes.UnknownTransaction); await this.cancelVerification(CancelReason.UnknownTransaction);
return; return;
} }
console.log("event", event); 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 devices = await this.deviceTracker.devicesForUsers([this.otherUserId], this.hsApi, log);
const otherDevices = devices.filter(device => device.deviceId !== fromDevice && device.deviceId !== this.ourDeviceId); const otherDevices = devices.filter(device => device.deviceId !== fromDevice && device.deviceId !== this.ourDeviceId);
const cancelMessage = { const cancelMessage = {
code: CancelTypes.OtherDeviceAccepted, code: CancelReason.OtherDeviceAccepted,
reason: messageFromErrorType[CancelTypes.OtherDeviceAccepted], reason: messageFromErrorType[CancelReason.OtherDeviceAccepted],
transaction_id: this.id, transaction_id: this.id,
}; };
const deviceMessages = otherDevices.reduce((acc, device) => { acc[device.deviceId] = cancelMessage; return acc; }, {}); 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(); 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 => { await this.log.wrap("Channel.cancelVerification", async log => {
if (this.isCancelled) { if (this.isCancelled) {
throw new VerificationCancelledError(); throw new VerificationCancelledError();

View File

@ -2,7 +2,7 @@ import type {ILogItem} from "../../../../lib";
import {createCalculateMAC} from "../mac"; import {createCalculateMAC} from "../mac";
import {VerificationCancelledError} from "../VerificationCancelledError"; import {VerificationCancelledError} from "../VerificationCancelledError";
import {IChannel} from "./Channel"; import {IChannel} from "./Channel";
import {CancelTypes, VerificationEventTypes} from "./types"; import {CancelReason, VerificationEventTypes} from "./types";
import anotherjson from "another-json"; import anotherjson from "another-json";
interface ITestChannel extends IChannel { interface ITestChannel extends IChannel {
@ -115,7 +115,7 @@ export class MockChannel implements ITestChannel {
this.ourUserDeviceId = id; this.ourUserDeviceId = id;
} }
async cancelVerification(_: CancelTypes): Promise<void> { async cancelVerification(_: CancelReason): Promise<void> {
console.log("MockChannel.cancelVerification()"); console.log("MockChannel.cancelVerification()");
this.isCancelled = true; this.isCancelled = true;
} }

View File

@ -9,7 +9,7 @@ export const enum VerificationEventTypes {
Done = "m.key.verification.done", Done = "m.key.verification.done",
} }
export const enum CancelTypes { export const enum CancelReason {
UserCancelled = "m.user", UserCancelled = "m.user",
TimedOut = "m.timeout", TimedOut = "m.timeout",
UnknownTransaction = "m.unknown_transaction", UnknownTransaction = "m.unknown_transaction",

View File

@ -15,7 +15,7 @@ limitations under the License.
*/ */
import anotherjson from "another-json"; import anotherjson from "another-json";
import {BaseSASVerificationStage} from "./BaseSASVerificationStage"; import {BaseSASVerificationStage} from "./BaseSASVerificationStage";
import {CancelTypes, VerificationEventTypes} from "../channel/types"; import {CancelReason, VerificationEventTypes} from "../channel/types";
import {generateEmojiSas} from "../generator"; import {generateEmojiSas} from "../generator";
import {ILogItem} from "../../../../logging/types"; import {ILogItem} from "../../../../logging/types";
import {SendMacStage} from "./SendMacStage"; import {SendMacStage} from "./SendMacStage";
@ -89,7 +89,7 @@ export class CalculateSASStage extends BaseSASVerificationStage {
const hash = this.olmUtil.sha256(commitmentStr); const hash = this.olmUtil.sha256(commitmentStr);
if (hash !== receivedCommitment) { if (hash !== receivedCommitment) {
log.log({l: "Commitment mismatched!", received: receivedCommitment, calculated: hash}); log.log({l: "Commitment mismatched!", received: receivedCommitment, calculated: hash});
await this.channel.cancelVerification(CancelTypes.MismatchedCommitment); await this.channel.cancelVerification(CancelReason.MismatchedCommitment);
return false; return false;
} }
return true; return true;
@ -130,7 +130,7 @@ export class CalculateSASStage extends BaseSASVerificationStage {
this.resolve(); this.resolve();
} }
else { else {
await this.channel.cancelVerification(CancelTypes.MismatchedSAS); await this.channel.cancelVerification(CancelReason.MismatchedSAS);
this.reject(new VerificationCancelledError()); this.reject(new VerificationCancelledError());
} }
} }

View File

@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
import {BaseSASVerificationStage} from "./BaseSASVerificationStage"; 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 {KEY_AGREEMENT_LIST, HASHES_LIST, MAC_LIST, SAS_LIST} from "./constants";
import {SendAcceptVerificationStage} from "./SendAcceptVerificationStage"; import {SendAcceptVerificationStage} from "./SendAcceptVerificationStage";
import {SendKeyStage} from "./SendKeyStage"; import {SendKeyStage} from "./SendKeyStage";
@ -69,7 +69,7 @@ export class SelectVerificationMethodStage extends BaseSASVerificationStage {
received: receivedStartMessage.content.method, received: receivedStartMessage.content.method,
sent: sentStartMessage.content.method, sent: sentStartMessage.content.method,
}); });
await this.channel.cancelVerification(CancelTypes.UnexpectedMessage); await this.channel.cancelVerification(CancelReason.UnexpectedMessage);
return; return;
} }
// In the case of conflict, the lexicographically smaller id wins // In the case of conflict, the lexicographically smaller id wins

View File

@ -16,7 +16,7 @@ limitations under the License.
import anotherjson from "another-json"; import anotherjson from "another-json";
import {BaseSASVerificationStage} from "./BaseSASVerificationStage"; import {BaseSASVerificationStage} from "./BaseSASVerificationStage";
import {HASHES_LIST, MAC_LIST, SAS_SET, KEY_AGREEMENT_LIST} from "./constants"; 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"; import {SendKeyStage} from "./SendKeyStage";
// from element-web // 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 macMethod = intersection(MAC_LIST, new Set(startMessage.message_authentication_codes))[0];
const sasMethod = intersection(startMessage.short_authentication_string, SAS_SET); const sasMethod = intersection(startMessage.short_authentication_string, SAS_SET);
if (!keyAgreement || !hashMethod || !macMethod || !sasMethod.length) { if (!keyAgreement || !hashMethod || !macMethod || !sasMethod.length) {
await this.channel.cancelVerification(CancelTypes.UnknownMethod); await this.channel.cancelVerification(CancelReason.UnknownMethod);
return; return;
} }
const ourPubKey = this.olmSAS.get_pubkey(); const ourPubKey = this.olmSAS.get_pubkey();

View File

@ -15,7 +15,7 @@ limitations under the License.
*/ */
import {BaseSASVerificationStage} from "./BaseSASVerificationStage"; import {BaseSASVerificationStage} from "./BaseSASVerificationStage";
import {ILogItem} from "../../../../logging/types"; import {ILogItem} from "../../../../logging/types";
import {CancelTypes, VerificationEventTypes} from "../channel/types"; import {CancelReason, VerificationEventTypes} from "../channel/types";
import {createCalculateMAC} from "../mac"; import {createCalculateMAC} from "../mac";
import {SendDoneStage} from "./SendDoneStage"; 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"); const calculatedMAC = calculateMAC(Object.keys(content.mac).sort().join(","), baseInfo + "KEY_IDS");
if (content.keys !== calculatedMAC) { if (content.keys !== calculatedMAC) {
log.log({ l: "MAC verification failed for keys field", keys: content.keys, calculated: 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; return;
} }
@ -54,7 +54,7 @@ export class VerifyMacStage extends BaseSASVerificationStage {
const calculatedMAC = calculateMAC(key, baseInfo + keyId); const calculatedMAC = calculateMAC(key, baseInfo + keyId);
if (keyInfo !== calculatedMAC) { if (keyInfo !== calculatedMAC) {
log.log({ l: "Mac verification failed for key", keyMac: keyInfo, calculatedMAC, keyId, key }); log.log({ l: "Mac verification failed for key", keyMac: keyInfo, calculatedMAC, keyId, key });
this.channel.cancelVerification(CancelTypes.KeyMismatch); this.channel.cancelVerification(CancelReason.KeyMismatch);
return; return;
} }
}, log); }, log);