# Google2FA
### Google Two-Factor Authentication for PHP Package Google2FA is a PHP implementation of the Google Two-Factor Authentication Module, supporting the HMAC-Based One-time Password (HOTP) algorithm specified in [RFC 4226](https://tools.ietf.org/html/rfc4226) and the Time-based One-time Password (TOTP) algorithm specified in [RFC 6238](https://tools.ietf.org/html/rfc6238). This package is agnostic, but there's a [Laravel bridge](https://github.com/antonioribeiro/google2fa-laravel). ## Demos, Example & Playground Please check the [Google2FA Package Playground](http://pragmarx.com/playground/google2fa). ![playground](docs/playground.jpg) Here's an demo app showing how to use Google2FA: [google2fa-example](https://github.com/antonioribeiro/google2fa-example). You can scan the QR code on [this (old) demo page](https://antoniocarlosribeiro.com/technology/google2fa) with a Google Authenticator app and view the code changing (almost) in real time. ## Requirements - PHP 5.4+ ## Installing Use Composer to install it: composer require pragmarx/google2fa If you prefer inline QRCodes instead of a Google generated url, you'll need to install [BaconQrCode](https://github.com/Bacon/BaconQrCode): composer require bacon/bacon-qr-code ## Using It ### Instantiate it directly ```php use PragmaRX\Google2FA\Google2FA; $google2fa = new Google2FA(); return $google2fa->generateSecretKey(); ``` ## How To Generate And Use Two Factor Authentication Generate a secret key for your user and save it: ```php $user->google2fa_secret = $google2fa->generateSecretKey(); ``` ## Generating QRCodes The securer way of creating QRCode is to do it yourself or using a library. First you have to install the BaconQrCode package, as stated above, then you just have to generate the inline string using: ```php $inlineUrl = $google2fa->getQRCodeInline( $companyName, $companyEmail, $secretKey ); ``` And use it in your blade template this way: ```html ``` ```php $secretKey = $google2fa->generateSecretKey(16, $userId); ``` ## Show the QR Code to your user, via Google Apis It's insecure to use it via Google Apis, so you have to enable it before using it. ```php $google2fa->setAllowInsecureCallToGoogleApis(true); $google2fa_url = $google2fa->getQRCodeGoogleUrl( 'YourCompany', $user->email, $user->google2fa_secret ); /// and in your view: ``` And they should see and scan the QR code to their applications: ![QRCode](https://chart.googleapis.com/chart?chs=200x200&chld=M|0&cht=qr&chl=otpauth%3A%2F%2Ftotp%2FPragmaRX%3Aacr%2Bpragmarx%40antoniocarlosribeiro.com%3Fsecret%3DADUMJO5634NPDEKW%26issuer%3DPragmaRX) And to verify, you just have to: ```php $secret = $request->input('secret'); $valid = $google2fa->verifyKey($user->google2fa_secret, $secret); ``` ## QR Code Packages This package suggests the use of Bacon/QRCode because it is known as a good QR Code package, but you can use it with any other package, for instance [Simple QrCode](https://www.simplesoftware.io/docs/simple-qrcode), which uses Bacon/QRCode to produce QR Codes. Usually you'll need a 2FA URL, so you just have to use the URL generator: ```php $google2fa->getQRCodeUrl($companyName, $companyEmail, $secretKey) ``` Here's an example using Simple QrCode: ```phpScan me to return to the original page.