Completed
Pull Request — develop (#619)
by
unknown
04:25
created

AbstractHttpStrategy   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 75%

Importance

Changes 0
Metric Value
wmc 8
c 0
b 0
f 0
lcom 1
cbo 1
dl 0
loc 68
ccs 15
cts 20
cp 0.75
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A extractFieldInfo() 0 9 3
A stopPropagation() 0 4 1
B validateField() 0 22 4
1
<?php
2
/**
3
 * abstract strategy for checking auth against parts of the request
4
 */
5
6
namespace Graviton\SecurityBundle\Authentication\Strategies;
7
8
use Symfony\Component\HttpFoundation\HeaderBag;
9
use Symfony\Component\HttpFoundation\ParameterBag;
10
use Symfony\Component\HttpFoundation\Response;
11
use Symfony\Component\HttpKernel\Exception\HttpException;
12
13
/**
14
 * Class AbstractHttpStrategy
15
 *
16
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
17
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
18
 * @link     http://swisscom.ch
19
 */
20
abstract class AbstractHttpStrategy implements StrategyInterface
21
{
22
    /**
23
     * Will stop propagation if params exists
24
     * @var bool
25
     */
26
    private $passed = false;
27
28
    /**
29
     * Extracts information from the a request header field.
30
     *
31
     * @param ParameterBag|HeaderBag $header    object representation of the request header.
32
     * @param string                 $fieldName Name of the field to be read.
33
     *
34
     * @return string
35
     */
36 6
    protected function extractFieldInfo($header, $fieldName)
37
    {
38 6
        if ($header instanceof ParameterBag || $header instanceof HeaderBag) {
39 6
            $this->validateField($header, $fieldName);
40 6
            return $header->get($fieldName, '');
41
        }
42
43
        throw new \InvalidArgumentException('Provided request information are not valid.');
44
    }
45
46
    /**
47
     * Verifies that the provided header has the expected/mandatory fields.
48
     *
49
     * @param ParameterBag|HeaderBag $header    object representation of the request header.
50
     * @param string                 $fieldName Name of the header field to be validated.
51
     *
52
     * @return void
53
     * @throws \Symfony\Component\HttpKernel\Exception\HttpException
54
     */
55 6
    protected function validateField($header, $fieldName)
56
    {
57 6
        $this->passed = $header->has($fieldName);
58 6
        if (!$this->passed) {
59 4
            return;
60
        }
61
62
        // get rid of anything not a valid character
63 4
        $authInfo = filter_var($header->get($fieldName), FILTER_SANITIZE_STRING);
64
65
        // get rid of whitespaces
66 4
        $patterns = array("\r\n", "\n", "\r", "\s", "\t");
67 4
        $authInfo = str_replace($patterns, "", trim($authInfo));
68
69
        // get rid of control characters
70 4
        if (empty($authInfo) || $authInfo !== preg_replace('#[[:cntrl:]]#i', '', $authInfo)) {
71
            throw new HttpException(
72
                Response::HTTP_NETWORK_AUTHENTICATION_REQUIRED,
73
                'Mandatory header field (' . $fieldName . ') not provided or invalid.'
74
            );
75
        }
76 4
    }
77
78
    /**
79
     * Decider to stop other strategies running after from being considered.
80
     *
81
     * @return boolean
82
     */
83 2
    public function stopPropagation()
84
    {
85 2
        return $this->passed;
86
    }
87
}
88