### Instantiate Google2FA Directly Source: https://github.com/antonioribeiro/google2fa/blob/9.x/README.md Instantiate the Google2FA class directly to begin using its features. This is the primary way to get started with the package. ```php use PragmaRX\Google2FA\Google2FA; $google2fa = new Google2FA(); return $google2fa->generateSecretKey(); ``` -------------------------------- ### Run PHPUnit Tests Source: https://github.com/antonioribeiro/google2fa/blob/9.x/README.md Execute the package's tests using Composer and PHPUnit. Ensure Composer is installed and the package dependencies are managed. ```bash composer test ``` -------------------------------- ### Set OTP Window for Verification Source: https://github.com/antonioribeiro/google2fa/blob/9.x/README.md Verifies a key using a specified window size. A window of 0 means the OTP is valid for 30 seconds, while a window of 2 makes it valid for 120 seconds. ```php $isValid = $google2fa->verifyKey($seed, $key, 4); ``` -------------------------------- ### Render QR Code with Simple QrCode Source: https://github.com/antonioribeiro/google2fa/blob/9.x/README.md Renders a QR code using the Simple QrCode package, suitable for printing. ```php
{!! QrCode::size(100)->generate($google2fa->getQRCodeUrl($companyName, $companyEmail, $secretKey)); !!}

Scan me to return to the original page.

``` -------------------------------- ### Generate QR Code Image with Bacon/QRCode Source: https://github.com/antonioribeiro/google2fa/blob/9.x/README.md Generates a QR code image using Bacon/QRCode and encodes it as a base64 string for display. ```php getQRCodeUrl( 'pragmarx', 'google2fa@pragmarx.com', $google2fa->generateSecretKey() ); $writer = new Writer( new ImageRenderer( new RendererStyle(400), new ImagickImageBackEnd() ) ); $qrcode_image = base64_encode($writer->writeString($g2faUrl)); ?> ``` -------------------------------- ### Verify Google Authenticator Key with Window Source: https://github.com/antonioribeiro/google2fa/blob/9.x/README.md Validates a provided key against the user's secret, considering a window of past and future keys to account for clock drift. Defaults to a window of 1. ```php $secret = $request->input('secret'); $window = 8; // 8 keys (respectively 4 minutes) past and future $valid = $google2fa->verifyKey($user->google2fa_secret, $secret, $window); ``` -------------------------------- ### Set OTP Window Globally Source: https://github.com/antonioribeiro/google2fa/blob/9.x/README.md Sets the window property for OTP validity. This determines how many 30-second cycles an OTP will be considered valid. ```php $secretKey = $google2fa->setWindow(4); ``` -------------------------------- ### Display QR Code Data URI with Endroid Source: https://github.com/antonioribeiro/google2fa/blob/9.x/README.md Displays a QR code generated as a data URI in an HTML view. ```php
{!! $google2fa_url !!}

Scan me to return to the original page.

``` -------------------------------- ### Generate Secret Key with Prefix Source: https://github.com/antonioribeiro/google2fa/blob/9.x/README.md Generates a secret key with a specified prefix. The prefix length must be compatible with base 32 conversion and the secret key's power-of-2 length. ```php $prefix = strpad($userId, 10, 'X'); $secretKey = $google2fa->generateSecretKey(16, $prefix); ``` -------------------------------- ### Original GPLv3 License Text Source: https://github.com/antonioribeiro/google2fa/blob/9.x/RELICENSED.md This block contains the full text of the GNU General Public License v3, under which the Google2FA package was originally licensed. It outlines the terms for redistribution and modification. ```php /** * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * * PHP Google two-factor authentication module. * * See https://www.idontplaydarts.com/2011/07/google-totp-two-factor-authentication-for-php/ * for more details * * @author Phil **/ ``` -------------------------------- ### Generate QR Code Data URI with Endroid Source: https://github.com/antonioribeiro/google2fa/blob/9.x/README.md Generates a data URI for a QR code using the Endroid QR Code library. ```php $qrCode = new \Endroid\QrCode\QrCode($value); $qrCode->setSize(100); $google2fa_url = $qrCode->writeDataUri(); ``` -------------------------------- ### Perform PHPStan Analysis Source: https://github.com/antonioribeiro/google2fa/blob/9.x/README.md Run static analysis on the codebase using PHPStan via Composer. This helps identify potential code quality issues. ```bash composer analyse ``` -------------------------------- ### Pad Secret Key for Google Authenticator Compatibility Source: https://github.com/antonioribeiro/google2fa/blob/9.x/README.md Use this to pad a secret key to the nearest power of 2 length (8, 16, 32, etc.) to ensure compatibility with Google Authenticator. The padding character 'X' is used here. ```php $secretKey = '123456789'; $secretKey = str_pad($secretKey, pow(2,ceil(log(strlen($secretKey),2))), 'X'); ``` -------------------------------- ### Generate 16-character secret key (v8.x and below) Source: https://github.com/antonioribeiro/google2fa/blob/9.x/README.md This snippet shows how to generate a 16-character secret key, replicating the default behavior of versions prior to 9.0.0. ```php $secret = $google2fa->generateSecretKey(); ``` -------------------------------- ### Verify Google Authenticator Key (Newer) Source: https://github.com/antonioribeiro/google2fa/blob/9.x/README.md Verifies a key and ensures it has not been used before by checking against the last used timestamp. Updates the timestamp upon successful validation. ```php $secret = $request->input('secret'); $timestamp = $google2fa->verifyKeyNewer($user->google2fa_secret, $secret, $user->google2fa_ts); if ($timestamp !== false) { $user->update(['google2fa_ts' => $timestamp]); // successful } else { // failed } ``` -------------------------------- ### Display Base64 Encoded QR Code Image Source: https://github.com/antonioribeiro/google2fa/blob/9.x/README.md Displays a QR code image that has been encoded as a base64 string. ```php ``` -------------------------------- ### Generate Data URL for QR Code Source: https://github.com/antonioribeiro/google2fa/blob/9.x/README.md Generates a data URL for a QR code using a custom function, suitable for embedding in HTML. ```php // Use your own QR Code generator to generate a data URL: $google2fa_url = custom_generate_qrcode_url($qrCodeUrl); /// and in your view: ``` -------------------------------- ### Generate Secret Key for User Source: https://github.com/antonioribeiro/google2fa/blob/9.x/README.md Generate a secret key for a user and store it. The default length is 32 characters, but you can specify 16 characters for compatibility. ```php // Generates a 32-character secret key (v9.0.0+ default) $user->google2fa_secret = $google2fa->generateSecretKey(); ``` ```php // Or explicitly specify 16 characters for compatibility $user->google2fa_secret = $google2fa->generateSecretKey(16); ``` -------------------------------- ### Generate QR Code URL Source: https://github.com/antonioribeiro/google2fa/blob/9.x/README.md Generates the URL for a QR code that can be used with any QR code generator library. ```php $qrCodeUrl = $google2fa->getQRCodeUrl( $companyName, $companyEmail, $secretKey ); ``` -------------------------------- ### Generate Inline QR Code Source: https://github.com/antonioribeiro/google2fa/blob/9.x/README.md Generates a QR code directly as an inline data URI for immediate use in an HTML image tag. ```php $google2fa = (new \PragmaRX\Google2FAQRCode\Google2FA()); $inlineUrl = $google2fa->getQRCodeInline( 'Company Name', 'company@email.com', $google2fa->generateSecretKey() ); ``` -------------------------------- ### Set HMAC Algorithm Source: https://github.com/antonioribeiro/google2fa/blob/9.x/README.md Use `setAlgorithm()` to specify SHA256 or SHA512 for enhanced security. Defaults to SHA1. ```php use PragmaRX\Google2FA\Support\Constants; $google2fa->setAlgorithm(Constants::SHA512); ``` -------------------------------- ### Set Key Regeneration Interval Source: https://github.com/antonioribeiro/google2fa/blob/9.x/README.md Changes the key regeneration interval, which defaults to 30 seconds. Modifying this may cause sync issues with standard authenticator apps. ```php $google2fa->setKeyRegeneration(40); ``` -------------------------------- ### Generate 16-character secret key (v9.0+) Source: https://github.com/antonioribeiro/google2fa/blob/9.x/README.md This snippet demonstrates how to explicitly generate a 16-character secret key in versions 9.0.0 and above, maintaining compatibility with older defaults. ```php $secret = $google2fa->generateSecretKey(16); ``` -------------------------------- ### Verify Google Authenticator Code Source: https://github.com/antonioribeiro/google2fa/blob/9.x/README.md Verifies a user-provided 2FA code against their stored secret key. ```php $secret = $request->input('secret'); $valid = $google2fa->verifyKey($user->google2fa_secret, $secret); ``` -------------------------------- ### Generate Secret Key with Specific Length Source: https://github.com/antonioribeiro/google2fa/blob/9.x/README.md Generates a secret key. Use 32 bytes for the default behavior in v9.0.0+ or 16 bytes for v8.x behavior. ```php $secretKey = $google2fa->generateSecretKey(32); // now defaults to 32 bytes (v9.0.0+) $secretKey = $google2fa->generateSecretKey(16); // for 16 byte keys (v8.x behavior) ``` -------------------------------- ### Disable Google Authenticator Compatibility Enforcement Source: https://github.com/antonioribeiro/google2fa/blob/9.x/README.md Call this method to disable the package's default enforcement of Google Authenticator secret key compatibility if it's not a target requirement. ```php $google2fa->setEnforceGoogleAuthenticatorCompatibility(false); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.