Skip to content

Commit

Permalink
Merge pull request #29 from steffenweber/patch-1
Browse files Browse the repository at this point in the history
Fix encoding of special characters in the label
  • Loading branch information
ChristianRiesen committed May 4, 2018
2 parents 54bb0f4 + 8a1ae40 commit 9b31f02
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/GoogleAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public static function getKeyUri($type, $label, $secret, $counter = null, $optio
}

// This is the base, these are at least required
$otpauth = 'otpauth://' . $type . '/' . str_replace(array(':', ' '), array('%3A', '%20'), $label) . '?secret=' . rawurlencode($secret);
$otpauth = 'otpauth://' . $type . '/' . rawurlencode($label) . '?secret=' . rawurlencode($secret);

if ($type == 'hotp' && !is_null($counter)) {
$otpauth .= '&counter=' . intval($counter);
Expand Down
16 changes: 8 additions & 8 deletions tests/GoogleAuthenticatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ public function testGetQrCodeUrl()

// Standard totp case
$this->assertEquals(
'https://chart.googleapis.com/chart?chs=200x200&cht=qr&chld=M|0&chl=otpauth%3A%2F%2Ftotp%2Fuser%40host.com%3Fsecret%3DMEP3EYVA6XNFNVNM',
'https://chart.googleapis.com/chart?chs=200x200&cht=qr&chld=M|0&chl=otpauth%3A%2F%2Ftotp%2Fuser%2540host.com%3Fsecret%3DMEP3EYVA6XNFNVNM',
GoogleAuthenticator::getQrCodeUrl('totp', 'user@host.com', $secret)
);

// hotp (include a counter)
$this->assertEquals(
'https://chart.googleapis.com/chart?chs=200x200&cht=qr&chld=M|0&chl=otpauth%3A%2F%2Fhotp%2Fuser%40host.com%3Fsecret%3DMEP3EYVA6XNFNVNM%26counter%3D1234',
'https://chart.googleapis.com/chart?chs=200x200&cht=qr&chld=M|0&chl=otpauth%3A%2F%2Fhotp%2Fuser%2540host.com%3Fsecret%3DMEP3EYVA6XNFNVNM%26counter%3D1234',
GoogleAuthenticator::getQrCodeUrl('hotp', 'user@host.com', $secret, 1234)
);

// totp, this time with a parameter for chaning the size of the QR
$this->assertEquals(
'https://chart.googleapis.com/chart?chs=300x300&cht=qr&chld=M|0&chl=otpauth%3A%2F%2Ftotp%2Fuser%40host.com%3Fsecret%3DMEP3EYVA6XNFNVNM',
'https://chart.googleapis.com/chart?chs=300x300&cht=qr&chld=M|0&chl=otpauth%3A%2F%2Ftotp%2Fuser%2540host.com%3Fsecret%3DMEP3EYVA6XNFNVNM',
GoogleAuthenticator::getQrCodeUrl('totp', 'user@host.com', $secret, null, array('height' => 300, 'width' => 300))
);

Expand All @@ -46,31 +46,31 @@ public function testGetKeyUri()

// Standard totp case
$this->assertEquals(
'otpauth://totp/user@host.com?secret=MEP3EYVA6XNFNVNM',
'otpauth://totp/user%40host.com?secret=MEP3EYVA6XNFNVNM',
GoogleAuthenticator::getKeyUri('totp', 'user@host.com', $secret)
);

// hotp (include a counter)
$this->assertEquals(
'otpauth://hotp/user@host.com?secret=MEP3EYVA6XNFNVNM&counter=1234',
'otpauth://hotp/user%40host.com?secret=MEP3EYVA6XNFNVNM&counter=1234',
GoogleAuthenticator::getKeyUri('hotp', 'user@host.com', $secret, 1234)
);

// totp/hotp with an issuer in the label
$this->assertEquals(
'otpauth://hotp/issuer%3Auser@host.com?secret=MEP3EYVA6XNFNVNM&counter=1234',
'otpauth://hotp/issuer%3Auser%40host.com?secret=MEP3EYVA6XNFNVNM&counter=1234',
GoogleAuthenticator::getKeyUri('hotp', 'issuer:user@host.com', $secret, 1234)
);

// totp/hotp with an issuer and spaces in the label
$this->assertEquals(
'otpauth://hotp/an%20issuer%3A%20user@host.com?secret=MEP3EYVA6XNFNVNM&counter=1234',
'otpauth://hotp/an%20issuer%3A%20user%40host.com?secret=MEP3EYVA6XNFNVNM&counter=1234',
GoogleAuthenticator::getKeyUri('hotp', 'an issuer: user@host.com', $secret, 1234)
);

// totp/hotp with an issuer as option
$this->assertEquals(
'otpauth://hotp/an%20issuer%3Auser@host.com?secret=MEP3EYVA6XNFNVNM&counter=1234&issuer=an%20issuer',
'otpauth://hotp/an%20issuer%3Auser%40host.com?secret=MEP3EYVA6XNFNVNM&counter=1234&issuer=an%20issuer',
GoogleAuthenticator::getKeyUri('hotp', 'an issuer:user@host.com', $secret, 1234, array('issuer' => 'an issuer'))
);
}
Expand Down

0 comments on commit 9b31f02

Please sign in to comment.