2021-12-26 13:29:04 +01:00
|
|
|
/*
|
|
|
|
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";
|
2022-02-01 13:24:08 +01:00
|
|
|
import {registrationStageFromType} from "./registrationStageFromType";
|
2021-12-26 13:29:04 +01:00
|
|
|
import type {BaseRegistrationStage} from "./stages/BaseRegistrationStage";
|
|
|
|
|
2022-02-02 08:15:49 +01:00
|
|
|
export type RegistrationDetails = {
|
2022-02-01 13:26:43 +01:00
|
|
|
username: string | null;
|
2021-12-26 13:29:04 +01:00
|
|
|
password: string;
|
|
|
|
initialDeviceDisplayName: string;
|
|
|
|
inhibitLogin: boolean;
|
2022-02-02 09:28:03 +01:00
|
|
|
}
|
2021-12-26 13:29:04 +01:00
|
|
|
|
|
|
|
export type RegistrationResponse = {
|
2022-02-02 09:28:03 +01:00
|
|
|
completed: string[];
|
|
|
|
flows: Record<string, any>[];
|
|
|
|
params: Record<string, any>;
|
|
|
|
session: string;
|
|
|
|
} & error & success;
|
|
|
|
|
|
|
|
type error = {
|
|
|
|
errcode: string;
|
|
|
|
error: string;
|
|
|
|
}
|
|
|
|
type success = {
|
|
|
|
user_id: string;
|
|
|
|
device_id: string;
|
|
|
|
access_token?: string;
|
2021-12-26 13:29:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export class Registration {
|
|
|
|
private _hsApi: HomeServerApi;
|
2022-02-02 08:15:49 +01:00
|
|
|
private _data: RegistrationDetails;
|
2021-12-26 13:29:04 +01:00
|
|
|
|
2022-02-02 08:15:49 +01:00
|
|
|
constructor(hsApi: HomeServerApi, data: RegistrationDetails) {
|
2021-12-26 13:29:04 +01:00
|
|
|
this._hsApi = hsApi;
|
|
|
|
this._data = data;
|
|
|
|
}
|
|
|
|
|
2022-02-01 13:31:33 +01:00
|
|
|
async start(): Promise<BaseRegistrationStage> {
|
2021-12-26 13:29:04 +01:00
|
|
|
const response = await this._hsApi.register(
|
2022-02-01 13:56:20 +01:00
|
|
|
this._data.username,
|
|
|
|
this._data.password,
|
|
|
|
this._data.initialDeviceDisplayName,
|
2021-12-26 13:29:04 +01:00
|
|
|
undefined,
|
2022-02-01 13:56:20 +01:00
|
|
|
this._data.inhibitLogin).response();
|
2022-02-01 13:53:41 +01:00
|
|
|
return this.parseStagesFromResponse(response);
|
2021-12-26 13:29:04 +01:00
|
|
|
}
|
|
|
|
|
2022-02-01 13:53:41 +01:00
|
|
|
parseStagesFromResponse(response: RegistrationResponse): BaseRegistrationStage {
|
2021-12-27 08:24:54 +01:00
|
|
|
const { session, params } = response;
|
2021-12-26 13:29:04 +01:00
|
|
|
const flow = response.flows.pop();
|
2022-02-02 09:28:03 +01:00
|
|
|
if (!flow) {
|
|
|
|
throw new Error("No registration flows available!");
|
|
|
|
}
|
2022-02-01 13:53:41 +01:00
|
|
|
let firstStage: BaseRegistrationStage | undefined;
|
2021-12-26 13:29:04 +01:00
|
|
|
let lastStage: BaseRegistrationStage;
|
|
|
|
for (const stage of flow.stages) {
|
|
|
|
const stageClass = registrationStageFromType(stage);
|
|
|
|
if (!stageClass) {
|
|
|
|
throw new Error("Unknown stage");
|
|
|
|
}
|
2021-12-27 08:24:54 +01:00
|
|
|
const registrationStage = new stageClass(this._hsApi, this._data, session, params?.[stage]);
|
2022-02-01 13:53:41 +01:00
|
|
|
if (!firstStage) {
|
|
|
|
firstStage = registrationStage;
|
2021-12-26 13:29:04 +01:00
|
|
|
lastStage = registrationStage;
|
|
|
|
} else {
|
|
|
|
lastStage!.setNextStage(registrationStage);
|
|
|
|
lastStage = registrationStage;
|
|
|
|
}
|
|
|
|
}
|
2022-02-01 13:53:41 +01:00
|
|
|
return firstStage!;
|
2021-12-26 13:29:04 +01:00
|
|
|
}
|
|
|
|
}
|