Completed
Push — feature/evo-2472-whoami ( 843346...066c1c )
by
unknown
83:53 queued 67:03
created

AbstractHttpStrategy::validateField()   C

Complexity

Conditions 7
Paths 9

Size

Total Lines 36
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 36
rs 6.7272
ccs 21
cts 21
cp 1
cc 7
eloc 19
nc 9
nop 2
crap 7
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
    protected $strategyMatch;
24
    /**
25
     * Extracts information from the a request header field.
26
     *
27
     * @param ParameterBag|HeaderBag $header    object representation of the request header.
28
     * @param string                 $fieldname Name of the field to be read.
29
     *
30
     * @return string
31
     */
32 11
    protected function extractFieldInfo($header, $fieldname)
33
    {
34 11
        if ($header instanceof ParameterBag || $header instanceof HeaderBag) {
35 10
            $this->validateField($header, $fieldname);
36 10
            return $header->get($fieldname, '');
37
        }
38
39 1
        throw new \InvalidArgumentException('Provided request information are not valid.');
40
    }
41
42
    /**
43
     * Verifies that the provided header has the expected/mandatory fields.
44
     *
45
     * @param ParameterBag|HeaderBag $header    object representation of the request header.
46
     * @param string                 $fieldName Name of the header field to be validated.
47
     *
48
     * @return void
49
     * @throws \Symfony\Component\HttpKernel\Exception\HttpException
50
     */
51 13
    protected function validateField($header, $fieldName)
52
    {
53 13
        $passed = $header->has($fieldName);
54
        // return without exception so we can return a dummy user
55 13
        if (!$passed) {
56 6
            return false;
57
        }
58
59
        // get rid of anything not a valid character
60 11
        $authInfo = filter_var($header->get($fieldName), FILTER_SANITIZE_STRING);
61
62
        // get rid of whitespaces
63 11
        $patterns = array("\r\n", "\n", "\r", "\s", "\t");
64 11
        $authInfo = str_replace($patterns, "", trim($authInfo));
65
66 11
        if (false !== $passed && !empty($authInfo)) {
67 8
            $passed = true;
68 8
        } else {
69 3
            $passed = false;
70
        }
71
72
        // get rid of control characters
73 11
        if (false !== $passed && $authInfo === preg_replace('#[[:cntrl:]]#i', '', $authInfo)) {
74 8
            $passed = true;
75 8
        } else {
76 3
            $passed = false;
77
        }
78
79
80 11
        if (false === $passed) {
81 3
            throw new HttpException(
82 3
                Response::HTTP_NETWORK_AUTHENTICATION_REQUIRED,
83 3
                'Mandatory header field (' . $fieldName . ') not provided or invalid.'
84 3
            );
85
        }
86 8
    }
87
}
88