Failed Conditions
Pull Request — master (#4)
by Mathieu
10:58
created

OAuth2ServerProvider::setupControllers()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 25
rs 8.8571
cc 1
eloc 17
nc 1
nop 1
1
<?php
2
3
namespace TH\OAuth2\Pimple;
4
5
use Pimple\Container;
6
use Pimple\ServiceProviderInterface;
7
use Silex\Application;
8
use Silex\Api\ControllerProviderInterface;
9
use OAuth2\Server;
10
use TH\OAuth2\OAuth2AuthentificationProvider;
11
use TH\OAuth2\OAuth2EntryPoint;
12
use TH\OAuth2\OAuth2AuthenticationListener;
13
use TH\OAuth2\Controllers;
14
use TH\OAuth2\HTMLAuthorizeRenderer;
15
use OAuth2\Storage\Pdo;
16
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
        $container['oauth2_server'] = $this->OAuth2Server($container);
41
        $this->setupControllers($container);
42
        $container['oauth2_server.authorize_renderer.view'] = __DIR__ . '/../../views/authorize.php';
43
        $container['oauth2_server.authorize_renderer'] = function (Container $container) {
44
            return new HTMLAuthorizeRenderer($container['oauth2_server.authorize_renderer.view']);
45
        };
46
    }
47
48
    private function factory(Container $container)
49
    {
50
        return function ($name) use ($container) {
51
            $this->registerFactoryDeps($container, $name);
52
            return [
53
                'security.authentication_provider.'.$name.'.dao',
54
                'security.authentication_listener.'.$name.'.oauth2',
55
                'security.entry_point.'.$name.'.oauth2',
56
                'pre_auth'
57
            ];
58
        };
59
    }
60
61
    private function registerFactoryDeps(Container $container, $name)
62
    {
63
        if (!isset($container['security.entry_point.'.$name.'.oauth2.realm'])) {
64
            $container['security.entry_point.'.$name.'.oauth2'] = 'AppName';
65
        }
66
        if (!isset($container['security.entry_point.'.$name.'.oauth2'])) {
67
            $realm = $container['security.entry_point.'.$name.'.oauth2.realm'];
68
            $container['security.entry_point.'.$name.'.oauth2'] = new OAuth2EntryPoint($realm);
69
        }
70
        $this->registerAuthenticationListener($container, $name);
71
        if (!isset($container['security.authentication_provider.'.$name.'.dao'])) {
72
            $container['security.authentication_provider.'.$name.'.dao'] = function () use ($container, $name) {
73
                return new OAuth2AuthentificationProvider(
74
                    $container['security.user_provider.'.$name],
75
                    $container['security.user_checker'],
76
                    $name
77
                );
78
            };
79
        }
80
    }
81
82
    private function registerAuthenticationListener(Container $container, $name)
83
    {
84
        if (!isset($container['security.authentication_listener.'.$name.'.oauth2'])) {
85
            $authListener = function () use ($container, $name) {
86
                return new OAuth2AuthenticationListener(
87
                    $container['oauth2_server'],
88
                    $container['security.token_storage'],
89
                    $container['security.authentication_manager'],
90
                    $name,
91
                    $container['security.entry_point.'.$name.'.oauth2'],
92
                    $container['logger']
93
                );
94
            };
95
            $container['security.authentication_listener.'.$name.'.oauth2'] = $authListener;
96
        }
97
    }
98
99
    private function OAuth2Server(Container $container)
100
    {
101
        $container['oauth2_server.parameters'] = [];
102
        $container['oauth2_server.storage.default'] = function (Container $container) {
103
            return new Pdo($container['oauth2_server.storage.pdo_connection']);
104
        };
105
        $container['oauth2_server.storage.types'] = ['client', 'access_token'];
106
        $container['oauth2_server.storage'] = function (Container $container) {
107
            $storages = [];
108
            foreach ($container['oauth2_server.storage.types'] as $storageType) {
109
                $storages[$storageType] = $container['oauth2_server.storage.'.$storageType];
110
            }
111
            return $storages;
112
        };
113
        foreach ($this->storagesTypes as $storageType) {
114
            $container['oauth2_server.storage.'.$storageType] = function (Container $container) {
115
                return $container['oauth2_server.storage.default'];
116
            };
117
        }
118
        $container['oauth2_server.config'] = function () {
119
            return ['allow_implicit' => true, 'enforce_state' => false,];
120
        };
121
        $container['oauth2_server.grant_types'] = function () {
122
            return [];
123
        };
124
        $container['oauth2_server.response_types'] = function () {
125
            return [];
126
        };
127
        $container['oauth2_server.token_type'] = function () {
128
            return null;
129
        };
130
        $container['oauth2_server.scope_util'] = function () {
131
            return null;
132
        };
133
        $container['oauth2_server.client_assertion_type'] = function () {
134
            return null;
135
        };
136
        return function (Container $container) {
137
            return new Server(
138
                $container['oauth2_server.storage'],
139
                $container['oauth2_server.config'],
140
                $container['oauth2_server.grant_types'],
141
                $container['oauth2_server.response_types'],
142
                $container['oauth2_server.token_type'],
143
                $container['oauth2_server.scope_util'],
144
                $container['oauth2_server.client_assertion_type']
145
            );
146
        };
147
    }
148
149
    private function setupControllers(Container $container) {
150
        $container['oauth2_server.controllers_as_service'] = false;
151
        $container['oauth2_server.controllers.authorize'] = function (Container $container) {
152
            return new Controllers\AuthorizeHandler(
153
                $container['oauth2_server']->getAuthorizeController(),
154
                $container['oauth2_server.authorize_renderer']
155
            );
156
        };
157
        $container['oauth2_server.controllers.authorize_validator'] = function (Container $container) {
158
            return new Controllers\AuthorizeValidator(
159
                $container['url_generator'],
160
                $container['oauth2_server']->getAuthorizeController(),
161
                $container['oauth2_server.authorize_renderer']
162
            );
163
        };
164
        $container['oauth2_server.controllers.authorize_handler'] = function (Container $container) {
165
            return new Controllers\AuthorizeHandler(
166
                $container['oauth2_server']->getAuthorizeController(),
167
                $container['oauth2_server.authorize_renderer']
168
            );
169
        };
170
        $container['oauth2_server.controllers.token'] = function (Container $container) {
171
            return new Controllers\TokenHandler($container['oauth2_server']->getTokenController());
172
        };
173
    }
174
175
    /**
176
     * @inherit
177
     */
178
    public function connect(Application $app)
179
    {
180
        $controllers = $app['controllers_factory'];
181
        if ($app['oauth2_server.controllers_as_service']) {
182
            $controllers->post('/authorize', 'oauth2_server.controllers.authorize_handler:__invoke')
183
                ->bind('oauth2_authorize_handler');
184
            $controllers->get('/authorize', 'oauth2_server.controllers.authorize_validator:__invoke')
185
                ->bind('oauth2_authorize_validator');
186
            $controllers->post('/token', 'oauth2_server.controllers.token:__invoke')->bind('oauth2_token_handler');
187
        } else {
188
            $controllers->post('/authorize', $app['oauth2_server.controllers.authorize_handler'])
189
                ->bind('oauth2_authorize_handler');
190
            $controllers->get('/authorize', $app['oauth2_server.controllers.authorize_validator'])
191
                ->bind('oauth2_authorize_validator');
192
            $controllers->post('/token', $app['oauth2_server.controllers.token'])->bind('oauth2_token_handler');
193
        }
194
        return $controllers;
195
    }
196
}
197