1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* authentification strategy based on a username cookie |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace Graviton\SecurityBundle\Authentication\Strategies; |
7
|
|
|
|
8
|
|
|
use Symfony\Component\HttpFoundation\Request; |
9
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class CookieFieldStrategy |
13
|
|
|
* |
14
|
|
|
* @author List of contributors <https://github.com/libgraviton/graviton/graphs/contributors> |
15
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
16
|
|
|
* @link http://swisscom.ch |
17
|
|
|
*/ |
18
|
|
|
class CookieFieldStrategy extends AbstractHttpStrategy |
19
|
|
|
{ |
20
|
|
|
/** @var string */ |
21
|
|
|
const COOKIE_FIELD_NAME = 'username'; |
22
|
|
|
|
23
|
|
|
/** @var string */ |
24
|
|
|
const COOKIE_VALUE_CORE_ID = 'finnova_id'; |
25
|
|
|
|
26
|
|
|
/** @var string */ |
27
|
|
|
const CONFIGURATION_PARAMETER_ID = 'graviton.security.core_id'; |
28
|
|
|
|
29
|
|
|
/** @var string */ |
30
|
|
|
protected $field; |
31
|
4 |
|
|
32
|
|
|
/** |
33
|
4 |
|
* @param string $field cookie field to be examined |
34
|
|
|
*/ |
35
|
|
|
public function __construct($field) |
36
|
|
|
{ |
37
|
|
|
$this->field = $field; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Applies the defined strategy on the provided request. |
42
|
|
|
* Value may contain a coma separated string values, we use first as identifier. |
43
|
|
|
* |
44
|
|
|
* @param Request $request request to handle |
45
|
|
|
* |
46
|
|
|
* @return string |
47
|
|
|
*/ |
48
|
|
|
public function apply(Request $request) |
49
|
|
|
{ |
50
|
|
|
$bagValue = $this->extractFieldInfo($request->cookies, $this->field); |
51
|
|
|
|
52
|
|
|
// this needs to be available in a later state of the application |
53
|
|
|
$this->extractCoreId($request, $bagValue); |
54
|
|
|
|
55
|
|
|
return $this->extractAdUsername($bagValue); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Finds and extracts the ad username from the cookie. |
60
|
|
|
* |
61
|
|
|
* @param string $value The string the value of self::COOKIE_FIELD_NAME shall be extracted from. |
62
|
|
|
* |
63
|
|
|
* @return string |
64
|
|
|
*/ |
65
|
|
|
protected function extractAdUsername($value) |
66
|
|
|
{ |
67
|
|
|
$pattern = "/((?m)(?<=\b".self::COOKIE_FIELD_NAME."=)[^,]*)/i"; |
68
|
|
|
preg_match($pattern, $value, $matches); |
69
|
|
|
|
70
|
|
|
return (!$matches)? $value : $matches[0]; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Finds and extracts the core system id from tha cookie. |
75
|
|
|
* |
76
|
|
|
* |
77
|
|
|
* @param Request $request Request stack that controls the lifecycle of requests |
78
|
|
|
* @param string $text String to be examined for the core id. |
79
|
|
|
* |
80
|
|
|
* @return null |
81
|
|
|
*/ |
82
|
|
|
protected function extractCoreId(Request $request, $text) |
83
|
|
|
{ |
84
|
|
|
$pattern = "/((?m)(?<=\b".self::COOKIE_VALUE_CORE_ID."=)[^,]*)/i"; |
85
|
|
|
preg_match($pattern, $text, $matches); |
86
|
|
|
|
87
|
|
|
if ($matches) { |
|
|
|
|
88
|
|
|
$request->attributes->set(self::CONFIGURATION_PARAMETER_ID, $matches[0]); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.