|
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
|
|
|
* Extracts information from the a request header field. |
|
24
|
|
|
* |
|
25
|
|
|
* @param ParameterBag|HeaderBag $header object representation of the request header. |
|
26
|
|
|
* @param string $fieldname Name of the field to be read. |
|
27
|
|
|
* |
|
28
|
|
|
* @return string |
|
29
|
|
|
*/ |
|
30
|
|
|
protected function extractFieldInfo($header, $fieldname) |
|
31
|
|
|
{ |
|
32
|
|
|
if ($header instanceof ParameterBag || $header instanceof HeaderBag) { |
|
33
|
|
|
$this->validateField($header, $fieldname); |
|
34
|
|
|
return $header->get($fieldname, ''); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
throw new \InvalidArgumentException('Provided request information are not valid.'); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Verifies that the provided header has the expected/mandatory fields. |
|
42
|
|
|
* |
|
43
|
|
|
* @param ParameterBag|HeaderBag $header object representation of the request header. |
|
44
|
|
|
* @param string $fieldName Name of the header field to be validated. |
|
45
|
|
|
* |
|
46
|
|
|
* @return void |
|
47
|
|
|
* @throws \Symfony\Component\HttpKernel\Exception\HttpException |
|
48
|
|
|
*/ |
|
49
|
|
|
protected function validateField($header, $fieldName) |
|
50
|
|
|
{ |
|
51
|
|
|
$passed = $header->has($fieldName); |
|
52
|
|
|
|
|
53
|
|
|
// return without exception so we can return a dummy user |
|
54
|
|
|
if (true === $passed) { |
|
55
|
|
|
// get rid of anything not a valid character |
|
56
|
|
|
$authInfo = filter_var($header->get($fieldName), FILTER_SANITIZE_STRING); |
|
57
|
|
|
|
|
58
|
|
|
// get rid of whitespaces |
|
59
|
|
|
$patterns = array("\r\n", "\n", "\r", "\s", "\t"); |
|
60
|
|
|
$authInfo = str_replace($patterns, "", trim($authInfo)); |
|
61
|
|
|
|
|
62
|
|
|
// get rid of control characters |
|
63
|
|
|
if (empty($authInfo) || $authInfo !== preg_replace('#[[:cntrl:]]#i', '', $authInfo)) { |
|
64
|
|
|
throw new HttpException( |
|
65
|
|
|
Response::HTTP_NETWORK_AUTHENTICATION_REQUIRED, |
|
66
|
|
|
'Mandatory header field (' . $fieldName . ') not provided or invalid.' |
|
67
|
|
|
); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Decider to stop other strategies running after from being considered. |
|
74
|
|
|
* |
|
75
|
|
|
* @return boolean |
|
76
|
|
|
*/ |
|
77
|
|
|
public function stopPropagation() |
|
78
|
|
|
{ |
|
79
|
|
|
return true; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|