From 50e572b4377ac7ecc2ca41149126734565f27ecf Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Mon, 24 Jul 2023 13:00:24 +0100 Subject: [PATCH 1/3] Fix registration check your emails stage regression --- src/http-api/errors.ts | 6 +++--- src/interactive-auth.ts | 16 ++++++++++++---- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/http-api/errors.ts b/src/http-api/errors.ts index e48fc029c7d..8dbef758e39 100644 --- a/src/http-api/errors.ts +++ b/src/http-api/errors.ts @@ -17,7 +17,7 @@ limitations under the License. import { IUsageLimit } from "../@types/partials"; import { MatrixEvent } from "../models/event"; -interface IErrorJson extends Partial { +export interface MatrixErrorJson extends Partial { [key: string]: any; // extensible errcode?: string; error?: string; @@ -39,7 +39,7 @@ export class MatrixError extends HTTPError { // The Matrix 'errcode' value, e.g. "M_FORBIDDEN". public readonly errcode?: string; // The raw Matrix error JSON used to construct this object. - public data: IErrorJson; + public data: MatrixErrorJson; /** * Construct a Matrix error. This is a JavaScript Error with additional @@ -48,7 +48,7 @@ export class MatrixError extends HTTPError { * @param httpStatus - The numeric HTTP status code given */ public constructor( - errorJson: IErrorJson = {}, + errorJson: MatrixErrorJson = {}, public readonly httpStatus?: number, public url?: string, public event?: MatrixEvent, diff --git a/src/interactive-auth.ts b/src/interactive-auth.ts index 0a75ab5232f..fb5e651e00d 100644 --- a/src/interactive-auth.ts +++ b/src/interactive-auth.ts @@ -19,7 +19,7 @@ limitations under the License. import { logger } from "./logger"; import { MatrixClient } from "./client"; import { defer, IDeferred } from "./utils"; -import { MatrixError } from "./http-api"; +import { MatrixError, MatrixErrorJson } from "./http-api"; import { UIAResponse } from "./@types/uia"; import { UserIdentifier } from "./@types/auth"; @@ -264,8 +264,8 @@ export class InteractiveAuth { private readonly requestEmailTokenCallback: IOpts["requestEmailToken"]; private readonly supportedStages?: Set; - // The current latest data received from the server during the user interactive auth flow. - private data: IAuthData; + // The current latest data or error received from the server during the user interactive auth flow. + private data: IAuthData & MatrixErrorJson; private emailSid?: string; private requestingEmailToken = false; private attemptAuthDeferred: IDeferred | null = null; @@ -549,7 +549,7 @@ export class InteractiveAuth { matrixError.data.session = (this.data as IAuthData).session; } if (matrixError) { - this.data = matrixError.data as IAuthData; + this.data = matrixError.data; } try { this.startNextAuthStage(); @@ -602,6 +602,14 @@ export class InteractiveAuth { return; } + if (this.data?.errcode || this.data?.error) { + this.stateUpdatedCallback(nextStage, { + errcode: this.data?.errcode || "", + error: this.data?.error || "", + }); + return; + } + this.stateUpdatedCallback(nextStage, nextStage === EMAIL_STAGE_TYPE ? { emailSid: this.emailSid } : {}); } From d840ad4754ff70650a42ac9bf46b32c4d7b5d7e3 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Mon, 24 Jul 2023 13:08:46 +0100 Subject: [PATCH 2/3] Simplify diff --- src/http-api/errors.ts | 6 +++--- src/interactive-auth.ts | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/http-api/errors.ts b/src/http-api/errors.ts index 8dbef758e39..e48fc029c7d 100644 --- a/src/http-api/errors.ts +++ b/src/http-api/errors.ts @@ -17,7 +17,7 @@ limitations under the License. import { IUsageLimit } from "../@types/partials"; import { MatrixEvent } from "../models/event"; -export interface MatrixErrorJson extends Partial { +interface IErrorJson extends Partial { [key: string]: any; // extensible errcode?: string; error?: string; @@ -39,7 +39,7 @@ export class MatrixError extends HTTPError { // The Matrix 'errcode' value, e.g. "M_FORBIDDEN". public readonly errcode?: string; // The raw Matrix error JSON used to construct this object. - public data: MatrixErrorJson; + public data: IErrorJson; /** * Construct a Matrix error. This is a JavaScript Error with additional @@ -48,7 +48,7 @@ export class MatrixError extends HTTPError { * @param httpStatus - The numeric HTTP status code given */ public constructor( - errorJson: MatrixErrorJson = {}, + errorJson: IErrorJson = {}, public readonly httpStatus?: number, public url?: string, public event?: MatrixEvent, diff --git a/src/interactive-auth.ts b/src/interactive-auth.ts index fb5e651e00d..3617ce1765f 100644 --- a/src/interactive-auth.ts +++ b/src/interactive-auth.ts @@ -19,7 +19,7 @@ limitations under the License. import { logger } from "./logger"; import { MatrixClient } from "./client"; import { defer, IDeferred } from "./utils"; -import { MatrixError, MatrixErrorJson } from "./http-api"; +import { MatrixError } from "./http-api"; import { UIAResponse } from "./@types/uia"; import { UserIdentifier } from "./@types/auth"; @@ -265,7 +265,7 @@ export class InteractiveAuth { private readonly supportedStages?: Set; // The current latest data or error received from the server during the user interactive auth flow. - private data: IAuthData & MatrixErrorJson; + private data: IAuthData & MatrixError["data"]; private emailSid?: string; private requestingEmailToken = false; private attemptAuthDeferred: IDeferred | null = null; From 3c331ee16e34aad94ae584351ab532cb46c545c5 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Mon, 24 Jul 2023 13:47:57 +0100 Subject: [PATCH 3/3] Add test --- spec/unit/interactive-auth.spec.ts | 38 +++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/spec/unit/interactive-auth.spec.ts b/spec/unit/interactive-auth.spec.ts index ea11fc4c900..bd3c8e653bf 100644 --- a/spec/unit/interactive-auth.spec.ts +++ b/spec/unit/interactive-auth.spec.ts @@ -375,7 +375,7 @@ describe("InteractiveAuth", () => { await expect(ia.attemptAuth.bind(ia)).rejects.toThrow(new Error("No appropriate authentication flow found")); }); - it("should handle unexpected error types without data propery set", async () => { + it("should handle unexpected error types without data property set", async () => { const doRequest = jest.fn(); const stateUpdated = jest.fn(); const requestEmailToken = jest.fn(); @@ -559,4 +559,40 @@ describe("InteractiveAuth", () => { ia.chooseStage(); expect(ia.getChosenFlow()?.stages).toEqual([AuthType.Password]); }); + + it("should fire stateUpdated callback if with error when encountered", async () => { + const doRequest = jest.fn(); + const stateUpdated = jest.fn(); + + const ia = new InteractiveAuth({ + matrixClient: getFakeClient(), + doRequest: doRequest, + stateUpdated: stateUpdated, + requestEmailToken: jest.fn(), + authData: { + session: "sessionId", + flows: [{ stages: [AuthType.Password] }], + params: { + [AuthType.Password]: { param: "aa" }, + }, + }, + }); + + // first we expect a call here + stateUpdated.mockImplementation((stage) => { + expect(stage).toEqual(AuthType.Password); + ia.submitAuthDict({ + type: AuthType.Password, + }); + }); + + // .. which should trigger a call here + doRequest.mockRejectedValue(new MatrixError({ errcode: "M_UNKNOWN", error: "This is an error" })); + + await Promise.allSettled([ia.attemptAuth()]); + expect(stateUpdated).toHaveBeenCalledWith("m.login.password", { + errcode: "M_UNKNOWN", + error: "This is an error", + }); + }); });