PageRenderTime 29ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/pragmarx/google2fa/README.md

https://gitlab.com/jjpa2018/dashboard
Markdown | 410 lines | 279 code | 131 blank | 0 comment | 0 complexity | 97f22b54df64a09e6c11e9b6616d7a6b MD5 | raw file
  1. # Google2FA - Google Two-Factor Authentication for PHP
  2. 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).
  3. ---
  4. <p align="center">
  5. <a href="https://packagist.org/packages/pragmarx/google2fa"><img alt="Latest Stable Version" src="https://img.shields.io/packagist/v/pragmarx/google2fa.svg?style=flat-square"></a>
  6. <a href="LICENSE.md"><img alt="License" src="https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square"></a>
  7. <a href="https://scrutinizer-ci.com/g/antonioribeiro/google2fa/?branch=master"><img alt="Code Quality" src="https://img.shields.io/scrutinizer/g/antonioribeiro/google2fa.svg?style=flat-square"></a>
  8. <a href="https://travis-ci.org/antonioribeiro/google2fa"><img alt="Build" src="https://img.shields.io/travis/antonioribeiro/google2fa.svg?style=flat-square"></a>
  9. <a href="https://packagist.org/packages/pragmarx/google2fa"><img alt="Downloads" src="https://img.shields.io/packagist/dt/pragmarx/google2fa.svg?style=flat-square"></a>
  10. </p>
  11. <p align="center">
  12. <a href="https://packagist.org/packages/pragmarx/google2fa"><img alt="Monthly Downloads" src="https://poser.pugx.org/pragmarx/google2fa/d/monthly?format=flat-square"></a>
  13. <a href="https://scrutinizer-ci.com/g/antonioribeiro/google2fa/?branch=master"><img alt="Coverage" src="https://img.shields.io/scrutinizer/coverage/g/antonioribeiro/google2fa.svg?style=flat-square"></a>
  14. <a href="https://travis-ci.org/antonioribeiro/google2fa"><img alt="PHP" src="https://img.shields.io/badge/PHP-7.1%20--%208.0-brightgreen.svg?style=flat-square"></a>
  15. </p>
  16. ---
  17. ## Menu
  18. - [Version Compatibility](#version-compatibility)
  19. - [Google Two-Factor Authentication for PHP Package](#google-two-factor-authentication-for-php-package)
  20. - [Laravel bridge](#laravel-bridge)
  21. - [Demos, Example & Playground](#demos--example---playground)
  22. - [Requirements](#requirements)
  23. - [Installing](#installing)
  24. - [Usage](#using-it)
  25. - [How To Generate And Use Two Factor Authentication](#how-to-generate-and-use-two-factor-authentication)
  26. - [Generating QRCodes](#generating-qrcodes)
  27. - [QR Code Packages](#qr-code-packages)
  28. - [Examples of Usage](#examples-of-usage)
  29. - [HMAC Algorithms](#hmac-algorithms)
  30. - [Server Time](#server-time)
  31. - [Validation Window](#validation-window)
  32. - [Using a Bigger and Prefixing the Secret Key](#using-a-bigger-and-prefixing-the-secret-key)
  33. - [Google Authenticator secret key compatibility](#google-authenticator-secret-key-compatibility)
  34. - [Google Authenticator Apps:](#google-authenticator-apps-)
  35. - [Deprecation Warning](#deprecation-warning)
  36. - [Tests](#tests)
  37. - [Authors](#authors)
  38. - [License](#license)
  39. - [Contributing](#contributing)
  40. ## Version Compatibility
  41. PHP | Google2FA
  42. :-------|:----------
  43. 5.4 | 7.x LTS
  44. 5.5 | 7.x LTS
  45. 5.6 | 7.x LTS
  46. 7.1 | 8.x
  47. 7.2 | 8.x
  48. 7.3 | 8.x
  49. 7.4 | 8.x
  50. ## Laravel bridge
  51. This package is agnostic, but there's a [Laravel bridge](https://github.com/antonioribeiro/google2fa-laravel).
  52. ## About QRCode generation
  53. This package does not generate QRCodes for 2FA.
  54. If you are looking for Google Two-Factor Authentication, but also need to generate QRCode for it, you can use the [Google2FA QRCode package](https://github.com/antonioribeiro/google2fa-qrcode), which integrates this package and also generates QRCodes using the BaconQRCode library, or check options on how to do it yourself [here in the docs](#qr-code-packages).
  55. ## Demos, Example & Playground
  56. Please check the [Google2FA Package Playground](http://pragmarx.com/playground/google2fa).
  57. ![playground](docs/playground.jpg)
  58. Here's an demo app showing how to use Google2FA: [google2fa-example](https://github.com/antonioribeiro/google2fa-example).
  59. 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.
  60. ## Requirements
  61. - PHP 7.1 or greater
  62. ## Installing
  63. Use Composer to install it:
  64. composer require pragmarx/google2fa
  65. To generate inline QRCodes, you'll need to install a QR code generator, e.g. [BaconQrCode](https://github.com/Bacon/BaconQrCode):
  66. composer require bacon/bacon-qr-code
  67. ## Usage
  68. ### Instantiate it directly
  69. ```php
  70. use PragmaRX\Google2FA\Google2FA;
  71. $google2fa = new Google2FA();
  72. return $google2fa->generateSecretKey();
  73. ```
  74. ## How To Generate And Use Two Factor Authentication
  75. Generate a secret key for your user and save it:
  76. ```php
  77. $user->google2fa_secret = $google2fa->generateSecretKey();
  78. ```
  79. ## Generating QRCodes
  80. The securer way of creating QRCode is to do it yourself or using a library. First you have to install a QR code generator e.g. BaconQrCode, as stated above, then you just have to generate the QR code url using:
  81. ```php
  82. $qrCodeUrl = $google2fa->getQRCodeUrl(
  83. $companyName,
  84. $companyEmail,
  85. $secretKey
  86. );
  87. ```
  88. Once you have the QR code url, you can feed it to your preferred QR code generator.
  89. ```php
  90. // Use your own QR Code generator to generate a data URL:
  91. $google2fa_url = custom_generate_qrcode_url($qrCodeUrl);
  92. /// and in your view:
  93. <img src="{{ $google2fa_url }}" alt="">
  94. ```
  95. And to verify, you just have to:
  96. ```php
  97. $secret = $request->input('secret');
  98. $valid = $google2fa->verifyKey($user->google2fa_secret, $secret);
  99. ```
  100. ## QR Code Packages
  101. This package suggests the use of [Bacon/QRCode](https://github.com/Bacon/BaconQrCode) because
  102. it is known as a good QR Code package, but you can use it with any other package, for
  103. instance [Google2FA QRCode](https://github.com/antonioribeiro/google2fa-qrcode),
  104. [Simple QrCode](https://www.simplesoftware.io/docs/simple-qrcode)
  105. or [Endroid QR Code](https://github.com/endroid/qr-code), all of them use
  106. [Bacon/QRCode](https://github.com/Bacon/BaconQrCode) to produce QR Codes.
  107. Usually you'll need a 2FA URL, so you just have to use the URL generator:
  108. ```php
  109. $google2fa->getQRCodeUrl($companyName, $companyEmail, $secretKey)
  110. ```
  111. ## Examples of Usage
  112. ### [Google2FA QRCode](https://github.com/antonioribeiro/google2fa-qrcode)
  113. Get a QRCode to be used inline:
  114. ```php
  115. $google2fa = (new \PragmaRX\Google2FAQRCode\Google2FA());
  116. $inlineUrl = $google2fa->getQRCodeInline(
  117. 'Company Name',
  118. 'company@email.com',
  119. $google2fa->generateSecretKey()
  120. );
  121. ```
  122. And use in your template:
  123. ```php
  124. <img src="{{ $inlineUrl }}">
  125. ```
  126. ### [Simple QrCode](https://www.simplesoftware.io/docs/simple-qrcode)
  127. ```php
  128. <div class="visible-print text-center">
  129. {!! QrCode::size(100)->generate($google2fa->getQRCodeUrl($companyName, $companyEmail, $secretKey)); !!}
  130. <p>Scan me to return to the original page.</p>
  131. </div>
  132. ```
  133. ### [Endroid QR Code Generator](https://github.com/endroid/qr-code)
  134. Generate the data URL
  135. ```php
  136. $qrCode = new \Endroid\QrCode\QrCode($value);
  137. $qrCode->setSize(100);
  138. $google2fa_url = $qrCode->writeDataUri();
  139. ```
  140. And in your view
  141. ```php
  142. <div class="visible-print text-center">
  143. {!! $google2fa_url !!}
  144. <p>Scan me to return to the original page.</p>
  145. </div>
  146. ```
  147. ### [Bacon/QRCode](https://github.com/Bacon/BaconQrCode)
  148. ```php
  149. <?php
  150. use PragmaRX\Google2FA\Google2FA;
  151. use BaconQrCode\Renderer\ImageRenderer;
  152. use BaconQrCode\Renderer\Image\ImagickImageBackEnd;
  153. use BaconQrCode\Renderer\RendererStyle\RendererStyle;
  154. use BaconQrCode\Writer;
  155. $google2fa = app(Google2FA::class);
  156. $g2faUrl = $google2fa->getQRCodeUrl(
  157. 'pragmarx',
  158. 'google2fa@pragmarx.com',
  159. $google2fa->generateSecretKey()
  160. );
  161. $writer = new Writer(
  162. new ImageRenderer(
  163. new RendererStyle(400),
  164. new ImagickImageBackEnd()
  165. )
  166. );
  167. $qrcode_image = base64_encode($writer->writeString($g2faUrl));
  168. ```
  169. And show it as an image:
  170. ```php
  171. <img src="data:image/png;base64, <?php echo $qrcode_image; ?> "/>
  172. ```
  173. ## HMAC Algorithms
  174. To comply with [RFC6238](https://tools.ietf.org/html/rfc6238), this package supports SHA1, SHA256 and SHA512. It defaults to SHA1, so to use a different algorithm you just have to use the method `setAlgorith()`:
  175. ``` php
  176. $google2fa->setAlgorithm(Constants::SHA512);
  177. ```
  178. ## Server Time
  179. It's really important that you keep your server time in sync with some NTP server, on Ubuntu you can add this to the crontab:
  180. ```bash
  181. sudo service ntp stop
  182. sudo ntpd -gq
  183. sudo service ntp start
  184. ```
  185. ## Validation Window
  186. To avoid problems with clocks that are slightly out of sync, we do not check against the current key only but also consider `$window` keys each from the past and future. You can pass `$window` as optional third parameter to `verifyKey`, it defaults to `4`. A new key is generated every 30 seconds, so this window includes keys from the previous two and next two minutes.
  187. ```php
  188. $secret = $request->input('secret');
  189. $window = 8; // 8 keys (respectively 4 minutes) past and future
  190. $valid = $google2fa->verifyKey($user->google2fa_secret, $secret, $window);
  191. ```
  192. An attacker might be able to watch the user entering his credentials and one time key.
  193. Without further precautions, the key remains valid until it is no longer within the window of the server time. In order to prevent usage of a one time key that has already been used, you can utilize the `verifyKeyNewer` function.
  194. ```php
  195. $secret = $request->input('secret');
  196. $timestamp = $google2fa->verifyKeyNewer($user->google2fa_secret, $secret, $user->google2fa_ts);
  197. if ($timestamp !== false) {
  198. $user->update(['google2fa_ts' => $timestamp]);
  199. // successful
  200. } else {
  201. // failed
  202. }
  203. ```
  204. Note that `$timestamp` either `false` (if the key is invalid or has been used before) or the provided key's unix timestamp divided by the key regeneration period of 30 seconds.
  205. ## Using a Bigger and Prefixing the Secret Key
  206. Although the probability of collision of a 16 bytes (128 bits) random string is very low, you can harden it by:
  207. #### Use a bigger key
  208. ```php
  209. $secretKey = $google2fa->generateSecretKey(32); // defaults to 16 bytes
  210. ```
  211. #### You can prefix your secret keys
  212. You may prefix your secret keys, but you have to understand that, as your secret key must have length in power of 2, your prefix will have to have a complementary size. So if your key is 16 bytes long, if you add a prefix it must be also 16 bytes long, but as your prefixes will be converted to base 32, the max length of your prefix is 10 bytes. So, those are the sizes you can use in your prefixes:
  213. ```
  214. 1, 2, 5, 10, 20, 40, 80...
  215. ```
  216. And it can be used like so:
  217. ```php
  218. $prefix = strpad($userId, 10, 'X');
  219. $secretKey = $google2fa->generateSecretKey(16, $prefix);
  220. ```
  221. #### Window
  222. The Window property defines how long a OTP will work, or how many cycles it will last. A key has a 30 seconds cycle, setting the window to 0 will make the key lasts for those 30 seconds, setting it to 2 will make it last for 120 seconds. This is how you set the window:
  223. ```php
  224. $secretKey = $google2fa->setWindow(4);
  225. ```
  226. But you can also set the window while checking the key. If you need to set a window of 4 during key verification, this is how you do:
  227. ```php
  228. $isValid = $google2fa->verifyKey($seed, $key, 4);
  229. ```
  230. #### Key Regeneration Interval
  231. You can change key regeneration interval, which defaults to 30 seconds, but remember that this is a default value on most authentication apps, like Google Authenticator, which will, basically, make your app out of sync with them.
  232. ```php
  233. $google2fa->setKeyRegeneration(40);
  234. ```
  235. ## Google Authenticator secret key compatibility
  236. To be compatible with Google Authenticator, your (converted to base 32) secret key length must be at least 8 chars and be a power of 2: 8, 16, 32, 64...
  237. So, to prevent errors, you can do something like this while generating it:
  238. ```php
  239. $secretKey = '123456789';
  240. $secretKey = str_pad($secretKey, pow(2,ceil(log(strlen($secretKey),2))), 'X');
  241. ```
  242. And it will generate
  243. ```
  244. 123456789XXXXXXX
  245. ```
  246. By default, this package will enforce compatibility, but, if Google Authenticator is not a target, you can disable it by doing
  247. ```php
  248. $google2fa->setEnforceGoogleAuthenticatorCompatibility(false);
  249. ```
  250. ## Google Authenticator Apps:
  251. To use the two factor authentication, your user will have to install a Google Authenticator compatible app, those are some of the currently available:
  252. * [Authy for iOS, Android, Chrome, OS X](https://www.authy.com/)
  253. * [FreeOTP for iOS, Android and Pebble](https://apps.getpebble.com/en_US/application/52f1a4c3c4117252f9000bb8)
  254. * [Google Authenticator for iOS](https://itunes.apple.com/us/app/google-authenticator/id388497605?mt=8)
  255. * [Google Authenticator for Android](https://play.google.com/store/apps/details?id=com.google.android.apps.authenticator2)
  256. * [Google Authenticator (port) on Windows Store](https://www.microsoft.com/en-us/store/p/google-authenticator/9wzdncrdnkrf)
  257. * [Microsoft Authenticator for Windows Phone](https://www.microsoft.com/en-us/store/apps/authenticator/9wzdncrfj3rj)
  258. * [LastPass Authenticator for iOS, Android, OS X, Windows](https://lastpass.com/auth/)
  259. * [1Password for iOS, Android, OS X, Windows](https://1password.com)
  260. ## Deprecation Warning
  261. Google API for QR generator is turned off. All version of that package prior to 5.0.0 are deprecated. Please upgrade and check documentation regarding [QRCode generation](https://github.com/antonioribeiro/google2fa#generating-qrcodes).
  262. ## Testing
  263. The package tests were written with [PHPUnit](https://phpunit.de/). There are some Composer scripts to help you run tests and analysis:
  264. PHPUnit:
  265. ````
  266. composer test
  267. ````
  268. PHPStan analysis:
  269. ````
  270. composer analyse
  271. ````
  272. ## Authors
  273. - [Antonio Carlos Ribeiro](http://twitter.com/iantonioribeiro)
  274. - [Phil (Orginal author of this class)](https://www.idontplaydarts.com/static/ga.php_.txt)
  275. - [All Contributors](https://github.com/antonioribeiro/google2fa/graphs/contributors)
  276. ## License
  277. Google2FA is licensed under the MIT License - see the [LICENSE](LICENSE.md) file for details.
  278. ## Contributing
  279. Pull requests and issues are more than welcome.