Skip to content

Commit

Permalink
Bump minimum to 7.3, update deps, support PHP 8.0 (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
francislavoie authored Apr 20, 2021
1 parent 380b0f8 commit bf68799
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 21 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ jobs:
fail-fast: false
matrix:
operating-system: [ubuntu-latest]
php-versions: ['7.2', '7.3', '7.4']
# TODO: When bacon/bacon-qr-code supports PHP 8:
# php-versions: ['7.2', '7.3', '7.4', '8.0']
php-versions: ['7.3', '7.4', '8.0']

steps:
- name: Checkout
Expand All @@ -30,6 +28,7 @@ jobs:
php-version: ${{ matrix.php-versions }}
coverage: pcov
tools: composer:v2
extensions: gd
env:
COMPOSER_TOKEN: ${{ secrets.GITHUB_TOKEN }}

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ This is a fork of https://github.com/PHPGangsta/GoogleAuthenticator with the fol
- No longer generates Google's Chart API to make QR code links
- Uses namespacing
- Augmented test coverage to 100%
- Bumped minimum PHP version to 7.2
- Bumped minimum PHP version to 7.3

Original License:
-----------------
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
"issues": "https://github.com/Vectorface/GoogleAuthenticator/issues"
},
"require": {
"php": ">=7.2",
"endroid/qr-code": "^3.9.3"
"php": ">=7.3",
"endroid/qr-code": "^4.0.0"
},
"require-dev": {
"phpunit/phpunit": "^8"
"phpunit/phpunit": "^9"
},
"config": {
"sort-packages": true
Expand Down
25 changes: 12 additions & 13 deletions src/GoogleAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Vectorface;

use Endroid\QrCode\ErrorCorrectionLevel;
use Endroid\QrCode\QrCode;
use Endroid\QrCode\Builder\Builder;
use Endroid\QrCode\Writer\PngWriter;
use Exception;

/**
Expand Down Expand Up @@ -105,26 +105,25 @@ public function getCode(string $secret, int $timeSlice = null) : string
public function getQRCodeUrl(string $name, string $secret) : string
{
$uri = "otpauth://totp/$name?secret=$secret";
return 'data:image/png;base64,' . base64_encode($this->getQRCodeSRC($uri));
return $this->getQRCodeDataUri($uri);
}

/**
* Generate a QRCode for a given string
*
* @param string $uri to encode into a QRCode
* @return string binary data of the PNG of the QRCode
* @throws Exception
*/
protected function getQRCodeSRC(string $uri) : string
protected function getQRCodeDataUri(string $uri) : string
{
$qr_code = new QrCode($uri);
$qr_code->setSize(260);
$qr_code->setMargin(10);
$qr_code->setErrorCorrectionLevel(ErrorCorrectionLevel::LOW());
$qr_code->setForegroundColor(['r' => 0, 'g' => 0, 'b' => 0]);
$qr_code->setBackgroundColor(['r' => 255, 'g' => 255, 'b' => 255]);
$qr_code->setValidateResult(false);

return $qr_code->writeString();
return Builder::create()
->data($uri)
->writer(new PngWriter)
->size(260)
->margin(10)
->build()
->getDataUri();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/GoogleAuthenticatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public function testGetQRCodeUrl()
$this->assertStringStartsWith($prefix, $url);

$base64part = substr($url, strlen($prefix));
$this->assertRegExp("#^[a-zA-Z0-9/+]*={0,2}$#", $base64part);
$this->assertMatchesRegularExpression("#^[a-zA-Z0-9/+]*={0,2}$#", $base64part);
}

/**
Expand Down

0 comments on commit bf68799

Please sign in to comment.