Passed
Push — 3.x ( 57e360...c5f7aa )
by Enjoys
01:56
created

Csrf   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 90.91%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 5
eloc 9
c 2
b 0
f 0
dl 0
loc 41
ccs 10
cts 11
cp 0.9091
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A check() 0 3 1
A setMessage() 0 6 2
A validate() 0 10 2
1
<?php
2
3
/*
4
 * The MIT License
5
 *
6
 * Copyright 2020 Enjoys.
7
 *
8
 * Permission is hereby granted, free of charge, to any person obtaining a copy
9
 * of this software and associated documentation files (the "Software"), to deal
10
 * in the Software without restriction, including without limitation the rights
11
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
 * copies of the Software, and to permit persons to whom the Software is
13
 * furnished to do so, subject to the following conditions:
14
 *
15
 * The above copyright notice and this permission notice shall be included in
16
 * all copies or substantial portions of the Software.
17
 *
18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24
 * THE SOFTWARE.
25
 */
26
27
declare(strict_types=1);
28
29
namespace Enjoys\Forms\Rule;
30
31
use Enjoys\Forms\Element;
32
use Enjoys\Forms\Form;
33
use Enjoys\Forms\Rule\RuleInterface;
34
use Enjoys\Forms\Rules;
35
36
/**
37
 * Description of Required
38
 *
39
 * Csrf element will automatically set the rule(s)
40
 * and the form itself determines when csrf is needed
41
 *
42
 * @author Enjoys
43
 */
44
class Csrf extends Rules implements RuleInterface
45
{
46
47
    /**
48
     * 
49
     * @param string|null $message
50
     * @return string|null
51
     */
52 20
    public function setMessage(?string $message = null): ?string
53
    {
54 20
        if (is_null($message)) {
55 2
            $message = 'CSRF Attack detected';
56
        }
57 20
        return parent::setMessage($message);
58
    }
59
60
    /**
61
     * @psalm-suppress UndefinedMethod
62
     * @param Element $element
63
     * @return bool
64
     */
65 1
    public function validate(Element $element): bool
66
    {
67
68 1
        if (!$this->check($this->getRequest()->post(Form::_TOKEN_CSRF_, ''))) {
69 1
            $element->setRuleError($this->getMessage());
0 ignored issues
show
Bug introduced by
The method setRuleError() does not exist on Enjoys\Forms\Element. It seems like you code against a sub-type of said class. However, the method does not exist in Enjoys\Forms\Elements\Optgroup or Enjoys\Forms\Elements\Submit or Enjoys\Forms\Elements\Option or Enjoys\Forms\Elements\Button or Enjoys\Forms\Elements\Image or Enjoys\Forms\Elements\Reset or Enjoys\Forms\Elements\Header. Are you sure you never get one of those? ( Ignorable by Annotation )

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

69
            $element->/** @scrutinizer ignore-call */ 
70
                      setRuleError($this->getMessage());
Loading history...
70
            // throw new \Enjoys\Forms\Exception\ExceptionRule($this->getMessage());
71 1
            return false;
72
        }
73
74
        return true;
75
    }
76
77
    /**
78
     * @psalm-suppress PossiblyNullArgument
79
     * @param string $value
80
     * @return bool
81
     */
82 1
    private function check(string $value): bool
83
    {
84 1
        return hash_equals($value, crypt($this->getParam('csrf_key'), $value));
85
    }
86
}
87