Skip to content

Commit

Permalink
Merge pull request #1 from pascalockert/master
Browse files Browse the repository at this point in the history
Additional method to get Key URI without Google Charts
  • Loading branch information
ChristianRiesen committed Aug 21, 2014
2 parents dd1af68 + 9791be0 commit e6b4b9d
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 23 deletions.
5 changes: 4 additions & 1 deletion example/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
$currentTotp = $otp->totp(Base32::decode($secret));

$qrCode = GoogleAuthenticator::getQrCodeUrl('totp', 'otpsample@cr', $secret);
$keyUri = GoogleAuthenticator::getKeyUri('totp', 'otpsample@cr', $secret);

?><html>
<head>
Expand All @@ -45,6 +46,8 @@
QR Code for totp:<br />
<img src="<?php echo $qrCode; ?>" />
<br />
This QR Code contains the Key URI: <?php echo $keyUri; ?>
<br />
<hr />

Current totp would be <?php echo $currentTotp; ?><br />
Expand Down Expand Up @@ -94,4 +97,4 @@
?>

</body>
</html>
</html>
39 changes: 31 additions & 8 deletions src/Otp/GoogleAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/**
* Google Authenticator
*
* Last update: 2012-06-16
* Last update: 2014-08-19
*
* Can be easy used with Google Authenticator
* @link https://code.google.com/p/google-authenticator/
Expand All @@ -22,7 +22,7 @@ class GoogleAuthenticator
protected static $width = 200;

/**
* Returns the QR code url
* Returns the Key URI
*
* Format of encoded url is here:
* https://code.google.com/p/google-authenticator/wiki/KeyUriFormat
Expand All @@ -34,9 +34,9 @@ class GoogleAuthenticator
* @param integer $counter Required by hotp, otherwise ignored
* @param array $options Optional fields that will be set if present
*
* @return string URL to the QR code
* @return string Key URI
*/
public static function getQrCodeUrl($type, $label, $secret, $counter = null, $options = array())
public static function getKeyUri($type, $label, $secret, $counter = null, $options = array())
{
// two types only..
if (!in_array($type, self::$allowedTypes)) {
Expand All @@ -63,8 +63,8 @@ public static function getQrCodeUrl($type, $label, $secret, $counter = null, $op
// This is the base, these are at least required
$otpauth = 'otpauth://' . $type . '/' . $label . '?secret=' . $secret;

if ($type == 'hotp' && !is_null($counter)) {
$otpauth .= '&counter=' . $counter;
if ($type == 'hotp' && !is_null($counter)) {
$otpauth .= '&counter=' . $counter;
}

// Now check the options array
Expand All @@ -86,7 +86,28 @@ public static function getQrCodeUrl($type, $label, $secret, $counter = null, $op
if ($type == 'totp' && array_key_exists('period', $options)) {
$otpauth .= '&period=' . $options['period'];
}


return $otpauth;
}


/**
* Returns the QR code url
*
* Format of encoded url is here:
* https://code.google.com/p/google-authenticator/wiki/KeyUriFormat
* Should be done in a better fashion
*
* @param string $type totp or hotp
* @param string $label Label to display this as to the user
* @param string $secret Base32 encoded secret
* @param integer $counter Required by hotp, otherwise ignored
* @param array $options Optional fields that will be set if present
*
* @return string URL to the QR code
*/
public static function getQrCodeUrl($type, $label, $secret, $counter = null, $options = array())
{
// Width and height can be overwritten
$width = self::$width;

Expand All @@ -99,7 +120,9 @@ public static function getQrCodeUrl($type, $label, $secret, $counter = null, $op
if (array_key_exists('height', $options) && is_numeric($options['height'])) {
$height = $options['height'];
}


$otpauth = self::getKeyUri($type, $label, $secret, $counter, $options);

$url = 'https://chart.googleapis.com/chart?chs=' . $width . 'x'
. $height . '&cht=qr&chld=M|0&chl=' . urlencode($otpauth);

Expand Down
41 changes: 31 additions & 10 deletions tests/Otp/GoogleAuthenticatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,17 @@

require_once __DIR__ . '/../../src/Otp/GoogleAuthenticator.php';

require_once 'PHPUnit/Framework/TestCase.php';

use Otp\GoogleAuthenticator;

/**
* GoogleAuthenticator test case.
*/
class GoogleAuthenticatorTest extends \PHPUnit_Framework_TestCase
{
/**
* Tests getQrCodeUrl
*/
public function testGetQrCodeUrl()
/**
* Tests getQrCodeUrl
*/
public function testGetQrCodeUrl()
{
$secret = 'MEP3EYVA6XNFNVNM'; // testing secret

Expand All @@ -31,13 +29,36 @@ public function testGetQrCodeUrl()
);

// totp, this time with a parameter for chaning the size of the QR
$this->assertEquals(
'https://chart.googleapis.com/chart?chs=300x300&cht=qr&chld=M|0&chl=otpauth%3A%2F%2Ftotp%2Fuser%40host.com%3Fsecret%3DMEP3EYVA6XNFNVNM',
GoogleAuthenticator::getQrCodeUrl('totp', '[email protected]', $secret, null, array('height' => 300, 'width' => 300))
$this->assertEquals(
'https://chart.googleapis.com/chart?chs=300x300&cht=qr&chld=M|0&chl=otpauth%3A%2F%2Ftotp%2Fuser%40host.com%3Fsecret%3DMEP3EYVA6XNFNVNM',
GoogleAuthenticator::getQrCodeUrl('totp', '[email protected]', $secret, null, array('height' => 300, 'width' => 300))
);

}

/**
* Tests getKeyUri
*/
public function testGetKeyUri()
{
$secret = 'MEP3EYVA6XNFNVNM'; // testing secret

// Standard totp case
$this->assertEquals(
'otpauth://totp/[email protected]?secret=MEP3EYVA6XNFNVNM',
GoogleAuthenticator::getKeyUri('totp', '[email protected]', $secret)
);

// hotp (include a counter)
$this->assertEquals(
'otpauth://hotp/[email protected]?secret=MEP3EYVA6XNFNVNM&counter=1234',
GoogleAuthenticator::getKeyUri('hotp', '[email protected]', $secret, 1234)
);

}

/**
* Tests generateRandom
*/
public function testGenerateRandom()
{
// contains numbers 2-7 and letters A-Z in large letters, 16 chars long
Expand Down
6 changes: 2 additions & 4 deletions tests/Otp/OtpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
require_once __DIR__ . '/../../src/Otp/OtpInterface.php';
require_once __DIR__ . '/../../src/Otp/Otp.php';

require_once 'PHPUnit/Framework/TestCase.php';

use Otp\Otp;

/**
Expand Down Expand Up @@ -114,9 +112,9 @@ public function testTotpRfc()
*/
}

/**
/**
* @expectedException InvalidArgumentException
* @expectedExceptionMessage Counter must be integer
* @expectedExceptionMessage Counter must be integer
*/
public function testHotpInvalidCounter()
{
Expand Down

0 comments on commit e6b4b9d

Please sign in to comment.