Passed
Push — master ( 8677c2...c8946b )
by Simon
01:42
created

PasswordValidatorExtension::getParams()   A

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
     * @param PasswordValidator|array $validator
37
     * @return void
38
     * @throws \GuzzleHttp\Exception\GuzzleException
39
     */
40
    public function updateValidatePassword($pwd, $member, $valid, $validator = null)
0 ignored issues
show
Unused Code introduced by
The parameter $validator is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

40
    public function updateValidatePassword($pwd, $member, $valid, /** @scrutinizer ignore-unused */ $validator = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
41
    {
42
        $this->service = Injector::inst()->createWithArgs(HaveIBeenPwnedService::class, [$this->params]);
43
44
        if (!$member->PwndDisabled) {
45
            $allowPwnd = HaveIBeenPwnedService::config()->get('allow_pwnd');
46
            $savePwnd = HaveIBeenPwnedService::config()->get('save_pwnd');
47
48
            $isPwndCount = $this->checkPwnCount($pwd, $member);
49
            $breached = $this->checkPwndSites($savePwnd, $member);
50
51
            // Although it would be stupid, the pwnd check can be disabled
52
            // Or even allow for breached passwords. Not exactly ideal
53
            if ($isPwndCount && !$allowPwnd) {
54
                $this->addMessages($valid, $isPwndCount, $breached);
55
            }
56
        }
57
    }
58
59
    /**
60
     * @param $pwd
61
     * @param $member
62
     * @return int
63
     * @throws \GuzzleHttp\Exception\GuzzleException
64
     */
65
    protected function checkPwnCount($pwd, $member)
66
    {
67
        $isPwndCount = $this->service->checkPwnedPassword($pwd);
68
69
        // Always set amount of pwd's if it's true
70
        $member->PasswordIsPwnd = $isPwndCount;
71
72
        return $isPwndCount;
73
    }
74
75
    /**
76
     * @param $member
77
     * @param $savePwnd
78
     * @return string
79
     * @throws \GuzzleHttp\Exception\GuzzleException
80
     */
81
    protected function checkPwndSites($savePwnd, $member)
82
    {
83
        $breached = '';
84
        // If storing the breached sites, check the email as well
85
        if ($savePwnd) {
86
            $breached = $this->service->checkPwnedEmail($member);
87
            $member->BreachedSites = $breached;
88
        }
89
90
        return $breached;
91
    }
92
93
    /**
94
     * @return array
95
     */
96
    public function getParams()
97
    {
98
        return $this->params;
99
    }
100
101
    /**
102
     * @param array $params
103
     */
104
    public function setParams($params)
105
    {
106
        $this->params = $params;
107
    }
108
109
    /**
110
     * @param ValidationResult $valid
111
     * @param int $isPwndCount
112
     * @param string $breached
113
     */
114
    protected function addMessages($valid, $isPwndCount, $breached)
115
    {
116
        $valid->addFieldError(
117
            'Password',
118
            _t(
119
                self::class . '.KNOWN',
120
                'Your password appears {times} times in the Have I Been Pwnd database',
121
                ['times' => $isPwndCount]
122
            )
123
        );
124
        if ($breached) {
125
            $type = $valid->isValid() ? ValidationResult::TYPE_WARNING : ValidationResult::TYPE_INFO;
126
            $message = _t(
127
                self::class . '.KNOWNBREACHMESSAGE',
128
                'To help you identify where you have been breached, see the HaveIBeenPwned tab for information after a successful update of your password'
129
            );
130
131
            $valid->addMessage($message, $type);
132
        }
133
    }
134
}
135