Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add User model #175

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .pnp.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions packages/models/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"another-deep-freeze": "^1.0.0",
"context": "^3.0.31",
"dinero.js": "^1.9.1",
"luxon": "^3.5.0",
"object-code": "^1.3.3",
"polytype": "^0.17.0",
"tsd": "^0.31.2",
Expand All @@ -56,6 +57,7 @@
"@testing-library/react": "^16.0.1",
"@types/dinero.js": "^1",
"@types/jest": "^29.5.12",
"@types/luxon": "^3",
"@types/react": "^18.3.3",
"@types/react-dom": "^18",
"@typescript-eslint/eslint-plugin": "^7.18.0",
Expand Down
51 changes: 51 additions & 0 deletions packages/models/src/auth/Mfa/Mfa.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { DataModel } from "../../base/index.js";
import { MfaStatusData, RecoverMfaRequestData } from "./types.js";
import { provideReact } from "../../react/index.js";
import { config } from "../../config/config.js";
import { RecoveryCodes } from "../RecoveryCodes/RecoveryCodes.js";

export class Mfa {
public static getStatus = provideReact(
async (): Promise<MfaStatus | undefined> => {
const data = await config.behaviors.mfa.getStatus();
return new MfaStatus(data);
},
);

public static async confirm(multiFactorCode: string): Promise<RecoveryCodes> {
const response = await config.behaviors.mfa.confirm(multiFactorCode);
return new RecoveryCodes({
codes: response.recoveryCodesList,
});
}

public static async disable(multiFactorCode: string): Promise<void> {
await config.behaviors.mfa.disable(multiFactorCode);
}

public static async recover(data: RecoverMfaRequestData): Promise<void> {
await config.behaviors.mfa.recover(data);
}

public static async resetRecoveryCodes(
multiFactorCode: string,
): Promise<RecoveryCodes> {
const response =
await config.behaviors.mfa.resetRecoveryCodes(multiFactorCode);

return new RecoveryCodes({
codes: response.recoveryCodesList,
});
}
}

export class MfaStatus extends DataModel<MfaStatusData> {
public readonly isInitialized;
public readonly isConfirmed;

public constructor(data: MfaStatusData) {
super(data);
this.isConfirmed = data.confirmed;
this.isInitialized = data.initialized;
}
}
26 changes: 26 additions & 0 deletions packages/models/src/auth/Mfa/PendingMfaAuthentication.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { UserAuthenticateRequestData } from "../../user/index.js";
import { config } from "../../config/config.js";
import { Session } from "../Session/index.js";

export class PendingMfaAuthentication {
private authenticationRequestData: UserAuthenticateRequestData | undefined;

public constructor(authenticationRequestData: UserAuthenticateRequestData) {
this.authenticationRequestData = authenticationRequestData;
}

public async provideMultiFactorCode(multiFactorCode: string) {
const sessionData = await config.behaviors.mfa.authenticateMfa({
...this.authenticationRequestData,
multiFactorCode,
});

this.authenticationRequestData = undefined;

return new Session(sessionData);
}

public get isAlreadyConfirmed() {
return !this.authenticationRequestData;
}
}
64 changes: 64 additions & 0 deletions packages/models/src/auth/Mfa/behaviors/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { assertStatus, MittwaldAPIV2Client } from "@mittwald/api-client";
import { MfaBehaviors } from "./types.js";

export const apiMfaBehaviors = (client: MittwaldAPIV2Client): MfaBehaviors => ({
authenticateMfa: async (data) => {
const response = await client.user.authenticateMfa({
data,
});

assertStatus(response, 200);

return response.data;
},

getStatus: async () => {
const response = await client.user.getMfaStatus({});

assertStatus(response, 200);

return response.data;
},

recover: async (data) => {
const { recoveryCode, ...restData } = data;
const response = await client.user.authenticateMfa({
data: {
multiFactorCode: recoveryCode,
...restData,
},
});

assertStatus(response, 200);

return response.data;
},

confirm: async (multiFactorCode: string) => {
const response = await client.user.confirmMfa({
data: { multiFactorCode },
});

assertStatus(response, 200);

return response.data;
},

resetRecoveryCodes: async (multiFactorCode: string) => {
const response = await client.user.resetRecoverycodes({
data: { multiFactorCode },
});

assertStatus(response, 200);

return response.data;
},

disable: async (multiFactorCode: string) => {
const response = await client.user.disableMfa({
data: { multiFactorCode },
});

assertStatus(response, 204);
},
});
2 changes: 2 additions & 0 deletions packages/models/src/auth/Mfa/behaviors/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./api.js";
export * from "./types.js";
24 changes: 24 additions & 0 deletions packages/models/src/auth/Mfa/behaviors/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {
AuthenticateMfaRequestData,
ConfirmMfaResponseData,
MfaStatusData,
RecoverMfaRequestData,
ResetMfaRecoveryResponseData,
} from "../types.js";
import { SessionData } from "../../Session/types.js";

export interface MfaBehaviors {
getStatus: () => Promise<MfaStatusData>;

recover: (data: RecoverMfaRequestData) => Promise<SessionData>;

resetRecoveryCodes: (
multiFactorCode: string,
) => Promise<ResetMfaRecoveryResponseData>;

confirm: (multiFactorCode: string) => Promise<ConfirmMfaResponseData>;

disable: (multiFactorCode: string) => Promise<void>;

authenticateMfa: (data: AuthenticateMfaRequestData) => Promise<SessionData>;
}
1 change: 1 addition & 0 deletions packages/models/src/auth/Mfa/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./Mfa.js";
20 changes: 20 additions & 0 deletions packages/models/src/auth/Mfa/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { MittwaldAPIV2 } from "@mittwald/api-client";

export type MfaStatusData =
MittwaldAPIV2.Operations.UserGetMfaStatus.ResponseData;

export type RecoverMfaRequestData = Omit<
MittwaldAPIV2.Paths.V2AuthenticateMfa.Post.Parameters.RequestBody,
"multiFactorCode"
> & {
recoveryCode: string;
};

export type ResetMfaRecoveryResponseData =
MittwaldAPIV2.Operations.UserResetRecoverycodes.ResponseData;

export type ConfirmMfaResponseData =
MittwaldAPIV2.Operations.UserConfirmMfa.ResponseData;

export type AuthenticateMfaRequestData =
MittwaldAPIV2.Paths.V2AuthenticateMfa.Post.Parameters.RequestBody;
11 changes: 11 additions & 0 deletions packages/models/src/auth/RecoveryCodes/RecoveryCodes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { DataModel } from "../../base/index.js";
import { RecoveryCodesData } from "./types.js";

export class RecoveryCodes extends DataModel<RecoveryCodesData> {
public readonly codes: string[];

public constructor(data: RecoveryCodesData) {
super(data);
this.codes = data.codes;
}
}
1 change: 1 addition & 0 deletions packages/models/src/auth/RecoveryCodes/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./RecoveryCodes.js";
3 changes: 3 additions & 0 deletions packages/models/src/auth/RecoveryCodes/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface RecoveryCodesData {
codes: string[];
}
20 changes: 20 additions & 0 deletions packages/models/src/auth/Session/Session.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { DataModel } from "../../base/index.js";
import { SessionData } from "./types.js";
import { DateTime } from "luxon";

export class Session extends DataModel<SessionData> {
public readonly expirationDate: DateTime;
public readonly token: string;
public readonly refreshToken: string;

public constructor(data: SessionData) {
super(data);
this.expirationDate = DateTime.fromISO(data.expires);
this.token = data.token;
this.refreshToken = data.refreshToken;
}

public isExpired() {
return this.expirationDate > DateTime.now();
}
}
1 change: 1 addition & 0 deletions packages/models/src/auth/Session/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./Session.js";
5 changes: 5 additions & 0 deletions packages/models/src/auth/Session/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface SessionData {
expires: string;
token: string;
refreshToken: string;
}
4 changes: 4 additions & 0 deletions packages/models/src/config/behaviors/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { addUrlTagToProvideReactCache } from "../../react/asyncResourceInvalidat
import { apiArticleBehaviors } from "../../article/Article/behaviors/index.js";
import { apiContractBehaviors } from "../../contract/Contract/behaviors/index.js";
import { apiContractItemBehaviors } from "../../contract/ContractItem/behaviors/index.js";
import { apiUserBehaviors } from "../../user/User/behaviors/index.js";
import { apiMfaBehaviors } from "../../auth/Mfa/behaviors/index.js";

class ApiSetupState {
private _client: MittwaldAPIV2Client | undefined;
Expand All @@ -29,6 +31,8 @@ class ApiSetupState {
config.behaviors.customer = apiCustomerBehaviors(client);
config.behaviors.ingress = apiIngressBehaviors(client);
config.behaviors.appInstallation = apiAppInstallationBehaviors(client);
config.behaviors.user = apiUserBehaviors(client);
config.behaviors.mfa = apiMfaBehaviors(client);
config.behaviors.contract = apiContractBehaviors(client);
config.behaviors.contractItem = apiContractItemBehaviors(client);
}
Expand Down
6 changes: 6 additions & 0 deletions packages/models/src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import { CustomerBehaviors } from "../customer/Customer/behaviors/index.js";
import { IngressBehaviors } from "../domain/Ingress/behaviors/index.js";
import { ContractBehaviors } from "../contract/Contract/behaviors/index.js";
import { AppInstallationBehaviors } from "../app/AppInstallation/behaviors/index.js";
import { UserBehaviors } from "../user/User/behaviors/index.js";
import { ContractItemBehaviors } from "../contract/ContractItem/behaviors/index.js";
import { ArticleBehaviors } from "../article/Article/behaviors/index.js";
import { MfaBehaviors } from "../auth/Mfa/behaviors/index.js";

interface Config {
defaultPaginationLimit: number;
Expand All @@ -18,6 +20,8 @@ interface Config {
customer: CustomerBehaviors;
ingress: IngressBehaviors;
appInstallation: AppInstallationBehaviors;
user: UserBehaviors;
mfa: MfaBehaviors;
};
}

Expand All @@ -32,5 +36,7 @@ export const config: Config = {
customer: undefined as unknown as CustomerBehaviors,
ingress: undefined as unknown as IngressBehaviors,
appInstallation: undefined as unknown as AppInstallationBehaviors,
user: undefined as unknown as UserBehaviors,
mfa: undefined as unknown as MfaBehaviors,
},
};
Loading