Skip to content

Commit 7940127

Browse files
committed
build: v1.0.1
1 parent e9d167f commit 7940127

File tree

12 files changed

+156
-64
lines changed

12 files changed

+156
-64
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ php:
44
- 7.1
55
- 7.0
66
- 5.6
7+
- 5.3
78
before_script:
89
script:
910
- rm composer.lock

README.md

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@ A PHP library for RFC 4226 and RFC 6238.
1616
composer require lorddashme/php-two-factor-auth
1717
```
1818

19-
### Native Way
20-
21-
- @TODO
22-
2319
## Usage
2420

2521
### HOTP
@@ -34,8 +30,12 @@ require __DIR__ . '/vendor/autoload.php';
3430
use LordDashMe\TwoFactorAuth\RFC4226\HOTP;
3531
use LordDashMe\TwoFactorAuth\Utility\Base32;
3632

37-
$hotp = new HOTP(Base32::encode('P@ssw0rd!'));
33+
$secret = Base32::encode('P@ssw0rd!');
34+
35+
$hotp = new HOTP($secret);
36+
3837
$hotp->setLength(6)
38+
->setAlgorithm('sha1')
3939
->prepare()
4040
->generate();
4141

@@ -52,8 +52,12 @@ require __DIR__ . '/vendor/autoload.php';
5252
use LordDashMe\TwoFactorAuth\RFC4226\HOTP;
5353
use LordDashMe\TwoFactorAuth\Utility\Base32;
5454

55-
$hotp = new HOTP(Base32::encode('P@ssw0rd!'));
55+
$secret = Base32::encode('P@ssw0rd!');
56+
57+
$hotp = new HOTP($secret);
58+
5659
$hotp->setLength(6)
60+
->setAlgorithm('sha1')
5761
->prepare();
5862

5963
echo $hotp->verify('444555'); // true
@@ -71,10 +75,15 @@ require __DIR__ . '/vendor/autoload.php';
7175
use LordDashMe\TwoFactorAuth\RFC6238\TOTP;
7276
use LordDashMe\TwoFactorAuth\Utility\Base32;
7377

74-
$totp = new TOTP(Base32::encode('P@ssw0rd!'));
75-
$totp->setTimeRemainingInSeconds(30)
78+
$secret = Base32::encode('P@ssw0rd!');
79+
80+
$totp = new TOTP($secret);
81+
82+
$totp->setTimeZone('Asia/Manila')
83+
->setTimeRemainingInSeconds(30)
7684
->setTimeAdjustments(10)
7785
->setLength(6)
86+
->setAlgorithm('sha1')
7887
->prepare()
7988
->generate();
8089

@@ -91,10 +100,15 @@ require __DIR__ . '/vendor/autoload.php';
91100
use LordDashMe\TwoFactorAuth\RFC6238\TOTP;
92101
use LordDashMe\TwoFactorAuth\Utility\Base32;
93102

94-
$totp = new TOTP(Base32::encode('P@ssw0rd!'));
95-
$totp->setTimeRemainingInSeconds(30)
103+
$secret = Base32::encode('P@ssw0rd!');
104+
105+
$totp = new TOTP($secret);
106+
107+
$totp->setTimeZone('Asia/Manila')
108+
->setTimeRemainingInSeconds(30)
96109
->setTimeAdjustments(10)
97110
->setLength(6)
111+
->setAlgorithm('sha1')
98112
->prepare();
99113

100114
echo $totp->verify('552344'); // true
@@ -113,15 +127,18 @@ use LordDashMe\TwoFactorAuth\Utility\Base32;
113127
use LordDashMe\TwoFactorAuth\GoogleAuthenticator\BarcodeURL;
114128
use LordDashMe\TwoFactorAuth\GoogleAuthenticator\TOTPFormat;
115129

116-
$secret = Base32::encode('P@ssw0rd!', false);
130+
$secret = Base32::encode('P@ssw0rd!');
117131
$accountUser = 'reyesjoshuaclifford@gmail.com';
118132
$issuer = 'TwoFactorAuth';
119133
$digits = 6;
120134
$period = 30;
135+
$algorithm = 'sha1';
136+
137+
$format = new TOTPFormat($period);
138+
139+
$barcodeURL = new BarcodeURL($secret, $accountUser, $issuer, $format);
121140

122-
$totpFormat = new TOTPFormat($period);
123-
$barcodeURL = new BarcodeURL($secret, $accountUser, $issuer, $totpFormat);
124-
$barcodeURL->setAlgorithm('SHA1')
141+
$barcodeURL->setAlgorithm($algorithm) // sha1 (Default), sha256, sha512
125142
->setDigits($digits)
126143
->build();
127144

phpunit.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
<file>tests/Unit/OTPTest.php</file>
3838
</testsuite>
3939
<testsuite name="Integration">
40-
<directory suffix="Test.php">tests/Integration/</directory>
40+
<directory suffix="Test.php">tests/Integration/GoogleAuthenticator/</directory>
4141
</testsuite>
4242
</testsuites>
4343
<logging>

src/GoogleAuthenticator/BarcodeURL.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class BarcodeURL
5656
*
5757
* @var string
5858
*/
59-
private $algorithm = 'SHA1';
59+
private $algorithm = 'sha1';
6060

6161
/**
6262
* The number for digit(s) in a OTP password generation used.
@@ -118,11 +118,11 @@ public function build()
118118
{
119119
$parameters = array(
120120
'secret' => $this->secret,
121-
'algorithm' => $this->algorithm,
121+
'algorithm' => \strtoupper($this->algorithm),
122122
'digits' => $this->digits
123123
);
124124

125-
$parameters = array_merge($parameters, $this->format->getParameters());
125+
$parameters = \array_merge($parameters, $this->format->getParameters());
126126

127127
if ($this->issuer) {
128128
$this->accountName = $this->issuer . ':' . $this->accountName;

src/OTP.php

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,33 +21,40 @@
2121
class OTP
2222
{
2323
/**
24-
* The inputed secret string that will be use to generate one-time password.
24+
* The max number of verification for the generated one-time password.
2525
*
26-
* @var string
26+
* @var int
2727
*/
28-
protected $secret = '';
28+
protected $maxVerificationNumber = 1;
2929

3030
/**
31-
* The max number of verification for the generated one-time password.
31+
* The inputed secret string that will be use to generate one-time password.
3232
*
33-
* @var int
33+
* @var string
3434
*/
35-
protected $maxVerificationNumber = 1;
35+
private $secret = '';
3636

3737
/**
3838
* The size of the generated one-time password.
3939
*
4040
* @var int
4141
*/
42-
protected $length = 6;
42+
private $length = 6;
4343

4444
/**
4545
* The randomness counter for the generated password one-time password.
4646
* The value will depend on the sub class.
4747
*
4848
* @var null
4949
*/
50-
protected $counter = null;
50+
private $counter = null;
51+
52+
/**
53+
* The algorithm to be use for the hash_hmac.
54+
*
55+
* @var string
56+
*/
57+
private $algorithm = 'sha1';
5158

5259
/**
5360
* The generated one-time password.
@@ -89,6 +96,20 @@ public function setCounter($counter)
8996
return $this;
9097
}
9198

99+
/**
100+
* The setter method for the algorithm class property.
101+
*
102+
* @param int $algorithm The algorithm to be use for the hmac hashing.
103+
*
104+
* @return $this
105+
*/
106+
public function setAlgorithm($algorithm)
107+
{
108+
$this->algorithm = $algorithm;
109+
110+
return $this;
111+
}
112+
92113
/**
93114
* Generation process for the OTP.
94115
*
@@ -99,26 +120,26 @@ public function generate()
99120
// Pack the counter into 64-bit int.
100121
// Use bitwise operators to manipulate and shift bits.
101122
// The 0xFFFFFFFF Hex = 4294967295 Decimal.
102-
$counterBytes = pack(
123+
$counterBytes = \pack(
103124
'NN', ($this->counter & (0xFFFFFFFF << 32)) >> 32, $this->counter & 0xFFFFFFFF
104125
);
105126

106127
// Generate hmac hash based on the given counter bytes
107128
// using sha1 algorithm that produce 160-bit.
108-
$hash = hash_hmac('sha1', $counterBytes, Base32::decode($this->secret), true);
129+
$hash = \hash_hmac($this->algorithm, $counterBytes, Base32::decode($this->secret), true);
109130

110131
// Get the offset value in the hash data.
111132
// Using bitwise AND operator, if the value is more than 15 (0xF) then return 0.
112-
$offset = ord($hash[19]) & 0xF;
133+
$offset = \ord($hash[19]) & 0xF;
113134

114135
// Unpack the hmac hashed.
115-
$unpack = unpack('Nint', substr($hash, $offset, 4));
136+
$unpack = \unpack('Nint', \substr($hash, $offset, 4));
116137

117138
// The 0x7FFFFFFF Hex = 2147483647 Decimal.
118139
$code = $unpack['int'] & 0x7FFFFFFF;
119140

120-
$this->generatedOTP = str_pad(
121-
$code % pow(10, $this->length), $this->length, 0, STR_PAD_LEFT
141+
$this->generatedOTP = \str_pad(
142+
$code % \pow(10, $this->length), $this->length, 0, STR_PAD_LEFT
122143
);
123144

124145
return $this;

src/RFC6238/TOTP.php

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ class TOTP extends OTP
2929
*/
3030
protected $maxVerificationNumber = 3;
3131

32+
/**
33+
* The time zone for the TOTP.
34+
*
35+
* @var string
36+
*/
37+
private $timeZone = 'Asia/Manila';
38+
3239
/**
3340
* The time remaining for the TOTP.
3441
*
@@ -48,6 +55,20 @@ public function __construct($secret)
4855
parent::__construct($secret);
4956
}
5057

58+
/**
59+
* The setter method for the time zone class property.
60+
*
61+
* @param string $timeZone The time zone to be used for the TOTP.
62+
*
63+
* @return $this
64+
*/
65+
public function setTimeZone($timeZone)
66+
{
67+
$this->timeZone = $timeZone;
68+
69+
return $this;
70+
}
71+
5172
/**
5273
* The setter method for the time remaining in seconds class property.
5374
*
@@ -85,11 +106,16 @@ public function setTimeAdjustments($timeAdjustments)
85106
public function prepare()
86107
{
87108
$now = new DateTime();
88-
$timeZone = new DateTimeZone('Asia/Manila');
109+
110+
$timeZone = new DateTimeZone($this->timeZone);
111+
89112
$now->setTimezone($timeZone);
113+
90114
$timeFormatted = $now->format('Y-m-d H:i:s T');
91115

92-
$time = \floor((\strtotime($timeFormatted) + ($this->timeAdjustments)) / $this->timeRemainingInSeconds);
116+
$time = \floor(
117+
(\strtotime($timeFormatted) + ($this->timeAdjustments)) / $this->timeRemainingInSeconds
118+
);
93119

94120
$this->setCounter($time);
95121

tests/Integration/GoogleAuthenticatorTOTPTest.php renamed to tests/Integration/GoogleAuthenticator/GenerateBarcodeURLTest.php

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
namespace LordDashMe\TwoFactorAuth\Tests\Integration\GoogleAuthenticator;
4+
35
use Mockery as Mockery;
46
use PHPUnit\Framework\TestCase;
57

@@ -8,12 +10,12 @@
810
use LordDashMe\TwoFactorAuth\GoogleAuthenticator\BarcodeURL;
911
use LordDashMe\TwoFactorAuth\GoogleAuthenticator\TOTPFormat;
1012

11-
class GoogleAuthenticatorTOTPTest extends TestCase
13+
class GenerateBarcodeURLTest extends TestCase
1214
{
1315
/**
1416
* @test
1517
*/
16-
public function it_should_generate_google_authenticator_required_barcode()
18+
public function it_should_generate_barcode_url_for_google_authenticator()
1719
{
1820
$secret = Base32::encode('P@ssw0rd!', false);
1921
$accountUser = 'reyesjoshuaclifford@gmail.com';
@@ -22,19 +24,23 @@ public function it_should_generate_google_authenticator_required_barcode()
2224
$period = 30;
2325

2426
$totp = new TOTP($secret);
25-
$totp->setTimeRemainingInSeconds($period);
26-
$totp->setTimeAdjustments(10);
27-
$totp->prepare();
28-
$totp->setLength($digits);
29-
$totp->generate();
27+
28+
$totp->setTimeZone('Asia/Manila')
29+
->setTimeRemainingInSeconds($period)
30+
->setTimeAdjustments(10)
31+
->setLength($digits)
32+
->prepare()
33+
->generate();
3034

3135
// var_dump($totp->get());
3236

33-
$totpFormat = new TOTPFormat($period);
34-
$barcodeURL = new BarcodeURL($secret, $accountUser, $issuer, $totpFormat);
35-
$barcodeURL->setAlgorithm('SHA1');
36-
$barcodeURL->setDigits($digits);
37-
$barcodeURL->build();
37+
$format = new TOTPFormat($period);
38+
39+
$barcodeURL = new BarcodeURL($secret, $accountUser, $issuer, $format);
40+
41+
$barcodeURL->setAlgorithm('sha1')
42+
->setDigits($digits)
43+
->build();
3844

3945
$this->assertEquals('https://chart.googleapis.com/chart?chs=200x200&chld=M|0&cht=qr&chl=otpauth://totp/TwoFactorAuth:reyesjoshuaclifford@gmail.com?secret=KBAHG43XGBZGIII&algorithm=SHA1&digits=6&period=30&issuer=TwoFactorAuth', $barcodeURL->get());
4046
}

0 commit comments

Comments
 (0)