vector-im-hydrogen-web/src/matrix/registration/Registration.ts

134 lines
5.2 KiB
TypeScript
Raw Normal View History

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";
import type {BaseRegistrationStage} from "./stages/BaseRegistrationStage";
import {DummyAuth} from "./stages/DummyAuth";
import {TermsAuth} from "./stages/TermsAuth";
2022-02-16 08:51:04 +01:00
import {TokenAuth} from "./stages/TokenAuth";
import type {
AccountDetails,
RegistrationFlow,
RegistrationResponseMoreDataNeeded,
RegistrationResponse,
RegistrationResponseSuccess,
AuthData,
RegistrationParams,
} from "./types";
2022-02-02 11:21:02 +01:00
type FlowSelector = (flows: RegistrationFlow[]) => RegistrationFlow | void;
2021-12-26 13:29:04 +01:00
export class Registration {
private readonly _hsApi: HomeServerApi;
private readonly _accountDetails: AccountDetails;
private readonly _flowSelector: FlowSelector;
private _registerResponse?: RegistrationResponseSuccess;
public readonly homeserver: string;
2021-12-26 13:29:04 +01:00
constructor(homeserver: string, hsApi: HomeServerApi, accountDetails: AccountDetails, flowSelector?: FlowSelector) {
this.homeserver = homeserver;
2021-12-26 13:29:04 +01:00
this._hsApi = hsApi;
2022-02-02 12:11:38 +01:00
this._accountDetails = accountDetails;
2022-02-03 07:34:13 +01:00
this._flowSelector = flowSelector ?? (flows => flows[0]);
2021-12-26 13:29:04 +01:00
}
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-02 12:11:38 +01:00
this._accountDetails.username,
this._accountDetails.password,
this._accountDetails.initialDeviceDisplayName,
2021-12-26 13:29:04 +01:00
undefined,
2022-02-02 12:11:38 +01:00
this._accountDetails.inhibitLogin).response();
2022-02-01 13:53:41 +01:00
return this.parseStagesFromResponse(response);
2021-12-26 13:29:04 +01:00
}
/**
* 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;
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);
}
private parseStagesFromResponse(response: RegistrationResponseMoreDataNeeded): BaseRegistrationStage {
const { session, params } = response;
2022-02-02 11:21:02 +01:00
const flow = this._flowSelector(response.flows);
2022-02-02 09:28:03 +01:00
if (!flow) {
2022-02-02 11:21:02 +01:00
throw new Error("flowSelector did not return any flow!");
2022-02-02 09:28:03 +01:00
}
2022-02-01 13:53:41 +01:00
let firstStage: BaseRegistrationStage | undefined;
2022-02-04 12:41:33 +01:00
let lastStage: BaseRegistrationStage | undefined;
2021-12-26 13:29:04 +01:00
for (const stage of flow.stages) {
const registrationStage = this._createRegistrationStage(stage, session, params);
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
}
private async parseRegistrationResponse(response: RegistrationResponse, currentStage: BaseRegistrationStage) {
switch (response.status) {
case 200:
this._registerResponse = response;
return undefined;
case 401:
2022-02-16 10:07:18 +01:00
if (response.completed?.includes(currentStage.type)) {
return currentStage.nextStage;
}
else {
throw new Error("This stage could not be completed!");
}
}
}
private _createRegistrationStage(type: string, session: string, params?: RegistrationParams) {
switch (type) {
case "m.login.dummy":
2022-02-04 12:55:15 +01:00
return new DummyAuth(session, params?.[type]);
case "m.login.terms":
2022-02-04 12:55:15 +01:00
return new TermsAuth(session, params?.[type]);
2022-02-16 08:51:04 +01:00
case "org.matrix.msc3231.login.registration_token":
case "m.login.registration_token":
return new TokenAuth(session, params?.[type], type);
default:
throw new Error(`Unknown stage: ${type}`);
}
}
get authData(): AuthData | undefined {
if (this._registerResponse) {
return {
accessToken: this._registerResponse.access_token,
homeserver: this.homeserver,
userId: this._registerResponse.user_id,
deviceId: this._registerResponse.device_id,
};
}
}
2021-12-26 13:29:04 +01:00
}