Failed Conditions
Push — master ( d82019...3e98d5 )
by Florent
03:42
created

updateJoseBundleConfigurationForChecker()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
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-2017 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\Server\DependencyInjection\Source\Grant;
15
16
use Fluent\PhpConfigFileLoader;
17
use OAuth2Framework\Bundle\Server\DependencyInjection\Source\ActionableSource;
18
use OAuth2Framework\Bundle\Server\DependencyInjection\Source\SourceInterface;
19
use SpomkyLabs\JoseBundle\Helper\ConfigurationHelper;
20
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
21
use Symfony\Component\Config\FileLocator;
22
use Symfony\Component\DependencyInjection\ContainerBuilder;
23
use Symfony\Component\PropertyAccess\PropertyAccess;
24
25
final class IdTokenSource extends ActionableSource
26
{
27
    /**
28
     * @var SourceInterface[]
29
     */
30
    private $subSources = [];
31
32
    /**
33
     * UserinfoSource constructor.
34
     */
35
    public function __construct()
36
    {
37
        $this->subSources = [
38
            new IdTokenEncryptionSource(),
39
            new IdTokenUserinfoPairwiseSource(),
40
        ];
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function prepend(array $bundleConfig, string $path, ContainerBuilder $container)
47
    {
48
        foreach ($this->subSources as $source) {
49
            $source->prepend($bundleConfig, $path.'['.$this->name().']', $container);
50
        }
51
        $currentPath = $path.'['.$this->name().']';
52
        $accessor = PropertyAccess::createPropertyAccessor();
53
        $sourceConfig = $accessor->getValue($bundleConfig, $currentPath);
54
55
        if (true === $sourceConfig['enabled']) {
56
            $this->updateJoseBundleConfigurationForSigner($container, $sourceConfig);
57
            $this->updateJoseBundleConfigurationForVerifier($container, $sourceConfig);
58
            $this->updateJoseBundleConfigurationForChecker($container, $sourceConfig);
59
            $this->updateJoseBundleConfigurationForJWTCreator($container, $sourceConfig);
60
            $this->updateJoseBundleConfigurationForJWTLoader($container, $sourceConfig);
61
        }
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    protected function continueLoading(string $path, ContainerBuilder $container, array $config)
68
    {
69
        foreach (['lifetime', 'default_signature_algorithm', 'signature_algorithms', 'claim_checkers', 'header_checkers'] as $k) {
70
            $container->setParameter($path.'.'.$k, $config[$k]);
71
        }
72
        $container->setAlias($path.'.key_set', $config['key_set']);
73
74
        foreach ($this->subSources as $source) {
75
            $source->load($path, $container, $config);
76
        }
77
        $loader = new PhpConfigFileLoader($container, new FileLocator(__DIR__.'/../../../Resources/config/grant'));
78
        $loader->load('id_token.php');
79
        $loader->load('userinfo_scope_support.php');
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85
    protected function name(): string
86
    {
87
        return 'id_token';
88
    }
89
90
    protected function continueConfiguration(NodeDefinition $node)
91
    {
92
        parent::continueConfiguration($node);
93
        $node
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Symfony\Component\Config...\Builder\NodeDefinition as the method children() does only exist in the following sub-classes of Symfony\Component\Config...\Builder\NodeDefinition: Symfony\Component\Config...der\ArrayNodeDefinition. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
94
            ->validate()
95
                ->ifTrue(function ($config) {
96
                    return true === $config['enabled'] && empty($config['signature_algorithms']);
97
                })
98
                ->thenInvalid('The option "signature_algorithm" must contain at least one signature algorithm.')
99
            ->end()
100
            ->validate()
101
                ->ifTrue(function ($config) {
102
                    return true === $config['enabled'] && empty($config['key_set']);
103
                })
104
                ->thenInvalid('The option "key_set" must be set.')
105
            ->end()
106
            ->children()
107
                ->scalarNode('default_signature_algorithm')
108
                    ->info('Signature algorithm used if the client has not defined a preferred one. Recommended value is "RS256".')
109
                ->end()
110
                ->scalarNode('key_set')
111
                    ->info('Key set that contains a suitable signature key for the selected signature algorithms.')
112
                ->end()
113
                ->arrayNode('signature_algorithms')
114
                    ->info('Signature algorithm used to sign the ID Tokens.')
115
                    ->useAttributeAsKey('name')
116
                    ->prototype('scalar')->end()
117
                    ->treatNullLike([])
118
                ->end()
119
                ->arrayNode('claim_checkers')
120
                    ->info('Checkers will verify the JWT claims.')
121
                    ->useAttributeAsKey('name')
122
                    ->prototype('scalar')->end()
123
                    ->treatNullLike(['exp', 'iat', 'nbf'])
124
                ->end()
125
                ->arrayNode('header_checkers')
126
                    ->info('Checkers will verify the JWT headers.')
127
                    ->useAttributeAsKey('name')
128
                    ->prototype('scalar')->end()
129
                    ->treatNullLike(['crit'])
130
                ->end()
131
                ->integerNode('lifetime')
132
                    ->info('Lifetime of the ID Tokens (in seconds). If an access token is issued with the ID Token, the lifetime of the access token is used instead of this value.')
133
                    ->defaultValue(3600)
134
                    ->min(1)
135
                ->end()
136
            ->end();
137
        foreach ($this->subSources as $source) {
138
            $source->addConfiguration($node);
139
        }
140
    }
141
142
    /**
143
     * @param ContainerBuilder $container
144
     * @param array            $sourceConfig
145
     */
146
    private function updateJoseBundleConfigurationForSigner(ContainerBuilder $container, array $sourceConfig)
147
    {
148
        ConfigurationHelper::addSigner($container, $this->name(), $sourceConfig['signature_algorithms'], false);
149
    }
150
151
    /**
152
     * @param ContainerBuilder $container
153
     * @param array            $sourceConfig
154
     */
155
    private function updateJoseBundleConfigurationForVerifier(ContainerBuilder $container, array $sourceConfig)
156
    {
157
        ConfigurationHelper::addVerifier($container, $this->name(), $sourceConfig['signature_algorithms'], false);
158
    }
159
160
    /**
161
     * @param ContainerBuilder $container
162
     * @param array            $sourceConfig
163
     */
164
    private function updateJoseBundleConfigurationForChecker(ContainerBuilder $container, array $sourceConfig)
165
    {
166
        ConfigurationHelper::addChecker($container, $this->name(), $sourceConfig['header_checkers'], $sourceConfig['claim_checkers'], false);
167
    }
168
169
    /**
170
     * @param ContainerBuilder $container
171
     * @param array            $sourceConfig
172
     */
173
    private function updateJoseBundleConfigurationForJWTCreator(ContainerBuilder $container, array $sourceConfig)
174
    {
175
        $encrypter = null;
176
        if (true === $sourceConfig['encryption']['enabled']) {
177
            $encrypter = sprintf('jose.encrypter.%s', $this->name());
178
        }
179
        ConfigurationHelper::addJWTCreator($container, $this->name(), sprintf('jose.signer.%s', $this->name()), $encrypter, false);
180
    }
181
182
    /**
183
     * @param ContainerBuilder $container
184
     * @param array            $sourceConfig
185
     */
186
    private function updateJoseBundleConfigurationForJWTLoader(ContainerBuilder $container, array $sourceConfig)
187
    {
188
        $decrypter = null;
189
        if (true === $sourceConfig['encryption']['enabled']) {
190
            $decrypter = sprintf('jose.decrypter.%s', $this->name());
191
        }
192
        ConfigurationHelper::addJWTLoader($container, $this->name(), sprintf('jose.verifier.%s', $this->name()), sprintf('jose.checker.%s', $this->name()), $decrypter, false);
193
    }
194
}
195