summaryrefslogtreecommitdiff
path: root/web/src/views/Settings/Common/SecondFactorMethodMobilePush.tsx
blob: d749e08fd1f70dda71c6cac62b6bb705bf3090a7 (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import React, { Fragment, ReactNode, useCallback, useEffect, useState } from "react";

import { Box, Button, Link, Theme } from "@mui/material";
import makeStyles from "@mui/styles/makeStyles";
import { useTranslation } from "react-i18next";

import FailureIcon from "@components/FailureIcon";
import PushNotificationIcon from "@components/PushNotificationIcon";
import { useIsMountedRef } from "@hooks/Mounted";
import {
    DuoDevicePostRequest,
    completeDuoDeviceSelectionProcess,
    completePushNotificationSignIn,
    initiateDuoDeviceSelectionProcess,
} from "@services/PushNotification";
import DeviceSelectionContainer, {
    SelectableDevice,
    SelectedDevice,
} from "@views/LoginPortal/SecondFactor/DeviceSelectionContainer";

export enum State {
    SignInInProgress = 1,
    Success = 2,
    Failure = 3,
    Selection = 4,
}

export interface Props {
    closing: boolean;
    onSecondFactorSuccess: () => void;
}

const SecondFactorMethodMobilePush = function (props: Props) {
    const { t: translate } = useTranslation("settings");

    const styles = useStyles();
    const [state, setState] = useState(State.SignInInProgress);
    const mounted = useIsMountedRef();
    const [devices, setDevices] = useState([] as SelectableDevice[]);

    const handleSelectDevice = useCallback(async () => {
        try {
            const res = await initiateDuoDeviceSelectionProcess();
            if (!mounted.current) return;
            switch (res.result) {
                case "auth":
                    let selectableDevices = [] as SelectableDevice[];
                    res.devices.forEach((d: { device: any; display_name: any; capabilities: any }) =>
                        selectableDevices.push({ id: d.device, name: d.display_name, methods: d.capabilities }),
                    );
                    setDevices(selectableDevices);
                    setState(State.Selection);
                    break;
                case "allow":
                    console.error(new Error(translate("Device selection was bypassed by Duo policy")));
                    setState(State.Success);
                    break;
                case "deny":
                    console.error(new Error(translate("Device selection was denied by Duo policy")));
                    setState(State.Failure);
                    break;
                case "enroll":
                    console.error(new Error(translate("No compatible device found")));
                    setState(State.Failure);
                    break;
            }
        } catch (err) {
            if (!mounted.current) return;
            console.error(err);
            console.error(new Error(translate("There was an issue fetching Duo device(s)")));
        }
    }, [mounted, translate]);

    const handleDuoPush = useCallback(async () => {
        try {
            setState(State.SignInInProgress);
            const res = await completePushNotificationSignIn();
            // If the request was initiated and the user changed 2FA method in the meantime,
            // the process is interrupted to avoid updating state of unmounted component.
            if (!mounted.current) return;
            if (res) {
                switch (res.result) {
                    case "auth":
                        let selectableDevices = [] as SelectableDevice[];
                        res.devices.forEach((d) =>
                            selectableDevices.push({ id: d.device, name: d.display_name, methods: d.capabilities }),
                        );
                        setDevices(selectableDevices);
                        setState(State.Selection);
                        return;
                    case "enroll":
                        console.error(new Error(translate("No compatible device found")));
                        setState(State.Failure);
                        return;
                    case "deny":
                        console.error(new Error(translate("Device selection was denied by Duo policy")));
                        setState(State.Failure);
                        return;
                }
            }

            setState(State.Success);
            props.onSecondFactorSuccess();
        } catch (err) {
            // If the request was initiated and the user changed 2FA method in the meantime,
            // the process is interrupted to avoid updating state of unmounted component.
            if (!mounted.current || state !== State.SignInInProgress) return;

            console.error(err);
            console.error(new Error(translate("There was an issue completing sign in process")));
            setState(State.Failure);
        }
    }, [mounted, props, state, translate]);

    const updateDuoDevice = useCallback(
        async function (device: DuoDevicePostRequest) {
            try {
                await completeDuoDeviceSelectionProcess(device);
                setState(State.SignInInProgress);
            } catch (err) {
                console.error(err);
                console.error(new Error(translate("There was an issue updating preferred Duo device")));
            }
        },
        [translate],
    );

    const handleDuoDeviceSelected = useCallback(
        (device: SelectedDevice) => {
            updateDuoDevice({ device: device.id, method: device.method });
        },
        [updateDuoDevice],
    );

    useEffect(() => {
        if (state === State.SignInInProgress) handleDuoPush();
    }, [handleDuoPush, state]);

    if (state === State.Selection)
        return (
            <DeviceSelectionContainer
                devices={devices}
                onBack={() => setState(State.SignInInProgress)}
                onSelect={handleDuoDeviceSelected}
            />
        );

    let icon: ReactNode;
    switch (state) {
        case State.SignInInProgress:
        case State.Success:
            icon = <PushNotificationIcon width={64} height={64} animated />;
            break;
        case State.Failure:
            icon = <FailureIcon />;
    }

    return (
        <Fragment>
            <Box className={styles.container}>
                <Box className={styles.icon}>{icon}</Box>
                <Box className={state !== State.Failure ? "hidden" : ""}>
                    <Button color="secondary" onClick={handleDuoPush}>
                        Retry
                    </Button>
                </Box>
            </Box>
            {state !== State.Success ? (
                <Box>
                    <Link component="button" id="selection-link" onClick={handleSelectDevice} underline="hover">
                        {translate("Select a Device")}
                    </Link>
                </Box>
            ) : null}
        </Fragment>
    );
};

export default SecondFactorMethodMobilePush;

const useStyles = makeStyles((theme: Theme) => ({
    container: {
        height: "120px",
    },
    icon: {
        width: "64px",
        height: "64px",
        display: "inline-block",
    },
}));