Files
huntingclubroster/classes/MfaSetupQr.php
T

21 lines
1.2 KiB
PHP
Raw Normal View History

<?php
/** Render the pending authenticator secret without external services or files. */
final class MfaSetupQr {
public static function dataUri(string $secret,string $email): string {
$otp=OTPHP\TOTP::createFromSecret($secret,new MfaClock())
->withIssuer(Base::APPLICATION_NAME)->withLabel($email);
$barcode=new TCPDF2DBarcode($otp->getProvisioningUri(),'QRCODE,M');
$matrix=$barcode->getBarcodeArray();
$size=(int)$matrix['num_cols'];$height=(int)$matrix['num_rows'];
if($size<1||$height!==$size)throw new RuntimeException('Cannot generate authenticator QR code.');
// A four-module white quiet zone keeps the image scannable on dark pages.
$edge=$size+8;$path='';
for($y=0;$y<$size;$y++)for($x=0;$x<$size;$x++){
if($matrix['bcode'][$y][$x])$path.='M'.($x+4).' '.($y+4).'h1v1h-1z';
}
$svg='<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 '.$edge.' '.$edge.'" width="'.$edge.'" height="'.$edge.'" shape-rendering="crispEdges"><rect width="100%" height="100%" fill="white"/><path d="'.$path.'" fill="black"/></svg>';
return 'data:image/svg+xml;base64,'.base64_encode($svg);
}
}