Skip to content
This repository was archived by the owner on Apr 23, 2021. It is now read-only.

Commit 320ac87

Browse files
Add Laravel 7 compatibility (#12)
* Add Laravel 7 compatibility Co-authored-by: Claudio Dekker <[email protected]>
1 parent 3cdef56 commit 320ac87

21 files changed

+291
-137
lines changed

.editorconfig

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
; This file is for unifying the coding style for different editors and IDEs.
2+
; More information at https://editorconfig.org
3+
4+
root = true
5+
6+
[*]
7+
charset = utf-8
8+
indent_size = 4
9+
indent_style = space
10+
end_of_line = lf
11+
insert_final_newline = true
12+
trim_trailing_whitespace = true
13+
14+
[*.md]
15+
trim_trailing_whitespace = false

.gitattributes

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Path-based git attributes
2+
# https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html
3+
4+
# Ignore all test and documentation with "export-ignore".
5+
/.editorconfig export-ignore
6+
/.gitattributes export-ignore
7+
/.gitignore export-ignore
8+
/.styleci.yml export-ignore
9+
/.scrutinizer.yml export-ignore
10+
/.travis.yml export-ignore
11+
/phpunit.xml.dist export-ignore
12+
/tests export-ignore

.gitignore

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
/vendor/
2-
phpunit.xml
3-
composer.lock
41
.env
2+
.phpunit.result.cache
3+
composer.lock
4+
phpunit.xml
5+
/vendor

.scrutinizer.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ build:
3636
analysis:
3737
environment:
3838
php:
39-
version: 7.2
39+
version: 7.4
4040
tests:
4141
override:
4242
- php-scrutinizer-run

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ language: php
55
php:
66
- 7.2
77
- 7.3
8+
- 7.4
89

910
env:
1011
matrix:

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,15 @@
22

33
All notable changes to `ubient/laravel-pwned-passwords` will be documented in this file
44

5+
## 2.0.1 - 2020-04-11
6+
- Add support for Laravel 7
7+
- Fixed a bug where an error might be thrown for not being able to reach the Pwned Passwords API.
8+
Instead, the default behaviour now is to accept the password as non-pwned and send a warning to Laravel's Log.
9+
If you would like to override this behaviour, you can [create your own implementation of the LookupErrorHandler and bind it in your application](README.md#handling-lookup-errors).
10+
511
## 2.0.0 - 2019-09-03
612
- Drop support for Laravel 5.7 and older
7-
- Add support Laravel 6.0
13+
- Add support for Laravel 6
814

915
## 1.1.0 - 2019-02-27
1016
- Drop support for PHP 7.1

README.md

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,15 @@ In order to protect the value of the source password being searched for, Pwned P
1919
This works by hashing the source password with SHA-1, and only sending the first 5 characters of that hash to the API.
2020
By checking whether the rest of the SHA-1 hash occurs within the output, we can verify both whether the password was pwned previously, and how frequently.
2121

22-
### Usage
22+
## Installation
23+
24+
You can install the package via composer:
25+
26+
```bash
27+
composer require ubient/laravel-pwned-passwords
28+
```
29+
30+
## Usage
2331

2432
Here's a few short examples of what you can do:
2533

@@ -59,29 +67,34 @@ $request->validate([
5967
]);
6068
```
6169

62-
## Installation
70+
#### Handling Lookup Errors
71+
When the Pwned Passwords API cannot be queried, the default behavior is to accept the password as non-pwned and to send a warning message to the log.
72+
While this doesn't add much value, it does allow you to be aware of when a pwned password was allowed, and to potentially manually act on this.
6373

64-
You can install the package via composer:
74+
If you would like to automatically do something else based on this lookup error (such as marking the request as potentially pwned), or want to decline the password instead,
75+
you may create your own implementation of the [LookupErrorHandler](src/Contracts/LookupErrorHandler.php) and overwrite the default binding in your application:
6576

66-
```bash
67-
composer require ubient/laravel-pwned-passwords
77+
```php
78+
use Ubient\PwnedPasswords\Contracts\LookupErrorHandler;
79+
80+
$this->app->bind(LookupErrorHandler::class, MyCustomErrorHandler::class);
6881
```
6982

70-
### Testing
83+
## Testing
7184

7285
``` bash
7386
composer test
7487
```
7588

76-
### Changelog
89+
## Changelog
7790

7891
Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.
7992

8093
## Contributing
8194

8295
Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
8396

84-
### Security
97+
## Security
8598

8699
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
87100

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@
2323
"require": {
2424
"php": "^7.2",
2525
"guzzlehttp/guzzle": "^6.3",
26-
"illuminate/contracts": "^5.8|^6.0",
27-
"illuminate/support": "^5.8|^6.0"
26+
"illuminate/contracts": "^5.8|^6.0|^7.0",
27+
"illuminate/support": "^5.8|^6.0|^7.0"
2828
},
2929
"require-dev": {
30-
"orchestra/testbench": "^3.8",
30+
"orchestra/testbench": "^3.8|^4.0|^5.0",
3131
"phpunit/phpunit": "^8.0"
3232
},
3333
"autoload": {

src/Api/FakeApiGateway.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,22 @@
22

33
namespace Ubient\PwnedPasswords\Api;
44

5+
use Ubient\PwnedPasswords\Contracts\ApiGateway;
6+
57
class FakeApiGateway implements ApiGateway
68
{
79
/**
810
* Indicates how frequently a password was found to be pwned.
911
*
1012
* @param string $password
11-
* @throws \RuntimeException
1213
* @return int
1314
*/
1415
public function search(string $password): int
1516
{
17+
if ($password === 'password1') {
18+
throw new \RuntimeException('Simulated network connectivity issue.');
19+
}
20+
1621
return collect([
1722
'P@ssw0rd' => 49938,
1823
'hammertime6' => 5,

src/Api/PwnedPasswordsGateway.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
namespace Ubient\PwnedPasswords\Api;
44

5-
use RuntimeException;
5+
use GuzzleHttp\Client as GuzzleClient;
66
use Illuminate\Support\Collection;
77
use Illuminate\Support\Facades\Cache;
8-
use GuzzleHttp\Client as GuzzleClient;
8+
use RuntimeException;
9+
use Ubient\PwnedPasswords\Contracts\ApiGateway;
910

1011
class PwnedPasswordsGateway implements ApiGateway
1112
{
@@ -18,15 +19,15 @@ class PwnedPasswordsGateway implements ApiGateway
1819
public function search(string $password): int
1920
{
2021
$hash = strtoupper(sha1($password));
21-
2222
$hashPrefix = substr($hash, 0, 5);
2323
$hashSuffix = substr($hash, 5);
2424

25-
return Cache::remember("Ubient\PwnedPasswords::$hashPrefix", 7200, function () use ($hashPrefix, $hashSuffix) {
26-
return $this
27-
->fetchHashes($hashPrefix)
28-
->get($hashSuffix, 0);
25+
/** @var Collection $hashes */
26+
$hashes = Cache::remember("Ubient\PwnedPasswords::$hashPrefix", 7200, function () use ($hashPrefix) {
27+
return $this->fetchHashes($hashPrefix);
2928
});
29+
30+
return $hashes->get($hashSuffix, 0);
3031
}
3132

3233
/**
@@ -36,7 +37,7 @@ public function search(string $password): int
3637
* and the value is the amount of times the password was pwned.
3738
*
3839
* @param string $hashPrefix
39-
* @throws \RuntimeException
40+
* @throws RuntimeException
4041
* @return Collection
4142
*/
4243
protected function fetchHashes(string $hashPrefix): Collection

0 commit comments

Comments
 (0)