Skip to content

Commit 380b0f8

Browse files
Bump minimum to 7.2, update deps, use GitHub Actions (#1)
1 parent 9acf122 commit 380b0f8

File tree

7 files changed

+123
-95
lines changed

7 files changed

+123
-95
lines changed

.github/workflows/ci.yml

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
name: Test
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
pull_request:
8+
branches:
9+
- master
10+
11+
jobs:
12+
test:
13+
name: PHP ${{ matrix.php-versions }} on ${{ matrix.operating-system }}
14+
runs-on: ${{ matrix.operating-system }}
15+
strategy:
16+
fail-fast: false
17+
matrix:
18+
operating-system: [ubuntu-latest]
19+
php-versions: ['7.2', '7.3', '7.4']
20+
# TODO: When bacon/bacon-qr-code supports PHP 8:
21+
# php-versions: ['7.2', '7.3', '7.4', '8.0']
22+
23+
steps:
24+
- name: Checkout
25+
uses: actions/checkout@v2
26+
27+
- name: Setup PHP, with composer and extensions
28+
uses: shivammathur/setup-php@v2 #https://github.com/shivammathur/setup-php
29+
with:
30+
php-version: ${{ matrix.php-versions }}
31+
coverage: pcov
32+
tools: composer:v2
33+
env:
34+
COMPOSER_TOKEN: ${{ secrets.GITHUB_TOKEN }}
35+
36+
- name: Get composer cache directory
37+
id: composercache
38+
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
39+
40+
- name: Setup problem matchers
41+
run: |
42+
echo "::add-matcher::${{ runner.tool_cache }}/php.json"
43+
echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"
44+
45+
- name: Cache composer dependencies
46+
uses: actions/cache@v2
47+
with:
48+
path: ${{ steps.composercache.outputs.dir }}
49+
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
50+
restore-keys: ${{ runner.os }}-composer-
51+
52+
- name: Install Composer dependencies
53+
run: composer install --prefer-dist
54+
55+
- name: Test with phpunit
56+
run: composer test

.travis.yml

Lines changed: 0 additions & 23 deletions
This file was deleted.

README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
Google Authenticator (TOTP)
22
===========================
33

4-
[![Build Status](https://travis-ci.org/Vectorface/GoogleAuthenticator.png?branch=master)](https://travis-ci.org/Vectorface/GoogleAuthenticator)
4+
![Build Status](https://github.com/Vectorface/GoogleAuthenticator/workflows/Test/badge.svg)
55

66
This is a fork of https://github.com/PHPGangsta/GoogleAuthenticator with the following changes:
77

88
- Uses https://github.com/endroid/qr-code to generate QR code data URIs
99
- No longer generates Google's Chart API to make QR code links
1010
- Uses namespacing
1111
- Augmented test coverage to 100%
12-
- Bumped minimum PHP version to 5.6
12+
- Bumped minimum PHP version to 7.2
1313

1414
Original License:
1515
-----------------
@@ -36,21 +36,22 @@ See following example:
3636

3737
```php
3838
<?php
39-
require_once __DIR__ . 'vendor/autoload.php';
39+
require_once 'vendor/autoload.php';
4040

4141
use Vectorface\GoogleAuthenticator;
4242

4343
$ga = new GoogleAuthenticator();
4444
$secret = $ga->createSecret();
45-
echo "Secret is: ".$secret."\n\n";
45+
echo "Secret is: {$secret}\n\n";
4646

4747
$qrCodeUrl = $ga->getQRCodeUrl('Blog', $secret);
48-
echo "PNG Data URI for the QR-Code: ".$qrCodeUrl."\n\n";
48+
echo "PNG Data URI for the QR-Code: {$qrCodeUrl}\n\n";
4949

5050
$oneCode = $ga->getCode($secret);
5151
echo "Checking Code '$oneCode' and Secret '$secret':\n";
5252

53-
$checkResult = $ga->verifyCode($secret, $oneCode, 2); // 2 = 2*30sec clock tolerance
53+
// 2 = 2*30sec clock tolerance
54+
$checkResult = $ga->verifyCode($secret, $oneCode, 2);
5455
if ($checkResult) {
5556
echo 'OK';
5657
} else {

composer.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
{
22
"name": "vectorface/googleauthenticator",
33
"description": "Google Authenticator 2-factor authentication",
4-
"version": "1.0.0",
54
"type": "library",
65
"keywords": ["GoogleAuthenticator", "TOTP", "rfc6238"],
76
"license": ["BSD-2-Clause"],
@@ -24,11 +23,11 @@
2423
"issues": "https://github.com/Vectorface/GoogleAuthenticator/issues"
2524
},
2625
"require": {
27-
"php": ">=5.6",
28-
"endroid/qr-code": "^2"
26+
"php": ">=7.2",
27+
"endroid/qr-code": "^3.9.3"
2928
},
3029
"require-dev": {
31-
"phpunit/phpunit": "^5 || ^6"
30+
"phpunit/phpunit": "^8"
3231
},
3332
"config": {
3433
"sort-packages": true

phpunit.xml.dist

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<phpunit backupGlobals="false" bootstrap="./vendor/autoload.php" colors="true">
3-
<testsuites>
4-
<testsuite name="default">
5-
<directory>./tests</directory>
6-
</testsuite>
7-
</testsuites>
8-
<filter>
9-
<whitelist>
10-
<directory>./src</directory>
11-
</whitelist>
12-
</filter>
13-
<logging>
14-
<log type="coverage-text" target="php://stdout"/>
15-
<log type="coverage-html" target="build/coverage"/>
16-
<log type="coverage-clover" target="build/logs/clover.xml"/>
17-
</logging>
3+
<testsuites>
4+
<testsuite name="default">
5+
<directory>./tests</directory>
6+
</testsuite>
7+
</testsuites>
8+
<filter>
9+
<whitelist>
10+
<directory>./src</directory>
11+
</whitelist>
12+
</filter>
13+
<logging>
14+
<log type="coverage-text" target="php://stdout"/>
15+
<log type="coverage-html" target="build/coverage"/>
16+
<log type="coverage-clover" target="build/logs/clover.xml"/>
17+
</logging>
1818
</phpunit>

src/GoogleAuthenticator.php

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class GoogleAuthenticator
2727
* @return string
2828
* @throws Exception
2929
*/
30-
public function createSecret($secretLength = 16)
30+
public function createSecret(int $secretLength = 16) : string
3131
{
3232
$validChars = self::base32LookupTable();
3333

@@ -40,11 +40,6 @@ public function createSecret($secretLength = 16)
4040
$rnd = false;
4141
if (function_exists('random_bytes')) {
4242
$rnd = random_bytes($secretLength);
43-
} elseif (function_exists('openssl_random_pseudo_bytes')) {
44-
$rnd = openssl_random_pseudo_bytes($secretLength, $cryptoStrong);
45-
if (!$cryptoStrong) {
46-
$rnd = false;
47-
}
4843
}
4944

5045
if (!$rnd) {
@@ -68,7 +63,7 @@ public function createSecret($secretLength = 16)
6863
* @return string
6964
* @throws Exception
7065
*/
71-
public function getCode($secret, $timeSlice = null)
66+
public function getCode(string $secret, int $timeSlice = null) : string
7267
{
7368
if ($timeSlice === null) {
7469
$timeSlice = floor(time() / 30);
@@ -107,7 +102,7 @@ public function getCode($secret, $timeSlice = null)
107102
* @return string
108103
* @throws Exception on encoding error
109104
*/
110-
public function getQRCodeUrl($name, $secret)
105+
public function getQRCodeUrl(string $name, string $secret) : string
111106
{
112107
$uri = "otpauth://totp/$name?secret=$secret";
113108
return 'data:image/png;base64,' . base64_encode($this->getQRCodeSRC($uri));
@@ -119,12 +114,12 @@ public function getQRCodeUrl($name, $secret)
119114
* @param string $uri to encode into a QRCode
120115
* @return string binary data of the PNG of the QRCode
121116
*/
122-
protected function getQRCodeSRC($uri)
117+
protected function getQRCodeSRC(string $uri) : string
123118
{
124119
$qr_code = new QrCode($uri);
125120
$qr_code->setSize(260);
126121
$qr_code->setMargin(10);
127-
$qr_code->setErrorCorrectionLevel(ErrorCorrectionLevel::LOW);
122+
$qr_code->setErrorCorrectionLevel(ErrorCorrectionLevel::LOW());
128123
$qr_code->setForegroundColor(['r' => 0, 'g' => 0, 'b' => 0]);
129124
$qr_code->setBackgroundColor(['r' => 255, 'g' => 255, 'b' => 255]);
130125
$qr_code->setValidateResult(false);
@@ -140,7 +135,7 @@ protected function getQRCodeSRC($uri)
140135
* @param int $discrepancy This is the allowed time drift in 30 second units (8 means 4 minutes before or after)
141136
* @return bool
142137
*/
143-
public function verifyCode($secret, $code, $discrepancy = 1)
138+
public function verifyCode(string $secret, string $code, int $discrepancy = 1) : bool
144139
{
145140
$currentTimeSlice = floor(time() / 30);
146141

@@ -151,7 +146,7 @@ public function verifyCode($secret, $code, $discrepancy = 1)
151146
for ($i = -$discrepancy; $i <= $discrepancy; $i++) {
152147
try {
153148
$calculatedCode = $this->getCode($secret, $currentTimeSlice + $i);
154-
} catch (\Exception $e) {
149+
} catch (Exception $e) {
155150
return false;
156151
}
157152

@@ -169,7 +164,7 @@ public function verifyCode($secret, $code, $discrepancy = 1)
169164
* @param int $length
170165
* @return self
171166
*/
172-
public function setCodeLength($length)
167+
public function setCodeLength(int $length) : self
173168
{
174169
$this->_codeLength = $length;
175170
return $this;
@@ -179,9 +174,9 @@ public function setCodeLength($length)
179174
* Helper class to decode base32
180175
*
181176
* @param string $secret
182-
* @return bool|string
177+
* @return string
183178
*/
184-
private static function base32Decode($secret)
179+
private static function base32Decode(string $secret) : string
185180
{
186181
if (empty($secret)) {
187182
return '';
@@ -193,13 +188,13 @@ private static function base32Decode($secret)
193188
$paddingCharCount = substr_count($secret, $base32chars[32]);
194189
$allowedValues = [6, 4, 3, 1, 0];
195190
if (!in_array($paddingCharCount, $allowedValues)) {
196-
return false;
191+
return '';
197192
}
198193

199194
for ($i = 0; $i < 4; $i++){
200195
if ($paddingCharCount == $allowedValues[$i] &&
201196
substr($secret, -($allowedValues[$i])) != str_repeat($base32chars[32], $allowedValues[$i])) {
202-
return false;
197+
return '';
203198
}
204199
}
205200

@@ -208,13 +203,13 @@ private static function base32Decode($secret)
208203
$binaryString = "";
209204
for ($i = 0; $i < count($secret); $i = $i+8) {
210205
if (!in_array($secret[$i], $base32chars)) {
211-
return false;
206+
return '';
212207
}
213208

214209
$x = "";
215210
for ($j = 0; $j < 8; $j++) {
216-
$secretChar = isset($secret[$i + $j]) ? $secret[$i + $j] : 0;
217-
$base = isset($base32charsFlipped[$secretChar]) ? $base32charsFlipped[$secretChar] : 0;
211+
$secretChar = $secret[$i + $j] ?? 0;
212+
$base = $base32charsFlipped[$secretChar] ?? 0;
218213
$x .= str_pad(base_convert($base, 10, 2), 5, '0', STR_PAD_LEFT);
219214
}
220215
$eightBits = str_split($x, 8);
@@ -231,7 +226,7 @@ private static function base32Decode($secret)
231226
*
232227
* @return array
233228
*/
234-
private static function base32LookupTable()
229+
private static function base32LookupTable() : array
235230
{
236231
return [
237232
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', // 7

0 commit comments

Comments
 (0)