Implement flow seclector

This commit is contained in:
RMidhunSuresh 2022-02-02 15:51:02 +05:30
parent 6798a5e429
commit a249a1b2b5
4 changed files with 16 additions and 8 deletions

View file

@ -17,15 +17,19 @@ limitations under the License.
import type {HomeServerApi} from "../net/HomeServerApi"; import type {HomeServerApi} from "../net/HomeServerApi";
import {registrationStageFromType} from "./registrationStageFromType"; import {registrationStageFromType} from "./registrationStageFromType";
import type {BaseRegistrationStage} from "./stages/BaseRegistrationStage"; import type {BaseRegistrationStage} from "./stages/BaseRegistrationStage";
import type {RegistrationDetails, RegistrationResponse} from "./types/type"; import type {RegistrationDetails, RegistrationResponse, RegistrationFlow} from "./types/types";
type FlowSelector = (flows: RegistrationFlow[]) => RegistrationFlow | void;
export class Registration { export class Registration {
private _hsApi: HomeServerApi; private _hsApi: HomeServerApi;
private _data: RegistrationDetails; private _data: RegistrationDetails;
private _flowSelector: FlowSelector;
constructor(hsApi: HomeServerApi, data: RegistrationDetails) { constructor(hsApi: HomeServerApi, data: RegistrationDetails, flowSelector?: FlowSelector) {
this._hsApi = hsApi; this._hsApi = hsApi;
this._data = data; this._data = data;
this._flowSelector = flowSelector ?? (flows => flows.pop());
} }
async start(): Promise<BaseRegistrationStage> { async start(): Promise<BaseRegistrationStage> {
@ -40,16 +44,16 @@ export class Registration {
parseStagesFromResponse(response: RegistrationResponse): BaseRegistrationStage { parseStagesFromResponse(response: RegistrationResponse): BaseRegistrationStage {
const { session, params } = response; const { session, params } = response;
const flow = response.flows.pop(); const flow = this._flowSelector(response.flows);
if (!flow) { if (!flow) {
throw new Error("No registration flows available!"); throw new Error("flowSelector did not return any flow!");
} }
let firstStage: BaseRegistrationStage | undefined; let firstStage: BaseRegistrationStage | undefined;
let lastStage: BaseRegistrationStage; let lastStage: BaseRegistrationStage;
for (const stage of flow.stages) { for (const stage of flow.stages) {
const stageClass = registrationStageFromType(stage); const stageClass = registrationStageFromType(stage);
if (!stageClass) { if (!stageClass) {
throw new Error("Unknown stage"); throw new Error(`Unknown stage: ${stage}`);
} }
const registrationStage = new stageClass(this._hsApi, this._data, session, params?.[stage]); const registrationStage = new stageClass(this._hsApi, this._data, session, params?.[stage]);
if (!firstStage) { if (!firstStage) {

View file

@ -16,7 +16,7 @@ limitations under the License.
import type {BaseRegistrationStage} from "./stages/BaseRegistrationStage"; import type {BaseRegistrationStage} from "./stages/BaseRegistrationStage";
import type {HomeServerApi} from "../net/HomeServerApi"; import type {HomeServerApi} from "../net/HomeServerApi";
import type {RegistrationDetails} from "./types/type"; import type {RegistrationDetails} from "./types/types";
import {DummyAuth} from "./stages/DummyAuth"; import {DummyAuth} from "./stages/DummyAuth";
import {TermsAuth} from "./stages/TermsAuth"; import {TermsAuth} from "./stages/TermsAuth";

View file

@ -15,7 +15,7 @@ limitations under the License.
*/ */
import type {HomeServerApi} from "../../net/HomeServerApi"; import type {HomeServerApi} from "../../net/HomeServerApi";
import type {RegistrationDetails, RegistrationResponse, AuthenticationData, RegistrationParams} from "../types/type"; import type {RegistrationDetails, RegistrationResponse, AuthenticationData, RegistrationParams} from "../types/types";
export abstract class BaseRegistrationStage { export abstract class BaseRegistrationStage {
protected _hsApi: HomeServerApi; protected _hsApi: HomeServerApi;

View file

@ -25,7 +25,7 @@ export type RegistrationResponse = RegistrationResponse401 & RegistrationRespons
type RegistrationResponse401 = { type RegistrationResponse401 = {
completed: string[]; completed: string[];
flows: Record<string, any>[]; flows: RegistrationFlow[];
params: Record<string, any>; params: Record<string, any>;
session: string; session: string;
} }
@ -41,6 +41,10 @@ type RegistrationResponseSuccess = {
access_token?: string; access_token?: string;
} }
export type RegistrationFlow = {
stages: string[];
}
/* Types for Registration Stage */ /* Types for Registration Stage */
export type AuthenticationData = { export type AuthenticationData = {