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