Failed Conditions
Push — ng ( c00098...4b490a )
by Florent
06:57
created

AuthorizationCodeSource::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\Component\Grant\AuthorizationCode;
15
16
use OAuth2Framework\Bundle\Component\Component;
17
use OAuth2Framework\Component\AuthorizationCodeGrant\AuthorizationCodeGrantType;
18
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
19
use Symfony\Component\Config\FileLocator;
20
use Symfony\Component\DependencyInjection\ContainerBuilder;
21
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
22
23
class AuthorizationCodeSource implements Component
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function name(): string
29
    {
30
        return 'authorization_code';
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function load(array $configs, ContainerBuilder $container)
37
    {
38
        if ($configs['grant']['authorization_code']['enabled']) {
39
            $container->setParameter('oauth2_server.grant.authorization_code.min_length', $configs['grant']['authorization_code']['min_length']);
40
            $container->setParameter('oauth2_server.grant.authorization_code.max_length', $configs['grant']['authorization_code']['max_length']);
41
            $container->setParameter('oauth2_server.grant.authorization_code.lifetime', $configs['grant']['authorization_code']['lifetime']);
42
            $container->setParameter('oauth2_server.grant.authorization_code.enforce_pkce', $configs['grant']['authorization_code']['enforce_pkce']);
43
            $container->setAlias('oauth2_server.grant.authorization_code.repository', $configs['grant']['authorization_code']['repository']);
44
45
            $loader = new PhpFileLoader($container, new FileLocator(__DIR__.'/../../../Resources/config/grant'));
46
            $loader->load('authorization_code.php');
47
        }
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function getNodeDefinition(NodeDefinition $node)
54
    {
55
        $node->children()
56
            ->arrayNode('authorization_code')
57
                ->validate()
58
                    ->ifTrue(function ($config) {
59
                        return $config['max_length'] < $config['min_length'];
60
                    })
61
                    ->thenInvalid('The option "max_length" must be greater than "min_length".')
62
                ->end()
63
                ->validate()
64
                    ->ifTrue(function ($config) {
65
                        return $config['enabled'] && !class_exists(AuthorizationCodeGrantType::class);
66
                    })
67
                    ->thenInvalid('The option "max_length" must be greater than "min_length".')
68
                ->end()
69
                ->canBeEnabled()
70
                ->children()
71
                    ->integerNode('min_length')
72
                        ->defaultValue(50)
73
                        ->min(0)
74
                        ->info('Minimum length of the randomly generated authorization code')
75
                    ->end()
76
                    ->integerNode('max_length')
77
                        ->defaultValue(100)
78
                        ->min(1)
79
                        ->info('Maximum length of the randomly generated authorization code')
80
                    ->end()
81
                    ->integerNode('lifetime')
82
                        ->defaultValue(30)
83
                        ->min(1)
84
                        ->info('Authorization code lifetime (in seconds)')
85
                    ->end()
86
                    ->scalarNode('repository')
87
                        ->isRequired()
88
                        ->info('The authorization code repository')
89
                    ->end()
90
                    ->booleanNode('enforce_pkce')
91
                        ->defaultFalse()
92
                        ->info('If true, the PKCE is required for all requests including the ones from confidential clients')
93
                    ->end()
94
                ->end()
95
            ->end()
96
        ->end();
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102
    public function build(ContainerBuilder $container)
103
    {
104
        //Nothing to do
105
    }
106
107
    /**
108
     * {@inheritdoc}
109
     */
110
    public function prepend(ContainerBuilder $container, array $config): array
111
    {
112
        //Nothing to do
113
        return [];
114
    }
115
}
116