Two-Factor & Security

Scaffold comes with two factor authentication, recovery codes, spam protection, secure headers and device management built in.

Two Factor Authentication

Users can choose between two platforms on their security settings page (user/security/two-factor):

  • Email - a code is emailed to the user at login (TwoFactorNotification).
  • Authenticator - an authenticator app such as 1Password or Google Authenticator, set up by scanning a QR code (pragmarx/google2fa-laravel and bacon/bacon-qr-code).

Choosing the authenticator shows the QR code and manual key, and the user confirms with a code from the app before it takes effect. Changing platforms is handled by the ProcessUserTwoFactorSettings action, which resets the old codes and logs an activity.

GET   user/security/two-factor            // setup
PUT   user/security/two-factor            // choose a platform
POST  user/security/two-factor/confirm    // confirm the authenticator

Middleware

The two-factor middleware (App\Http\Middleware\TwoFactor) protects the authenticated app routes alongside verified. If the user hasn't confirmed their code for this login it sends the email code, or checks the authenticator, and redirects to verify/two-factor.

Route::middleware(['verified', 'two-factor'])->group(function () {
    // ...
});

The two factor state lives in the HasTwoFactor concern on User.

Recovery codes

Recovery codes are generated with pragmarx/recovery and emailed to the user (TwoFactorRecoveryNotification). A user who loses access can enter their email and a recovery code at /recovery, which turns off two factor so they can log in and set it up again.

Spam Protection

Honeypot

The auth routes (login, register, password reset etc.) and the recovery routes are wrapped in spatie/laravel-honeypot's ProtectAgainstSpam middleware, and the auth routes are throttled to 5 attempts a minute. Settings are in config/honeypot.php.

hCaptcha

The hCaptcha middleware verifies an h-captcha-response with hCaptcha whenever a request includes one, using services.hcaptcha.secret. Add it to any route whose form renders an hCaptcha widget.

Disposable emails

Registration rejects disposable email addresses with erag/laravel-disposable-email's DisposableEmailRule.

Registration

Public registration is controlled by REGISTRATION (general.registration_available). With it off, people can still register through an invite.

Secure Headers

bepsvpt/secure-headers is appended as global middleware and sets the security headers, including the content security policy. HSTS and the CSP are enabled. Settings are in config/secure-headers.php. A @nonce Blade directive is registered in AppServiceProvider as a placeholder (it outputs nothing) for when you add CSP nonces to inline scripts.

Sessions and Devices

Each login records the device (browser, IP address and last login) through the HasDevices concern, using hisorange/browser-detect to describe it. Users can see their devices on their security page and log out of every other device, which requires their password.

POST  user/logout    // log out other devices

Admin routes also require a recently confirmed password (password.confirm).

Log Scrubbing

yorcreative/laravel-scrubber scrubs sensitive values out of logs. Add your own patterns with:

php artisan make:regex-class

Socialite

Routes for social logins with laravel/socialite are in place:

GET  auth/{provider}/redirect
GET  auth/{provider}/callback