Skip to content
This repository has been archived by the owner on Dec 2, 2021. It is now read-only.

Commit

Permalink
Support server name is null
Browse files Browse the repository at this point in the history
  • Loading branch information
scheb committed Aug 25, 2015
1 parent d727197 commit ef6830d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 32 deletions.
11 changes: 5 additions & 6 deletions Security/TwoFactor/Provider/Google/GoogleAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,19 @@ public function checkCode(TwoFactorInterface $user, $code)
public function getUrl(TwoFactorInterface $user)
{
$encoder = "https://chart.googleapis.com/chart?chs=200x200&chld=M|0&cht=qr&chl=";
$userAndHost = rawurlencode($user->getUsername()) . ($this->server ? '@' . rawurlencode($this->server) : '');
if ($this->issuer) {
$encoderURL = sprintf(
"otpauth://totp/%s:%s@%s?secret=%s&issuer=%s",
"otpauth://totp/%s:%s?secret=%s&issuer=%s",
rawurlencode($this->issuer),
rawurlencode($user->getUsername()),
rawurlencode($this->server),
$userAndHost,
$user->getGoogleAuthenticatorSecret(),
rawurlencode($this->issuer)
);
} else {
$encoderURL = sprintf(
"otpauth://totp/%s@%s?secret=%s",
rawurlencode($user->getUsername()),
rawurlencode($this->server),
"otpauth://totp/%s?secret=%s",
$userAndHost,
$user->getGoogleAuthenticatorSecret()
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ public function setUp()
}

/**
* @param string|null $hostname
* @param string|null $issuer
* @return \Scheb\TwoFactorBundle\Security\TwoFactor\Provider\Google\GoogleAuthenticator
*/
private function createAuthenticator($issuer = null)
private function createAuthenticator($hostname = null, $issuer = null)
{
return new GoogleAuthenticator($this->google, "Hostname", $issuer);
return new GoogleAuthenticator($this->google, $hostname, $issuer);
}

/**
Expand Down Expand Up @@ -66,46 +67,33 @@ public function getCheckCodeData()

/**
* @test
* @dataProvider getHostnameAndIssuerToTest
*/
public function getUrl_createQrCodeUrl_returnUrl()
public function getUrl_createQrCodeUrl_returnUrl($hostname, $issuer, $expectedUrl)
{
//Mock the user object
$user = $this->getMock("Scheb\TwoFactorBundle\Model\Google\TwoFactorInterface");
$user
->expects($this->once())
->method("getUsername")
->will($this->returnValue("Username"));
->will($this->returnValue("User name"));
$user
->expects($this->once())
->method("getGoogleAuthenticatorSecret")
->will($this->returnValue("SECRET"));

$authenticator = $this->createAuthenticator();
$authenticator = $this->createAuthenticator($hostname, $issuer);
$returnValue = $authenticator->getUrl($user);
$expectedUrl = 'https://chart.googleapis.com/chart?chs=200x200&chld=M|0&cht=qr&chl=otpauth%3A%2F%2Ftotp%2FUsername%40Hostname%3Fsecret%3DSECRET';
$this->assertEquals($expectedUrl, $returnValue);
}

/**
* @test
*/
public function getUrl_createQrCodeUrlWithIssuer_returnUrl()
{
//Mock the user object
$user = $this->getMock("Scheb\TwoFactorBundle\Model\Google\TwoFactorInterface");
$user
->expects($this->once())
->method("getUsername")
->will($this->returnValue("User name"));
$user
->expects($this->once())
->method("getGoogleAuthenticatorSecret")
->will($this->returnValue("SECRET"));

$authenticator = $this->createAuthenticator('Issuer Name');
$returnValue = $authenticator->getUrl($user);
$expectedUrl = 'https://chart.googleapis.com/chart?chs=200x200&chld=M|0&cht=qr&chl=otpauth%3A%2F%2Ftotp%2FIssuer%2520Name%3AUser%2520name%40Hostname%3Fsecret%3DSECRET%26issuer%3DIssuer%2520Name';
$this->assertEquals($expectedUrl, $returnValue);
public function getHostnameAndIssuerToTest() {
return array(
array(null, null, 'https://chart.googleapis.com/chart?chs=200x200&chld=M|0&cht=qr&chl=otpauth%3A%2F%2Ftotp%2FUser%2520name%3Fsecret%3DSECRET'),
array('Hostname', null, 'https://chart.googleapis.com/chart?chs=200x200&chld=M|0&cht=qr&chl=otpauth%3A%2F%2Ftotp%2FUser%2520name%40Hostname%3Fsecret%3DSECRET'),
array(null, 'Issuer Name', 'https://chart.googleapis.com/chart?chs=200x200&chld=M|0&cht=qr&chl=otpauth%3A%2F%2Ftotp%2FIssuer%2520Name%3AUser%2520name%3Fsecret%3DSECRET%26issuer%3DIssuer%2520Name'),
array('Hostname', 'Issuer Name', 'https://chart.googleapis.com/chart?chs=200x200&chld=M|0&cht=qr&chl=otpauth%3A%2F%2Ftotp%2FIssuer%2520Name%3AUser%2520name%40Hostname%3Fsecret%3DSECRET%26issuer%3DIssuer%2520Name'),
);
}

/**
Expand Down

0 comments on commit ef6830d

Please sign in to comment.