Failed Conditions
Push — ng ( bd776e...f12c1b )
by Florent
03:56
created

AuthorizationEndpointSource::build()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 10
nc 2
nop 1
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\Component\AuthorizationEndpoint\ConsentScreen\Extension;
25
use OAuth2Framework\Component\AuthorizationEndpoint\ParameterChecker\ParameterChecker;
26
use OAuth2Framework\Component\AuthorizationEndpoint\ResponseMode\ResponseMode;
27
use OAuth2Framework\Component\AuthorizationEndpoint\ResponseType;
28
use OAuth2Framework\Component\AuthorizationEndpoint\UserAccountDiscovery\UserAccountDiscovery;
29
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
30
use Symfony\Component\Config\FileLocator;
31
use Symfony\Component\DependencyInjection\ContainerBuilder;
32
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
33
34
class AuthorizationEndpointSource implements Component
35
{
36
    /**
37
     * @var Component[]
38
     */
39
    private $subComponents = [];
40
41
    /**
42
     * AuthorizationEndpointSource constructor.
43
     */
44
    public function __construct()
45
    {
46
        $this->subComponents = [
47
        ];
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function name(): string
54
    {
55
        return 'authorization';
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function load(array $configs, ContainerBuilder $container)
62
    {
63
        if (!$configs['endpoint']['authorization']['enabled']) {
64
            return;
65
        }
66
67
        $config = $configs['endpoint']['authorization'];
68
69
        $container->registerForAutoconfiguration(ResponseType::class)->addTag('oauth2_server_response_type');
70
        $container->registerForAutoconfiguration(ResponseMode::class)->addTag('oauth2_server_response_mode');
71
        $container->registerForAutoconfiguration(ParameterChecker::class)->addTag('oauth2_server_authorization_parameter_checker');
72
        $container->registerForAutoconfiguration(UserAccountDiscovery::class)->addTag('oauth2_server_user_account_discovery');
73
        $container->registerForAutoconfiguration(Extension::class)->addTag('oauth2_server_consent_screen_extension');
74
75
        $loader = new PhpFileLoader($container, new FileLocator(__DIR__.'/../../../Resources/config/endpoint/authorization'));
76
        $loader->load('authorization.php');
77
78
        $container->setParameter('oauth2_server.endpoint.authorization.enabled', $config['enabled']);
79
        $container->setParameter('oauth2_server.endpoint.authorization.path', $config['path']);
80
        $container->setParameter('oauth2_server.endpoint.authorization.login_route_name', $config['login_route_name']);
81
        $container->setParameter('oauth2_server.endpoint.authorization.login_route_parameters', $config['login_route_parameters']);
82
        $container->setParameter('oauth2_server.endpoint.authorization.template', $config['template']);
83
        $container->setParameter('oauth2_server.endpoint.authorization.enforce_state', $config['enforce_state']);
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    public function getNodeDefinition(ArrayNodeDefinition $node, ArrayNodeDefinition $rootNode)
90
    {
91
        $childNode = $node->children()
92
            ->arrayNode($this->name())
93
                ->canBeEnabled()
94
                ->addDefaultsIfNotSet();
95
96
        $childNode->children()
97
            ->scalarNode('path')
98
                ->info('The path to the authorization endpoint.')
99
                ->defaultValue('/authorize')
100
            ->end()
101
            ->scalarNode('login_route_name')
102
                ->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".')
103
            ->end()
104
            ->arrayNode('login_route_parameters')
105
                ->info('Parameters associated to the login route (optional).')
106
                ->useAttributeAsKey('name')
107
                ->scalarPrototype()->end()
108
                ->treatNullLike([])
109
            ->end()
110
            ->scalarNode('template')
111
                ->info('The consent page template.')
112
                ->defaultValue('@OAuth2FrameworkBundle/authorization/authorization.html.twig')
113
            ->end()
114
            ->scalarNode('enforce_state')
115
                ->info('If true the "state" parameter is mandatory (recommended).')
116
                ->defaultFalse()
117
            ->end()
118
        ->end();
119
120
        foreach ($this->subComponents as $subComponent) {
121
            $subComponent->getNodeDefinition($childNode, $node);
122
        }
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128
    public function prepend(ContainerBuilder $container, array $config): array
129
    {
130
        $updatedConfig = [];
131
        foreach ($this->subComponents as $subComponent) {
132
            $updatedConfig = array_merge(
133
                $updatedConfig,
134
                $subComponent->prepend($container, $config)
135
            );
136
        }
137
138
        return $updatedConfig;
139
    }
140
141
    /**
142
     * {@inheritdoc}
143
     */
144
    public function build(ContainerBuilder $container)
145
    {
146
        $container->addCompilerPass(new AuthorizationEndpointRouteCompilerPass());
147
        $container->addCompilerPass(new AuthorizationRequestMetadataCompilerPass());
148
        $container->addCompilerPass(new ConsentScreenExtensionCompilerPass());
149
        $container->addCompilerPass(new ParameterCheckerCompilerPass());
150
        $container->addCompilerPass(new ResponseModeCompilerPass());
151
        $container->addCompilerPass(new ResponseTypeCompilerPass());
152
        $container->addCompilerPass(new TemplatePathCompilerPass());
153
154
        foreach ($this->subComponents as $component) {
155
            $component->build($container);
156
        }
157
    }
158
}
159