summaryrefslogtreecommitdiff
path: root/web/src/services/UserInfo.ts
blob: db53026247b722e79952978458761a1a8abd4e27 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { SecondFactorMethod } from "@models/Methods";
import { UserInfo } from "@models/UserInfo";
import { UserInfo2FAMethodPath, UserInfoPath } from "@services/Api";
import { Get, Post, PostWithOptionalResponse } from "@services/Client";

export type Method2FA = "webauthn" | "totp" | "mobile_push";

export interface UserInfoPayload {
    display_name: string;
    emails: string[];
    method: Method2FA;
    has_webauthn: boolean;
    has_totp: boolean;
    has_duo: boolean;
}

export interface MethodPreferencePayload {
    method: Method2FA;
}

export function isMethod2FA(method: string) {
    return ["webauthn", "totp", "mobile_push"].includes(method);
}

export function toSecondFactorMethod(method: Method2FA): SecondFactorMethod {
    switch (method) {
        case "totp":
            return SecondFactorMethod.TOTP;
        case "webauthn":
            return SecondFactorMethod.WebAuthn;
        case "mobile_push":
            return SecondFactorMethod.MobilePush;
    }
}

export function toMethod2FA(method: SecondFactorMethod): Method2FA {
    switch (method) {
        case SecondFactorMethod.TOTP:
            return "totp";
        case SecondFactorMethod.WebAuthn:
            return "webauthn";
        case SecondFactorMethod.MobilePush:
            return "mobile_push";
    }
}

export async function postUserInfo(): Promise<UserInfo> {
    const res = await Post<UserInfoPayload>(UserInfoPath);
    return { ...res, method: toSecondFactorMethod(res.method) };
}

export async function getUserInfo(): Promise<UserInfo> {
    const res = await Get<UserInfoPayload>(UserInfoPath);
    return { ...res, method: toSecondFactorMethod(res.method) };
}

export function setPreferred2FAMethod(method: SecondFactorMethod) {
    return PostWithOptionalResponse(UserInfo2FAMethodPath, { method: toMethod2FA(method) } as MethodPreferencePayload);
}