Skip to content

Commit

Permalink
Add Oath::encodeKey to show secrets for manual setup without QR code
Browse files Browse the repository at this point in the history
  • Loading branch information
kelunik committed Jun 13, 2016
1 parent 7f6a5a8 commit 65da897
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ There's a [runnable demo](./examples/demo.php) contained in this repository.

```php
$oath = new Oath;

// this generates a key in binary format
$key = $oath->generateKey();

// store key for user
```

Expand All @@ -35,7 +38,12 @@ $key = $oath->generateKey();
```php
$oath = new Oath;
$key = "..."; // load user key from storage

// Use the URI to provide an easy to scan QR code
$uri = $oath->getUri($key);

// Alternatively display the key for manual input
$secret = $oath->encodeKey($key);
```

You can use your favourite JavaScript or PHP library to generate the QR code. For a working example, we're using [`qr.js`](http://neocotic.com/qr.js/).
Expand Down
12 changes: 10 additions & 2 deletions src/Oath.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ public function generateKey($length = 20) {
return random_bytes($length);
}

public function encodeKey($key) {
if (!is_string($key)) {
throw new \InvalidArgumentException("Key must be string");
}

return Base32::encode($key);
}

public function generateHotp($key, $counter) {
if (!is_string($key)) {
throw new \InvalidArgumentException("Key must be string");
Expand Down Expand Up @@ -121,7 +129,7 @@ public function getUri($key, $issuer, $account) {

return "otpauth://totp/" . urlencode($issuer) . ":" . urlencode($account) . "?" . http_build_query([
"algorithm" => "SHA1",
"secret" => Base32::encode($key),
"secret" => $this->encodeKey($key),
"digits" => $this->length,
"period" => $this->windowSize,
"issuer" => $issuer,
Expand Down Expand Up @@ -152,4 +160,4 @@ private function oathTruncate($rawHmac) {
// And extract HOTP value according to OTP_LENGTH
return ($p[1] & 0x7FFFFFFF) % pow(10, $this->length);
}
}
}

0 comments on commit 65da897

Please sign in to comment.