From c40801efd991ba93ae09139d354a90398006a447 Mon Sep 17 00:00:00 2001 From: RMidhunSuresh Date: Wed, 16 Feb 2022 12:33:24 +0530 Subject: [PATCH 1/5] Implement the registration stage --- src/matrix/registration/stages/TokenAuth.ts | 43 +++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/matrix/registration/stages/TokenAuth.ts diff --git a/src/matrix/registration/stages/TokenAuth.ts b/src/matrix/registration/stages/TokenAuth.ts new file mode 100644 index 00000000..6f1a3335 --- /dev/null +++ b/src/matrix/registration/stages/TokenAuth.ts @@ -0,0 +1,43 @@ +/* +Copyright 2022 The Matrix.org Foundation C.I.C. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +import {AuthenticationData} from "../types"; +import {BaseRegistrationStage} from "./BaseRegistrationStage"; + +export const TOKEN_AUTH_TYPE = "org.matrix.msc3231.login.registration_token"; + +export class TokenAuth extends BaseRegistrationStage { + private _token?: string; + + generateAuthenticationData(): AuthenticationData { + if (!this._token) { + throw new Error("No token provided for TokenAuth"); + } + return { + session: this._session, + type: this.type, + token: this._token, + }; + } + + setToken(token: string) { + this._token = token; + } + + get type(): string { + return TOKEN_AUTH_TYPE; + } +} From ed151c8567b175b2c785710b299bc7a618e7c3a5 Mon Sep 17 00:00:00 2001 From: RMidhunSuresh Date: Wed, 16 Feb 2022 12:33:59 +0530 Subject: [PATCH 2/5] Return token stage from createRegistrationStage --- src/matrix/registration/Registration.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/matrix/registration/Registration.ts b/src/matrix/registration/Registration.ts index c9c9af87..0d342639 100644 --- a/src/matrix/registration/Registration.ts +++ b/src/matrix/registration/Registration.ts @@ -18,6 +18,7 @@ import type {HomeServerApi} from "../net/HomeServerApi"; import type {BaseRegistrationStage} from "./stages/BaseRegistrationStage"; import {DummyAuth} from "./stages/DummyAuth"; import {TermsAuth} from "./stages/TermsAuth"; +import {TokenAuth, TOKEN_AUTH_TYPE} from "./stages/TokenAuth"; import type { AccountDetails, RegistrationFlow, @@ -108,6 +109,8 @@ export class Registration { return new DummyAuth(session, params?.[type]); case "m.login.terms": return new TermsAuth(session, params?.[type]); + case TOKEN_AUTH_TYPE: + return new TokenAuth(session, params?.[type]); default: throw new Error(`Unknown stage: ${type}`); } From 60bc4450f3c6d26bf4f2e9f17a4b6210f59d98d0 Mon Sep 17 00:00:00 2001 From: RMidhunSuresh Date: Wed, 16 Feb 2022 13:21:04 +0530 Subject: [PATCH 3/5] Use type from server --- src/matrix/registration/Registration.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/matrix/registration/Registration.ts b/src/matrix/registration/Registration.ts index 0d342639..8bfce8ad 100644 --- a/src/matrix/registration/Registration.ts +++ b/src/matrix/registration/Registration.ts @@ -18,7 +18,7 @@ import type {HomeServerApi} from "../net/HomeServerApi"; import type {BaseRegistrationStage} from "./stages/BaseRegistrationStage"; import {DummyAuth} from "./stages/DummyAuth"; import {TermsAuth} from "./stages/TermsAuth"; -import {TokenAuth, TOKEN_AUTH_TYPE} from "./stages/TokenAuth"; +import {TokenAuth} from "./stages/TokenAuth"; import type { AccountDetails, RegistrationFlow, @@ -94,7 +94,9 @@ export class Registration { this._sessionInfo = response; return undefined; case 401: - if (response.completed?.includes(currentStage.type)) { + // Support unstable prefix for TokenAuth + const typeFromServer = (currentStage as TokenAuth).typeFromServer; + if (response.completed?.includes(typeFromServer ?? currentStage.type)) { return currentStage.nextStage; } else { @@ -109,8 +111,9 @@ export class Registration { return new DummyAuth(session, params?.[type]); case "m.login.terms": return new TermsAuth(session, params?.[type]); - case TOKEN_AUTH_TYPE: - return new TokenAuth(session, params?.[type]); + case "org.matrix.msc3231.login.registration_token": + case "m.login.registration_token": + return new TokenAuth(session, params?.[type], type); default: throw new Error(`Unknown stage: ${type}`); } From a76bcd1739cd87770d716cdfdc0e317af970c1ac Mon Sep 17 00:00:00 2001 From: RMidhunSuresh Date: Wed, 16 Feb 2022 13:36:24 +0530 Subject: [PATCH 4/5] Changes in TokenAuth --- src/matrix/registration/stages/TokenAuth.ts | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/matrix/registration/stages/TokenAuth.ts b/src/matrix/registration/stages/TokenAuth.ts index 6f1a3335..607ee910 100644 --- a/src/matrix/registration/stages/TokenAuth.ts +++ b/src/matrix/registration/stages/TokenAuth.ts @@ -14,13 +14,18 @@ See the License for the specific language governing permissions and limitations under the License. */ -import {AuthenticationData} from "../types"; +import {AuthenticationData, RegistrationParams} from "../types"; import {BaseRegistrationStage} from "./BaseRegistrationStage"; -export const TOKEN_AUTH_TYPE = "org.matrix.msc3231.login.registration_token"; - export class TokenAuth extends BaseRegistrationStage { private _token?: string; + private readonly _type: string; + + constructor(session: string, params: RegistrationParams | undefined, type: string) { + super(session, params); + this._type = type; + } + generateAuthenticationData(): AuthenticationData { if (!this._token) { @@ -28,7 +33,7 @@ export class TokenAuth extends BaseRegistrationStage { } return { session: this._session, - type: this.type, + type: this._type, token: this._token, }; } @@ -38,6 +43,10 @@ export class TokenAuth extends BaseRegistrationStage { } get type(): string { - return TOKEN_AUTH_TYPE; + return "m.login.registration_token"; + } + + get typeFromServer(): string { + return this._type; } } From 7a9298328f3ac0b2348177f6919c1bc1018deca1 Mon Sep 17 00:00:00 2001 From: RMidhunSuresh Date: Wed, 16 Feb 2022 14:37:18 +0530 Subject: [PATCH 5/5] Return _type from getter --- src/matrix/registration/Registration.ts | 4 +--- src/matrix/registration/stages/TokenAuth.ts | 4 ---- 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/src/matrix/registration/Registration.ts b/src/matrix/registration/Registration.ts index 8bfce8ad..ded66719 100644 --- a/src/matrix/registration/Registration.ts +++ b/src/matrix/registration/Registration.ts @@ -94,9 +94,7 @@ export class Registration { this._sessionInfo = response; return undefined; case 401: - // Support unstable prefix for TokenAuth - const typeFromServer = (currentStage as TokenAuth).typeFromServer; - if (response.completed?.includes(typeFromServer ?? currentStage.type)) { + if (response.completed?.includes(currentStage.type)) { return currentStage.nextStage; } else { diff --git a/src/matrix/registration/stages/TokenAuth.ts b/src/matrix/registration/stages/TokenAuth.ts index 607ee910..cb238bcb 100644 --- a/src/matrix/registration/stages/TokenAuth.ts +++ b/src/matrix/registration/stages/TokenAuth.ts @@ -43,10 +43,6 @@ export class TokenAuth extends BaseRegistrationStage { } get type(): string { - return "m.login.registration_token"; - } - - get typeFromServer(): string { return this._type; } }