2021-12-26 17:59:04 +05:30
|
|
|
/*
|
|
|
|
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 17:54:08 +05:30
|
|
|
import {registrationStageFromType} from "./registrationStageFromType";
|
2021-12-26 17:59:04 +05:30
|
|
|
import type {BaseRegistrationStage} from "./stages/BaseRegistrationStage";
|
2022-02-03 15:10:56 +05:30
|
|
|
import type {
|
|
|
|
AccountDetails,
|
|
|
|
RegistrationFlow,
|
|
|
|
RegistrationResponseMoreDataNeeded,
|
|
|
|
RegistrationResponse,
|
|
|
|
RegistrationResponseSuccess,
|
|
|
|
} from "./types";
|
2022-02-02 15:51:02 +05:30
|
|
|
|
|
|
|
type FlowSelector = (flows: RegistrationFlow[]) => RegistrationFlow | void;
|
2021-12-26 17:59:04 +05:30
|
|
|
|
|
|
|
export class Registration {
|
2022-02-03 19:37:14 +05:30
|
|
|
private readonly _hsApi: HomeServerApi;
|
|
|
|
private readonly _accountDetails: AccountDetails;
|
|
|
|
private readonly _flowSelector: FlowSelector;
|
2022-02-03 15:10:56 +05:30
|
|
|
private _sessionInfo?: RegistrationResponseSuccess
|
2021-12-26 17:59:04 +05:30
|
|
|
|
2022-02-02 16:41:38 +05:30
|
|
|
constructor(hsApi: HomeServerApi, accountDetails: AccountDetails, flowSelector?: FlowSelector) {
|
2021-12-26 17:59:04 +05:30
|
|
|
this._hsApi = hsApi;
|
2022-02-02 16:41:38 +05:30
|
|
|
this._accountDetails = accountDetails;
|
2022-02-03 12:04:13 +05:30
|
|
|
this._flowSelector = flowSelector ?? (flows => flows[0]);
|
2021-12-26 17:59:04 +05:30
|
|
|
}
|
|
|
|
|
2022-02-01 18:01:33 +05:30
|
|
|
async start(): Promise<BaseRegistrationStage> {
|
2021-12-26 17:59:04 +05:30
|
|
|
const response = await this._hsApi.register(
|
2022-02-02 16:41:38 +05:30
|
|
|
this._accountDetails.username,
|
|
|
|
this._accountDetails.password,
|
|
|
|
this._accountDetails.initialDeviceDisplayName,
|
2021-12-26 17:59:04 +05:30
|
|
|
undefined,
|
2022-02-02 16:41:38 +05:30
|
|
|
this._accountDetails.inhibitLogin).response();
|
2022-02-01 18:23:41 +05:30
|
|
|
return this.parseStagesFromResponse(response);
|
2021-12-26 17:59:04 +05:30
|
|
|
}
|
|
|
|
|
2022-02-03 15:10:56 +05:30
|
|
|
/**
|
|
|
|
* Finish a registration stage, return value is:
|
|
|
|
* - the next stage if this stage was completed successfully
|
|
|
|
* - undefined if registration is completed
|
|
|
|
*/
|
|
|
|
async submitStage(stage: BaseRegistrationStage): Promise<BaseRegistrationStage | undefined> {
|
|
|
|
const auth = stage.generateAuthenticationData();
|
|
|
|
const { username, password, initialDeviceDisplayName, inhibitLogin } = this._accountDetails;
|
2022-02-03 18:08:19 +05:30
|
|
|
const request = this._hsApi.register(username, password, initialDeviceDisplayName, auth, inhibitLogin);
|
|
|
|
const response = await request.response();
|
|
|
|
const status = await request.responseCode();
|
|
|
|
const registrationResponse: RegistrationResponse = { ...response, status };
|
|
|
|
return this.parseRegistrationResponse(registrationResponse, stage);
|
2022-02-03 15:10:56 +05:30
|
|
|
}
|
|
|
|
|
2022-02-03 19:37:14 +05:30
|
|
|
private parseStagesFromResponse(response: RegistrationResponseMoreDataNeeded): BaseRegistrationStage {
|
2021-12-27 12:54:54 +05:30
|
|
|
const { session, params } = response;
|
2022-02-02 15:51:02 +05:30
|
|
|
const flow = this._flowSelector(response.flows);
|
2022-02-02 13:58:03 +05:30
|
|
|
if (!flow) {
|
2022-02-02 15:51:02 +05:30
|
|
|
throw new Error("flowSelector did not return any flow!");
|
2022-02-02 13:58:03 +05:30
|
|
|
}
|
2022-02-01 18:23:41 +05:30
|
|
|
let firstStage: BaseRegistrationStage | undefined;
|
2021-12-26 17:59:04 +05:30
|
|
|
let lastStage: BaseRegistrationStage;
|
|
|
|
for (const stage of flow.stages) {
|
|
|
|
const stageClass = registrationStageFromType(stage);
|
|
|
|
if (!stageClass) {
|
2022-02-02 15:51:02 +05:30
|
|
|
throw new Error(`Unknown stage: ${stage}`);
|
2021-12-26 17:59:04 +05:30
|
|
|
}
|
2022-02-04 16:40:49 +05:30
|
|
|
const registrationStage = new stageClass(session, params?.[stage]);
|
2022-02-01 18:23:41 +05:30
|
|
|
if (!firstStage) {
|
|
|
|
firstStage = registrationStage;
|
2021-12-26 17:59:04 +05:30
|
|
|
lastStage = registrationStage;
|
|
|
|
} else {
|
|
|
|
lastStage!.setNextStage(registrationStage);
|
|
|
|
lastStage = registrationStage;
|
|
|
|
}
|
|
|
|
}
|
2022-02-01 18:23:41 +05:30
|
|
|
return firstStage!;
|
2021-12-26 17:59:04 +05:30
|
|
|
}
|
2022-02-03 15:10:56 +05:30
|
|
|
|
2022-02-03 19:37:14 +05:30
|
|
|
private async parseRegistrationResponse(response: RegistrationResponse, currentStage: BaseRegistrationStage) {
|
2022-02-03 18:08:19 +05:30
|
|
|
switch (response.status) {
|
|
|
|
case 200:
|
|
|
|
this._sessionInfo = response;
|
|
|
|
return undefined;
|
|
|
|
case 401:
|
|
|
|
if (response.completed?.includes(currentStage.type)) {
|
|
|
|
return currentStage.nextStage;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
throw new Error("This stage could not be completed!");
|
|
|
|
}
|
2022-02-03 15:10:56 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get sessionInfo(): RegistrationResponseSuccess | undefined {
|
|
|
|
return this._sessionInfo;
|
|
|
|
}
|
2021-12-26 17:59:04 +05:30
|
|
|
}
|