Skip to content

Commit

Permalink
Added the issuer to the URL encoder.
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Brecht-Precht committed Feb 1, 2017
1 parent ae526f0 commit ffac304
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,25 @@ class SharedSecretQrCodeProvider
*/
private $qrCode;

/**
* @var string
*/
private $issuer;

/**
* SecretQrCodeProvider constructor.
*
* @param SharedSecretUrlEncoderInterface $contentEncoder
* @param string $keyName
* @param string $secret
* @param string $issuer
*/
public function __construct(SharedSecretUrlEncoderInterface $contentEncoder, $keyName, $secret)
public function __construct(SharedSecretUrlEncoderInterface $contentEncoder, $keyName, $secret, $issuer = null)
{
$this->contentEncoder = $contentEncoder;
$this->keyName = $keyName;
$this->secret = $secret;
$this->issuer = $issuer;
$this->qrEncoder = new QrEncoder();
}

Expand All @@ -71,7 +78,7 @@ public function __construct(SharedSecretUrlEncoderInterface $contentEncoder, $ke
*/
public function provideQrCode($path)
{
$this->qrCodeContents = $this->contentEncoder->encode($this->keyName, $this->secret);
$this->qrCodeContents = $this->contentEncoder->encode($this->keyName, $this->secret, $this->issuer);
$this->qrCode = $this->qrEncoder->encodeQrCode($this->qrCodeContents);
$this->qrRenderer = new QrCodeRendererPng();
$this->qrRenderer->render($this->qrCode, $path);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ interface SharedSecretUrlEncoderInterface
/**
* @param string $keyName
* @param string $sharedSecret
* @param string $issuer
* @return string
*/
public function encode($keyName, $sharedSecret);
public function encode($keyName, $sharedSecret, $issuer=null);

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,18 @@ class HotpBase32SharedSecretUrlEncoder implements Base\SharedSecretUrlEncoderInt
/**
* @param string $keyName
* @param string $sharedSecret
* @param string $issuer
* @return string
*/
public function encode($keyName, $sharedSecret)
public function encode($keyName, $sharedSecret, $issuer = null)
{
$sharedSecret = bin2hex($sharedSecret);
$sharedSecret = Base32::encodeByteStr($sharedSecret, true);
return 'otpauth://hotp/' . rawurlencode($keyName) . '?secret=' . $sharedSecret;
$encoded = 'otpauth://hotp/' . rawurlencode($keyName) . '?secret=' . $sharedSecret;
if (!is_null($issuer)) {
$encoded .= '&issuer=' . rawurlencode($issuer);
}
return $encoded;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@ class HotpSharedSecretUrlEncoder implements Base\SharedSecretUrlEncoderInterface
/**
* @param string $keyName
* @param string $sharedSecret
* @param string $issuer
* @return string
*/
public function encode($keyName, $sharedSecret)
public function encode($keyName, $sharedSecret, $issuer = null)
{
$sharedSecret = bin2hex($sharedSecret);
return 'otpauth://hotp/' . rawurlencode($keyName) . '?secret=' . $sharedSecret;
$encoded = 'otpauth://hotp/' . rawurlencode($keyName) . '?secret=' . $sharedSecret;
if (!is_null($issuer)) {
$encoded .= '&issuer=' . $issuer;
}
return $encoded;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,18 @@ class TotpBase32SharedSecretUrlEncoder implements Base\SharedSecretUrlEncoderInt
/**
* @param string $keyName
* @param string $sharedSecret
* @param string $issuer
* @return string
*/
public function encode($keyName, $sharedSecret)
public function encode($keyName, $sharedSecret, $issuer = null)
{
$sharedSecret = bin2hex($sharedSecret);
$sharedSecret = Base32::encodeByteStr($sharedSecret, true);
return 'otpauth://totp/' . rawurlencode($keyName) . '?secret=' . $sharedSecret;
$encoded = 'otpauth://totp/' . rawurlencode($keyName) . '?secret=' . $sharedSecret;
if (!is_null($issuer)) {
$encoded .= '&issuer=' . $issuer;
}
return $encoded;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@ class TotpSharedSecretUrlEncoder implements Base\SharedSecretUrlEncoderInterface
/**
* @param string $keyName
* @param string $sharedSecret
* @param string $issuer
* @return string
*/
public function encode($keyName, $sharedSecret)
public function encode($keyName, $sharedSecret, $issuer = null)
{
$sharedSecret = bin2hex($sharedSecret);
return 'otpauth://totp/' . rawurlencode($keyName) . '?secret=' . $sharedSecret;
$encoded = 'otpauth://totp/' . rawurlencode($keyName) . '?secret=' . $sharedSecret;
if (!is_null($issuer)) {
$encoded .= '&issuer=' . $issuer;
}
return $encoded;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ class SharedSecretQrCodeProviderTest extends \PHPUnit_Framework_TestCase
{

const SHARED_SECRET ='9nxnvWgVw5Ca2YLUIkou2CkV2K15QI';
const EXPECTED_HOTP_BASE32_SHARED_SECRET_URL = 'otpauth://hotp/Awesome%20Application?secret=GM4TMZJXHA3GKNZWGU3TMNZVGY3TOMZVGQZTMMJTGI2TSNDDGU2TIOJWMI3GMNZVGMZDIMZWMI2TMMZSGRRDGMJTGU2TCNBZ';
const EXPECTED_TOTP_BASE32_SHARED_SECRET_URL = 'otpauth://totp/Awesome%20Application?secret=GM4TMZJXHA3GKNZWGU3TMNZVGY3TOMZVGQZTMMJTGI2TSNDDGU2TIOJWMI3GMNZVGMZDIMZWMI2TMMZSGRRDGMJTGU2TCNBZ';
const EXPECTED_HOTP_SHARED_SECRET_URL = 'otpauth://hotp/Awesome%20Application?secret=396e786e765767567735436132594c55496b6f7532436b56324b31355149';
const EXPECTED_TOTP_SHARED_SECRET_URL = 'otpauth://totp/Awesome%20Application?secret=396e786e765767567735436132594c55496b6f7532436b56324b31355149';
const EXPECTED_HOTP_BASE32_SHARED_SECRET_URL = 'otpauth://hotp/Awesome%20Application?secret=GM4TMZJXHA3GKNZWGU3TMNZVGY3TOMZVGQZTMMJTGI2TSNDDGU2TIOJWMI3GMNZVGMZDIMZWMI2TMMZSGRRDGMJTGU2TCNBZ&issuer=Markenwerk';
const EXPECTED_TOTP_BASE32_SHARED_SECRET_URL = 'otpauth://totp/Awesome%20Application?secret=GM4TMZJXHA3GKNZWGU3TMNZVGY3TOMZVGQZTMMJTGI2TSNDDGU2TIOJWMI3GMNZVGMZDIMZWMI2TMMZSGRRDGMJTGU2TCNBZ&issuer=Markenwerk';
const EXPECTED_HOTP_SHARED_SECRET_URL = 'otpauth://hotp/Awesome%20Application?secret=396e786e765767567735436132594c55496b6f7532436b56324b31355149&issuer=Markenwerk';
const EXPECTED_TOTP_SHARED_SECRET_URL = 'otpauth://totp/Awesome%20Application?secret=396e786e765767567735436132594c55496b6f7532436b56324b31355149&issuer=Markenwerk';

public function testHotpBase32ProvideQrCode()
{
// Init QR code renderer
$sharedSecretQrCodeProvider = new SharedSecretQrCodeProvider(new HotpBase32SharedSecretUrlEncoder(), 'Awesome Application', self::SHARED_SECRET);
$sharedSecretQrCodeProvider = new SharedSecretQrCodeProvider(new HotpBase32SharedSecretUrlEncoder(), 'Awesome Application', self::SHARED_SECRET, 'Markenwerk');
$sharedSecretQrCodeProvider->getQrEncoder()
->setLevel(QrEncoder::QR_CODE_LEVEL_LOW)
->setTempDir(__DIR__ . '/tmp/');
Expand All @@ -50,7 +50,7 @@ public function testHotpBase32ProvideQrCode()
public function testTotpBase32ProvideQrCode()
{
// Init QR code renderer
$sharedSecretQrCodeProvider = new SharedSecretQrCodeProvider(new TotpBase32SharedSecretUrlEncoder(), 'Awesome Application', self::SHARED_SECRET);
$sharedSecretQrCodeProvider = new SharedSecretQrCodeProvider(new TotpBase32SharedSecretUrlEncoder(), 'Awesome Application', self::SHARED_SECRET, 'Markenwerk');
$sharedSecretQrCodeProvider->getQrEncoder()
->setLevel(QrEncoder::QR_CODE_LEVEL_LOW)
->setTempDir(__DIR__ . '/tmp/');
Expand All @@ -75,7 +75,7 @@ public function testTotpBase32ProvideQrCode()
public function testHotpProvideQrCode()
{
// Init QR code renderer
$sharedSecretQrCodeProvider = new SharedSecretQrCodeProvider(new HotpSharedSecretUrlEncoder(), 'Awesome Application', self::SHARED_SECRET);
$sharedSecretQrCodeProvider = new SharedSecretQrCodeProvider(new HotpSharedSecretUrlEncoder(), 'Awesome Application', self::SHARED_SECRET, 'Markenwerk');
$sharedSecretQrCodeProvider->getQrEncoder()
->setLevel(QrEncoder::QR_CODE_LEVEL_LOW)
->setTempDir(__DIR__ . '/tmp/');
Expand All @@ -100,7 +100,7 @@ public function testHotpProvideQrCode()
public function testTotpProvideQrCode()
{
// Init QR code renderer
$sharedSecretQrCodeProvider = new SharedSecretQrCodeProvider(new TotpSharedSecretUrlEncoder(), 'Awesome Application', self::SHARED_SECRET);
$sharedSecretQrCodeProvider = new SharedSecretQrCodeProvider(new TotpSharedSecretUrlEncoder(), 'Awesome Application', self::SHARED_SECRET, 'Markenwerk');
$sharedSecretQrCodeProvider->getQrEncoder()
->setLevel(QrEncoder::QR_CODE_LEVEL_LOW)
->setTempDir(__DIR__ . '/tmp/');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,36 @@ class SharedSecretUrlEncoderTest extends \PHPUnit_Framework_TestCase
{

const SHARED_SECRET ='9nxnvWgVw5Ca2YLUIkou2CkV2K15QI';
const EXPECTED_HOTP_BASE32_SHARED_SECRET_URL = 'otpauth://hotp/Awesome%20Application?secret=GM4TMZJXHA3GKNZWGU3TMNZVGY3TOMZVGQZTMMJTGI2TSNDDGU2TIOJWMI3GMNZVGMZDIMZWMI2TMMZSGRRDGMJTGU2TCNBZ';
const EXPECTED_TOTP_BASE32_SHARED_SECRET_URL = 'otpauth://totp/Awesome%20Application?secret=GM4TMZJXHA3GKNZWGU3TMNZVGY3TOMZVGQZTMMJTGI2TSNDDGU2TIOJWMI3GMNZVGMZDIMZWMI2TMMZSGRRDGMJTGU2TCNBZ';
const EXPECTED_HOTP_SHARED_SECRET_URL = 'otpauth://hotp/Awesome%20Application?secret=396e786e765767567735436132594c55496b6f7532436b56324b31355149';
const EXPECTED_TOTP_SHARED_SECRET_URL = 'otpauth://totp/Awesome%20Application?secret=396e786e765767567735436132594c55496b6f7532436b56324b31355149';
const EXPECTED_HOTP_BASE32_SHARED_SECRET_URL = 'otpauth://hotp/Awesome%20Application?secret=GM4TMZJXHA3GKNZWGU3TMNZVGY3TOMZVGQZTMMJTGI2TSNDDGU2TIOJWMI3GMNZVGMZDIMZWMI2TMMZSGRRDGMJTGU2TCNBZ&issuer=Markenwerk';
const EXPECTED_TOTP_BASE32_SHARED_SECRET_URL = 'otpauth://totp/Awesome%20Application?secret=GM4TMZJXHA3GKNZWGU3TMNZVGY3TOMZVGQZTMMJTGI2TSNDDGU2TIOJWMI3GMNZVGMZDIMZWMI2TMMZSGRRDGMJTGU2TCNBZ&issuer=Markenwerk';
const EXPECTED_HOTP_SHARED_SECRET_URL = 'otpauth://hotp/Awesome%20Application?secret=396e786e765767567735436132594c55496b6f7532436b56324b31355149&issuer=Markenwerk';
const EXPECTED_TOTP_SHARED_SECRET_URL = 'otpauth://totp/Awesome%20Application?secret=396e786e765767567735436132594c55496b6f7532436b56324b31355149&issuer=Markenwerk';

public function testHotpBase32UrlEncode()
{
$sharedSecretUrlEncoder = new HotpBase32SharedSecretUrlEncoder();
$sharedSecretUrl = $sharedSecretUrlEncoder->encode('Awesome Application', self::SHARED_SECRET);
$sharedSecretUrl = $sharedSecretUrlEncoder->encode('Awesome Application', self::SHARED_SECRET, 'Markenwerk');
$this->assertEquals(self::EXPECTED_HOTP_BASE32_SHARED_SECRET_URL, $sharedSecretUrl);
}

public function testTotpBase32UrlEncode()
{
$sharedSecretUrlEncoder = new TotpBase32SharedSecretUrlEncoder();
$sharedSecretUrl = $sharedSecretUrlEncoder->encode('Awesome Application', self::SHARED_SECRET);
$sharedSecretUrl = $sharedSecretUrlEncoder->encode('Awesome Application', self::SHARED_SECRET, 'Markenwerk');
$this->assertEquals(self::EXPECTED_TOTP_BASE32_SHARED_SECRET_URL, $sharedSecretUrl);
}

public function testHotpUrlEncode()
{
$sharedSecretUrlEncoder = new HotpSharedSecretUrlEncoder();
$sharedSecretUrl = $sharedSecretUrlEncoder->encode('Awesome Application', self::SHARED_SECRET);
$sharedSecretUrl = $sharedSecretUrlEncoder->encode('Awesome Application', self::SHARED_SECRET, 'Markenwerk');
$this->assertEquals(self::EXPECTED_HOTP_SHARED_SECRET_URL, $sharedSecretUrl);
}

public function testTotpUrlEncode()
{
$sharedSecretUrlEncoder = new TotpSharedSecretUrlEncoder();
$sharedSecretUrl = $sharedSecretUrlEncoder->encode('Awesome Application', self::SHARED_SECRET);
$sharedSecretUrl = $sharedSecretUrlEncoder->encode('Awesome Application', self::SHARED_SECRET, 'Markenwerk');
$this->assertEquals(self::EXPECTED_TOTP_SHARED_SECRET_URL, $sharedSecretUrl);
}

Expand Down

0 comments on commit ffac304

Please sign in to comment.