From 91f2a96403e466be902d5a1e4015dd5d421cb8ae Mon Sep 17 00:00:00 2001 From: RMidhunSuresh Date: Wed, 24 Nov 2021 13:40:04 +0530 Subject: [PATCH 1/7] Make LoginMethod an interface --- .../login/{LoginMethod.js => LoginMethod.ts} | 17 +++++------------ src/matrix/login/PasswordLoginMethod.js | 2 +- src/matrix/login/TokenLoginMethod.js | 2 +- 3 files changed, 7 insertions(+), 14 deletions(-) rename src/matrix/login/{LoginMethod.js => LoginMethod.ts} (61%) diff --git a/src/matrix/login/LoginMethod.js b/src/matrix/login/LoginMethod.ts similarity index 61% rename from src/matrix/login/LoginMethod.js rename to src/matrix/login/LoginMethod.ts index ece18871..342632a6 100644 --- a/src/matrix/login/LoginMethod.js +++ b/src/matrix/login/LoginMethod.ts @@ -14,17 +14,10 @@ See the License for the specific language governing permissions and limitations under the License. */ -export class LoginMethod { - constructor({homeserver}) { - this.homeserver = homeserver; - } +import type {ILogItem} from "../../logging/types"; +import type {HomeServerApi} from "../net/HomeServerApi.js"; - // eslint-disable-next-line no-unused-vars - async login(hsApi, deviceName, log) { - /* - Regardless of the login method, SessionContainer.startWithLogin() - can do SomeLoginMethod.login() - */ - throw("Not Implemented"); - } +export interface ILoginMethod { + homeserver: string; + login(hsApi: HomeServerApi, deviceName: string, log: ILogItem): Response["body"]; } diff --git a/src/matrix/login/PasswordLoginMethod.js b/src/matrix/login/PasswordLoginMethod.js index 5c90ccf8..f147f229 100644 --- a/src/matrix/login/PasswordLoginMethod.js +++ b/src/matrix/login/PasswordLoginMethod.js @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -import {LoginMethod} from "./LoginMethod.js"; +import {LoginMethod} from "./LoginMethod"; export class PasswordLoginMethod extends LoginMethod { constructor(options) { diff --git a/src/matrix/login/TokenLoginMethod.js b/src/matrix/login/TokenLoginMethod.js index e55cedcf..1c623045 100644 --- a/src/matrix/login/TokenLoginMethod.js +++ b/src/matrix/login/TokenLoginMethod.js @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -import {LoginMethod} from "./LoginMethod.js"; +import {LoginMethod} from "./LoginMethod"; import {makeTxnId} from "../common.js"; export class TokenLoginMethod extends LoginMethod { From e4c443c73a9544f26c44022fd1e3b021f725742b Mon Sep 17 00:00:00 2001 From: RMidhunSuresh Date: Wed, 24 Nov 2021 13:47:26 +0530 Subject: [PATCH 2/7] Convert PasswordLoginMethod to ts --- src/matrix/login/LoginMethod.ts | 2 +- ...dLoginMethod.js => PasswordLoginMethod.ts} | 20 ++++++++++++------- 2 files changed, 14 insertions(+), 8 deletions(-) rename src/matrix/login/{PasswordLoginMethod.js => PasswordLoginMethod.ts} (54%) diff --git a/src/matrix/login/LoginMethod.ts b/src/matrix/login/LoginMethod.ts index 342632a6..c4456180 100644 --- a/src/matrix/login/LoginMethod.ts +++ b/src/matrix/login/LoginMethod.ts @@ -19,5 +19,5 @@ import type {HomeServerApi} from "../net/HomeServerApi.js"; export interface ILoginMethod { homeserver: string; - login(hsApi: HomeServerApi, deviceName: string, log: ILogItem): Response["body"]; + login(hsApi: HomeServerApi, deviceName: string, log: ILogItem): Promise; } diff --git a/src/matrix/login/PasswordLoginMethod.js b/src/matrix/login/PasswordLoginMethod.ts similarity index 54% rename from src/matrix/login/PasswordLoginMethod.js rename to src/matrix/login/PasswordLoginMethod.ts index f147f229..d9edd3e7 100644 --- a/src/matrix/login/PasswordLoginMethod.js +++ b/src/matrix/login/PasswordLoginMethod.ts @@ -14,16 +14,22 @@ See the License for the specific language governing permissions and limitations under the License. */ -import {LoginMethod} from "./LoginMethod"; +import {ILogItem} from "../../logging/types"; +import {ILoginMethod} from "./LoginMethod"; +import {HomeServerApi} from "../net/HomeServerApi.js"; -export class PasswordLoginMethod extends LoginMethod { - constructor(options) { - super(options); - this.username = options.username; - this.password = options.password; +export class PasswordLoginMethod implements ILoginMethod { + public username: string; + public password: string; + public homeserver: string; + + constructor({username, password, homeserver}: {username: string, password: string, homeserver: string}) { + this.username = username; + this.password = password; + this.homeserver = homeserver; } - async login(hsApi, deviceName, log) { + async login(hsApi: HomeServerApi, deviceName: string, log: ILogItem) { return await hsApi.passwordLogin(this.username, this.password, deviceName, {log}).response(); } } From 64037cb32a24e6a045fcb59f3d3a23d00f45ae83 Mon Sep 17 00:00:00 2001 From: RMidhunSuresh Date: Wed, 24 Nov 2021 13:56:47 +0530 Subject: [PATCH 3/7] Convert TokenLoginMethod to ts --- src/matrix/SessionContainer.js | 2 +- ...{TokenLoginMethod.js => TokenLoginMethod.ts} | 17 +++++++++++------ 2 files changed, 12 insertions(+), 7 deletions(-) rename src/matrix/login/{TokenLoginMethod.js => TokenLoginMethod.ts} (57%) diff --git a/src/matrix/SessionContainer.js b/src/matrix/SessionContainer.js index 3e3dab66..01ea85ff 100644 --- a/src/matrix/SessionContainer.js +++ b/src/matrix/SessionContainer.js @@ -27,7 +27,7 @@ import {RequestScheduler} from "./net/RequestScheduler.js"; import {Sync, SyncStatus} from "./Sync.js"; import {Session} from "./Session.js"; import {PasswordLoginMethod} from "./login/PasswordLoginMethod.js"; -import {TokenLoginMethod} from "./login/TokenLoginMethod.js"; +import {TokenLoginMethod} from "./login/TokenLoginMethod"; import {SSOLoginHelper} from "./login/SSOLoginHelper.js"; import {getDehydratedDevice} from "./e2ee/Dehydration.js"; diff --git a/src/matrix/login/TokenLoginMethod.js b/src/matrix/login/TokenLoginMethod.ts similarity index 57% rename from src/matrix/login/TokenLoginMethod.js rename to src/matrix/login/TokenLoginMethod.ts index 1c623045..63701604 100644 --- a/src/matrix/login/TokenLoginMethod.js +++ b/src/matrix/login/TokenLoginMethod.ts @@ -14,16 +14,21 @@ See the License for the specific language governing permissions and limitations under the License. */ -import {LoginMethod} from "./LoginMethod"; import {makeTxnId} from "../common.js"; +import {ILogItem} from "../../logging/types"; +import {ILoginMethod} from "./LoginMethod"; +import {HomeServerApi} from "../net/HomeServerApi.js"; -export class TokenLoginMethod extends LoginMethod { - constructor(options) { - super(options); - this._loginToken = options.loginToken; +export class TokenLoginMethod implements ILoginMethod { + public readonly homeserver: string; + private readonly _loginToken: string; + + constructor({ homeserver, loginToken }: { homeserver: string, loginToken: string}) { + this.homeserver = homeserver; + this._loginToken = loginToken; } - async login(hsApi, deviceName, log) { + async login(hsApi: HomeServerApi, deviceName: string, log: ILogItem): Promise { return await hsApi.tokenLogin(this._loginToken, makeTxnId(), deviceName, {log}).response(); } } From a1367f8e724e850124a8a9924c35943bd713ce9b Mon Sep 17 00:00:00 2001 From: RMidhunSuresh Date: Wed, 24 Nov 2021 14:00:26 +0530 Subject: [PATCH 4/7] Fix password login --- src/matrix/SessionContainer.js | 2 +- src/matrix/login/PasswordLoginMethod.ts | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/matrix/SessionContainer.js b/src/matrix/SessionContainer.js index 01ea85ff..2e64a2f6 100644 --- a/src/matrix/SessionContainer.js +++ b/src/matrix/SessionContainer.js @@ -26,7 +26,7 @@ import {MediaRepository} from "./net/MediaRepository.js"; import {RequestScheduler} from "./net/RequestScheduler.js"; import {Sync, SyncStatus} from "./Sync.js"; import {Session} from "./Session.js"; -import {PasswordLoginMethod} from "./login/PasswordLoginMethod.js"; +import {PasswordLoginMethod} from "./login/PasswordLoginMethod"; import {TokenLoginMethod} from "./login/TokenLoginMethod"; import {SSOLoginHelper} from "./login/SSOLoginHelper.js"; import {getDehydratedDevice} from "./e2ee/Dehydration.js"; diff --git a/src/matrix/login/PasswordLoginMethod.ts b/src/matrix/login/PasswordLoginMethod.ts index d9edd3e7..46fdd211 100644 --- a/src/matrix/login/PasswordLoginMethod.ts +++ b/src/matrix/login/PasswordLoginMethod.ts @@ -19,17 +19,17 @@ import {ILoginMethod} from "./LoginMethod"; import {HomeServerApi} from "../net/HomeServerApi.js"; export class PasswordLoginMethod implements ILoginMethod { - public username: string; - public password: string; - public homeserver: string; + private readonly _username: string; + private readonly _password: string; + public readonly homeserver: string; constructor({username, password, homeserver}: {username: string, password: string, homeserver: string}) { - this.username = username; - this.password = password; + this._username = username; + this._password = password; this.homeserver = homeserver; } - async login(hsApi: HomeServerApi, deviceName: string, log: ILogItem) { - return await hsApi.passwordLogin(this.username, this.password, deviceName, {log}).response(); + async login(hsApi: HomeServerApi, deviceName: string, log: ILogItem): Promise { + return await hsApi.passwordLogin(this._username, this._password, deviceName, {log}).response(); } } From c54ca168ed90cc75f233c2052af7dc626955aafa Mon Sep 17 00:00:00 2001 From: RMidhunSuresh Date: Wed, 24 Nov 2021 14:49:08 +0530 Subject: [PATCH 5/7] Convert SSOLoginHelper.js to ts --- src/matrix/SessionContainer.js | 2 +- src/matrix/login/{SSOLoginHelper.js => SSOLoginHelper.ts} | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) rename src/matrix/login/{SSOLoginHelper.js => SSOLoginHelper.ts} (80%) diff --git a/src/matrix/SessionContainer.js b/src/matrix/SessionContainer.js index 2e64a2f6..0b917426 100644 --- a/src/matrix/SessionContainer.js +++ b/src/matrix/SessionContainer.js @@ -28,7 +28,7 @@ import {Sync, SyncStatus} from "./Sync.js"; import {Session} from "./Session.js"; import {PasswordLoginMethod} from "./login/PasswordLoginMethod"; import {TokenLoginMethod} from "./login/TokenLoginMethod"; -import {SSOLoginHelper} from "./login/SSOLoginHelper.js"; +import {SSOLoginHelper} from "./login/SSOLoginHelper"; import {getDehydratedDevice} from "./e2ee/Dehydration.js"; export const LoadStatus = createEnum( diff --git a/src/matrix/login/SSOLoginHelper.js b/src/matrix/login/SSOLoginHelper.ts similarity index 80% rename from src/matrix/login/SSOLoginHelper.js rename to src/matrix/login/SSOLoginHelper.ts index a15c8ef9..0fe3d6b8 100644 --- a/src/matrix/login/SSOLoginHelper.js +++ b/src/matrix/login/SSOLoginHelper.ts @@ -15,13 +15,15 @@ limitations under the License. */ export class SSOLoginHelper{ - constructor(homeserver) { + private _homeserver: string; + + constructor(homeserver: string) { this._homeserver = homeserver; } - get homeserver() { return this._homeserver; } + get homeserver(): string { return this._homeserver; } - createSSORedirectURL(returnURL) { + createSSORedirectURL(returnURL: string): string { return `${this._homeserver}/_matrix/client/r0/login/sso/redirect?redirectUrl=${returnURL}`; } } From a31860dc5f15d37a208232424a9735a7b00c9955 Mon Sep 17 00:00:00 2001 From: RMidhunSuresh Date: Wed, 24 Nov 2021 14:55:44 +0530 Subject: [PATCH 6/7] Fix formatting --- src/matrix/login/LoginMethod.ts | 2 +- src/matrix/login/TokenLoginMethod.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/matrix/login/LoginMethod.ts b/src/matrix/login/LoginMethod.ts index c4456180..91977bf1 100644 --- a/src/matrix/login/LoginMethod.ts +++ b/src/matrix/login/LoginMethod.ts @@ -19,5 +19,5 @@ import type {HomeServerApi} from "../net/HomeServerApi.js"; export interface ILoginMethod { homeserver: string; - login(hsApi: HomeServerApi, deviceName: string, log: ILogItem): Promise; + login(hsApi: HomeServerApi, deviceName: string, log: ILogItem): Promise; } diff --git a/src/matrix/login/TokenLoginMethod.ts b/src/matrix/login/TokenLoginMethod.ts index 63701604..5caae19f 100644 --- a/src/matrix/login/TokenLoginMethod.ts +++ b/src/matrix/login/TokenLoginMethod.ts @@ -20,8 +20,8 @@ import {ILoginMethod} from "./LoginMethod"; import {HomeServerApi} from "../net/HomeServerApi.js"; export class TokenLoginMethod implements ILoginMethod { - public readonly homeserver: string; private readonly _loginToken: string; + public readonly homeserver: string; constructor({ homeserver, loginToken }: { homeserver: string, loginToken: string}) { this.homeserver = homeserver; From 346e95c33cb8e9cb801484a2dbb96bc33ab768ff Mon Sep 17 00:00:00 2001 From: RMidhunSuresh Date: Thu, 25 Nov 2021 12:33:12 +0530 Subject: [PATCH 7/7] Change return type --- src/matrix/login/LoginMethod.ts | 2 +- src/matrix/login/PasswordLoginMethod.ts | 2 +- src/matrix/login/TokenLoginMethod.ts | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/matrix/login/LoginMethod.ts b/src/matrix/login/LoginMethod.ts index 91977bf1..2b2a8ff3 100644 --- a/src/matrix/login/LoginMethod.ts +++ b/src/matrix/login/LoginMethod.ts @@ -19,5 +19,5 @@ import type {HomeServerApi} from "../net/HomeServerApi.js"; export interface ILoginMethod { homeserver: string; - login(hsApi: HomeServerApi, deviceName: string, log: ILogItem): Promise; + login(hsApi: HomeServerApi, deviceName: string, log: ILogItem): Promise>; } diff --git a/src/matrix/login/PasswordLoginMethod.ts b/src/matrix/login/PasswordLoginMethod.ts index 46fdd211..65e96ff0 100644 --- a/src/matrix/login/PasswordLoginMethod.ts +++ b/src/matrix/login/PasswordLoginMethod.ts @@ -29,7 +29,7 @@ export class PasswordLoginMethod implements ILoginMethod { this.homeserver = homeserver; } - async login(hsApi: HomeServerApi, deviceName: string, log: ILogItem): Promise { + async login(hsApi: HomeServerApi, deviceName: string, log: ILogItem): Promise> { return await hsApi.passwordLogin(this._username, this._password, deviceName, {log}).response(); } } diff --git a/src/matrix/login/TokenLoginMethod.ts b/src/matrix/login/TokenLoginMethod.ts index 5caae19f..4f1e3cc7 100644 --- a/src/matrix/login/TokenLoginMethod.ts +++ b/src/matrix/login/TokenLoginMethod.ts @@ -28,7 +28,7 @@ export class TokenLoginMethod implements ILoginMethod { this._loginToken = loginToken; } - async login(hsApi: HomeServerApi, deviceName: string, log: ILogItem): Promise { + async login(hsApi: HomeServerApi, deviceName: string, log: ILogItem): Promise> { return await hsApi.tokenLogin(this._loginToken, makeTxnId(), deviceName, {log}).response(); } }