Skip to content

Instantly share code, notes, and snippets.

@Spomky
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Spomky/c2aee6ee061817f8102b to your computer and use it in GitHub Desktop.
Save Spomky/c2aee6ee061817f8102b to your computer and use it in GitHub Desktop.
{
"name": "test",
"description": "TOTP test",
"require": {
"spomky-labs/otphp": "~4.0"
}
}
<?php
require_once dirname(__FILE__).'/vendor/autoload.php';
use OTPHP\TOTP;
class MyOTP extends TOTP
{
protected $secret = null;
protected $issuer = null;
protected $issuer_included_as_parameter = false;
protected $label = null;
protected $digest = 'sha1';
protected $digits = 6;
protected $interval = 30;
/**
* @param string $secret
*/
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;
}
/**
* @param string $label
*/
public function setLabel($label)
{
if ($this->hasSemicolon($label)) {
throw new \Exception("Label must not contain a semi-colon.");
}
$this->label = $label;
return $this;
}
public function getLabel()
{
return $this->label;
}
/**
* @param string $issuer
*/
public function setIssuer($issuer)
{
if ($this->hasSemicolon($issuer)) {
throw new \Exception("Issuer must not contain a semi-colon.");
}
$this->issuer = $issuer;
return $this;
}
public function getIssuer()
{
return $this->issuer;
}
public function isIssuerIncludedAsParameter()
{
return $this->issuer_included_as_parameter;
}
/**
* @param boolean $issuer_included_as_parameter
*/
public function setIssuerIncludedAsParameter($issuer_included_as_parameter)
{
$this->issuer_included_as_parameter = $issuer_included_as_parameter;
return $this;
}
/**
* @param integer $digits
*/
public function setDigits($digits)
{
if (!is_integer($digits) || $digits < 1) {
throw new \Exception("Digits must be at least 1.");
}
$this->digits = $digits;
return $this;
}
public function getDigits()
{
return $this->digits;
}
/**
* @param string $digest
*/
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;
}
/**
* @param integer $interval
*/
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;
}
}
$test = new MyOtp();
$test->setLabel("alice@google.com")
->setDigits(6)
->setDigest('sha1')
->setInterval(30)
->setSecret("JBSWY3DPEHPK3PXP");
echo sprintf("Provisioning URI: %s\n",$test->getProvisioningUri());
echo sprintf("Current OTP: %s\n",$test->now());
Execute the following lines in your terminal
php -r "readfile('https://getcomposer.org/installer');" | php
php composer.phar install
php example.php
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment