-
-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Spomky
committed
Nov 2, 2014
1 parent
2667ac8
commit 71eb26f
Showing
7 changed files
with
298 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |