This repository has been archived by the owner on Sep 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
RememberMeServiceProvider.php
107 lines (91 loc) · 4.36 KB
/
RememberMeServiceProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
/*
* This file is part of the Silex framework.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Silex\Provider;
use Pimple\Container;
use Pimple\ServiceProviderInterface;
use Silex\Api\EventListenerProviderInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Security\Core\Authentication\Provider\RememberMeAuthenticationProvider;
use Symfony\Component\Security\Http\Firewall\RememberMeListener;
use Symfony\Component\Security\Http\RememberMe\TokenBasedRememberMeServices;
use Symfony\Component\Security\Http\RememberMe\ResponseListener;
/**
* Remember-me authentication for the SecurityServiceProvider.
*
* @author Jérôme Tamarelle <[email protected]>
*/
class RememberMeServiceProvider implements ServiceProviderInterface, EventListenerProviderInterface
{
public function register(Container $app)
{
$app['security.remember_me.response_listener'] = function ($app) {
if (!isset($app['security.token_storage'])) {
throw new \LogicException('You must register the SecurityServiceProvider to use the RememberMeServiceProvider');
}
return new ResponseListener();
};
$app['security.authentication_listener.factory.remember_me'] = $app->protect(function ($name, $options) use ($app) {
if (empty($options['key'])) {
$options['key'] = $name;
}
if (!isset($app['security.remember_me.service.'.$name])) {
$app['security.remember_me.service.'.$name] = $app['security.remember_me.service._proto']($name, $options);
}
if (!isset($app['security.authentication_listener.'.$name.'.remember_me'])) {
$app['security.authentication_listener.'.$name.'.remember_me'] = $app['security.authentication_listener.remember_me._proto']($name, $options);
}
if (!isset($app['security.authentication_provider.'.$name.'.remember_me'])) {
$app['security.authentication_provider.'.$name.'.remember_me'] = $app['security.authentication_provider.remember_me._proto']($name, $options);
}
return [
'security.authentication_provider.'.$name.'.remember_me',
'security.authentication_listener.'.$name.'.remember_me',
null, // entry point
'remember_me',
];
});
$app['security.remember_me.service._proto'] = $app->protect(function ($providerKey, $options) use ($app) {
return function () use ($providerKey, $options, $app) {
$options = array_replace([
'name' => 'REMEMBERME',
'lifetime' => 31536000,
'path' => '/',
'domain' => null,
'secure' => false,
'httponly' => true,
'always_remember_me' => false,
'remember_me_parameter' => '_remember_me',
], $options);
return new TokenBasedRememberMeServices([$app['security.user_provider.'.$providerKey]], $options['key'], $providerKey, $options, $app['logger']);
};
});
$app['security.authentication_listener.remember_me._proto'] = $app->protect(function ($providerKey) use ($app) {
return function () use ($app, $providerKey) {
$listener = new RememberMeListener(
$app['security.token_storage'],
$app['security.remember_me.service.'.$providerKey],
$app['security.authentication_manager'],
$app['logger'],
$app['dispatcher']
);
return $listener;
};
});
$app['security.authentication_provider.remember_me._proto'] = $app->protect(function ($name, $options) use ($app) {
return function () use ($app, $name, $options) {
return new RememberMeAuthenticationProvider($app['security.user_checker'], $options['key'], $name);
};
});
}
public function subscribe(Container $app, EventDispatcherInterface $dispatcher)
{
$dispatcher->addSubscriber($app['security.remember_me.response_listener']);
}
}