Failed Conditions
Push — ng ( 7fee5c...6dc266 )
by Florent
27:34 queued 22:22
created

AuthorizationEndpointSource   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 147
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

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