Merge pull request #593 from vector-im/ts-conversion-matrix-login

Convert /matrix/login to typescript
This commit is contained in:
Bruno Windels 2021-12-02 09:28:47 +01:00 committed by GitHub
commit d3ab961364
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 59 additions and 53 deletions

View file

@ -26,9 +26,9 @@ import {MediaRepository} from "./net/MediaRepository.js";
import {RequestScheduler} from "./net/RequestScheduler.js"; import {RequestScheduler} from "./net/RequestScheduler.js";
import {Sync, SyncStatus} from "./Sync.js"; import {Sync, SyncStatus} from "./Sync.js";
import {Session} from "./Session.js"; import {Session} from "./Session.js";
import {PasswordLoginMethod} from "./login/PasswordLoginMethod.js"; import {PasswordLoginMethod} from "./login/PasswordLoginMethod";
import {TokenLoginMethod} from "./login/TokenLoginMethod.js"; import {TokenLoginMethod} from "./login/TokenLoginMethod";
import {SSOLoginHelper} from "./login/SSOLoginHelper.js"; import {SSOLoginHelper} from "./login/SSOLoginHelper";
import {getDehydratedDevice} from "./e2ee/Dehydration.js"; import {getDehydratedDevice} from "./e2ee/Dehydration.js";
export const LoadStatus = createEnum( export const LoadStatus = createEnum(

View file

@ -14,17 +14,10 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
export class LoginMethod { import type {ILogItem} from "../../logging/types";
constructor({homeserver}) { import type {HomeServerApi} from "../net/HomeServerApi.js";
this.homeserver = homeserver;
}
// eslint-disable-next-line no-unused-vars export interface ILoginMethod {
async login(hsApi, deviceName, log) { homeserver: string;
/* login(hsApi: HomeServerApi, deviceName: string, log: ILogItem): Promise<Record<string, any>>;
Regardless of the login method, SessionContainer.startWithLogin()
can do SomeLoginMethod.login()
*/
throw("Not Implemented");
}
} }

View file

@ -1,29 +0,0 @@
/*
Copyright 2021 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 {LoginMethod} from "./LoginMethod.js";
export class PasswordLoginMethod extends LoginMethod {
constructor(options) {
super(options);
this.username = options.username;
this.password = options.password;
}
async login(hsApi, deviceName, log) {
return await hsApi.passwordLogin(this.username, this.password, deviceName, {log}).response();
}
}

View file

@ -0,0 +1,35 @@
/*
Copyright 2021 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 {ILogItem} from "../../logging/types";
import {ILoginMethod} from "./LoginMethod";
import {HomeServerApi} from "../net/HomeServerApi.js";
export class PasswordLoginMethod implements ILoginMethod {
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.homeserver = homeserver;
}
async login(hsApi: HomeServerApi, deviceName: string, log: ILogItem): Promise<Record<string, any>> {
return await hsApi.passwordLogin(this._username, this._password, deviceName, {log}).response();
}
}

View file

@ -15,13 +15,15 @@ limitations under the License.
*/ */
export class SSOLoginHelper{ export class SSOLoginHelper{
constructor(homeserver) { private _homeserver: string;
constructor(homeserver: string) {
this._homeserver = homeserver; 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}`; return `${this._homeserver}/_matrix/client/r0/login/sso/redirect?redirectUrl=${returnURL}`;
} }
} }

View file

@ -14,16 +14,21 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
import {LoginMethod} from "./LoginMethod.js";
import {makeTxnId} from "../common.js"; 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 { export class TokenLoginMethod implements ILoginMethod {
constructor(options) { private readonly _loginToken: string;
super(options); public readonly homeserver: string;
this._loginToken = options.loginToken;
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<Record<string, any>> {
return await hsApi.tokenLogin(this._loginToken, makeTxnId(), deviceName, {log}).response(); return await hsApi.tokenLogin(this._loginToken, makeTxnId(), deviceName, {log}).response();
} }
} }