Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make GoogleAuthenticator class PSR-4 & PSR-2 compilant #68

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ install:
- export PATH="$HOME/.composer/vendor/bin:$PATH"
- composer install

script: phpunit --coverage-text --configuration tests/phpunit.xml
script: phpunit --coverage-text --configuration phpunit.xml
20 changes: 12 additions & 8 deletions PHPGangsta/GoogleAuthenticator.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

namespace PHPGangsta;

/**
* PHP Class for handling Google Authenticator 2-factor authentication.
*
Expand All @@ -9,7 +11,7 @@
*
* @link http://www.phpgangsta.de/
*/
class PHPGangsta_GoogleAuthenticator
class GoogleAuthenticator
{
protected $_codeLength = 6;

Expand Down Expand Up @@ -69,7 +71,7 @@ public function getCode($secret, $timeSlice = null)
$secretkey = $this->_base32Decode($secret);

// Pack time into binary string
$time = chr(0).chr(0).chr(0).chr(0).pack('N*', $timeSlice);
$time = chr(0) . chr(0) . chr(0) . chr(0) . pack('N*', $timeSlice);
// Hash it with users secret key
$hm = hash_hmac('SHA1', $time, $secretkey, true);
// Use last nipple of result as index/offset
Expand Down Expand Up @@ -100,13 +102,13 @@ public function getCode($secret, $timeSlice = null)
*/
public function getQRCodeGoogleUrl($name, $secret, $title = null, $params = array())
{
$width = !empty($params['width']) && (int) $params['width'] > 0 ? (int) $params['width'] : 200;
$height = !empty($params['height']) && (int) $params['height'] > 0 ? (int) $params['height'] : 200;
$width = !empty($params['width']) && (int)$params['width'] > 0 ? (int)$params['width'] : 200;
$height = !empty($params['height']) && (int)$params['height'] > 0 ? (int)$params['height'] : 200;
$level = !empty($params['level']) && array_search($params['level'], array('L', 'M', 'Q', 'H')) !== false ? $params['level'] : 'M';

$urlencoded = urlencode('otpauth://totp/'.$name.'?secret='.$secret.'');
$urlencoded = urlencode('otpauth://totp/' . $name . '?secret=' . $secret . '');
if (isset($title)) {
$urlencoded .= urlencode('&issuer='.urlencode($title));
$urlencoded .= urlencode('&issuer=' . urlencode($title));
}

return "https://api.qrserver.com/v1/create-qr-code/?data=$urlencoded&size=${width}x${height}&ecc=$level";
Expand Down Expand Up @@ -178,8 +180,10 @@ protected function _base32Decode($secret)
return false;
}
for ($i = 0; $i < 4; ++$i) {
if ($paddingCharCount == $allowedValues[$i] &&
substr($secret, -($allowedValues[$i])) != str_repeat($base32chars[32], $allowedValues[$i])) {
if (
$paddingCharCount == $allowedValues[$i] &&
substr($secret, -($allowedValues[$i])) != str_repeat($base32chars[32], $allowedValues[$i])
) {
return false;
}
}
Expand Down
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ See following example:
<?php
require_once 'PHPGangsta/GoogleAuthenticator.php';

$ga = new PHPGangsta_GoogleAuthenticator();
$ga = new PHPGangsta\GoogleAuthenticator();
$secret = $ga->createSecret();
echo "Secret is: ".$secret."\n\n";

Expand Down Expand Up @@ -64,15 +64,14 @@ Installation:
- [Composer](https://getcomposer.org/doc/01-basic-usage.md) will take care of autoloading
the library. Just include the following at the top of your file

`require_once __DIR__ . '/../vendor/autoload.php';`
`require_once __DIR__ . '/vendor/autoload.php';`

Run Tests:
----------

- All tests are inside `tests` folder.
- Execute `composer install` and then run the tests from project root
directory
- Run as `phpunit tests` from the project root directory
- Execute `composer install`
- Run as `./vendor/bin/phpunit` from the project root directory


ToDo:
Expand Down
13 changes: 7 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@
"php": ">=5.3"
},
"autoload": {
"classmap": ["PHPGangsta/GoogleAuthenticator.php"]
}
"psr-4": {
"PHPGangsta\\": "PHPGangsta/"
}
},
"require-dev": {
"phpunit/phpunit": "^8.1"
}
}




23 changes: 23 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit bootstrap="vendor/autoload.php"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnError="false"
stopOnFailure="false"
colors="true"
verbose="true">
<testsuites>
<testsuite name="GoogleAuthenticator Test Suite">
<directory suffix="Test.php">./tests/</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory suffix=".php">./PHPGangsta/</directory>
</whitelist>
</filter>
</phpunit>
27 changes: 17 additions & 10 deletions tests/GoogleAuthenticatorTest.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
<?php

require_once __DIR__.'/../vendor/autoload.php';
namespace Tests;

class GoogleAuthenticatorTest extends PHPUnit_Framework_TestCase
use PHPGangsta\GoogleAuthenticator;
use PHPUnit\Framework\TestCase;

class GoogleAuthenticatorTest extends TestCase
{
/* @var $googleAuthenticator PHPGangsta_GoogleAuthenticator */
/**
* Authenticator class instance
*
* @var GoogleAuthenticator
*/
protected $googleAuthenticator;

protected function setUp()
protected function setUp(): void
{
$this->googleAuthenticator = new PHPGangsta_GoogleAuthenticator();
$this->googleAuthenticator = new GoogleAuthenticator();
}

public function codeProvider()
Expand All @@ -24,9 +31,9 @@ public function codeProvider()

public function testItCanBeInstantiated()
{
$ga = new PHPGangsta_GoogleAuthenticator();
$ga = new GoogleAuthenticator();

$this->assertInstanceOf('PHPGangsta_GoogleAuthenticator', $ga);
$this->assertInstanceOf('PHPGangsta\GoogleAuthenticator', $ga);
}

public function testCreateSecretDefaultsToSixteenCharacters()
Expand Down Expand Up @@ -71,7 +78,7 @@ public function testGetQRCodeGoogleUrlReturnsCorrectUrl()
$this->assertEquals($urlParts['host'], 'api.qrserver.com');
$this->assertEquals($urlParts['path'], '/v1/create-qr-code/');

$expectedChl = 'otpauth://totp/'.$name.'?secret='.$secret;
$expectedChl = 'otpauth://totp/' . $name . '?secret=' . $secret;

$this->assertEquals($queryStringArray['data'], $expectedChl);
}
Expand All @@ -97,7 +104,7 @@ public function testVerifyCodeWithLeadingZero()
$result = $this->googleAuthenticator->verifyCode($secret, $code);
$this->assertEquals(true, $result);

$code = '0'.$code;
$code = '0' . $code;
$result = $this->googleAuthenticator->verifyCode($secret, $code);
$this->assertEquals(false, $result);
}
Expand All @@ -106,6 +113,6 @@ public function testSetCodeLength()
{
$result = $this->googleAuthenticator->setCodeLength(6);

$this->assertInstanceOf('PHPGangsta_GoogleAuthenticator', $result);
$this->assertInstanceOf('PHPGangsta\GoogleAuthenticator', $result);
}
}
3 changes: 0 additions & 3 deletions tests/bootstrap.php

This file was deleted.

15 changes: 0 additions & 15 deletions tests/phpunit.xml

This file was deleted.