summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristian Riesen <chris.riesen@gmail.com>2012-06-16 16:11:56 +0200
committerChristian Riesen <chris.riesen@gmail.com>2012-06-16 16:11:56 +0200
commitd88c29a25a2199af393643cf74c24508c660e932 (patch)
treef3d5d6b7c9a026e6f7314af9e28a4ab2f7520807
parent2d5f52cc637c04f005b973aa140b2b809fc2b92c (diff)
downloadotp-d88c29a25a2199af393643cf74c24508c660e932.zip
otp-d88c29a25a2199af393643cf74c24508c660e932.tar.gz
otp-d88c29a25a2199af393643cf74c24508c660e932.tar.bz2
GoogleAuthenticator class now tested as well, and can change the size of
the output qr code
-rw-r--r--src/Otp/GoogleAuthenticator.php25
-rw-r--r--tests/Otp/GoogleAuthenticatorTest.php40
2 files changed, 61 insertions, 4 deletions
diff --git a/src/Otp/GoogleAuthenticator.php b/src/Otp/GoogleAuthenticator.php
index 7ecd722..ed9fd69 100644
--- a/src/Otp/GoogleAuthenticator.php
+++ b/src/Otp/GoogleAuthenticator.php
@@ -18,6 +18,9 @@ class GoogleAuthenticator
{
protected static $allowedTypes = array('hotp', 'totp');
+ protected static $height = 200;
+ protected static $width = 200;
+
/**
* Returns the QR code url
*
@@ -36,7 +39,7 @@ class GoogleAuthenticator
public static function getQrCodeUrl($type, $label, $secret, $counter = null, $options = array())
{
// two types only..
- if (!in_array($type, $this->allowedTypes)) {
+ if (!in_array($type, self::$allowedTypes)) {
throw new \InvalidArgumentException('Type has to be of allowed types list');
}
@@ -60,8 +63,8 @@ class GoogleAuthenticator
// 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
@@ -84,7 +87,21 @@ class GoogleAuthenticator
$otpauth .= '&period=' . $options['period'];
}
- $url = 'https://chart.googleapis.com/chart?chs=200x200&cht=qr&chld=M|0&chl=' . urlencode($otpauth);
+ // Width and height can be overwritten
+ $width = self::$width;
+
+ if (array_key_exists('width', $options) && is_numeric($options['width'])) {
+ $width = $options['width'];
+ }
+
+ $height = self::$height;
+
+ if (array_key_exists('height', $options) && is_numeric($options['height'])) {
+ $height = $options['height'];
+ }
+
+ $url = 'https://chart.googleapis.com/chart?chs=' . $width . 'x'
+ . $height . '&cht=qr&chld=M|0&chl=' . urlencode($otpauth);
return $url;
}
diff --git a/tests/Otp/GoogleAuthenticatorTest.php b/tests/Otp/GoogleAuthenticatorTest.php
new file mode 100644
index 0000000..e087406
--- /dev/null
+++ b/tests/Otp/GoogleAuthenticatorTest.php
@@ -0,0 +1,40 @@
+<?php
+
+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()
+ {
+ $secret = 'MEP3EYVA6XNFNVNM'; // testing secret
+
+ // Standard totp case
+ $this->assertEquals(
+ 'https://chart.googleapis.com/chart?chs=200x200&cht=qr&chld=M|0&chl=otpauth%3A%2F%2Ftotp%2Fuser%40host.com%3Fsecret%3DMEP3EYVA6XNFNVNM',
+ GoogleAuthenticator::getQrCodeUrl('totp', 'user@host.com', $secret)
+ );
+
+ // hotp (include a counter)
+ $this->assertEquals(
+ 'https://chart.googleapis.com/chart?chs=200x200&cht=qr&chld=M|0&chl=otpauth%3A%2F%2Fhotp%2Fuser%40host.com%3Fsecret%3DMEP3EYVA6XNFNVNM%26counter%3D1234',
+ GoogleAuthenticator::getQrCodeUrl('hotp', 'user@host.com', $secret, 1234)
+ );
+
+ // 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', 'user@host.com', $secret, null, array('height' => 300, 'width' => 300))
+ );
+
+ }
+}