diff options
author | Antonio Carlos Ribeiro <acr@antoniocarlosribeiro.com> | 2016-07-17 22:06:37 -0400 |
---|---|---|
committer | StyleCI Bot <bot@styleci.io> | 2016-07-17 22:06:37 -0400 |
commit | 96825889955963e4747f9db104e8d90665346b9a (patch) | |
tree | f9a953fd00de84fba1fba6f5f28b7fd09717215e | |
parent | 79b431865034c7963b63c68936ce5724f6d787ca (diff) | |
download | google2fa-origin/analysis-q54wEP.zip google2fa-origin/analysis-q54wEP.tar.gz google2fa-origin/analysis-q54wEP.tar.bz2 |
Applied fixes from StyleCIorigin/analysis-q54wEP
-rw-r--r-- | src/Contracts/Google2FA.php | 188 | ||||
-rw-r--r-- | src/Exceptions/InvalidCharactersException.php | 21 | ||||
-rw-r--r-- | src/Exceptions/SecretKeyTooShortException.php | 21 | ||||
-rw-r--r-- | src/Google2FA.php | 603 | ||||
-rw-r--r-- | src/Support/Url.php | 16 | ||||
-rw-r--r-- | src/Vendor/Laravel/Facade.php | 37 | ||||
-rw-r--r-- | src/Vendor/Laravel/ServiceProvider.php | 56 | ||||
-rw-r--r-- | tests/spec/Google2FASpec.php | 174 |
8 files changed, 563 insertions, 553 deletions
diff --git a/src/Contracts/Google2FA.php b/src/Contracts/Google2FA.php index 55ed345..c1e4712 100644 --- a/src/Contracts/Google2FA.php +++ b/src/Contracts/Google2FA.php @@ -4,102 +4,112 @@ namespace PragmaRX\Google2FA\Contracts; interface Google2FA { + /** + * Generate a digit secret key in base32 format. + * + * @param int $length + * + * @return string + */ + public function generateSecretKey($length = 16); - /** - * Generate a digit secret key in base32 format. - * - * @param int $length - * @return string - */ - public function generateSecretKey($length = 16); + /** + * Returns the current Unix Timestamp devided by the KEY_REGENERATION + * period. + * + * @return int + **/ + public function getTimestamp(); - /** - * Returns the current Unix Timestamp devided by the KEY_REGENERATION - * period. - * - * @return integer - **/ - public function getTimestamp(); + /** + * Decodes a base32 string into a binary string. + * + * @param string $b32 + * + * @throws InvalidCharactersException + * + * @return int + */ + public function base32Decode($b32); - /** - * Decodes a base32 string into a binary string. - * - * @param string $b32 - * @throws InvalidCharactersException - * @return integer - */ - public function base32Decode($b32); + /** + * Takes the secret key and the timestamp and returns the one time + * password. + * + * @param string $key - Secret key in binary form. + * @param int $counter - Timestamp as returned by getTimestamp. + * + * @throws SecretKeyTooShortException + * + * @return string + */ + public function oathHotp($key, $counter); - /** - * Takes the secret key and the timestamp and returns the one time - * password. - * - * @param string $key - Secret key in binary form. - * @param integer $counter - Timestamp as returned by getTimestamp. - * @throws SecretKeyTooShortException - * @return string - */ - public function oathHotp($key, $counter); + /** + * Get the current one time password for a key. + * + * @param string $initalizationKey + * + * @throws InvalidCharactersException + * @throws SecretKeyTooShortException + * + * @return string + */ + public function getCurrentOtp($initalizationKey); - /** - * Get the current one time password for a key. - * - * @param string $initalizationKey - * @return string - * @throws InvalidCharactersException - * @throws SecretKeyTooShortException - */ - public function getCurrentOtp($initalizationKey); + /** + * Verifies a user inputted key against the current timestamp. Checks $window + * keys either side of the timestamp. + * + * @param string $b32seed + * @param string $key - User specified key + * @param int $window + * @param bool $useTimeStamp + * + * @return bool + **/ + public function verifyKey($b32seed, $key, $window = 4, $useTimeStamp = true); - /** - * Verifies a user inputted key against the current timestamp. Checks $window - * keys either side of the timestamp. - * - * @param string $b32seed - * @param string $key - User specified key - * @param integer $window - * @param boolean $useTimeStamp - * @return boolean - **/ - public function verifyKey($b32seed, $key, $window = 4, $useTimeStamp = true); + /** + * Extracts the OTP from the SHA1 hash. + * + * @param string $hash + * + * @return int + **/ + public function oathTruncate($hash); - /** - * Extracts the OTP from the SHA1 hash. - * - * @param string $hash - * @return integer - **/ - public function oathTruncate($hash); + /** + * Remove invalid chars from a base 32 string. + * + * @param $string + * + * @return mixed + */ + public function removeInvalidChars($string); - /** - * Remove invalid chars from a base 32 string. - * - * @param $string - * @return mixed - */ - public function removeInvalidChars($string); - - /** - * Creates a Google QR code url. - * - * @param string $company - * @param string $holder - * @param string $secret - * @param integer $size - * @return string - */ - public function getQRCodeGoogleUrl($company, $holder, $secret, $size = 200); - - /** - * Generates a QR code data url to display inline. - * - * @param string $company - * @param string $holder - * @param string $secret - * @param integer $size - * @param string $encoding Default to UTF-8 - * @return string - */ - public function getQRCodeInline($company, $holder, $secret, $size = 100, $encoding = 'utf-8'); + /** + * Creates a Google QR code url. + * + * @param string $company + * @param string $holder + * @param string $secret + * @param int $size + * + * @return string + */ + public function getQRCodeGoogleUrl($company, $holder, $secret, $size = 200); + /** + * Generates a QR code data url to display inline. + * + * @param string $company + * @param string $holder + * @param string $secret + * @param int $size + * @param string $encoding Default to UTF-8 + * + * @return string + */ + public function getQRCodeInline($company, $holder, $secret, $size = 100, $encoding = 'utf-8'); } diff --git a/src/Exceptions/InvalidCharactersException.php b/src/Exceptions/InvalidCharactersException.php index 405a093..41f8c19 100644 --- a/src/Exceptions/InvalidCharactersException.php +++ b/src/Exceptions/InvalidCharactersException.php @@ -1,11 +1,10 @@ -<?php
-
-namespace PragmaRX\Google2FA\Exceptions;
-
-use Exception;
-
-class InvalidCharactersException extends Exception {
-
- protected $message = 'Invalid characters in the base32 string.';
-
-}
+<?php + +namespace PragmaRX\Google2FA\Exceptions; + +use Exception; + +class InvalidCharactersException extends Exception +{ + protected $message = 'Invalid characters in the base32 string.'; +} diff --git a/src/Exceptions/SecretKeyTooShortException.php b/src/Exceptions/SecretKeyTooShortException.php index 3a0c907..a7bf1b9 100644 --- a/src/Exceptions/SecretKeyTooShortException.php +++ b/src/Exceptions/SecretKeyTooShortException.php @@ -1,11 +1,10 @@ -<?php
-
-namespace PragmaRX\Google2FA\Exceptions;
-
-use Exception;
-
-class SecretKeyTooShortException extends Exception {
-
- protected $message = 'Secret key is too short. Must be at least 16 base 32 characters';
-
-}
+<?php + +namespace PragmaRX\Google2FA\Exceptions; + +use Exception; + +class SecretKeyTooShortException extends Exception +{ + protected $message = 'Secret key is too short. Must be at least 16 base 32 characters'; +} diff --git a/src/Google2FA.php b/src/Google2FA.php index 4e1c7f6..4c04b0a 100644 --- a/src/Google2FA.php +++ b/src/Google2FA.php @@ -1,297 +1,306 @@ -<?php
-
-namespace PragmaRX\Google2FA;
-
-/**
- * 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 <http://www.gnu.org/licenses/>.
- *
- * PHP Google two-factor authentication module.
- *
- * See http://www.idontplaydarts.com/2011/07/google-totp-two-factor-authentication-for-php/
- * for more details
- *
- * @author Phil (Orginal author of this class)
- *
- * Changes have been made in the original class to remove all static methods and, also,
- * provide some other methods.
- *
- * @package Google2FA
- * @author Antonio Carlos Ribeiro @ PragmaRX
- **/
-
-use Base32\Base32;
-use BaconQrCode\Writer;
-use BaconQrCode\Renderer\Image\Png;
-use PragmaRX\Google2FA\Support\Url;
-use PragmaRX\Google2FA\Exceptions\InvalidCharactersException;
-use PragmaRX\Google2FA\Exceptions\SecretKeyTooShortException;
-use PragmaRX\Google2FA\Contracts\Google2FA as Google2FAContract;
-
-class Google2FA implements Google2FAContract
-{
- /**
- * Interval between key regeneration
- */
- const KEY_REGENERATION = 30;
-
- /**
- * Length of the Token generated.
- *
- */
- const OPT_LENGTH = 6;
-
- /**
- * Characters valid for Base 32.
- *
- */
- const VALID_FOR_B32 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567';
-
- /**
- * Generate a digit secret key in base32 format.
- *
- * @param int $length
- * @return string
- */
- public function generateSecretKey($length = 16, $prefix = '')
- {
- $b32 = "234567QWERTYUIOPASDFGHJKLZXCVBNM";
-
- $secret = $prefix ? $this->toBase32($prefix) : '';
-
- for ($i = 0; $i < $length; $i++)
- {
- $secret .= $b32[$this->getRandomNumber()];
- }
-
- $this->validateSecret($secret);
-
- return $secret;
- }
-
- /**
- * Returns the current Unix Timestamp divided by the KEY_REGENERATION
- * period.
- *
- * @return integer
- **/
- public function getTimestamp()
- {
- return floor(microtime(true) / static::KEY_REGENERATION);
- }
-
- /**
- * Decodes a base32 string into a binary string.
- *
- * @param string $b32
- * @throws InvalidCharactersException
- * @return integer
- */
- public function base32Decode($b32)
- {
- $b32 = strtoupper($b32);
-
- $this->validateSecret($b32);
-
- return Base32::decode($b32);
- }
-
- /**
- * Takes the secret key and the timestamp and returns the one time
- * password.
- *
- * @param string $key - Secret key in binary form.
- * @param integer $counter - Timestamp as returned by getTimestamp.
- * @throws SecretKeyTooShortException
- * @return string
- */
- public function oathHotp($key, $counter)
- {
- if (strlen($key) < 8)
- {
- throw new SecretKeyTooShortException();
- }
-
- // Counter must be 64-bit int
- $bin_counter = pack('N*', 0, $counter);
-
- $hash = hash_hmac('sha1', $bin_counter, $key, true);
-
- return str_pad($this->oathTruncate($hash), static::OPT_LENGTH, '0', STR_PAD_LEFT);
- }
-
- /**
- * Get the current one time password for a key.
- *
- * @param string $initalizationKey
- * @return string
- * @throws InvalidCharactersException
- * @throws SecretKeyTooShortException
- */
- public function getCurrentOtp($initalizationKey)
- {
- $timestamp = $this->getTimestamp();
-
- $secretKey = $this->base32Decode($initalizationKey);
-
- return $this->oathHotp($secretKey, $timestamp);
- }
-
- /**
- * Verifies a user inputted key against the current timestamp. Checks $window
- * keys either side of the timestamp.
- *
- * @param string $b32seed
- * @param string $key - User specified key
- * @param integer $window
- * @param boolean $useTimeStamp
- * @return boolean
- **/
- public function verifyKey($b32seed, $key, $window = 4, $useTimeStamp = true)
- {
- $timeStamp = $this->getTimestamp();
-
- if ($useTimeStamp !== true)
- {
- $timeStamp = (int)$useTimeStamp;
- }
-
- $binarySeed = $this->base32Decode($b32seed);
-
- for ($ts = $timeStamp - $window; $ts <= $timeStamp + $window; $ts++)
- {
- if (hash_equals($this->oathHotp($binarySeed, $ts), $key))
- {
- return true;
- }
- }
-
- return false;
- }
-
- /**
- * Extracts the OTP from the SHA1 hash.
- *
- * @param string $hash
- * @return integer
- **/
- public function oathTruncate($hash)
- {
- $offset = ord($hash[19]) & 0xf;
- $temp = unpack('N', substr($hash, $offset, 4));
- return substr($temp[1] & 0x7fffffff, -static::OPT_LENGTH);
- }
-
- /**
- * Remove invalid chars from a base 32 string.
- *
- * @param $string
- * @return mixed
- */
- public function removeInvalidChars($string)
- {
- return preg_replace('/[^'.static::VALID_FOR_B32.']/', '', $string);
- }
-
- /**
- * Creates a Google QR code url.
- *
- * @param string $company
- * @param string $holder
- * @param string $secret
- * @param integer $size
- * @return string
- */
- public function getQRCodeGoogleUrl($company, $holder, $secret, $size = 200)
- {
- $url = $this->getQRCodeUrl($company, $holder, $secret);
-
- return Url::generateGoogleQRCodeUrl('https://chart.googleapis.com/', 'chart', 'chs='.$size.'x'.$size.'&chld=M|0&cht=qr&chl=', $url);
- }
-
- /**
- * Generates a QR code data url to display inline.
- *
- * @param string $company
- * @param string $holder
- * @param string $secret
- * @param integer $size
- * @param string $encoding Default to UTF-8
- * @return string
- */
- public function getQRCodeInline($company, $holder, $secret, $size = 200, $encoding = 'utf-8')
- {
- $url = $this->getQRCodeUrl($company, $holder, $secret);
-
- $renderer = new Png();
- $renderer->setWidth($size);
- $renderer->setHeight($size);
-
- $writer = new Writer($renderer);
- $data = $writer->writeString($url, $encoding);
-
- return 'data:image/png;base64,' . base64_encode($data);
- }
-
- /**
- * Creates a QR code url.
- *
- * @param $company
- * @param $holder
- * @param $secret
- * @return string
- */
- public function getQRCodeUrl($company, $holder, $secret)
- {
- return 'otpauth://totp/'.$company.':'.$holder.'?secret='.$secret.'&issuer='.$company.'';
- }
-
- /**
- * Get a random number.
- *
- * @param $from
- * @param $to
- * @return int
- */
- private function getRandomNumber($from = 0, $to = 31)
- {
- return random_int($from, $to);
- }
-
- /**
- * Validate the secret.
- *
- * @param $b32
- * @throws InvalidCharactersException
- */
- private function validateSecret($b32)
- {
- if (!preg_match('/^[' . static::VALID_FOR_B32 . ']+$/', $b32, $match))
- {
- throw new InvalidCharactersException();
- }
- }
-
- /**
- * Encode a string to Base32.
- *
- * @param $string
- * @return mixed
- */
- public function toBase32($string)
- {
- $encoded = Base32::encode($string);
-
- return str_replace('=', '', $encoded);
- }
-}
+<?php + +namespace PragmaRX\Google2FA; + +/* + * 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 <http://www.gnu.org/licenses/>. + * + * PHP Google two-factor authentication module. + * + * See http://www.idontplaydarts.com/2011/07/google-totp-two-factor-authentication-for-php/ + * for more details + * + * @author Phil (Orginal author of this class) + * + * Changes have been made in the original class to remove all static methods and, also, + * provide some other methods. + * + * @package Google2FA + * @author Antonio Carlos Ribeiro @ PragmaRX + **/ + +use BaconQrCode\Renderer\Image\Png; +use BaconQrCode\Writer; +use Base32\Base32; +use PragmaRX\Google2FA\Contracts\Google2FA as Google2FAContract; +use PragmaRX\Google2FA\Exceptions\InvalidCharactersException; +use PragmaRX\Google2FA\Exceptions\SecretKeyTooShortException; +use PragmaRX\Google2FA\Support\Url; + +class Google2FA implements Google2FAContract +{ + /** + * Interval between key regeneration. + */ + const KEY_REGENERATION = 30; + + /** + * Length of the Token generated. + */ + const OPT_LENGTH = 6; + + /** + * Characters valid for Base 32. + */ + const VALID_FOR_B32 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'; + + /** + * Generate a digit secret key in base32 format. + * + * @param int $length + * + * @return string + */ + public function generateSecretKey($length = 16, $prefix = '') + { + $b32 = '234567QWERTYUIOPASDFGHJKLZXCVBNM'; + + $secret = $prefix ? $this->toBase32($prefix) : ''; + + for ($i = 0; $i < $length; $i++) { + $secret .= $b32[$this->getRandomNumber()]; + } + + $this->validateSecret($secret); + + return $secret; + } + + /** + * Returns the current Unix Timestamp divided by the KEY_REGENERATION + * period. + * + * @return int + **/ + public function getTimestamp() + { + return floor(microtime(true) / static::KEY_REGENERATION); + } + + /** + * Decodes a base32 string into a binary string. + * + * @param string $b32 + * + * @throws InvalidCharactersException + * + * @return int + */ + public function base32Decode($b32) + { + $b32 = strtoupper($b32); + + $this->validateSecret($b32); + + return Base32::decode($b32); + } + + /** + * Takes the secret key and the timestamp and returns the one time + * password. + * + * @param string $key - Secret key in binary form. + * @param int $counter - Timestamp as returned by getTimestamp. + * + * @throws SecretKeyTooShortException + * + * @return string + */ + public function oathHotp($key, $counter) + { + if (strlen($key) < 8) { + throw new SecretKeyTooShortException(); + } + + // Counter must be 64-bit int + $bin_counter = pack('N*', 0, $counter); + + $hash = hash_hmac('sha1', $bin_counter, $key, true); + + return str_pad($this->oathTruncate($hash), static::OPT_LENGTH, '0', STR_PAD_LEFT); + } + + /** + * Get the current one time password for a key. + * + * @param string $initalizationKey + * + * @throws InvalidCharactersException + * @throws SecretKeyTooShortException + * + * @return string + */ + public function getCurrentOtp($initalizationKey) + { + $timestamp = $this->getTimestamp(); + + $secretKey = $this->base32Decode($initalizationKey); + + return $this->oathHotp($secretKey, $timestamp); + } + + /** + * Verifies a user inputted key against the current timestamp. Checks $window + * keys either side of the timestamp. + * + * @param string $b32seed + * @param string $key - User specified key + * @param int $window + * @param bool $useTimeStamp + * + * @return bool + **/ + public function verifyKey($b32seed, $key, $window = 4, $useTimeStamp = true) + { + $timeStamp = $this->getTimestamp(); + + if ($useTimeStamp !== true) { + $timeStamp = (int) $useTimeStamp; + } + + $binarySeed = $this->base32Decode($b32seed); + + for ($ts = $timeStamp - $window; $ts <= $timeStamp + $window; $ts++) { + if (hash_equals($this->oathHotp($binarySeed, $ts), $key)) { + return true; + } + } + + return false; + } + + /** + * Extracts the OTP from the SHA1 hash. + * + * @param string $hash + * + * @return int + **/ + public function oathTruncate($hash) + { + $offset = ord($hash[19]) & 0xf; + $temp = unpack('N', substr($hash, $offset, 4)); + + return substr($temp[1] & 0x7fffffff, -static::OPT_LENGTH); + } + + /** + * Remove invalid chars from a base 32 string. + * + * @param $string + * + * @return mixed + */ + public function removeInvalidChars($string) + { + return preg_replace('/[^'.static::VALID_FOR_B32.']/', '', $string); + } + + /** + * Creates a Google QR code url. + * + * @param string $company + * @param string $holder + * @param string $secret + * @param int $size + * + * @return string + */ + public function getQRCodeGoogleUrl($company, $holder, $secret, $size = 200) + { + $url = $this->getQRCodeUrl($company, $holder, $secret); + + return Url::generateGoogleQRCodeUrl('https://chart.googleapis.com/', 'chart', 'chs='.$size.'x'.$size.'&chld=M|0&cht=qr&chl=', $url); + } + + /** + * Generates a QR code data url to display inline. + * + * @param string $company + * @param string $holder + * @param string $secret + * @param int $size + * @param string $encoding Default to UTF-8 + * + * @return string + */ + public function getQRCodeInline($company, $holder, $secret, $size = 200, $encoding = 'utf-8') + { + $url = $this->getQRCodeUrl($company, $holder, $secret); + + $renderer = new Png(); + $renderer->setWidth($size); + $renderer->setHeight($size); + + $writer = new Writer($renderer); + $data = $writer->writeString($url, $encoding); + + return 'data:image/png;base64,'.base64_encode($data); + } + + /** + * Creates a QR code url. + * + * @param $company + * @param $holder + * @param $secret + * + * @return string + */ + public function getQRCodeUrl($company, $holder, $secret) + { + return 'otpauth://totp/'.$company.':'.$holder.'?secret='.$secret.'&issuer='.$company.''; + } + + /** + * Get a random number. + * + * @param $from + * @param $to + * + * @return int + */ + private function getRandomNumber($from = 0, $to = 31) + { + return random_int($from, $to); + } + + /** + * Validate the secret. + * + * @param $b32 + * + * @throws InvalidCharactersException + */ + private function validateSecret($b32) + { + if (!preg_match('/^['.static::VALID_FOR_B32.']+$/', $b32, $match)) { + throw new InvalidCharactersException(); + } + } + + /** + * Encode a string to Base32. + * + * @param $string + * + * @return mixed + */ + public function toBase32($string) + { + $encoded = Base32::encode($string); + + return str_replace('=', '', $encoded); + } +} diff --git a/src/Support/Url.php b/src/Support/Url.php index 9750a62..bcaed87 100644 --- a/src/Support/Url.php +++ b/src/Support/Url.php @@ -4,13 +4,13 @@ namespace PragmaRX\Google2FA\Support; class Url { - public static function generateGoogleQRCodeUrl($domain, $page, $queryParameters, $qrCodeUrl) - { - $url = $domain . - rawurlencode($page) . - '?' . $queryParameters . - urlencode($qrCodeUrl); + public static function generateGoogleQRCodeUrl($domain, $page, $queryParameters, $qrCodeUrl) + { + $url = $domain. + rawurlencode($page). + '?'.$queryParameters. + urlencode($qrCodeUrl); - return $url; - } + return $url; + } } diff --git a/src/Vendor/Laravel/Facade.php b/src/Vendor/Laravel/Facade.php index 918162f..4b5c325 100644 --- a/src/Vendor/Laravel/Facade.php +++ b/src/Vendor/Laravel/Facade.php @@ -1,19 +1,18 @@ -<?php
-
-namespace PragmaRX\Google2FA\Vendor\Laravel;
-
-use Illuminate\Support\Facades\Facade as IlluminateFacade;
-
-class Facade extends IlluminateFacade {
-
- /**
- * Get the registered name of the component.
- *
- * @return string
- */
- protected static function getFacadeAccessor()
- {
- return 'PragmaRX\Google2FA\Contracts\Google2FA';
- }
-
-}
+<?php + +namespace PragmaRX\Google2FA\Vendor\Laravel; + +use Illuminate\Support\Facades\Facade as IlluminateFacade; + +class Facade extends IlluminateFacade +{ + /** + * Get the registered name of the component. + * + * @return string + */ + protected static function getFacadeAccessor() + { + return 'PragmaRX\Google2FA\Contracts\Google2FA'; + } +} diff --git a/src/Vendor/Laravel/ServiceProvider.php b/src/Vendor/Laravel/ServiceProvider.php index 7c5108a..d42a409 100644 --- a/src/Vendor/Laravel/ServiceProvider.php +++ b/src/Vendor/Laravel/ServiceProvider.php @@ -6,35 +6,33 @@ use Illuminate\Support\ServiceProvider as IlluminateServiceProvider; class ServiceProvider extends IlluminateServiceProvider { + /** + * Indicates if loading of the provider is deferred. + * + * @var bool + */ + protected $defer = true; - /** - * Indicates if loading of the provider is deferred. - * - * @var bool - */ - protected $defer = true; - - /** - * Register the service provider. - * - * @return void - */ - public function register() - { - $this->app->bind( - 'PragmaRX\Google2FA\Contracts\Google2FA', - 'PragmaRX\Google2FA\Google2FA' - ); - } - - /** - * Get the services provided by the provider. - * - * @return array - */ - public function provides() - { - return array('PragmaRX\Google2FA\Contracts\Google2FA'); - } + /** + * Register the service provider. + * + * @return void + */ + public function register() + { + $this->app->bind( + 'PragmaRX\Google2FA\Contracts\Google2FA', + 'PragmaRX\Google2FA\Google2FA' + ); + } + /** + * Get the services provided by the provider. + * + * @return array + */ + public function provides() + { + return ['PragmaRX\Google2FA\Contracts\Google2FA']; + } } diff --git a/tests/spec/Google2FASpec.php b/tests/spec/Google2FASpec.php index 67c55ad..f53aa9e 100644 --- a/tests/spec/Google2FASpec.php +++ b/tests/spec/Google2FASpec.php @@ -7,99 +7,95 @@ use PragmaRX\Google2FA\Google2FA; class Google2FASpec extends ObjectBehavior { - public $secret = 'ADUMJO5634NPDEKW'; + public $secret = 'ADUMJO5634NPDEKW'; - public $url = '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'; + public $url = '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'; - function it_is_initializable() + public function it_is_initializable() { $this->shouldHaveType('PragmaRX\Google2FA\Google2FA'); } - function it_generates_a_valid_secret_key() - { - $this->generateSecretKey()->shouldHaveLength(16); - - $this->generateSecretKey(17)->shouldHaveLength(17); - - $this->generateSecretKey(17, 'antoniocarlos')->shouldStartWith('MFXHI33ONFXWGYLSNRXXG'); - - $this->generateSecretKey()->shouldBeAmongst(Google2FA::VALID_FOR_B32); - } - - function it_gets_valid_timestamps() - { - $this->getTimestamp()->shouldBeValidTimestamp(); - } - - function it_decodes_base32_strings() - { - $this->base32Decode($this->secret)->shouldBe( - chr(0) - . chr(232) - . chr(196) - . chr(187) - . chr(190) - . chr(223) - . chr(26) - . chr(241) - . chr(145) - . chr(86) - ); - } - - function it_creates_a_one_time_password() - { - $this->getCurrentOtp($this->secret)->shouldHaveLength(6); - } - - function it_verifies_a_key() - { - // 26213400 = Human time (GMT): Sat, 31 Oct 1970 09:30:00 GMT - - $this->verifyKey($this->secret, '410272', 4, 26213400)->shouldBe(true); - } - - function it_removes_invalid_chars_from_secret() - { - $this->removeInvalidChars($this->secret . '!1-@@@')->shouldBe($this->secret); - } - - function it_creates_a_qr_code() - { - $this->getQRCodeGoogleUrl('PragmaRX', 'acr+pragmarx@antoniocarlosribeiro.com', $this->secret)->shouldBe($this->url); - } - - function it_converts_to_base32() - { - $this->toBase32('PragmaRX')->shouldBe('KBZGCZ3NMFJFQ'); - } - - public function getMatchers() - { - return [ - 'haveLength' => function($subject, $key) - { - return strlen($subject) == $key; - }, - - 'shouldStartWith' => function($subject, $key) - { - return substr($key, 0, strlen($subject)) == $subject; - }, - - 'beAmongst' => function($subject, $key) - { - return preg_replace('/[^'.$key.']/', '', $subject) === $subject; - }, - - 'beValidTimestamp' => function($timestamp) - { - return is_double($timestamp) - && ($timestamp <= PHP_INT_MAX) - && ($timestamp >= ~PHP_INT_MAX); - }, - - ]; - } + public function it_generates_a_valid_secret_key() + { + $this->generateSecretKey()->shouldHaveLength(16); + + $this->generateSecretKey(17)->shouldHaveLength(17); + + $this->generateSecretKey(17, 'antoniocarlos')->shouldStartWith('MFXHI33ONFXWGYLSNRXXG'); + + $this->generateSecretKey()->shouldBeAmongst(Google2FA::VALID_FOR_B32); + } + + public function it_gets_valid_timestamps() + { + $this->getTimestamp()->shouldBeValidTimestamp(); + } + + public function it_decodes_base32_strings() + { + $this->base32Decode($this->secret)->shouldBe( + chr(0) + .chr(232) + .chr(196) + .chr(187) + .chr(190) + .chr(223) + .chr(26) + .chr(241) + .chr(145) + .chr(86) + ); + } + + public function it_creates_a_one_time_password() + { + $this->getCurrentOtp($this->secret)->shouldHaveLength(6); + } + + public function it_verifies_a_key() + { + // 26213400 = Human time (GMT): Sat, 31 Oct 1970 09:30:00 GMT + + $this->verifyKey($this->secret, '410272', 4, 26213400)->shouldBe(true); + } + + public function it_removes_invalid_chars_from_secret() + { + $this->removeInvalidChars($this->secret.'!1-@@@')->shouldBe($this->secret); + } + + public function it_creates_a_qr_code() + { + $this->getQRCodeGoogleUrl('PragmaRX', 'acr+pragmarx@antoniocarlosribeiro.com', $this->secret)->shouldBe($this->url); + } + + public function it_converts_to_base32() + { + $this->toBase32('PragmaRX')->shouldBe('KBZGCZ3NMFJFQ'); + } + + public function getMatchers() + { + return [ + 'haveLength' => function ($subject, $key) { + return strlen($subject) == $key; + }, + + 'shouldStartWith' => function ($subject, $key) { + return substr($key, 0, strlen($subject)) == $subject; + }, + + 'beAmongst' => function ($subject, $key) { + return preg_replace('/[^'.$key.']/', '', $subject) === $subject; + }, + + 'beValidTimestamp' => function ($timestamp) { + return is_float($timestamp) + && ($timestamp <= PHP_INT_MAX) + && ($timestamp >= ~PHP_INT_MAX); + }, + + ]; + } } |