PasswordValidatorExtension::getParams()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Firesphere\HaveIBeenPwned\Extensions;
4
5
use Firesphere\HaveIBeenPwned\Services\HaveIBeenPwnedService;
6
use SilverStripe\Core\Config\Configurable;
7
use SilverStripe\Core\Extension;
8
use SilverStripe\Core\Injector\Injector;
9
use SilverStripe\ORM\ValidationResult;
10
use SilverStripe\Security\Member;
11
use SilverStripe\Security\PasswordValidator;
12
13
/**
14
 * Class \Firesphere\HaveIBeenPwned\Extensions\PasswordValidatorExtension
15
 *
16
 * @property PasswordValidator|PasswordValidatorExtension $owner
17
 */
18
class PasswordValidatorExtension extends Extension
19
{
20
    use Configurable;
21
22
    /**
23
     * @var HaveIBeenPwnedService
24
     */
25
    protected $service;
26
27
    /**
28
     * @var array
29
     */
30
    protected $params = [];
31
32
    /**
33
     * @param string $pwd
34
     * @param Member|MemberExtension $member
35
     * @param ValidationResult $valid
36
     * @return void
37
     * @throws \GuzzleHttp\Exception\GuzzleException
38
     */
39
    public function updateValidatePassword($pwd, $member, $valid)
40
    {
41
        $this->service = Injector::inst()->createWithArgs(HaveIBeenPwnedService::class, [$this->params]);
42
43
        if (!$member->PwndDisabled) {
44
            $allowPwnd = HaveIBeenPwnedService::config()->get('allow_pwnd');
45
            $savePwnd = HaveIBeenPwnedService::config()->get('save_pwnd');
46
47
            $isPwndCount = $this->checkPwnCount($pwd, $member);
48
            $breached = $this->checkPwndSites($savePwnd, $member);
49
50
            // Although it would be stupid, the pwnd check can be disabled
51
            // Or even allow for breached passwords. Not exactly ideal
52
            if ($isPwndCount && !$allowPwnd) {
53
                $this->addMessages($valid, $isPwndCount, $breached);
54
            }
55
        }
56
    }
57
58
    /**
59
     * @param $pwd
60
     * @param $member
61
     * @return int
62
     * @throws \GuzzleHttp\Exception\GuzzleException
63
     */
64
    protected function checkPwnCount($pwd, $member)
65
    {
66
        $isPwndCount = $this->service->checkPwnedPassword($pwd);
67
68
        // Always set amount of pwd's if it's true
69
        $member->PasswordIsPwnd = $isPwndCount;
70
71
        return $isPwndCount;
72
    }
73
74
    /**
75
     * @param $member
76
     * @param $savePwnd
77
     * @return string
78
     * @throws \GuzzleHttp\Exception\GuzzleException
79
     */
80
    protected function checkPwndSites($savePwnd, $member)
81
    {
82
        $breached = '';
83
        // If storing the breached sites, check the email as well
84
        if ($savePwnd) {
85
            $breached = $this->service->checkPwnedEmail($member);
86
            $member->BreachedSites = $breached;
87
        }
88
89
        return $breached;
90
    }
91
92
    /**
93
     * @return array
94
     */
95
    public function getParams()
96
    {
97
        return $this->params;
98
    }
99
100
    /**
101
     * @param array $params
102
     */
103
    public function setParams($params)
104
    {
105
        $this->params = $params;
106
    }
107
108
    /**
109
     * @param ValidationResult $valid
110
     * @param int $isPwndCount
111
     * @param string $breached
112
     */
113
    protected function addMessages($valid, $isPwndCount, $breached)
114
    {
115
        $valid->addFieldError(
116
            'Password',
117
            _t(
118
                self::class . '.KNOWN',
119
                'Your password appears {times} times in the Have I Been Pwnd database',
120
                ['times' => $isPwndCount]
121
            )
122
        );
123
        if ($breached) {
124
            $type = $valid->isValid() ? ValidationResult::TYPE_WARNING : ValidationResult::TYPE_INFO;
125
            $message = _t(
126
                self::class . '.KNOWNBREACHMESSAGE',
127
                'To help you identify where you have been breached, see the HaveIBeenPwned tab for information after a successful update of your password'
128
            );
129
130
            $valid->addMessage($message, $type);
131
        }
132
    }
133
}
134