Failed Conditions
Push — master ( 7c3864...930f9b )
by Florent
14:15
created

AuthorizationEndpointSource::load()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 44
rs 8.5937
c 0
b 0
f 0
cc 6
nc 10
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2018 Spomky-Labs
9
 *
10
 * This software may be modified and distributed under the terms
11
 * of the MIT license.  See the LICENSE file for details.
12
 */
13
14
namespace OAuth2Framework\ServerBundle\Component\Endpoint\Authorization;
15
16
use OAuth2Framework\Component\AuthorizationEndpoint\AuthorizationEndpoint;
17
use OAuth2Framework\Component\AuthorizationEndpoint\Consent\ConsentRepository;
18
use OAuth2Framework\Component\AuthorizationEndpoint\Extension\Extension;
19
use OAuth2Framework\Component\AuthorizationEndpoint\ParameterChecker\ParameterChecker;
20
use OAuth2Framework\Component\AuthorizationEndpoint\ResponseMode\ResponseMode;
21
use OAuth2Framework\Component\AuthorizationEndpoint\ResponseType\ResponseType;
22
use OAuth2Framework\Component\AuthorizationEndpoint\User\UserAuthenticationChecker;
23
use OAuth2Framework\Component\AuthorizationEndpoint\User\UserDiscovery;
24
use OAuth2Framework\Component\Core\User\UserRepository;
25
use OAuth2Framework\ServerBundle\Component\Component;
26
use OAuth2Framework\ServerBundle\Component\Endpoint\Authorization\Compiler\AuthorizationEndpointRouteCompilerPass;
27
use OAuth2Framework\ServerBundle\Component\Endpoint\Authorization\Compiler\AuthorizationRequestMetadataCompilerPass;
28
use OAuth2Framework\ServerBundle\Component\Endpoint\Authorization\Compiler\ConsentScreenExtensionCompilerPass;
29
use OAuth2Framework\ServerBundle\Component\Endpoint\Authorization\Compiler\ParameterCheckerCompilerPass;
30
use OAuth2Framework\ServerBundle\Component\Endpoint\Authorization\Compiler\ResponseModeCompilerPass;
31
use OAuth2Framework\ServerBundle\Component\Endpoint\Authorization\Compiler\ResponseTypeCompilerPass;
32
use OAuth2Framework\ServerBundle\Component\Endpoint\Authorization\Compiler\TemplatePathCompilerPass;
33
use OAuth2Framework\ServerBundle\Component\Endpoint\Authorization\Compiler\UserAuthenticationCheckerCompilerPass;
34
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
35
use Symfony\Component\Config\FileLocator;
36
use Symfony\Component\DependencyInjection\ContainerBuilder;
37
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
38
39
class AuthorizationEndpointSource implements Component
40
{
41
    /**
42
     * @var Component[]
43
     */
44
    private $subComponents = [];
45
46
    public function __construct()
47
    {
48
        $this->subComponents = [
49
            new ResponseModeSource(),
50
            new RequestObjectSource(),
51
        ];
52
    }
53
54
    public function name(): string
55
    {
56
        return 'authorization';
57
    }
58
59
    public function load(array $configs, ContainerBuilder $container)
60
    {
61
        if (!\class_exists(AuthorizationEndpoint::class)) {
62
            return;
63
        }
64
        $config = $configs['endpoint']['authorization'];
65
        $container->setParameter('oauth2_server.endpoint.authorization.enabled', $config['enabled']);
66
        if (!$config['enabled']) {
67
            return;
68
        }
69
70
        $container->registerForAutoconfiguration(ResponseType::class)->addTag('oauth2_server_response_type');
71
        $container->registerForAutoconfiguration(ResponseMode::class)->addTag('oauth2_server_response_mode');
72
        $container->registerForAutoconfiguration(ParameterChecker::class)->addTag('oauth2_server_authorization_parameter_checker');
73
        $container->registerForAutoconfiguration(UserAuthenticationChecker::class)->addTag('oauth2_server_user_authentication_checker');
74
        $container->registerForAutoconfiguration(Extension::class)->addTag('oauth2_server_consent_screen_extension');
75
76
        $loader = new PhpFileLoader($container, new FileLocator(__DIR__.'/../../../Resources/config/endpoint/authorization'));
77
        $loader->load('authorization.php');
78
79
        $container->setAlias(UserDiscovery::class, $config['user_discovery']);
80
        $container->setAlias(UserRepository::class, $config['user_repository']);
81
        if (!empty($config['consent_repository'])) {
82
            $container->setAlias(ConsentRepository::class, $config['consent_repository']);
83
        }
84
85
        $container->setParameter('oauth2_server.endpoint.authorization.authorization_endpoint_path', $config['authorization_endpoint_path']);
86
        $container->setParameter('oauth2_server.endpoint.authorization.login_endpoint_path', $config['login_endpoint_path']);
87
        $container->setParameter('oauth2_server.endpoint.authorization.consent_endpoint_path', $config['consent_endpoint_path']);
88
        $container->setParameter('oauth2_server.endpoint.authorization.select_account_endpoint_path', $config['select_account_endpoint_path']);
89
        $container->setParameter('oauth2_server.endpoint.authorization.process_endpoint_path', $config['process_endpoint_path']);
90
        $container->setParameter('oauth2_server.endpoint.authorization.host', $config['host']);
91
        $container->setParameter('oauth2_server.endpoint.authorization.enforce_state', $config['enforce_state']);
92
        $container->setParameter('oauth2_server.endpoint.authorization.handler.consent', $config['consent_handler']);
93
        $container->setParameter('oauth2_server.endpoint.authorization.handler.login', $config['login_handler']);
94
        $container->setParameter('oauth2_server.endpoint.authorization.handler.select_account', $config['select_account_handler']);
95
96
        if ($container->hasAlias('oauth2_server.http_client')) {
97
            $loader->load('sector_identifier_uri.php');
98
        }
99
        foreach ($this->subComponents as $subComponent) {
100
            $subComponent->load($configs, $container);
101
        }
102
    }
103
104
    public function getNodeDefinition(ArrayNodeDefinition $node, ArrayNodeDefinition $rootNode)
105
    {
106
        if (!\class_exists(AuthorizationEndpoint::class)) {
107
            return;
108
        }
109
        $childNode = $node->children()
110
            ->arrayNode($this->name())
111
            ->canBeEnabled();
112
113
        $childNode->children()
114
            ->scalarNode('authorization_endpoint_path')
115
            ->info('The path to the authorization endpoint.')
116
            ->defaultValue('/authorize')
117
            ->end()
118
            ->scalarNode('login_endpoint_path')
119
            ->info('The path to the login endpoint.')
120
            ->defaultValue('/authorize/{authorization_id}/login')
121
            ->end()
122
            ->scalarNode('consent_endpoint_path')
123
            ->info('The path to the consent endpoint.')
124
            ->defaultValue('/authorize/{authorization_id}/consent')
125
            ->end()
126
            ->scalarNode('select_account_endpoint_path')
127
            ->info('The path to the select account endpoint.')
128
            ->defaultValue('/authorize/{authorization_id}/select_account')
129
            ->end()
130
            ->scalarNode('process_endpoint_path')
131
            ->info('The path to the process endpoint.')
132
            ->defaultValue('/authorize/{authorization_id}/process')
133
            ->end()
134
            ->scalarNode('host')
135
            ->info('If set, the routes will be limited to that host')
136
            ->defaultValue('')
137
            ->treatFalseLike('')
138
            ->treatNullLike('')
139
            ->end()
140
            ->scalarNode('user_discovery')
141
            ->info('The user discovery service.')
142
            ->isRequired()
143
            ->end()
144
            ->scalarNode('user_repository')
145
            ->info('The user repository service.')
146
            ->isRequired()
147
            ->end()
148
            ->scalarNode('consent_repository')
149
            ->info('The pre-configured consent repository service.')
150
            ->defaultNull()
151
            ->end()
152
            ->scalarNode('enforce_state')
153
            ->info('If true the "state" parameter is mandatory (recommended).')
154
            ->defaultFalse()
155
            ->end()
156
            ->scalarNode('consent_handler')
157
            ->info('The consent handler.')
158
            ->isRequired()
159
            ->end()
160
            ->scalarNode('login_handler')
161
            ->info('The login handler.')
162
            ->isRequired()
163
            ->end()
164
            ->scalarNode('select_account_handler')
165
            ->info('The account selection handler.')
166
            ->isRequired()
167
            ->end()
168
            ->end();
169
170
        foreach ($this->subComponents as $subComponent) {
171
            $subComponent->getNodeDefinition($childNode, $node);
172
        }
173
    }
174
175
    public function prepend(ContainerBuilder $container, array $config): array
176
    {
177
        if (!\class_exists(AuthorizationEndpoint::class)) {
178
            return [];
179
        }
180
        if (!$config['endpoint']['authorization']['enabled']) {
181
            return [];
182
        }
183
184
        $updatedConfig = [];
185
        foreach ($this->subComponents as $subComponent) {
186
            $updatedConfig = \array_merge(
187
                $updatedConfig,
188
                $subComponent->prepend($container, $config)
189
            );
190
        }
191
192
        return $updatedConfig;
193
    }
194
195
    public function build(ContainerBuilder $container)
196
    {
197
        if (!\class_exists(AuthorizationEndpoint::class)) {
198
            return;
199
        }
200
        $container->addCompilerPass(new AuthorizationEndpointRouteCompilerPass());
201
        $container->addCompilerPass(new AuthorizationRequestMetadataCompilerPass());
202
        $container->addCompilerPass(new ConsentScreenExtensionCompilerPass());
203
        $container->addCompilerPass(new ParameterCheckerCompilerPass());
204
        $container->addCompilerPass(new ResponseModeCompilerPass());
205
        $container->addCompilerPass(new ResponseTypeCompilerPass());
206
        $container->addCompilerPass(new TemplatePathCompilerPass());
207
        $container->addCompilerPass(new UserAuthenticationCheckerCompilerPass());
208
209
        foreach ($this->subComponents as $component) {
210
            $component->build($container);
211
        }
212
    }
213
}
214