-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOAuthSecurityServiceProvider.php
137 lines (113 loc) · 5.65 KB
/
OAuthSecurityServiceProvider.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
namespace Benjamin\Provider;
use Silex\Provider\SecurityServiceProvider;
use Silex\Application;
use Knp\Bundle\OAuthBundle\Security\Core\Authentication\Provider\OAuthProvider;
use Knp\Bundle\OAuthBundle\Security\Http\Firewall\OAuthListener;
use Knp\Bundle\OAuthBundle\Security\Http\EntryPoint\OAuthEntryPoint;
/**
* OAuthSecurityServiceProvider class.
*
* @author Benjamin Grandfond <[email protected]>
*/
class OAuthSecurityServiceProvider extends SecurityServiceProvider
{
/**
* Registers services on the given app.
*
* This method should only be used to configure services and parameters.
* It should not get services.
*
* @param Application $app An Application instance
*/
public function register(Application $app)
{
parent::register($app);
$that = $this;
$app['security.authentication_listener.factory.oauth'] = $app->protect(function ($name, $options) use ($app) {
if (!isset($app['security.oauth.provider'])) {
$app['security.oauth.provider'] = $app['security.oauth.provider.factory']($options);
}
if (!isset($app['security.entry_point.'.$name.'.oauth'])) {
$app['security.entry_point.'.$name.'.oauth'] = $app['security.entry_point.oauth._proto']($name, $options);
}
if (!isset($app['security.authentication_listener.'.$name.'.oauth'])) {
$app['security.authentication_listener.'.$name.'.oauth'] = $app['security.authentication_listener.oauth._proto']($name, $options);
}
if (!isset($app['security.authentication_provider.'.$name])) {
$app['security.authentication_provider.'.$name] = $app['security.authentication_provider.oauth._proto']($name);
}
return array(
'security.authentication_provider.'.$name,
'security.authentication_listener.'.$name.'.oauth',
'security.entry_point.'.$name.'.oauth',
'form'
);
});
$app['security.entry_point.oauth._proto'] = $app->protect(function($name, $options) use ($app) {
return $app->share(function () use ($app, $name, $options) {
$loginPath = isset($options['login_path']) ? $options['login_path'] : '/login';
$checkPath = isset($options['check_path']) ? $options['check_path'] : '/login_check';
return new OAuthEntryPoint(
$app['security.http_utils'],
$app['security.oauth.provider'],
$checkPath,
$loginPath
);
});
});
$app['security.oauth.provider.factory'] = $app->protect(function($options) use ($app) {
$classes = array(
'oauth' => 'Knp\\Bundle\\OAuthBundle\\Security\\Http\\OAuth\\OAuthProvider',
'facebook' => 'Knp\\Bundle\\OAuthBundle\\Security\\Http\\OAuth\\FacebookProvider',
'github' => 'Knp\\Bundle\\OAuthBundle\\Security\\Http\\OAuth\\GithubProvider',
'google' => 'Knp\\Bundle\\OAuthBundle\\Security\\Http\\OAuth\\GoogleProvider',
);
$type = isset($options['oauth_provider']) ? $options['oauth_provider'] : 'oauth';
if (false == isset($classes[$type])) {
throw new \InvalidArgumentException(sprintf('The provider "%s" does not exist.', $type));
}
$class = $classes[$type];
return $app->share(function () use ($app, $class, $options) {
return new $class(
$app['buzz.client'],
$app['security.http_utils'],
$options
);
});
});
$app['security.authentication_listener.oauth._proto'] = $app->protect(function ($name, $options) use ($app, $that) {
return $app->share(function () use ($app, $name, $options, $that) {
$that->addFakeRoute('match', $tmp = isset($options['check_path']) ? $options['check_path'] : '/login_check', str_replace('/', '_', ltrim($tmp, '/')));
if (!isset($app['security.authentication.success_handler.'.$name])) {
$app['security.authentication.success_handler.'.$name] = $app['security.authentication.success_handler._proto']($name, $options);
}
if (!isset($app['security.authentication.failure_handler.'.$name])) {
$app['security.authentication.failure_handler.'.$name] = $app['security.authentication.failure_handler._proto']($name, $options);
}
$listener = new OAuthListener(
$app['security'],
$app['security.authentication_manager'],
$app['security.session_strategy'],
$app['security.http_utils'],
$name,
$app['security.authentication.success_handler.'.$name],
$app['security.authentication.failure_handler.'.$name],
$options,
$app['logger'],
$app['dispatcher']
);
$listener->setOAuthProvider($app['security.oauth.provider']);
return $listener;
});
});
$app['security.authentication_provider.oauth._proto'] = $app->protect(function ($name) use ($app) {
return $app->share(function() use ($app, $name) {
return new OAuthProvider(
$app['security.user_provider.'.$name],
$app['security.oauth.provider']
);
});
});
}
}