Failed Conditions
Push — ng ( f36f4b...d6eec9 )
by Florent
04:33
created

GrantSource::prepend()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 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\DependencyInjection\Component\Grant;
15
16
use OAuth2Framework\Bundle\DependencyInjection\Component\Component;
17
use OAuth2Framework\Component\AuthorizationEndpoint\ResponseType;
18
use OAuth2Framework\Component\TokenEndpoint\GrantType;
19
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
20
use Symfony\Component\Config\FileLocator;
21
use Symfony\Component\DependencyInjection\ContainerBuilder;
22
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
23
24
final class GrantSource implements Component
25
{
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function name(): string
30
    {
31
        return 'grant';
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function load(array $configs, ContainerBuilder $container)
38
    {
39
        $container->registerForAutoconfiguration(GrantType::class)->addTag('oauth2_server_grant_type');
40
        $container->registerForAutoconfiguration(ResponseType::class)->addTag('oauth2_server_response_type');
41
42
        $loader = new PhpFileLoader($container, new FileLocator(__DIR__.'/../../../Resources/config/grant'));
43
        $loader->load('grant.php');
44
45
        if ($configs['grant']['authorization_code']['enabled']) {
46
            $container->setParameter('oauth2_server.grant.authorization_code.min_length', $configs['grant']['authorization_code']['min_length']);
47
            $container->setParameter('oauth2_server.grant.authorization_code.max_length', $configs['grant']['authorization_code']['max_length']);
48
            $container->setParameter('oauth2_server.grant.authorization_code.lifetime', $configs['grant']['authorization_code']['lifetime']);
49
            $container->setParameter('oauth2_server.grant.authorization_code.enforce_pkce', $configs['grant']['authorization_code']['enforce_pkce']);
50
            $container->setAlias('oauth2_server.grant.authorization_code.repository', $configs['grant']['authorization_code']['repository']);
51
            $loader->load('authorization_code.php');
52
        }
53
54
        if ($configs['grant']['client_credentials']['enabled']) {
55
            $container->setParameter('oauth2_server.grant.client_credentials.issue_refresh_token', $configs['grant']['client_credentials']['issue_refresh_token']);
56
            $loader->load('client_credentials.php');
57
        }
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function getNodeDefinition(NodeDefinition $node)
64
    {
65
        $node->children()
66
            ->arrayNode($this->name())
67
                ->addDefaultsIfNotSet()
68
                ->children()
69
                    ->arrayNode('authorization_code')
70
                        ->validate()
71
                            ->ifTrue(function ($config) {
72
                                return $config['max_length'] < $config['min_length'];
73
                            })
74
                            ->thenInvalid('The option "max_length" must be greater than "min_length".')
75
                        ->end()
76
                        ->canBeEnabled()
77
                        ->children()
78
                            ->integerNode('min_length')
79
                                ->defaultValue(50)
80
                                ->min(0)
81
                                ->info('Minimum length of the randomly generated authorization code')
82
                            ->end()
83
                            ->integerNode('max_length')
84
                                ->defaultValue(100)
85
                                ->min(1)
86
                                ->info('Maximum length of the randomly generated authorization code')
87
                            ->end()
88
                            ->integerNode('lifetime')
89
                                ->defaultValue(30)
90
                                ->min(1)
91
                                ->info('Authorization code lifetime (in seconds)')
92
                            ->end()
93
                            ->scalarNode('repository')
94
                                ->isRequired()
95
                                ->info('The authorization code repository')
96
                            ->end()
97
                            ->booleanNode('enforce_pkce')
98
                                ->defaultFalse()
99
                                ->info('If true, the PKCE is required for all requests including the ones from confidential clients')
100
                            ->end()
101
                        ->end()
102
                    ->end()
103
                    ->arrayNode('client_credentials')
104
                        ->canBeEnabled()
105
                        ->children()
106
                            ->booleanNode('issue_refresh_token')
107
                                ->info('If enabled, a refresh token will be issued with an access token (not recommended)')
108
                                ->defaultFalse()
109
                            ->end()
110
                        ->end()
111
                    ->end()
112
                ->end()
113
            ->end()
114
        ->end();
115
    }
116
117
    /**
118
     * {@inheritdoc}
119
     */
120
    public function prepend(ContainerBuilder $container, array $config): array
121
    {
122
        //Nothing to do
123
        return [];
124
    }
125
}
126