Skip to content

Commit 38f349f

Browse files
committed
Merge remote-tracking branch 'igorsantos/qr-logos'
2 parents 3e9d402 + cc2ae19 commit 38f349f

File tree

5 files changed

+50
-7
lines changed

5 files changed

+50
-7
lines changed

README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ PHP library for [two-factor (or multi-factor) authentication](http://en.wikipedi
1616

1717
Optionally, you may need:
1818

19-
* [endroid/qr-code](https://github.com/endroid/qr-code) if using `EndroidQrCodeProvider`.
19+
* [endroid/qr-code](https://github.com/endroid/qr-code) if using `EndroidQrCodeProvider` or `EndroidQrCodeWithLogoProvider`.
2020
* [bacon/bacon-qr-code](https://github.com/Bacon/BaconQrCode) if using `BaconQrCodeProvider`.
2121

2222
## Installation
@@ -73,7 +73,8 @@ Another, more user-friendly, way to get the shared secret into the app is to gen
7373
2. `ImageChartsQRCodeProvider`
7474
3. `QRicketProvider`
7575
4. `EndroidQrCodeProvider` (requires `endroid/qr-code` to be installed)
76-
5. `BaconQrCodeProvider` (requires `bacon/bacon-qr-code` to be installed)
76+
5. `EndroidQrCodeWithLogoProvider` (same, but supporting embedded images)
77+
6. `BaconQrCodeProvider` (requires `bacon/bacon-qr-code` to be installed)
7778

7879
...or implement your own provider. To implement your own provider all you need to do is implement the `IQRCodeProvider` interface. You can use the built-in providers mentioned before to serve as an example or read the next chapter in this file. The built-in classes all use a 3rd (e.g. external) party (Image-charts, QRServer and QRicket) for the hard work of generating QR-codes (note: each of these services might at some point not be available or impose limitations to the number of codes generated per day, hour etc.). You could, however, easily use a project like [PHP QR Code](http://phpqrcode.sourceforge.net/) (or one of the [many others](https://packagist.org/search/?q=qr)) to generate your QR-codes without depending on external sources. Later on we'll [demonstrate](#qr-code-providers) how to do this.
7980

@@ -96,7 +97,9 @@ When the shared secret is added to the app, the app will be ready to start gener
9697
$result = $tfa->verifyCode($_SESSION['secret'], $_POST['verification']);
9798
````
9899

99-
`verifyCode()` will return either `true` (the code was valid) or `false` (the code was invalid; no points for you!). You may need to store `$secret` in a `$_SESSION` or other persistent storage between requests. The `verifyCode()` accepts, aside from `$secret` and `$code`, three more arguments. The first being `$discrepancy`. Since TOTP codes are based on time("slices") it is very important that the server (but also client) have a correct date/time. But because the two *may* differ a bit we usually allow a certain amount of leeway. Because generated codes are valid for a specific period (remember the `$period` argument in the `TwoFactorAuth`'s constructor?) we usually check the period directly before and the period directly after the current time when validating codes. So when the current time is `14:34:21`, which results in a 'current timeslice' of `14:34:00` to `14:34:30` we also calculate/verify the codes for `14:33:30` to `14:34:00` and for `14:34:30` to `14:35:00`. This gives us a 'window' of `14:33:30` to `14:35:00`. The `$discrepancy` argument specifies how many periods (or: timeslices) we check in either direction of the current time. The default `$discrepancy` of `1` results in (max.) 3 period checks: -1, current and +1 period. A `$discrepancy` of `4` would result in a larger window (or: bigger time difference between client and server) of -4, -3, -2, -1, current, +1, +2, +3 and +4 periods.
100+
If you do extra validations with your `$_POST` values, just make sure the code is still submitted as string - even if that's a numeric code, casting it to integer is unreliable. Also, you may need to store `$secret` in a `$_SESSION` or other persistent storage between requests. `verifyCode()` will return either `true` (the code was valid) or `false` (the code was invalid; no points for you!).
101+
102+
The `verifyCode()` accepts, aside from `$secret` and `$code`, three more arguments, with the first being `$discrepancy`. Since TOTP codes are based on time("slices") it is very important that the server (but also client) have a correct date/time. But because the two *may* differ a bit we usually allow a certain amount of leeway. Because generated codes are valid for a specific period (remember the `$period` argument in the `TwoFactorAuth`'s constructor?) we usually check the period directly before and the period directly after the current time when validating codes. So when the current time is `14:34:21`, which results in a 'current timeslice' of `14:34:00` to `14:34:30` we also calculate/verify the codes for `14:33:30` to `14:34:00` and for `14:34:30` to `14:35:00`. This gives us a 'window' of `14:33:30` to `14:35:00`. The `$discrepancy` argument specifies how many periods (or: timeslices) we check in either direction of the current time. The default `$discrepancy` of `1` results in (max.) 3 period checks: -1, current and +1 period. A `$discrepancy` of `4` would result in a larger window (or: bigger time difference between client and server) of -4, -3, -2, -1, current, +1, +2, +3 and +4 periods.
100103

101104
The second, `$time`, allows you to check a code for a specific point in time. This argument has no real practical use but can be handy for unittesting etc. The default value, `null`, means: use the current time.
102105

@@ -128,7 +131,7 @@ public function verifyCode($secret, $code, $discrepancy = 1, $time = null): bool
128131

129132
As mentioned before, this library comes with five 'built-in' QR-code providers. This chapter will touch the subject a bit but most of it should be self-explanatory. The `TwoFactorAuth`-class accepts a `$qrcodeprovider` argument which lets you specify a built-in or custom QR-code provider. All five built-in providers do a simple HTTP request to retrieve an image using cURL and implement the [`IQRCodeProvider`](lib/Providers/Qr/IQRCodeProvider.php) interface which is all you need to implement to write your own QR-code provider.
130133

131-
The default provider is the [`QRServerProvider`](lib/Providers/Qr/QRServerProvider.php) which uses the [goqr.me API](http://goqr.me/api/doc/create-qr-code/) to render QR-codes. Then we have the [`ImageChartsQRCodeProvider`](lib/Providers/Qr/ImageChartsQRCodeProvider.php) which uses the [image-charts.com replacement for Google Image Charts](https://image-charts.com) to render QR-codes and the [`QRicketProvider`](lib/Providers/Qr/QRicketProvider.php) which uses the [QRickit API](http://qrickit.com/qrickit_apps/qrickit_api.php). These three providers all inherit from a common (abstract) baseclass named [`BaseHTTPQRCodeProvider`](lib/Providers/Qr/BaseHTTPQRCodeProvider.php) because all three share the same functionality: retrieve an image from a 3rd party over HTTP. Finally, we have [`EndroidQrCodeProvider`](lib/Providers/Qr/EndroidQrCodeProvider.php) and [`BaconQrCodeProvider`](lib/Providers/Qr/BaconQrCodeProvider.php) which require an optional dependency to be installed to use (see Requirements section above), but will generate the QR codes locally. All five classes have constructors that allow you to tweak some settings and most, if not all, arguments should speak for themselves. If you're not sure which values are supported, click the links in this paragraph for documentation on the API's that are utilized by these classes.
134+
The default provider is the [`QRServerProvider`](lib/Providers/Qr/QRServerProvider.php) which uses the [goqr.me API](http://goqr.me/api/doc/create-qr-code/) to render QR-codes. Then we have the [`ImageChartsQRCodeProvider`](lib/Providers/Qr/ImageChartsQRCodeProvider.php) which uses the [image-charts.com replacement for Google Image Charts](https://image-charts.com) to render QR-codes and the [`QRicketProvider`](lib/Providers/Qr/QRicketProvider.php) which uses the [QRickit API](http://qrickit.com/qrickit_apps/qrickit_api.php). These three providers all inherit from a common (abstract) baseclass named [`BaseHTTPQRCodeProvider`](lib/Providers/Qr/BaseHTTPQRCodeProvider.php) because all three share the same functionality: retrieve an image from a 3rd party over HTTP. Finally, we have [`EndroidQrCodeProvider`](lib/Providers/Qr/EndroidQrCodeProvider.php), [`EndroidQrCodeWithLogoProvider`](lib/Providers/Qr/EndroidQrCodeWithLogoProvider.php) and [`BaconQrCodeProvider`](lib/Providers/Qr/BaconQrCodeProvider.php) which require an optional dependency to be installed to use (see Requirements section above), but will generate the QR codes locally. All five classes have constructors that allow you to tweak some settings and most, if not all, arguments should speak for themselves. If you're not sure which values are supported, click the links in this paragraph for documentation on the API's that are utilized by these classes.
132135

133136
If you don't like any of the built-in classes because you don't want to rely on external resources for example or because you're paranoid about sending the TOTP secret to these 3rd parties (which is useless to them since they miss *at least one* other factor in the [MFA process](http://en.wikipedia.org/wiki/Multi-factor_authentication)), feel tree to implement your own. The `IQRCodeProvider` interface couldn't be any simpler. All you need to do is implement 2 methods:
134137

lib/Providers/Qr/EndroidQrCodeProvider.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ public function getMimeType()
2525
}
2626

2727
public function getQRCodeImage($qrtext, $size)
28+
{
29+
return $this->qrCodeInstance($qrtext, $size)->writeString();
30+
}
31+
32+
protected function qrCodeInstance($qrtext, $size)
2833
{
2934
$qrCode = new QrCode($qrtext);
3035
$qrCode->setSize($size);
@@ -34,7 +39,7 @@ public function getQRCodeImage($qrtext, $size)
3439
$qrCode->setBackgroundColor($this->bgcolor);
3540
$qrCode->setForegroundColor($this->color);
3641

37-
return $qrCode->writeString();
42+
return $qrCode;
3843
}
3944

4045
private function handleColor($color)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
namespace RobThree\Auth\Providers\Qr;
3+
4+
use Endroid\QrCode\ErrorCorrectionLevel;
5+
use Endroid\QrCode\QrCode;
6+
7+
class EndroidQrCodeWithLogoProvider extends EndroidQrCodeProvider
8+
{
9+
protected $logoPath;
10+
protected $logoSize;
11+
12+
/**
13+
* Adds an image to the middle of the QR Code.
14+
* @param string $path Path to an image file
15+
* @param array|int $size Just the width, or [width, height]
16+
*/
17+
public function setLogo($path, $size = null)
18+
{
19+
$this->logoPath = $path;
20+
$this->logoSize = (array)$size;
21+
}
22+
23+
protected function qrCodeInstance($qrtext, $size) {
24+
$qrCode = parent::qrCodeInstance($qrtext, $size);
25+
26+
if ($this->logoPath) {
27+
$qrCode->setLogoPath($this->logoPath);
28+
if ($this->logoSize) {
29+
$qrCode->setLogoSize($this->logoSize[0], $this->logoSize[1]);
30+
}
31+
}
32+
33+
return $qrCode;
34+
}
35+
}

lib/TwoFactorAuth.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public function getCode($secret, $time = null)
143143
*/
144144
public function verifyCode($secret, $code, $discrepancy = 1, $time = null, &$timeslice = 0)
145145
{
146-
$timetamp = $this->getTime($time);
146+
$timestamp = $this->getTime($time);
147147

148148
$timeslice = 0;
149149

@@ -152,7 +152,7 @@ public function verifyCode($secret, $code, $discrepancy = 1, $time = null, &$tim
152152
// of the match. Each iteration we either set the timeslice variable to the timeslice of the match
153153
// or set the value to itself. This is an effort to maintain constant execution time for the code.
154154
for ($i = -$discrepancy; $i <= $discrepancy; $i++) {
155-
$ts = $timetamp + ($i * $this->period);
155+
$ts = $timestamp + ($i * $this->period);
156156
$slice = $this->getTimeSlice($ts);
157157
$timeslice = $this->codeEquals($this->getCode($secret, $ts), $code) ? $slice : $timeslice;
158158
}

phpunit.xml.tmppica

Whitespace-only changes.

0 commit comments

Comments
 (0)