Failed Conditions
Push — ng ( 8fdb29...f5f23c )
by Florent
07:02
created

getNodeDefinition()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 23
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\Endpoint\TokenIntrospection;
15
16
use OAuth2Framework\Bundle\Component\Component;
17
use OAuth2Framework\Bundle\Component\Endpoint\TokenIntrospection\Compiler\TokenIntrospectionRouteCompilerPass;
18
use OAuth2Framework\Bundle\Component\Endpoint\TokenIntrospection\Compiler\TokenTypeHintCompilerPass;
19
use OAuth2Framework\Component\TokenIntrospectionEndpoint\TokenTypeHint;
20
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
21
use Symfony\Component\Config\FileLocator;
22
use Symfony\Component\DependencyInjection\ContainerBuilder;
23
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
24
25
class TokenIntrospectionEndpointSource implements Component
26
{
27
    /**
28
     * @return string
29
     */
30
    public function name(): string
31
    {
32
        return 'token_introspection';
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function load(array $configs, ContainerBuilder $container)
39
    {
40
        $config = $configs['endpoint']['token_introspection'];
41
        $container->setParameter('oauth2_server.endpoint.token_introspection.enabled', $config['enabled']);
42
        if (!$config['enabled']) {
43
            return;
44
        }
45
        $container->registerForAutoconfiguration(TokenTypeHint::class)->addTag('oauth2_server_introspection_type_hint');
46
        $container->setParameter('oauth2_server.endpoint.token_introspection.path', $config['path']);
47
        $container->setParameter('oauth2_server.endpoint.token_introspection.host', $config['host']);
48
49
        $loader = new PhpFileLoader($container, new FileLocator(__DIR__.'/../../../Resources/config/endpoint/token_introspection'));
50
        $loader->load('introspection.php');
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function getNodeDefinition(ArrayNodeDefinition $node, ArrayNodeDefinition $rootNode)
57
    {
58
        $rootNode->validate()
59
            ->ifTrue(function ($config) {
60
                return null === $config['resource_server']['repository'];
61
            })
62
            ->thenInvalid('The resource server repository must be set when the introspection endpoint is enabled')
63
        ->end();
64
65
        $node->children()
66
            ->arrayNode($this->name())
67
                ->canBeEnabled()
68
                ->children()
69
                    ->scalarNode('path')
70
                        ->info('The token introspection endpoint path')
71
                        ->defaultValue('/token/introspection')
72
                    ->end()
73
                    ->scalarNode('host')
74
                        ->info('If set, the route will be limited to that host')
75
                        ->defaultValue('')
76
                        ->treatNullLike('')
77
                        ->treatFalseLike('')
78
                    ->end()
79
                ->end()
80
            ->end()
81
        ->end();
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    public function build(ContainerBuilder $container)
88
    {
89
        $container->addCompilerPass(new TokenTypeHintCompilerPass());
90
        $container->addCompilerPass(new TokenIntrospectionRouteCompilerPass());
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96
    public function prepend(ContainerBuilder $container, array $config): array
97
    {
98
        return [];
99
    }
100
}
101