Failed Conditions
Push — master ( b0e939...893034 )
by Florent
19:07
created

AuthorizationEndpointSource::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
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\Component\AuthorizationEndpoint\ConsentScreen\Extension;
28
use OAuth2Framework\Component\AuthorizationEndpoint\ParameterChecker\ParameterChecker;
29
use OAuth2Framework\Component\AuthorizationEndpoint\ResponseMode\ResponseMode;
30
use OAuth2Framework\Component\AuthorizationEndpoint\ResponseType;
31
use OAuth2Framework\ServerBundle\Component\Endpoint\Authorization\Compiler\UserAccountCheckerCompilerPass;
32
use OAuth2Framework\ServerBundle\Form\Type\AuthorizationType;
33
use OAuth2Framework\ServerBundle\Service\SymfonyUserDiscovery;
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
    /**
47
     * AuthorizationEndpointSource constructor.
48
     */
49
    public function __construct()
50
    {
51
        $this->subComponents = [
52
            new ResponseModeSource(),
53
            new RequestObjectSource(),
54
        ];
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function name(): string
61
    {
62
        return 'authorization';
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function load(array $configs, ContainerBuilder $container)
69
    {
70
        if (!class_exists(AuthorizationEndpoint::class)) {
71
            return;
72
        }
73
        $config = $configs['endpoint']['authorization'];
74
        $container->setParameter('oauth2_server.endpoint.authorization.enabled', $config['enabled']);
75
        if (!$config['enabled']) {
76
            return;
77
        }
78
79
        $container->registerForAutoconfiguration(ResponseType::class)->addTag('oauth2_server_response_type');
80
        $container->registerForAutoconfiguration(ResponseMode::class)->addTag('oauth2_server_response_mode');
81
        $container->registerForAutoconfiguration(ParameterChecker::class)->addTag('oauth2_server_authorization_parameter_checker');
82
        $container->registerForAutoconfiguration(UserAccountChecker::class)->addTag('oauth2_server_user_account_checker');
83
        $container->registerForAutoconfiguration(Extension::class)->addTag('oauth2_server_consent_screen_extension');
84
85
        $loader = new PhpFileLoader($container, new FileLocator(__DIR__.'/../../../Resources/config/endpoint/authorization'));
86
        $loader->load('authorization.php');
87
88
        $container->setAlias(UserAccountDiscovery::class, $config['user_account_discovery']);
89
90
        $container->setParameter('oauth2_server.endpoint.authorization.path', $config['path']);
91
        $container->setParameter('oauth2_server.endpoint.authorization.host', $config['host']);
92
        $container->setParameter('oauth2_server.endpoint.authorization.login_route_name', $config['login_route_name']);
93
        $container->setParameter('oauth2_server.endpoint.authorization.login_route_parameters', $config['login_route_parameters']);
94
        $container->setParameter('oauth2_server.endpoint.authorization.template', $config['template']);
95
        $container->setParameter('oauth2_server.endpoint.authorization.enforce_state', $config['enforce_state']);
96
        $container->setParameter('oauth2_server.endpoint.authorization.form', $config['form']);
97
        $container->setParameter('oauth2_server.endpoint.authorization.type', $config['type']);
98
99
        if ($container->hasAlias('oauth2_server.http_client')) {
100
            $loader->load('sector_identifier_uri.php');
101
        }
102
        foreach ($this->subComponents as $subComponent) {
103
            $subComponent->load($configs, $container);
104
        }
105
    }
106
107
    /**
108
     * {@inheritdoc}
109
     */
110
    public function getNodeDefinition(ArrayNodeDefinition $node, ArrayNodeDefinition $rootNode)
111
    {
112
        if (!class_exists(AuthorizationEndpoint::class)) {
113
            return;
114
        }
115
        $childNode = $node->children()
116
            ->arrayNode($this->name())
117
                ->canBeEnabled();
118
119
        $childNode->children()
120
            ->scalarNode('path')
121
                ->info('The path to the authorization endpoint.')
122
                ->defaultValue('/authorize')
123
            ->end()
124
            ->scalarNode('host')
125
            ->info('If set, the route will be limited to that host')
126
                ->defaultValue('')
127
                ->treatFalseLike('')
128
                ->treatNullLike('')
129
            ->end()
130
            ->scalarNode('login_route_name')
131
                ->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".')
132
            ->end()
133
            ->arrayNode('login_route_parameters')
134
                ->info('Parameters associated to the login route (optional).')
135
                ->useAttributeAsKey('name')
136
                ->scalarPrototype()->end()
137
                ->treatNullLike([])
138
            ->end()
139
            ->scalarNode('user_account_discovery')
140
                ->info('The user account discovery service.')
141
                ->defaultValue(SymfonyUserDiscovery::class)
142
            ->end()
143
            ->scalarNode('template')
144
                ->info('The consent page template.')
145
                ->defaultValue('@OAuth2FrameworkServerBundle/authorization/authorization.html.twig')
146
            ->end()
147
            ->scalarNode('enforce_state')
148
                ->info('If true the "state" parameter is mandatory (recommended).')
149
                ->defaultFalse()
150
            ->end()
151
            ->scalarNode('form')
152
                ->info('If form used for authorization requests.')
153
                ->defaultValue('oauth2_server_authorization_form')
154
            ->end()
155
            ->scalarNode('type')
156
                ->info('Form type.')
157
                ->defaultValue(AuthorizationType::class)
158
            ->end()
159
        ->end();
160
161
        foreach ($this->subComponents as $subComponent) {
162
            $subComponent->getNodeDefinition($childNode, $node);
163
        }
164
    }
165
166
    /**
167
     * {@inheritdoc}
168
     */
169
    public function prepend(ContainerBuilder $container, array $config): array
170
    {
171
        if (!class_exists(AuthorizationEndpoint::class)) {
172
            return [];
173
        }
174
        if (!$config['endpoint']['authorization']['enabled']) {
175
            return [];
176
        }
177
178
        $updatedConfig = [];
179
        foreach ($this->subComponents as $subComponent) {
180
            $updatedConfig = array_merge(
181
                $updatedConfig,
182
                $subComponent->prepend($container, $config)
183
            );
184
        }
185
186
        return $updatedConfig;
187
    }
188
189
    /**
190
     * {@inheritdoc}
191
     */
192
    public function build(ContainerBuilder $container)
193
    {
194
        if (!class_exists(AuthorizationEndpoint::class)) {
195
            return;
196
        }
197
        $container->addCompilerPass(new AuthorizationEndpointRouteCompilerPass());
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