Implement dummy registration logic

This commit is contained in:
RMidhunSuresh 2021-12-26 17:59:04 +05:30
parent eb146830ba
commit d28ab919bb
4 changed files with 206 additions and 0 deletions

View File

@ -0,0 +1,85 @@
/*
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 type {HomeServerApi} from "../net/HomeServerApi";
import { registrationStageFromType } from "./registrationStageFromType";
import type {BaseRegistrationStage} from "./stages/BaseRegistrationStage";
export type RegistrationParameters = {
username: string;
password: string;
initialDeviceDisplayName: string;
inhibitLogin: boolean;
}
// todo: type this later
export type RegistrationResponse = {
[key: string]: any;
}
export class Registration {
private _hsApi: HomeServerApi;
private _data: RegistrationParameters;
private _firstStage: BaseRegistrationStage;
private _session: string;
constructor(hsApi: HomeServerApi, data: RegistrationParameters) {
this._hsApi = hsApi;
this._data = data;
}
private async _fetchFlows(): Promise<RegistrationResponse> {
const response = await this._hsApi.register(
this._username,
this._password,
this._initialDeviceDisplayName,
undefined,
this._inhibitLogin).response();
return response;
}
async start(): Promise<BaseRegistrationStage> {
const response = await this._fetchFlows();
this.parseStagesFromResponse(response);
await new Promise(r => setTimeout(r, 2000));
return this._firstStage;
}
parseStagesFromResponse(response: RegistrationResponse) {
this._session = response.session;
const flow = response.flows.pop();
let lastStage: BaseRegistrationStage;
for (const stage of flow.stages) {
const stageClass = registrationStageFromType(stage);
if (!stageClass) {
throw new Error("Unknown stage");
}
const registrationStage = new stageClass(this._hsApi, this._data, this._session);
if (!this._firstStage) {
this._firstStage = registrationStage;
lastStage = registrationStage;
} else {
lastStage!.setNextStage(registrationStage);
lastStage = registrationStage;
}
}
}
private get _username() { return this._data.username; }
private get _password() { return this._data.password; }
private get _initialDeviceDisplayName() { return this._data.initialDeviceDisplayName; }
private get _inhibitLogin() { return this._data.inhibitLogin; }
}

View File

@ -0,0 +1,24 @@
/*
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 {DummyAuth} from "./stages/DummyAuth";
export function registrationStageFromType(type: string): typeof DummyAuth | undefined {
switch (type) {
case "m.login.dummy":
return DummyAuth;
}
}

View File

@ -0,0 +1,64 @@
/*
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.
*/
export type Auth = {
[key: string]: any;
}
import type {HomeServerApi} from "../../net/HomeServerApi";
import type {RegistrationParameters, RegistrationResponse} from "../Registration";
export abstract class BaseRegistrationStage {
protected _hsApi: HomeServerApi;
protected _registrationData: RegistrationParameters;
protected _session: string;
protected _nextStage: BaseRegistrationStage;
constructor(hsApi: HomeServerApi, registrationData: RegistrationParameters, session: string) {
this._hsApi = hsApi;
this._registrationData = registrationData;
this._session = session;
}
/**
* eg: m.login.recaptcha or m.login.dummy
*/
abstract get type(): string;
/**
* Finish a registration stage, return value is:
* - the next stage if this stage was completed successfully
* - true if registration is completed
* - an error if something went wrong
*/
abstract complete(auth?: Auth): Promise<BaseRegistrationStage | Error | true>;
setNextStage(stage: BaseRegistrationStage) {
this._nextStage = stage;
}
parseResponse(response: RegistrationResponse) {
if (response.user_id) {
// registration completed successfully
return true;
}
else if (response.completed?.forEach(c => c === this.type)) {
return this._nextStage;
}
const error = response.error ?? "Could not parse response";
return new Error(error);
}
}

View File

@ -0,0 +1,33 @@
/*
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 {BaseRegistrationStage} from "./BaseRegistrationStage";
export class DummyAuth extends BaseRegistrationStage {
async complete() {
const { username, password, initialDeviceDisplayName, inhibitLogin } = this._registrationData;
const response = await this._hsApi.register(username, password, initialDeviceDisplayName, {
session: this._session,
type: this.type
}, inhibitLogin).response();
return this.parseResponse(response);
}
get type(): string {
return "m.login.dummy";
}
}