phpqrcode и аутентификатор Google - не уверен в формате вещей и в том, как заставить его правильно проверять

Я использую phpqrcode для создания qrcode для использования с двухфакторной аутентификацией. Я всегда использовал приложение Google Authenticator для всех моих потребностей 2FA. Хотя я могу создать qrcode, я не уверен в точном формате, поэтому он правильно проверяется. На данный момент, когда я пытаюсь отсканировать его, я получаю "недействительный штрих-код" из приложения.

Могу ли я использовать секрет, URL-адрес или их комбинацию при создании qrcode? Мне не хватает чего-то глупого, и я уверен, что это потому, что я не понимаю, где и как использовать параметры и URL-адрес otpauth://.

require $_SERVER['DOCUMENT_ROOT'].'/assets/phpqrcode/phpqrcode.php';

//get params
$secret = create2FASecret();
$name = 'somename';
$issuer = 'example.com';

//url encode, but not sure where or how I use this
$urlencoded = urlencode('otpauth://totp/'.$name.'?secret='.$secret.'&issuer='.$issuer.'');

//create the qrcode, base64 it, output it
ob_start();
QRCode::png($urlencoded, null, QR_ECLEVEL_L, 3, 4);
$newpng = base64_encode( ob_get_contents() );
ob_end_clean();

$src = 'data: image/png; base64,'.$newpng;


//show secret created and the qrcode
echo 'This is the secret that was generated : '.$secret,'<br>';
echo '<img src="' . $src . '" />';

//create a secret
function create2FASecret($secretLength = 16)
{
    $validChars = array(
        'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', //  7
        'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', // 15
        'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', // 23
        'Y', 'Z', '2', '3', '4', '5', '6', '7', // 31
        '='  // padding char
    );
    
    unset($validChars[32]);

    $secret = '';
    for ($i = 0; $i < $secretLength; $i++) {
        $secret .= $validChars[array_rand($validChars)];
    }
    return $secret;
}

1 ответ

Оказывается - urlencoded должны быть только $ name и $ Issuer, и все работает как надо. Я также изменил формат URL-адреса в соответствии с https://github.com/google/google-authenticator/wiki/Key-Uri-Format

$name = urlencode($name);
$issuer = urlencode($issuer);

//%3A is encoded colon
$url = 'otpauth://totp/'.$issuer.'%3A'.$name.'?secret='.$secret.'&issuer='.$issuer.'&algorithm=SHA1&digits=6&period=30';


//create the qrcode, base64 it, output it
ob_start();
QRCode::png($url, null, QR_ECLEVEL_L, 3, 4);
$newpng = base64_encode( ob_get_contents() );
ob_end_clean();
Другие вопросы по тегам