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

Introducing namespace #47

Open
wants to merge 8 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 @@ -12,4 +12,4 @@ install:
- export PATH="$HOME/.composer/vendor/bin:$PATH"
- composer install

script: phpunit --coverage-text --configuration tests/phpunit.xml
script: vendor/bin/phpunit --coverage-text --configuration tests/phpunit.xml
91 changes: 53 additions & 38 deletions PHPGangsta/GoogleAuthenticator.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
<?php

namespace PHPGangsta;

use Exception;

/**
* PHP Class for handling Google Authenticator 2-factor authentication.
*
* @author Michael Kliewe
* @author Michael Kliewe
* @copyright 2012 Michael Kliewe
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
*
* @link http://www.phpgangsta.de/
* @link http://www.phpgangsta.de/
*/
class PHPGangsta_GoogleAuthenticator
{
class GoogleAuthenticator {
protected $_codeLength = 6;

/**
Expand All @@ -20,17 +23,17 @@ class PHPGangsta_GoogleAuthenticator
* @param int $secretLength
*
* @return string
* @throws Exception
*/
public function createSecret($secretLength = 16)
{
public function createSecret($secretLength = 16) {
$validChars = $this->_getBase32LookupTable();

// Valid secret lengths are 80 to 640 bits
if ($secretLength < 16 || $secretLength > 128) {
throw new Exception('Bad secret length');
}
$secret = '';
$rnd = false;
$rnd = false;
if (function_exists('random_bytes')) {
$rnd = random_bytes($secretLength);
} elseif (function_exists('mcrypt_create_iv')) {
Expand Down Expand Up @@ -60,16 +63,15 @@ public function createSecret($secretLength = 16)
*
* @return string
*/
public function getCode($secret, $timeSlice = null)
{
public function getCode($secret, $timeSlice = null) {
if ($timeSlice === null) {
$timeSlice = floor(time() / 30);
}

$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 @@ -98,18 +100,17 @@ public function getCode($secret, $timeSlice = null)
*
* @return string
*/
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;
$level = !empty($params['level']) && array_search($params['level'], array('L', 'M', 'Q', 'H')) !== false ? $params['level'] : 'M';
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;
$level = !empty($params['level']) && array_search($params['level'], array('L', 'M', 'Q', 'H')) !== false ? $params['level'] : 'M';

$urlencoded = urlencode('otpauth://totp/'.$name.'?secret='.$secret.'');
if (isset($title)) {
$urlencoded .= urlencode('&issuer='.urlencode($title));
}
$otpauthUrl = $this->getOtpauthUrl($name, $secret, $title);

return 'https://chart.googleapis.com/chart?chs='.$width.'x'.$height.'&chld='.$level.'|0&cht=qr&chl='.$urlencoded.'';
return 'https://chart.googleapis.com/chart?chs=' .
$width . 'x' . $height .
'&chld=' . $level .
'|0&cht=qr&chl=' . urlencode($otpauthUrl);
}

/**
Expand All @@ -122,8 +123,7 @@ public function getQRCodeGoogleUrl($name, $secret, $title = null, $params = arra
*
* @return bool
*/
public function verifyCode($secret, $code, $discrepancy = 1, $currentTimeSlice = null)
{
public function verifyCode($secret, $code, $discrepancy = 1, $currentTimeSlice = null) {
if ($currentTimeSlice === null) {
$currentTimeSlice = floor(time() / 30);
}
Expand All @@ -147,10 +147,9 @@ public function verifyCode($secret, $code, $discrepancy = 1, $currentTimeSlice =
*
* @param int $length
*
* @return PHPGangsta_GoogleAuthenticator
* @return GoogleAuthenticator
*/
public function setCodeLength($length)
{
public function setCodeLength($length) {
$this->_codeLength = $length;

return $this;
Expand All @@ -163,28 +162,28 @@ public function setCodeLength($length)
*
* @return bool|string
*/
protected function _base32Decode($secret)
{
protected function _base32Decode($secret) {
if (empty($secret)) {
return '';
}

$base32chars = $this->_getBase32LookupTable();
$base32chars = $this->_getBase32LookupTable();
$base32charsFlipped = array_flip($base32chars);

$paddingCharCount = substr_count($secret, $base32chars[32]);
$allowedValues = array(6, 4, 3, 1, 0);
$allowedValues = array(6, 4, 3, 1, 0);
if (!in_array($paddingCharCount, $allowedValues)) {
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;
}
}
$secret = str_replace('=', '', $secret);
$secret = str_split($secret);
$secret = str_replace('=', '', $secret);
$secret = str_split($secret);
$binaryString = '';
for ($i = 0; $i < count($secret); $i = $i + 8) {
$x = '';
Expand All @@ -208,8 +207,7 @@ protected function _base32Decode($secret)
*
* @return array
*/
protected function _getBase32LookupTable()
{
protected function _getBase32LookupTable() {
return array(
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', // 7
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', // 15
Expand All @@ -228,8 +226,7 @@ protected function _getBase32LookupTable()
*
* @return bool True if the two strings are identical
*/
private function timingSafeEquals($safeString, $userString)
{
private function timingSafeEquals($safeString, $userString) {
if (function_exists('hash_equals')) {
return hash_equals($safeString, $userString);
}
Expand All @@ -249,4 +246,22 @@ private function timingSafeEquals($safeString, $userString)
// They are only identical strings if $result is exactly 0...
return $result === 0;
}

/**
* getOtpauthUrl
*
* @param string $name
* @param string $secret
* @param string $title
*
* @return string
*/
public function getOtpauthUrl($name, $secret, $title) {
$otpauthUrl = 'otpauth://totp/' . urlencode($name) . '?secret=' . urlencode($secret);
if (isset($title)) {
$otpauthUrl .= '&issuer=' . urlencode($title);
}

return $otpauthUrl;
}
}
9 changes: 6 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@
"require": {
"php": ">=5.3"
},
"autoload": {
"classmap": ["PHPGangsta/GoogleAuthenticator.php"]
}
"require-dev": {
"phpunit/phpunit": "*"
},
"autoload": {
"classmap": ["PHPGangsta/GoogleAuthenticator.php"]
}
}


Expand Down
Loading