3 namespace BookStack\Http\Controllers\Auth;
5 use BookStack\Actions\ActivityType;
6 use BookStack\Auth\Access\Mfa\MfaValue;
7 use BookStack\Auth\Access\Mfa\TotpService;
8 use BookStack\Auth\Access\Mfa\TotpValidationRule;
9 use BookStack\Exceptions\NotFoundException;
10 use BookStack\Http\Controllers\Controller;
11 use Illuminate\Http\Request;
12 use Illuminate\Validation\ValidationException;
14 class MfaTotpController extends Controller
16 use HandlesPartialLogins;
18 protected const SETUP_SECRET_SESSION_KEY = 'mfa-setup-totp-secret';
21 * Show a view that generates and displays a TOTP QR code.
23 public function generate(TotpService $totp)
25 if (session()->has(static::SETUP_SECRET_SESSION_KEY)) {
26 $totpSecret = decrypt(session()->get(static::SETUP_SECRET_SESSION_KEY));
28 $totpSecret = $totp->generateSecret();
29 session()->put(static::SETUP_SECRET_SESSION_KEY, encrypt($totpSecret));
32 $qrCodeUrl = $totp->generateUrl($totpSecret);
33 $svg = $totp->generateQrCodeSvg($qrCodeUrl);
35 return view('mfa.totp-generate', [
36 'secret' => $totpSecret,
42 * Confirm the setup of TOTP and save the auth method secret
43 * against the current user.
44 * @throws ValidationException
45 * @throws NotFoundException
47 public function confirm(Request $request)
49 $totpSecret = decrypt(session()->get(static::SETUP_SECRET_SESSION_KEY));
50 $this->validate($request, [
54 new TotpValidationRule($totpSecret),
58 MfaValue::upsertWithValue($this->currentOrLastAttemptedUser(), MfaValue::METHOD_TOTP, $totpSecret);
59 session()->remove(static::SETUP_SECRET_SESSION_KEY);
60 $this->logActivity(ActivityType::MFA_SETUP_METHOD, 'totp');
62 return redirect('/mfa/setup');