Skip to content

Commit bda1ff5

Browse files
[UI] Display the 2FA token, show spinner on load (pterodactyl#3367)
Co-authored-by: Dane Everitt <dane@daneeveritt.com>
1 parent 924f00a commit bda1ff5

File tree

5 files changed

+49
-27
lines changed

5 files changed

+49
-27
lines changed

app/Http/Controllers/Api/Client/TwoFactorController.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,7 @@ public function index(Request $request)
6161
}
6262

6363
return new JsonResponse([
64-
'data' => [
65-
'image_url_data' => $this->setupService->handle($request->user()),
66-
],
64+
'data' => $this->setupService->handle($request->user()),
6765
]);
6866
}
6967

app/Services/Users/TwoFactorSetupService.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function __construct(
4949
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
5050
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
5151
*/
52-
public function handle(User $user): string
52+
public function handle(User $user): array
5353
{
5454
$secret = '';
5555
try {
@@ -66,11 +66,14 @@ public function handle(User $user): string
6666

6767
$company = urlencode(preg_replace('/\s/', '', $this->config->get('app.name')));
6868

69-
return sprintf(
70-
'otpauth://totp/%1$s:%2$s?secret=%3$s&issuer=%1$s',
71-
rawurlencode($company),
72-
rawurlencode($user->email),
73-
rawurlencode($secret)
74-
);
69+
return [
70+
'image_url_data' => sprintf(
71+
'otpauth://totp/%1$s:%2$s?secret=%3$s&issuer=%1$s',
72+
rawurlencode($company),
73+
rawurlencode($user->email),
74+
rawurlencode($secret),
75+
),
76+
'secret' => $secret,
77+
];
7578
}
7679
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import http from '@/api/http';
2+
3+
export interface TwoFactorTokenData {
4+
// eslint-disable-next-line camelcase
5+
image_url_data: string;
6+
secret: string;
7+
}
8+
9+
export default (): Promise<TwoFactorTokenData> => {
10+
return new Promise((resolve, reject) => {
11+
http.get('/api/client/account/two-factor')
12+
.then(({ data }) => resolve(data.data))
13+
.catch(reject);
14+
});
15+
};

resources/scripts/api/account/getTwoFactorTokenUrl.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.

resources/scripts/components/dashboard/forms/SetupTwoFactorModal.tsx

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { useContext, useEffect, useState } from 'react';
22
import { Form, Formik, FormikHelpers } from 'formik';
33
import { object, string } from 'yup';
4-
import getTwoFactorTokenUrl from '@/api/account/getTwoFactorTokenUrl';
4+
import getTwoFactorTokenData, { TwoFactorTokenData } from '@/api/account/getTwoFactorTokenData';
55
import enableAccountTwoFactor from '@/api/account/enableAccountTwoFactor';
66
import { Actions, useStoreActions } from 'easy-peasy';
77
import { ApplicationStore } from '@/state';
@@ -12,21 +12,22 @@ import Button from '@/components/elements/Button';
1212
import asModal from '@/hoc/asModal';
1313
import ModalContext from '@/context/ModalContext';
1414
import QRCode from 'qrcode.react';
15+
import CopyOnClick from '@/components/elements/CopyOnClick';
1516

1617
interface Values {
1718
code: string;
1819
}
1920

2021
const SetupTwoFactorModal = () => {
21-
const [ token, setToken ] = useState('');
22+
const [ token, setToken ] = useState<TwoFactorTokenData | null>(null);
2223
const [ recoveryTokens, setRecoveryTokens ] = useState<string[]>([]);
2324

2425
const { dismiss, setPropOverrides } = useContext(ModalContext);
2526
const updateUserData = useStoreActions((actions: Actions<ApplicationStore>) => actions.user.updateUserData);
2627
const { clearAndAddHttpError } = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes);
2728

2829
useEffect(() => {
29-
getTwoFactorTokenUrl()
30+
getTwoFactorTokenData()
3031
.then(setToken)
3132
.catch(error => {
3233
console.error(error);
@@ -102,13 +103,17 @@ const SetupTwoFactorModal = () => {
102103
<div css={tw`flex flex-wrap`}>
103104
<div css={tw`w-full md:flex-1`}>
104105
<div css={tw`w-32 h-32 md:w-64 md:h-64 bg-neutral-600 p-2 rounded mx-auto`}>
105-
{!token || !token.length ?
106+
{!token ?
106107
<img
107108
src={'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII='}
108109
css={tw`w-64 h-64 rounded`}
109110
/>
110111
:
111-
<QRCode renderAs={'svg'} value={token} css={tw`w-full h-full shadow-none rounded-none`}/>
112+
<QRCode
113+
renderAs={'svg'}
114+
value={token.image_url_data}
115+
css={tw`w-full h-full shadow-none rounded-none`}
116+
/>
112117
}
113118
</div>
114119
</div>
@@ -121,11 +126,21 @@ const SetupTwoFactorModal = () => {
121126
title={'Code From Authenticator'}
122127
description={'Enter the code from your authenticator device after scanning the QR image.'}
123128
/>
129+
{token &&
130+
<div css={tw`mt-4 pt-4 border-t border-neutral-500 text-neutral-200`}>
131+
Alternatively, enter the following token into your authenticator application:
132+
<CopyOnClick text={token.secret}>
133+
<div css={tw`text-sm bg-neutral-900 rounded mt-2 py-2 px-4 font-mono`}>
134+
<code css={tw`font-mono`}>
135+
{token.secret}
136+
</code>
137+
</div>
138+
</CopyOnClick>
139+
</div>
140+
}
124141
</div>
125142
<div css={tw`mt-6 md:mt-0 text-right`}>
126-
<Button>
127-
Setup
128-
</Button>
143+
<Button>Setup</Button>
129144
</div>
130145
</div>
131146
</div>

0 commit comments

Comments
 (0)