1 | <?php |
||
17 | class OAuth2ServerProvider implements ServiceProviderInterface, ControllerProviderInterface |
||
18 | { |
||
19 | private $storagesTypes = [ |
||
20 | 'access_token', |
||
21 | 'authorization_code', |
||
22 | 'client_credentials', |
||
23 | 'client', |
||
24 | 'refresh_token', |
||
25 | 'user_credentials', |
||
26 | 'user_claims', |
||
27 | 'public_key', |
||
28 | 'jwt_bearer', |
||
29 | 'scope', |
||
30 | ]; |
||
31 | |||
32 | /** |
||
33 | * @inherit |
||
34 | */ |
||
35 | public function register(Container $container) |
||
36 | { |
||
37 | $container['security.authentication_listener.factory.oauth2'] = $container->protect( |
||
38 | $this->factory($container) |
||
39 | ); |
||
40 | |||
41 | $container['oauth2_server'] = $this->OAuth2Server($container); |
||
42 | |||
43 | $this->setupControllers($container); |
||
44 | |||
45 | $container['oauth2_server.authorize_renderer.view'] = __DIR__ . '/../../views/authorize.php'; |
||
46 | |||
47 | $container['oauth2_server.authorize_renderer'] = function (Container $container) { |
||
48 | return new HTMLAuthorizeRenderer($container['oauth2_server.authorize_renderer.view']); |
||
49 | }; |
||
50 | } |
||
51 | |||
52 | private function factory(Container $container) |
||
53 | { |
||
54 | return function ($name) use ($container) { |
||
55 | $this->registerFactoryDeps($container, $name); |
||
56 | |||
57 | return [ |
||
58 | 'security.authentication_provider.'.$name.'.dao', |
||
59 | 'security.authentication_listener.'.$name.'.oauth2', |
||
60 | 'security.entry_point.'.$name.'.oauth2', |
||
61 | 'pre_auth' |
||
62 | ]; |
||
63 | }; |
||
64 | } |
||
65 | |||
66 | private function registerFactoryDeps(Container $container, $name) |
||
67 | { |
||
68 | if (!isset($container['security.entry_point.'.$name.'.oauth2.realm'])) { |
||
69 | $container['security.entry_point.'.$name.'.oauth2'] = 'AppName'; |
||
70 | } |
||
71 | if (!isset($container['security.entry_point.'.$name.'.oauth2'])) { |
||
72 | $realm = $container['security.entry_point.'.$name.'.oauth2.realm']; |
||
73 | $container['security.entry_point.'.$name.'.oauth2'] = new OAuth2EntryPoint($realm); |
||
74 | } |
||
75 | $this->registerAuthenticationListener($container, $name); |
||
76 | if (!isset($container['security.authentication_provider.'.$name.'.dao'])) { |
||
77 | $container['security.authentication_provider.'.$name.'.dao'] = function () use ($container, $name) { |
||
78 | return new OAuth2AuthentificationProvider( |
||
79 | $container['security.user_provider.'.$name], |
||
80 | $container['security.user_checker'], |
||
81 | $name |
||
82 | ); |
||
83 | }; |
||
84 | } |
||
85 | } |
||
86 | |||
87 | private function registerAuthenticationListener(Container $container, $name) |
||
88 | { |
||
89 | if (!isset($container['security.authentication_listener.'.$name.'.oauth2'])) { |
||
90 | $authListener = function () use ($container, $name) { |
||
91 | return new OAuth2AuthenticationListener( |
||
92 | $container['oauth2_server'], |
||
93 | $container['security.token_storage'], |
||
94 | $container['security.authentication_manager'], |
||
95 | $name, |
||
96 | $container['security.entry_point.'.$name.'.oauth2'], |
||
97 | $container['logger'] |
||
98 | ); |
||
99 | }; |
||
100 | $container['security.authentication_listener.'.$name.'.oauth2'] = $authListener; |
||
101 | } |
||
102 | } |
||
103 | |||
104 | private function OAuth2Server(Container $container) |
||
105 | { |
||
106 | $container['oauth2_server.parameters'] = []; |
||
107 | |||
108 | $container['oauth2_server.storage.default'] = function (Container $container) { |
||
109 | return new Pdo($container['oauth2_server.storage.pdo_connection']); |
||
110 | }; |
||
111 | |||
112 | $container['oauth2_server.storage.types'] = ['client', 'access_token']; |
||
113 | |||
114 | $container['oauth2_server.storage'] = function (Container $container) { |
||
115 | $storages = []; |
||
116 | foreach ($container['oauth2_server.storage.types'] as $storageType) { |
||
117 | $storages[$storageType] = $container['oauth2_server.storage.'.$storageType]; |
||
118 | } |
||
119 | return $storages; |
||
120 | }; |
||
121 | |||
122 | foreach ($this->storagesTypes as $storageType) { |
||
123 | $container['oauth2_server.storage.'.$storageType] = function (Container $container) { |
||
124 | return $container['oauth2_server.storage.default']; |
||
125 | }; |
||
126 | } |
||
127 | |||
128 | $container['oauth2_server.config'] = function () { |
||
129 | return ['allow_implicit' => true, 'enforce_state' => false,]; |
||
130 | }; |
||
131 | |||
132 | $container['oauth2_server.grant_types'] = function () { |
||
133 | return []; |
||
134 | }; |
||
135 | |||
136 | $container['oauth2_server.response_types'] = function () { |
||
137 | return []; |
||
138 | }; |
||
139 | |||
140 | $container['oauth2_server.token_type'] = function () { |
||
141 | return null; |
||
142 | }; |
||
143 | |||
144 | $container['oauth2_server.scope_util'] = function () { |
||
145 | return null; |
||
146 | }; |
||
147 | |||
148 | $container['oauth2_server.client_assertion_type'] = function () { |
||
149 | return null; |
||
150 | }; |
||
151 | |||
152 | return function (Container $container) { |
||
153 | return new Server( |
||
154 | $container['oauth2_server.storage'], |
||
155 | $container['oauth2_server.config'], |
||
156 | $container['oauth2_server.grant_types'], |
||
157 | $container['oauth2_server.response_types'], |
||
158 | $container['oauth2_server.token_type'], |
||
159 | $container['oauth2_server.scope_util'], |
||
160 | $container['oauth2_server.client_assertion_type'] |
||
161 | ); |
||
162 | }; |
||
163 | } |
||
164 | |||
165 | private function setupControllers(Container $container) { |
||
166 | $container['oauth2_server.controllers_as_service'] = false; |
||
167 | $container['oauth2_server.controllers.authorize'] = function (Container $container) { |
||
168 | return new Controllers\AuthorizeHandler( |
||
169 | $container['oauth2_server']->getAuthorizeController(), |
||
170 | $container['oauth2_server.authorize_renderer'] |
||
171 | ); |
||
172 | }; |
||
173 | $container['oauth2_server.controllers.authorize_validator'] = function (Container $container) { |
||
174 | return new Controllers\AuthorizeValidator( |
||
175 | $container['url_generator'], |
||
176 | $container['oauth2_server']->getAuthorizeController(), |
||
177 | $container['oauth2_server.authorize_renderer'] |
||
178 | ); |
||
179 | }; |
||
180 | $container['oauth2_server.controllers.authorize_handler'] = function (Container $container) { |
||
181 | return new Controllers\AuthorizeHandler( |
||
182 | $container['oauth2_server']->getAuthorizeController(), |
||
183 | $container['oauth2_server.authorize_renderer'] |
||
184 | ); |
||
185 | }; |
||
186 | $container['oauth2_server.controllers.token'] = function (Container $container) { |
||
187 | return new Controllers\TokenHandler($container['oauth2_server']->getTokenController()); |
||
188 | }; |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * @inherit |
||
193 | */ |
||
194 | public function connect(Application $app) |
||
214 | } |
||
215 |