Failed Conditions
Push — master ( 1325ac...02feb2 )
by Florent
19:27
created

AuthorizationEndpointSource   C

Complexity

Total Complexity 16

Size/Duplication

Total Lines 171
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 19

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 19
dl 0
loc 171
rs 6.875
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A name() 0 4 1
B load() 0 36 4
A getNodeDefinition() 0 55 3
A prepend() 0 19 4
A build() 0 19 3
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\UserAccount\UserAccountChecker;
18
use OAuth2Framework\Component\AuthorizationEndpoint\UserAccount\UserAccountDiscovery;
19
use OAuth2Framework\ServerBundle\Component\Component;
20
use OAuth2Framework\ServerBundle\Component\Endpoint\Authorization\Compiler\AuthorizationEndpointRouteCompilerPass;
21
use OAuth2Framework\ServerBundle\Component\Endpoint\Authorization\Compiler\AuthorizationRequestMetadataCompilerPass;
22
use OAuth2Framework\ServerBundle\Component\Endpoint\Authorization\Compiler\ConsentScreenExtensionCompilerPass;
23
use OAuth2Framework\ServerBundle\Component\Endpoint\Authorization\Compiler\ParameterCheckerCompilerPass;
24
use OAuth2Framework\ServerBundle\Component\Endpoint\Authorization\Compiler\ResponseModeCompilerPass;
25
use OAuth2Framework\ServerBundle\Component\Endpoint\Authorization\Compiler\ResponseTypeCompilerPass;
26
use OAuth2Framework\ServerBundle\Component\Endpoint\Authorization\Compiler\TemplatePathCompilerPass;
27
use OAuth2Framework\ServerBundle\Component\Endpoint\Authorization\Compiler\RequestObjectCompilerPass;
28
use OAuth2Framework\Component\AuthorizationEndpoint\ConsentScreen\Extension;
29
use OAuth2Framework\Component\AuthorizationEndpoint\ParameterChecker\ParameterChecker;
30
use OAuth2Framework\Component\AuthorizationEndpoint\ResponseMode\ResponseMode;
31
use OAuth2Framework\Component\AuthorizationEndpoint\ResponseType;
32
use OAuth2Framework\ServerBundle\Component\Endpoint\Authorization\Compiler\UserAccountCheckerCompilerPass;
33
use OAuth2Framework\ServerBundle\Form\Type\AuthorizationType;
34
use OAuth2Framework\ServerBundle\Service\SymfonyUserDiscovery;
35
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
36
use Symfony\Component\Config\FileLocator;
37
use Symfony\Component\DependencyInjection\ContainerBuilder;
38
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
39
40
class AuthorizationEndpointSource implements Component
41
{
42
    /**
43
     * @var Component[]
44
     */
45
    private $subComponents = [];
46
47
    /**
48
     * AuthorizationEndpointSource constructor.
49
     */
50
    public function __construct()
51
    {
52
        $this->subComponents = [
53
            new ResponseModeSource(),
54
            new RequestObjectSource(),
55
        ];
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function name(): string
62
    {
63
        return 'authorization';
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function load(array $configs, ContainerBuilder $container)
70
    {
71
        if (!class_exists(AuthorizationEndpoint::class)) {
72
            return;
73
        }
74
        $config = $configs['endpoint']['authorization'];
75
        $container->setParameter('oauth2_server.endpoint.authorization.enabled', $config['enabled']);
76
        if (!$config['enabled']) {
77
            return;
78
        }
79
80
        $container->registerForAutoconfiguration(ResponseType::class)->addTag('oauth2_server_response_type');
81
        $container->registerForAutoconfiguration(ResponseMode::class)->addTag('oauth2_server_response_mode');
82
        $container->registerForAutoconfiguration(ParameterChecker::class)->addTag('oauth2_server_authorization_parameter_checker');
83
        $container->registerForAutoconfiguration(UserAccountChecker::class)->addTag('oauth2_server_user_account_checker');
84
        $container->registerForAutoconfiguration(Extension::class)->addTag('oauth2_server_consent_screen_extension');
85
86
        $loader = new PhpFileLoader($container, new FileLocator(__DIR__.'/../../../Resources/config/endpoint/authorization'));
87
        $loader->load('authorization.php');
88
        $loader->load('user_account_discovery.php');
89
90
        $container->setAlias(UserAccountDiscovery::class, $config['user_account_discovery']);
91
92
        $container->setParameter('oauth2_server.endpoint.authorization.path', $config['path']);
93
        $container->setParameter('oauth2_server.endpoint.authorization.host', $config['host']);
94
        $container->setParameter('oauth2_server.endpoint.authorization.login_route_name', $config['login_route_name']);
95
        $container->setParameter('oauth2_server.endpoint.authorization.login_route_parameters', $config['login_route_parameters']);
96
        $container->setParameter('oauth2_server.endpoint.authorization.template', $config['template']);
97
        $container->setParameter('oauth2_server.endpoint.authorization.enforce_state', $config['enforce_state']);
98
        $container->setParameter('oauth2_server.endpoint.authorization.form', $config['form']);
99
        $container->setParameter('oauth2_server.endpoint.authorization.type', $config['type']);
100
101
        foreach ($this->subComponents as $subComponent) {
102
            $subComponent->load($configs, $container);
103
        }
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109
    public function getNodeDefinition(ArrayNodeDefinition $node, ArrayNodeDefinition $rootNode)
110
    {
111
        if (!class_exists(AuthorizationEndpoint::class)) {
112
            return;
113
        }
114
        $childNode = $node->children()
115
            ->arrayNode($this->name())
116
                ->canBeEnabled();
117
118
        $childNode->children()
119
            ->scalarNode('path')
120
                ->info('The path to the authorization endpoint.')
121
                ->defaultValue('/authorize')
122
            ->end()
123
            ->scalarNode('host')
124
            ->info('If set, the route will be limited to that host')
125
                ->defaultValue('')
126
                ->treatFalseLike('')
127
                ->treatNullLike('')
128
            ->end()
129
            ->scalarNode('login_route_name')
130
                ->info('The name of the login route. Will be converted into URL and used to redirect the user if not logged in. If you use "FOSUserBundle", the route name should be "fos_user_security_login".')
131
            ->end()
132
            ->arrayNode('login_route_parameters')
133
                ->info('Parameters associated to the login route (optional).')
134
                ->useAttributeAsKey('name')
135
                ->scalarPrototype()->end()
136
                ->treatNullLike([])
137
            ->end()
138
            ->scalarNode('user_account_discovery')
139
                ->info('The user account discovery service.')
140
                ->defaultValue(SymfonyUserDiscovery::class)
141
            ->end()
142
            ->scalarNode('template')
143
                ->info('The consent page template.')
144
                ->defaultValue('@OAuth2FrameworkServerBundle/authorization/authorization.html.twig')
145
            ->end()
146
            ->scalarNode('enforce_state')
147
                ->info('If true the "state" parameter is mandatory (recommended).')
148
                ->defaultFalse()
149
            ->end()
150
            ->scalarNode('form')
151
                ->info('If form used for authorization requests.')
152
                ->defaultValue('oauth2_server_authorization_form')
153
            ->end()
154
            ->scalarNode('type')
155
                ->info('Form type.')
156
                ->defaultValue(AuthorizationType::class)
157
            ->end()
158
        ->end();
159
160
        foreach ($this->subComponents as $subComponent) {
161
            $subComponent->getNodeDefinition($childNode, $node);
162
        }
163
    }
164
165
    /**
166
     * {@inheritdoc}
167
     */
168
    public function prepend(ContainerBuilder $container, array $config): array
169
    {
170
        if (!class_exists(AuthorizationEndpoint::class)) {
171
            return [];
172
        }
173
        if (!$config['endpoint']['authorization']['enabled']) {
174
            return [];
175
        }
176
177
        $updatedConfig = [];
178
        foreach ($this->subComponents as $subComponent) {
179
            $updatedConfig = array_merge(
180
                $updatedConfig,
181
                $subComponent->prepend($container, $config)
182
            );
183
        }
184
185
        return $updatedConfig;
186
    }
187
188
    /**
189
     * {@inheritdoc}
190
     */
191
    public function build(ContainerBuilder $container)
192
    {
193
        if (!class_exists(AuthorizationEndpoint::class)) {
194
            return;
195
        }
196
        $container->addCompilerPass(new AuthorizationEndpointRouteCompilerPass());
197
        $container->addCompilerPass(new RequestObjectCompilerPass());
198
        $container->addCompilerPass(new AuthorizationRequestMetadataCompilerPass());
199
        $container->addCompilerPass(new ConsentScreenExtensionCompilerPass());
200
        $container->addCompilerPass(new ParameterCheckerCompilerPass());
201
        $container->addCompilerPass(new ResponseModeCompilerPass());
202
        $container->addCompilerPass(new ResponseTypeCompilerPass());
203
        $container->addCompilerPass(new TemplatePathCompilerPass());
204
        $container->addCompilerPass(new UserAccountCheckerCompilerPass());
205
206
        foreach ($this->subComponents as $component) {
207
            $component->build($container);
208
        }
209
    }
210
}
211