Skip to content

Commit ad5e67b

Browse files
committed
✨ v2
1 parent 34f3a7a commit ad5e67b

File tree

14 files changed

+74
-279
lines changed

14 files changed

+74
-279
lines changed

.github/workflows/ci.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ jobs:
2424
- ubuntu-latest
2525
- windows-latest
2626
php-version:
27-
- "5.6"
2827
- "7.0"
2928
- "7.1"
3029
- "7.2"

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,18 @@ A generator for counter based ([RFC 4226](https://tools.ietf.org/html/rfc4226))
1818

1919
# Documentation
2020
## Requirements
21-
- PHP 5.6+
21+
- PHP 7.0+
2222

2323
## Installation
2424
**requires [composer](https://getcomposer.org)**
2525

2626
via terminal: `composer require chillerlan/php-authenticator`
2727

28-
*composer.json* (note: replace `dev-main` with a [version constraint](https://getcomposer.org/doc/articles/versions.md#writing-version-constraints), e.g. `^1.2` - see [releases](https://github.com/chillerlan/php-authenticator/releases) for valid versions)
28+
*composer.json* (note: replace `dev-main` with a [version constraint](https://getcomposer.org/doc/articles/versions.md#writing-version-constraints), e.g. `^2.1` - see [releases](https://github.com/chillerlan/php-authenticator/releases) for valid versions)
2929
```json
3030
{
3131
"require": {
32-
"php": "^5.6 || ^7.0",
32+
"php": "^7.0",
3333
"chillerlan/php-authenticator": "dev-main"
3434
}
3535
}

composer.json

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "chillerlan/php-authenticator",
3-
"description": "A generator for counter- and time based 2-factor authentication codes (Google Authenticator). PHP 5.6+",
3+
"description": "A generator for counter- and time based 2-factor authentication codes (Google Authenticator). PHP 7.0+",
44
"homepage": "https://github.com/chillerlan/php-authenticator",
55
"license": "MIT",
66
"type": "library",
@@ -21,13 +21,12 @@
2121
"minimum-stability": "stable",
2222
"prefer-stable": true,
2323
"require": {
24-
"php": "^5.6 || ^7.0",
25-
"paragonie/constant_time_encoding": "^1.1 || ^v2.6",
26-
"paragonie/random_compat": "^2.0"
24+
"php": "^7.0",
25+
"paragonie/constant_time_encoding": "^2.6"
2726
},
2827
"require-dev": {
2928
"phpmd/phpmd": "^2.13",
30-
"phpunit/phpunit": "^5.7 || ^6.5",
29+
"phpunit/phpunit": "^6.5 || ^7.5",
3130
"squizlabs/php_codesniffer": "^3.7"
3231
},
3332
"suggest": {

phpunit.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/5.7/phpunit.xsd"
3+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/6.5/phpunit.xsd"
44
bootstrap="vendor/autoload.php"
55
colors="true"
66
verbose="true"

src/Authenticator.php

Lines changed: 15 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,9 @@
1212

1313
use chillerlan\Authenticator\Authenticators\AuthenticatorInterface;
1414
use InvalidArgumentException;
15-
use TypeError;
1615
use function array_keys;
1716
use function array_replace;
1817
use function http_build_query;
19-
use function is_string;
2018
use function rawurlencode;
2119
use function sprintf;
2220
use function strtolower;
@@ -108,17 +106,10 @@ class Authenticator{
108106

109107
/**
110108
* Authenticator constructor
111-
*
112-
* @param array|null $options
113-
* @param string|null $secret
114109
*/
115-
public function __construct(array $options = null, $secret = null){
116-
117-
if($options === null){
118-
$options = [];
119-
}
120-
121-
$this->setOptions($options);
110+
public function __construct(array $options = null, string $secret = null){
111+
// phpcs:ignore
112+
$this->setOptions($options ?? []);
122113

123114
if($secret !== null){
124115
$this->setSecret($secret);
@@ -132,33 +123,28 @@ public function __construct(array $options = null, $secret = null){
132123
* Please note that this will reset the secret phrase stored with the authenticator instance
133124
* if a different mode than the current is given.
134125
*
135-
* @param array $options
136-
*
137-
* @return \chillerlan\Authenticator\Authenticator
138126
* @throws \InvalidArgumentException
139127
*/
140-
public function setOptions(array $options){
141-
$defaults = self::DEFAULTS;
128+
public function setOptions(array $options):self{
142129
// replace settings with the current and given ones
143-
$this->options = array_replace($defaults, $this->options, $options);
130+
$this->options = array_replace(self::DEFAULTS, $this->options, $options);
144131

145132
// remove unwanted keys
146133
foreach(array_keys($this->options) as $key){
147-
if(!isset($defaults[$key])){
134+
if(!isset(self::DEFAULTS[$key])){
148135
unset($this->options[$key]);
149136
}
150137
}
151138

152139
// invoke a new authenticator interface if necessary
153140
if(!isset($this->authenticator) || $this->options['mode'] !== $this->mode){
154-
$mode = strtolower($this->options['mode']);
155-
$modes = AuthenticatorInterface::MODES;
141+
$mode = strtolower($this->options['mode']);
156142

157-
if(!isset($modes[$mode])){
143+
if(!isset(AuthenticatorInterface::MODES[$mode])){
158144
throw new InvalidArgumentException('Invalid mode: '.$mode);
159145
}
160146

161-
$class = $modes[$mode];
147+
$class = AuthenticatorInterface::MODES[$mode];
162148
$this->mode = $mode;
163149
$this->authenticator = new $class;
164150
}
@@ -171,12 +157,9 @@ public function setOptions(array $options){
171157
/**
172158
* Sets a secret phrase from a Base32 representation
173159
*
174-
* @param string $encodedSecret
175-
*
176-
* @return \chillerlan\Authenticator\Authenticator
177160
* @codeCoverageIgnore
178161
*/
179-
public function setSecret($encodedSecret){
162+
public function setSecret(string $encodedSecret):self{
180163
$this->authenticator->setSecret($encodedSecret);
181164

182165
return $this;
@@ -185,22 +168,18 @@ public function setSecret($encodedSecret){
185168
/**
186169
* Returns a Base32 representation of the current secret phrase
187170
*
188-
* @return string
189171
* @codeCoverageIgnore
190172
*/
191-
public function getSecret(){
173+
public function getSecret():string{
192174
return $this->authenticator->getSecret();
193175
}
194176

195177
/**
196178
* Generates a new (secure random) secret phrase
197179
*
198-
* @param int|null $length
199-
*
200-
* @return string
201180
* @codeCoverageIgnore
202181
*/
203-
public function createSecret($length = null){
182+
public function createSecret(int $length = null):string{
204183
return $this->authenticator->createSecret($length);
205184
}
206185

@@ -211,12 +190,9 @@ public function createSecret($length = null){
211190
* - a UNIX timestamp (TOTP)
212191
* - a counter value (HOTP)
213192
*
214-
* @param int|null $data
215-
*
216-
* @return string
217193
* @codeCoverageIgnore
218194
*/
219-
public function code($data = null){
195+
public function code(int $data = null):string{
220196
return $this->authenticator->code($data);
221197
}
222198

@@ -227,13 +203,9 @@ public function code($data = null){
227203
* - a UNIX timestamp (TOTP)
228204
* - a counter value (HOTP)
229205
*
230-
* @param string $otp
231-
* @param int|null $data
232-
*
233-
* @return bool
234206
* @codeCoverageIgnore
235207
*/
236-
public function verify($otp, $data = null){
208+
public function verify(string $otp, int $data = null):bool{
237209
return $this->authenticator->verify($otp, $data);
238210
}
239211

@@ -242,24 +214,9 @@ public function verify($otp, $data = null){
242214
*
243215
* @link https://github.com/google/google-authenticator/wiki/Key-Uri-Format#parameters
244216
*
245-
* @param string $label
246-
* @param string $issuer
247-
* @param int|null $hotpCounter
248-
* @param bool|null $omitSettings
249-
*
250-
* @return string
251217
* @throws \InvalidArgumentException
252218
*/
253-
public function getUri($label, $issuer, $hotpCounter = null, $omitSettings = null){
254-
255-
if(!is_string($label)){
256-
throw new TypeError('$label is expected to be string'); // @codeCoverageIgnore
257-
}
258-
259-
if(!is_string($issuer)){
260-
throw new TypeError('$issuer is expected to be string'); // @codeCoverageIgnore
261-
}
262-
219+
public function getUri(string $label, string $issuer, int $hotpCounter = null, bool $omitSettings = null):string{
263220
$label = trim($label);
264221
$issuer = trim($issuer);
265222

0 commit comments

Comments
 (0)