Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Validation;
4
 
5
use Exception;
6
use Illuminate\Contracts\Validation\UncompromisedVerifier;
7
use Illuminate\Support\Str;
8
 
9
class NotPwnedVerifier implements UncompromisedVerifier
10
{
11
    /**
12
     * The HTTP factory instance.
13
     *
14
     * @var \Illuminate\Http\Client\Factory
15
     */
16
    protected $factory;
17
 
18
    /**
19
     * The number of seconds the request can run before timing out.
20
     *
21
     * @var int
22
     */
23
    protected $timeout;
24
 
25
    /**
26
     * Create a new uncompromised verifier.
27
     *
28
     * @param  \Illuminate\Http\Client\Factory  $factory
29
     * @param  int|null  $timeout
30
     * @return void
31
     */
32
    public function __construct($factory, $timeout = null)
33
    {
34
        $this->factory = $factory;
35
        $this->timeout = $timeout ?? 30;
36
    }
37
 
38
    /**
39
     * Verify that the given data has not been compromised in public breaches.
40
     *
41
     * @param  array  $data
42
     * @return bool
43
     */
44
    public function verify($data)
45
    {
46
        $value = $data['value'];
47
        $threshold = $data['threshold'];
48
 
49
        if (empty($value = (string) $value)) {
50
            return false;
51
        }
52
 
53
        [$hash, $hashPrefix] = $this->getHash($value);
54
 
55
        return ! $this->search($hashPrefix)
56
            ->contains(function ($line) use ($hash, $hashPrefix, $threshold) {
57
                [$hashSuffix, $count] = explode(':', $line);
58
 
59
                return $hashPrefix.$hashSuffix == $hash && $count > $threshold;
60
            });
61
    }
62
 
63
    /**
64
     * Get the hash and its first 5 chars.
65
     *
66
     * @param  string  $value
67
     * @return array
68
     */
69
    protected function getHash($value)
70
    {
71
        $hash = strtoupper(sha1((string) $value));
72
 
73
        $hashPrefix = substr($hash, 0, 5);
74
 
75
        return [$hash, $hashPrefix];
76
    }
77
 
78
    /**
79
     * Search by the given hash prefix and returns all occurrences of leaked passwords.
80
     *
81
     * @param  string  $hashPrefix
82
     * @return \Illuminate\Support\Collection
83
     */
84
    protected function search($hashPrefix)
85
    {
86
        try {
87
            $response = $this->factory->withHeaders([
88
                'Add-Padding' => true,
89
            ])->timeout($this->timeout)->get(
90
                'https://api.pwnedpasswords.com/range/'.$hashPrefix
91
            );
92
        } catch (Exception $e) {
93
            report($e);
94
        }
95
 
96
        $body = (isset($response) && $response->successful())
97
            ? $response->body()
98
            : '';
99
 
100
        return Str::of($body)->trim()->explode("\n")->filter(function ($line) {
101
            return str_contains($line, ':');
102
        });
103
    }
104
}