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\Http\Controllers\Controller;
10 use Illuminate\Http\Request;
11 use Illuminate\Validation\ValidationException;
13 class MfaTotpController extends Controller
15 protected const SETUP_SECRET_SESSION_KEY = 'mfa-setup-totp-secret';
18 * Show a view that generates and displays a TOTP QR code.
20 public function generate(TotpService $totp)
22 if (session()->has(static::SETUP_SECRET_SESSION_KEY)) {
23 $totpSecret = decrypt(session()->get(static::SETUP_SECRET_SESSION_KEY));
25 $totpSecret = $totp->generateSecret();
26 session()->put(static::SETUP_SECRET_SESSION_KEY, encrypt($totpSecret));
29 $qrCodeUrl = $totp->generateUrl($totpSecret);
30 $svg = $totp->generateQrCodeSvg($qrCodeUrl);
32 return view('mfa.totp-generate', [
33 'secret' => $totpSecret,
39 * Confirm the setup of TOTP and save the auth method secret
40 * against the current user.
41 * @throws ValidationException
43 public function confirm(Request $request)
45 $totpSecret = decrypt(session()->get(static::SETUP_SECRET_SESSION_KEY));
46 $this->validate($request, [
50 new TotpValidationRule($totpSecret),
54 MfaValue::upsertWithValue(user(), MfaValue::METHOD_TOTP, $totpSecret);
55 session()->remove(static::SETUP_SECRET_SESSION_KEY);
56 $this->logActivity(ActivityType::MFA_SETUP_METHOD, 'totp');
58 return redirect('/mfa/setup');