Skip to content

Commit

Permalink
Examples added and doc updated
Browse files Browse the repository at this point in the history
  • Loading branch information
Spomky committed Nov 2, 2014
1 parent 2667ac8 commit 71eb26f
Show file tree
Hide file tree
Showing 7 changed files with 298 additions and 9 deletions.
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@
"psr-4": { "OTPHP\\": "lib/" }
},
"autoload-dev": {
"psr-4": { "OTPHP\\": "tests/" }
"psr-4": {
"OTPHP\\": "tests/",
"MyProject\\": "examples/"
}
},
"extra": {
"branch-alias": {
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions doc/Extend.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ The following class is a possible implementation of the TOTP Class:

public function setInterval($interval)
{
if( !is_numeric($interval) || $interval < 1 ) {
if( !is_integer($interval) || $interval < 1 ) {
throw new \Exception("Interval must be at least 1.");
}
$this->interval = $interval;
Expand Down Expand Up @@ -187,7 +187,7 @@ The following class is a possible implementation of the HOTP Class:
protected $label = null;
protected $digest = 'sha1';
protected $digits = 6;
protected counter = 0;
protected $counter = 0;

public function setSecret($secret)
{
Expand Down Expand Up @@ -268,16 +268,16 @@ The following class is a possible implementation of the HOTP Class:
return $this->digest;
}

public function setInterval($interval)
public function setCounter($counter)
{
if( !is_numeric($interval) || $interval < 1 ) {
throw new \Exception("Interval must be at least 1.");
if( !is_integer($counter) || $counter < 0 ) {
throw new \Exception("Counter must be at least 0.");
}
$this->interval = $interval;
$this->counter = $counter;
return $this;
}

public function getInitialCount()
public function getCounter()
{
return $this->counter;
}
Expand Down
127 changes: 127 additions & 0 deletions examples/HOTP.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?php

namespace MyProject;

use OTPHP\HOTP as BaseHOTP;

class HOTP extends BaseHOTP
{
protected $secret = null;
protected $issuer = null;
protected $issuer_included_as_parameter = false;
protected $label = null;
protected $digest = 'sha1';
protected $digits = 6;
protected $counter = 0;

public function setSecret($secret)
{
//You must check that the secret is a valid Base32 string
$this->secret = $secret;
return $this;
}

public function getSecret()
{
return $this->secret;
}

public function setLabel($label)
{
if ($this->hasSemicolon($label)) {
throw new \Exception("Label must not containt a semi-colon.");
}
$this->label = $label;
return $this;
}

public function getLabel()
{
return $this->label;
}

public function setIssuer($issuer)
{
if ($this->hasSemicolon($issuer)) {
throw new \Exception("Issuer must not containt a semi-colon.");
}
$this->issuer = $issuer;
return $this;
}

public function getIssuer()
{
return $this->issuer;
}

public function isIssuerIncludedAsParameter()
{
return $this->issuer_included_as_parameter;
}

public function setIssuerIncludedAsParameter($issuer_included_as_parameter)
{
$this->issuer_included_as_parameter = $issuer_included_as_parameter;
return $this;
}

public function setDigits($digits)
{
if( !is_numeric($digits) || $digits < 1 ) {
throw new \Exception("Digits must be at least 1.");
}
$this->digits = $digits;
return $this;
}

public function getDigits()
{
return $this->digits;
}

public function setDigest($digest)
{
if( !in_array($digest, array('md5', 'sha1', 'sha256', 'sha512')) ) {
throw new \Exception("'$digest' digest is not supported.");
}
$this->digest = $digest;
return $this;
}

public function getDigest()
{
return $this->digest;
}

public function setCounter($counter)
{
if( !is_integer($counter) || $counter < 0 ) {
throw new \Exception("Counter must be at least 0.");
}
$this->counter = $counter;
return $this;
}

public function getCounter()
{
return $this->counter;
}

public function updateCounter($counter)
{
$this->counter = $counter;
return $this;
}

private function hasSemicolon($value)
{
$semicolons = array(':', '%3A', '%3a');
foreach ($semicolons as $semicolon) {
if (false !== strpos($value, $semicolon)) {
return true;
}
}

return false;
}
}
121 changes: 121 additions & 0 deletions examples/TOTP.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php

namespace MyProject;

use OTPHP\TOTP as BaseTOTP;

class TOTP extends BaseTOTP
{
protected $secret = null;
protected $issuer = null;
protected $issuer_included_as_parameter = false;
protected $label = null;
protected $digest = 'sha1';
protected $digits = 6;
protected $interval = 30;

public function setSecret($secret)
{
//You must check that the secret is a valid Base32 string
$this->secret = $secret;
return $this;
}

public function getSecret()
{
return $this->secret;
}

public function setLabel($label)
{
if ($this->hasSemicolon($label)) {
throw new \Exception("Label must not containt a semi-colon.");
}
$this->label = $label;
return $this;
}

public function getLabel()
{
return $this->label;
}

public function setIssuer($issuer)
{
if ($this->hasSemicolon($issuer)) {
throw new \Exception("Issuer must not containt a semi-colon.");
}
$this->issuer = $issuer;
return $this;
}

public function getIssuer()
{
return $this->issuer;
}

public function isIssuerIncludedAsParameter()
{
return $this->issuer_included_as_parameter;
}

public function setIssuerIncludedAsParameter($issuer_included_as_parameter)
{
$this->issuer_included_as_parameter = $issuer_included_as_parameter;
return $this;
}

public function setDigits($digits)
{
if( !is_numeric($digits) || $digits < 1 ) {
throw new \Exception("Digits must be at least 1.");
}
$this->digits = $digits;
return $this;
}

public function getDigits()
{
return $this->digits;
}

public function setDigest($digest)
{
if( !in_array($digest, array('md5', 'sha1', 'sha256', 'sha512')) ) {
throw new \Exception("'$digest' digest is not supported.");
}
$this->digest = $digest;
return $this;
}

public function getDigest()
{
return $this->digest;
}

public function setInterval($interval)
{
if( !is_integer($interval) || $interval < 1 ) {
throw new \Exception("Interval must be at least 1.");
}
$this->interval = $interval;
return $this;
}

public function getInterval()
{
return $this->interval;
}

private function hasSemicolon($value)
{
$semicolons = array(':', '%3A', '%3a');
foreach ($semicolons as $semicolon) {
if (false !== strpos($value, $semicolon)) {
return true;
}
}

return false;
}
}
1 change: 1 addition & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
strict="true"
backupGlobals="false"
backupStaticAttributes="false"
convertErrorsToExceptions="true"
Expand Down
37 changes: 37 additions & 0 deletions tests/ExampleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace OTPHP;

use MyProject\TOTP;
use MyProject\HOTP;

class ExampleTest extends \PHPUnit_Framework_TestCase
{
public function testTotpExample()
{
$totp = new TOTP();
$totp->setLabel('[email protected]')
->setSecret('JDDK4U6G3BJLEZ7Y')
->setIssuer('My Project')
->setIssuerIncludedAsParameter(true)
->setDigest('sha512')
->setDigits(10)
->setInterval(60);

$this->assertEquals('otpauth://totp/My%20Project%3Aalice%40foo.bar?algorithm=sha512&digits=10&issuer=My%20Project&period=60&secret=JDDK4U6G3BJLEZ7Y', $totp->getProvisioningUri());
}

public function testHotpExample()
{
$totp = new HOTP();
$totp->setLabel('[email protected]')
->setSecret('JDDK4U6G3BJLEZ7Y')
->setIssuer('My Project')
->setIssuerIncludedAsParameter(true)
->setDigest('sha512')
->setDigits(10)
->setCounter(1000);

$this->assertEquals('otpauth://hotp/My%20Project%3Aalice%40foo.bar?algorithm=sha512&counter=1000&digits=10&issuer=My%20Project&secret=JDDK4U6G3BJLEZ7Y', $totp->getProvisioningUri());
}
}

0 comments on commit 71eb26f

Please sign in to comment.