Failed Conditions
Push — master ( 2eee97...42d85f )
by Florent
09:02
created

JwtBearerEncryptionSource::continueConfiguration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.0856
c 0
b 0
f 0
cc 1
eloc 21
nc 1
nop 1
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 Assert\Assertion;
17
use Jose\Bundle\JoseFramework\Helper\ConfigurationHelper;
18
use OAuth2Framework\Bundle\Server\DependencyInjection\Source\ActionableSource;
19
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
20
use Symfony\Component\DependencyInjection\ContainerBuilder;
21
22
final class JwtBearerEncryptionSource extends ActionableSource
23
{
24
    /**
25
     * {@inheritdoc}
26
     */
27
    protected function continueLoading(string $path, ContainerBuilder $container, array $config)
28
    {
29
        foreach (['key_encryption_algorithms', 'content_encryption_algorithms', 'required'] as $k) {
30
            $container->setParameter($path.'.'.$k, $config[$k]);
31
        }
32
        //$container->setAlias($path.'.key_set', 'jose.key_set.jwt_bearer.key_set.encryption');
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    protected function name(): string
39
    {
40
        return 'encryption';
41
    }
42
43
    protected function continueConfiguration(NodeDefinition $node)
44
    {
45
        parent::continueConfiguration($node);
46
        $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...
47
            ->children()
48
                ->booleanNode('required')
49
                    ->info('If set to true, all ID Token sent to the server must be encrypted.')
50
                    ->defaultFalse()
51
                ->end()
52
                ->arrayNode('key_encryption_algorithms')
53
                    ->info('Supported key encryption algorithms.')
54
                    ->useAttributeAsKey('name')
55
                    ->prototype('scalar')->end()
56
                    ->treatNullLike([])
57
                ->end()
58
                ->arrayNode('content_encryption_algorithms')
59
                    ->info('Supported content encryption algorithms.')
60
                    ->useAttributeAsKey('name')
61
                    ->prototype('scalar')->end()
62
                    ->treatNullLike([])
63
                ->end()
64
            ->end();
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function prepend(array $bundleConfig, string $path, ContainerBuilder $container)
71
    {
72
        parent::prepend($bundleConfig, $path, $container);
73
74
        Assertion::keyExists($bundleConfig['key_set'], 'encryption', 'The encryption key set must be enabled.');
75
        //ConfigurationHelper::addKeyset($container, 'jwt_bearer.key_set.encryption', 'jwkset', ['value' => $bundleConfig['key_set']['encryption']]);
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
76
    }
77
}
78