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
|
9 |
|
* @return string |
31
|
|
|
*/ |
32
|
9 |
|
protected function extractFieldInfo($header, $fieldname) |
33
|
8 |
|
{ |
34
|
|
|
if ($header instanceof ParameterBag || $header instanceof HeaderBag) { |
35
|
8 |
|
$this->validateField($header, $fieldname); |
36
|
|
|
return $header->get($fieldname, ''); |
37
|
|
|
} |
38
|
1 |
|
|
39
|
|
|
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
|
11 |
|
*/ |
51
|
|
|
protected function validateField($header, $fieldName) |
52
|
11 |
|
{ |
53
|
|
|
$passed = $header->has($fieldName); |
54
|
|
|
// return without exception so we can return a dummy user |
55
|
11 |
|
if(!$passed) { |
56
|
|
|
return false; |
57
|
|
|
} |
58
|
11 |
|
|
59
|
11 |
|
// get rid of anything not a valid character |
60
|
|
|
$authInfo = filter_var($header->get($fieldName), FILTER_SANITIZE_STRING); |
61
|
11 |
|
|
62
|
8 |
|
// get rid of whitespaces |
63
|
8 |
|
$patterns = array("\r\n", "\n", "\r", "\s", "\t"); |
64
|
3 |
|
$authInfo = str_replace($patterns, "", trim($authInfo)); |
65
|
|
|
|
66
|
|
|
if (false !== $passed && !empty($authInfo)) { |
67
|
|
|
$passed = true; |
68
|
11 |
|
} else { |
69
|
8 |
|
$passed = false; |
70
|
8 |
|
} |
71
|
3 |
|
|
72
|
|
|
// get rid of control characters |
73
|
|
|
if (false !== $passed && $authInfo === preg_replace('#[[:cntrl:]]#i', '', $authInfo)) { |
74
|
|
|
$passed = true; |
75
|
11 |
|
} else { |
76
|
3 |
|
$passed = false; |
77
|
3 |
|
} |
78
|
3 |
|
|
79
|
3 |
|
|
80
|
|
|
if (false === $passed) { |
81
|
8 |
|
throw new HttpException( |
82
|
|
|
Response::HTTP_NETWORK_AUTHENTICATION_REQUIRED, |
83
|
|
|
'Mandatory header field (' . $fieldName . ') not provided or invalid.' |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|