Skip to content

Commit ea1b617

Browse files
blanksedbu
authored andcommitted
Expand AnonymousRequestMatcher by user_identifier_headers option
1 parent 6276652 commit ea1b617

File tree

4 files changed

+63
-11
lines changed

4 files changed

+63
-11
lines changed

CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
11
Changelog
22
=========
33

4+
1.4.5
5+
-----
6+
7+
* Symfony user context: You can now also specify which headers are used for
8+
authentication to detect anonymous requests. By default, the headers are the
9+
previously hardcoded `Authorization`, `HTTP_AUTHORIZATION` and
10+
`PHP_AUTH_USER`.
11+
12+
1.4.4
13+
-----
14+
15+
* Avoid problem with [http_method_override](http://symfony.com/doc/current/reference/configuration/framework.html#configuration-framework-http-method-override).
16+
417
1.4.3
518
-----
619

doc/symfony-cache-configuration.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,10 @@ options through the constructor:
147147

148148
**default**: ``GET``
149149

150+
* **user_identifier_headers**: List of request headers that authenticate a non-anonymous request.
151+
152+
**default**: ``['Authorization', 'HTTP_AUTHORIZATION', 'PHP_AUTH_USER']``
153+
150154
* **session_name_prefix**: Prefix for session cookies. Must match your PHP session configuration.
151155

152156
**default**: ``PHPSESSID``

src/SymfonyCache/UserContextSubscriber.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\HttpFoundation\Request;
1717
use Symfony\Component\HttpFoundation\Response;
1818
use Symfony\Component\HttpKernel\HttpKernelInterface;
19+
use Symfony\Component\HttpKernel\Kernel;
1920
use Symfony\Component\OptionsResolver\OptionsResolver;
2021

2122
/**
@@ -51,6 +52,7 @@ class UserContextSubscriber implements EventSubscriberInterface
5152
* match the setup for the Vary header in the backend application.
5253
* - user_hash_uri: Target URI used in the request for user context hash generation.
5354
* - user_hash_method: HTTP Method used with the hash lookup request for user context hash generation.
55+
* - user_identifier_headers: List of request headers that authenticate a non-anonymous request.
5456
* - session_name_prefix: Prefix for session cookies. Must match your PHP session configuration.
5557
*
5658
* @param array $options Options to overwrite the default options
@@ -66,8 +68,21 @@ public function __construct(array $options = array())
6668
'user_hash_header' => 'X-User-Context-Hash',
6769
'user_hash_uri' => '/_fos_user_context_hash',
6870
'user_hash_method' => 'GET',
71+
'user_identifier_headers' => array('Authorization', 'HTTP_AUTHORIZATION', 'PHP_AUTH_USER'),
6972
'session_name_prefix' => 'PHPSESSID',
7073
));
74+
if (class_exists('Symfony\Component\HttpKernel\Kernel')
75+
&& (Kernel::MAJOR_VERSION > 2 || Kernel::MINOR_VERSION > 5)
76+
) {
77+
$resolver->setAllowedTypes('anonymous_hash', array('string'));
78+
$resolver->setAllowedTypes('user_hash_accept_header', array('string'));
79+
$resolver->setAllowedTypes('user_hash_header', array('string'));
80+
$resolver->setAllowedTypes('user_hash_uri', array('string'));
81+
$resolver->setAllowedTypes('user_hash_method', array('string'));
82+
// actually string[] but that is not supported by symfony < 3.4
83+
$resolver->setAllowedTypes('user_identifier_headers', array('array'));
84+
$resolver->setAllowedTypes('session_name_prefix', array('string'));
85+
}
7186

7287
$this->options = $resolver->resolve($options);
7388
}
@@ -186,7 +201,10 @@ private function getUserHash(HttpKernelInterface $kernel, Request $request)
186201
*/
187202
private function isAnonymous(Request $request)
188203
{
189-
$anonymousRequestMatcher = new AnonymousRequestMatcher($this->options['session_name_prefix']);
204+
$anonymousRequestMatcher = new AnonymousRequestMatcher(array(
205+
'user_identifier_headers' => $this->options['user_identifier_headers'],
206+
'session_name_prefix' => $this->options['session_name_prefix'],
207+
));
190208

191209
return $anonymousRequestMatcher->matches($request);
192210
}

src/UserContext/AnonymousRequestMatcher.php

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,36 +13,53 @@
1313

1414
use Symfony\Component\HttpFoundation\Request;
1515
use Symfony\Component\HttpFoundation\RequestMatcherInterface;
16+
use Symfony\Component\HttpKernel\Kernel;
17+
use Symfony\Component\OptionsResolver\OptionsResolver;
1618

1719
/**
1820
* Matches anonymous requests using a list of identification headers.
1921
*/
2022
class AnonymousRequestMatcher implements RequestMatcherInterface
2123
{
22-
private $sessionNamePrefix;
24+
/**
25+
* @var array
26+
*/
27+
private $options;
2328

2429
/**
25-
* @param string $sessionNamePrefix Prefix for session cookies. Must match your PHP session configuration
30+
* @param array $options Configuration for the matcher. All options are required because this matcher is usually
31+
* created by the UserContextSubscriber which provides the default values.
32+
*
33+
* @throws \InvalidArgumentException if unknown keys are found in $options
2634
*/
27-
public function __construct($sessionNamePrefix)
35+
public function __construct(array $options = array())
2836
{
29-
$this->sessionNamePrefix = $sessionNamePrefix;
37+
$resolver = new OptionsResolver();
38+
$resolver->setRequired(array('user_identifier_headers', 'session_name_prefix'));
39+
if (class_exists('Symfony\Component\HttpKernel\Kernel')
40+
&& (Kernel::MAJOR_VERSION > 2 || Kernel::MINOR_VERSION > 5)
41+
) {
42+
// actually string[] but that is not supported by symfony < 3.4
43+
$resolver->setAllowedTypes('user_identifier_headers', array('array'));
44+
$resolver->setAllowedTypes('session_name_prefix', array('string'));
45+
}
46+
47+
$this->options = $resolver->resolve($options);
3048
}
3149

3250
public function matches(Request $request)
3351
{
3452
// You might have to enable rewriting of the Authorization header in your server config or .htaccess:
3553
// RewriteEngine On
3654
// RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
37-
if ($request->server->has('AUTHORIZATION') ||
38-
$request->server->has('HTTP_AUTHORIZATION') ||
39-
$request->server->has('PHP_AUTH_USER')
40-
) {
41-
return false;
55+
foreach ($this->options['user_identifier_headers'] as $header) {
56+
if ($request->headers->has($header)) {
57+
return false;
58+
}
4259
}
4360

4461
foreach ($request->cookies as $name => $value) {
45-
if (0 === strpos($name, $this->sessionNamePrefix)) {
62+
if (0 === strpos($name, $this->options['session_name_prefix'])) {
4663
return false;
4764
}
4865
}

0 commit comments

Comments
 (0)