8000 Added verifyHash() option #2 · middlewares/http-authentication@f67e02f · GitHub
[go: up one dir, main page]

Skip to content

Commit f67e02f

Browse files
committed
Added verifyHash() option #2
1 parent fda14bd commit f67e02f

File tree

5 files changed

+66
-4
lines changed

5 file 8000 s changed

+66
-4
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](http://keepachangelog.com/)
66
and this project adheres to [Semantic Versioning](http://semver.org/).
77

8+
## [2.1.0] - 2020-04-24
9+
### Added
10+
- Option `BasicAuthentication::verifyHash()` [#2]
11+
812
## [2.0.0] - 2019-11-30
913
### Added
1014
- Added a second argument to the constructor to set a `ResponseFactory`
@@ -59,6 +63,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
5963
## 0.1.0 - 2016-10-02
6064
First version
6165

66+
[#2]: https://github.com/middlewares/http-authentication/issues/2
67+
68+
[2.1.0]: https://github.com/middlewares/http-authentication/compare/v2.0.0...v2.1.0
6269
[2.0.0]: https://github.com/middlewares/http-authentication/compare/v1.1.0...v2.0.0
6370
[1.1.0]: https://github.com/middlewares/http-authentication/compare/v1.0.0...v1.1.0
6471
[1.0.0]: https://github.com/middlewares/http-authentication/compare/v0.5.0...v1.0.0

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,28 @@ Dispatcher::run([
7171
]);
7272
```
7373

74+
### verifyHash
75+
76+
This option verifies the password using [`password_verify`](https://www.php.net/manual/en/function.password-verify.php). Useful if you don't want to provide the passwords in plain text.
77+
78+
```php
79+
$users = [
80+
'username' => password_hash('secret-password', PASSWORD_DEFAULT);
81+
]
82+
83+
Dispatcher::run([
84+
(new Middlewares\BasicAuthentication($users))
85+
->attribute('username')
86+
->verifyHash(),
87+
88+
function ($request) {
89+
$username = $request->getAttribute('username');
90+
91+
return new Response('Hello '.$username);
92+
}
93+
]);
94+
```
95+
7496
## DigestAuthentication
7597

7698
The [Digest access authentication](https://en.wikipedia.org/wiki/Digest_access_authentication) is more secure than basic.

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
},
2525
"require-dev": {
2626
"phpunit/phpunit": "^8.1",
27-
"zendframework/zend-diactoros": "^2.2",
27+
"laminas/laminas-diactoros": "^2.2",
2828
"friendsofphp/php-cs-fixer": "^2.0",
2929
"squizlabs/php_codesniffer": "^3.0",
3030
"oscarotero/php-cs-fixer-config": "^1.0"

src/BasicAuthentication.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
class BasicAuthentication extends HttpAuthentication implements MiddlewareInterface
1212
{
13+
private $verifyHash = false;
14+
1315
/**
1416
* Process a server request and return a response.
1517
*/
@@ -29,6 +31,13 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
2931
return $handler->handle($request);
3032
}
3133

34+
public function verifyHash($verifyHash = true): self
35+
{
36+
$this->verifyHash = $verifyHash;
37+
38+
return $this;
39+
}
40+
3241
/**
3342
* Check the user credentials and return the username
3443
*/
@@ -46,11 +55,15 @@ private function login(ServerRequestInterface $request): ?string
4655
return null;
4756
}
4857

49-
if ($this->users[$authorization['username']] !== $authorization['password']) {
50-
return null;
58+
if ($this->verifyHash) {
59+
return password_verify($authorization['password'], $this->users[$authorization['username']])
60+
? $authorization['username']
61+
: null;
5162
}
5263

53-
return $authorization['username'];
64+
return $this->users[$authorization['username']] === $authorization['password']
65+
? $authorization['username']
66+
: null;
5467
}
5568

5669
/**

tests/BasicAuthenticationTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,24 @@ function ($request) {
7474
$this->assertSame(200, $response->getStatusCode());
7575
$this->assertSame('user', (string) $response->getBody());
7676
}
77+
78+
public function testHashSuccess()
79+
{
80+
$request = Factory::createServerRequest('GET', '/')
81+
->withHeader('Authorization', 'Basic '.base64_encode('user:rasmuslerdorf'));
82+
83+
$response = Dispatcher::run([
84+
(new BasicAuthentication(['user' => '$2y$07$BCryptRequires22Chrcte/VlQH0piJtjXl.0t1XkA8pw9dMXTpOq']))
85+
->verifyHash()
86+
->realm('My realm')
87+
->attribute('auth-username'),
88+
89+
function ($request) {
90+
echo $request->getAttribute('auth-username');
91+
},
92+
], $request);
93+
94+
$this->assertSame(200, $response->getStatusCode());
95+
$this->assertSame('user', (string) $response->getBody());
96+
}
7797
}

0 commit comments

Comments
 (0)
0