Files
TwoFactorAuth/docs/qr-codes.md
T
Nicolas CARPi 2080319f6f make QR Code Provider a mandatory constructor argument
This change is discussed in #104
Currently, the library defaults to a QR Code Provider using an external
service, thus leaking secrets.

This change forces the definition of a QR Code Provider in the
constructor. It is a breaking change.

fixes #104
2024-04-25 21:23:39 +02:00

2.6 KiB

layout, title
layout title
post QR Codes

An alternative way of communicating the secret to the user is through the use of QR Codes which most if not all authenticator mobile apps can scan.

This can avoid accidental typing errors and also pre-set some text values within the users app.

You can display the QR Code as a base64 encoded image using the instance as follows, supplying the users name or other public identifier as the first argument

<p>Scan the following image with your app:</p>
<img src="<?php echo $tfa->getQRCodeImageAsDataUri('Bob Ross', $secret); ?>">

You can also specify a size as a third argument which is 200 by default.

Note: by default, the QR code returned by the instance is generated from a third party across the internet. If the third party is encountering problems or is not available from where you have hosted your code, your user will likely experience a delay in seeing the QR code, if it even loads at all. This can be overcome with offline providers configured when you create the instance.

Offline Providers

EndroidQrCodeProvider and EndroidQrCodeWithLogoProvider

BaconQRCodeProvider

Note: offline providers may have additional PHP requirements in order to function, you should study what is required before trying to make use of them.

Custom Provider

If you wish to make your own QR Code provider to reference another service or library, it must implement the IQRCodeProvider interface.

It is recommended to use similar constructor arguments as the included providers to avoid big shifts when trying different providers.

Example:

use RobThree\Auth\TwoFactorAuth;
// using a custom object implementing IQRCodeProvider
$tfa = new TwoFactorAuth(new MyQrCodeProvider());
// using named argument and a variable
$tfa = new TwoFactorAuth(qrcodeprovider: $qrGenerator);

Online Providers

Warning: Using an external service for generating QR codes encoding authentication secrets is not recommended! You should instead make use of the included offline providers listed above.

Example:

use RobThree\Auth\TwoFactorAuth;
use RobThree\Auth\Providers\Qr\GoogleChartsQrCodeProvider;
$tfa = new TwoFactorAuth(new GoogleChartsQrCodeProvider());