createSignatureQueryParameters()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 3
1
<?php
2
3
namespace Rezzza\SecurityBundle\DependencyInjection\Security\Factory;
4
5
use Symfony\Component\DependencyInjection\ContainerBuilder;
6
use Symfony\Component\DependencyInjection\Reference;
7
use Symfony\Component\DependencyInjection\DefinitionDecorator;
8
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
9
use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory\SecurityFactoryInterface;
10
11
class RequestSignatureFactory implements SecurityFactoryInterface
12
{
13
    public function create(ContainerBuilder $container, $id, $config, $userProvider, $defaultEntryPoint)
14
    {
15
        $signatureQueryParametersId = $this->createSignatureQueryParameters($container, $id, $config);
16
        $signatureConfigId = $this->createSignatureConfig($container, $id, $config);
17
        $replayProtectionId = $this->createReplayProtection($container, $id, $config);
18
        $providerId = 'security.authentication.provider.request_signature.'.$id;
19
20
        $container
21
            ->setDefinition($providerId, new DefinitionDecorator('rezzza.security.request_signature.provider'))
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Component\Depend...ion\DefinitionDecorator has been deprecated with message: The DefinitionDecorator class is deprecated since version 3.3 and will be removed in 4.0. Use the Symfony\Component\DependencyInjection\ChildDefinition class instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
22
            ->addArgument(new Reference($signatureConfigId))
23
            ->addArgument(new Reference($replayProtectionId))
24
        ;
25
26
        $listenerId = 'security.authentication.listener.request_signature.'.$id;
27
        $listener = $container
0 ignored issues
show
Unused Code introduced by
$listener is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
28
            ->setDefinition($listenerId, new DefinitionDecorator('rezzza.security.request_signature.listener'))
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Component\Depend...ion\DefinitionDecorator has been deprecated with message: The DefinitionDecorator class is deprecated since version 3.3 and will be removed in 4.0. Use the Symfony\Component\DependencyInjection\ChildDefinition class instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
29
            ->replaceArgument(2, new Reference($signatureQueryParametersId))
30
            ->replaceArgument(3, $config['ignore'])
31
        ;
32
33
        return array($providerId, $listenerId, $defaultEntryPoint);
34
    }
35
36
    public function getPosition()
37
    {
38
        return 'pre_auth';
39
    }
40
41
    public function getKey()
42
    {
43
        return 'request_signature';
44
    }
45
46
    public function createSignatureConfig($container, $id, $config)
47
    {
48
        $signatureConfigId = 'rezzza.security.request_signature.signature_config.'.$id;
49
        $container
50
            ->setDefinition($signatureConfigId, new DefinitionDecorator('rezzza.security.request_signature.signature_config'))
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Component\Depend...ion\DefinitionDecorator has been deprecated with message: The DefinitionDecorator class is deprecated since version 3.3 and will be removed in 4.0. Use the Symfony\Component\DependencyInjection\ChildDefinition class instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
51
            ->addArgument($config['replay_protection']['enabled'])
52
            ->addArgument($config['algorithm'])
53
            ->addArgument($config['secret'])
54
        ;
55
56
        return $signatureConfigId;
57
    }
58
59
    public function createSignatureQueryParameters($container, $id, $config)
60
    {
61
        $signatureQueryParametersId = 'rezzza.security.request_signature.signature_query_parameters.'.$id;
62
        $container
63
            ->setDefinition($signatureQueryParametersId, new DefinitionDecorator('rezzza.security.request_signature.signature_query_parameters'))
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Component\Depend...ion\DefinitionDecorator has been deprecated with message: The DefinitionDecorator class is deprecated since version 3.3 and will be removed in 4.0. Use the Symfony\Component\DependencyInjection\ChildDefinition class instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
64
            ->addArgument($config['parameter'])
65
            ->addArgument($config['replay_protection']['parameter'])
66
        ;
67
68
        return $signatureQueryParametersId;
69
    }
70
71
    public function createReplayProtection($container, $id, $config)
72
    {
73
        $replayProtectionId = 'rezzza.security.request_signature.replay_protection.'.$id;
74
        $container
75
            ->setDefinition($replayProtectionId, new DefinitionDecorator('rezzza.security.request_signature.replay_protection'))
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Component\Depend...ion\DefinitionDecorator has been deprecated with message: The DefinitionDecorator class is deprecated since version 3.3 and will be removed in 4.0. Use the Symfony\Component\DependencyInjection\ChildDefinition class instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
76
            ->addArgument($config['replay_protection']['enabled'])
77
            ->addArgument($config['replay_protection']['lifetime'])
78
        ;
79
80
        return $replayProtectionId;
81
    }
82
83
    public function addConfiguration(NodeDefinition $node)
84
    {
85
        $node->children()
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...
86
            ->scalarNode('algorithm')->defaultValue('SHA1')->cannotBeEmpty()->end()
87
            ->scalarNode('secret')->isRequired()->cannotBeEmpty()->end()
88
            ->booleanNode('ignore')->defaultFalse()->end()
89
            ->scalarNode('parameter')->defaultValue('_signature')->cannotBeEmpty()->end()
90
            ->arrayNode('replay_protection')
91
                ->addDefaultsIfNotSet()
92
                ->children()
93
                    ->booleanNode('enabled')->defaultTrue()->end()
94
                    ->scalarNode('lifetime')->defaultValue(600)->end()
95
                    ->scalarNode('parameter')->defaultValue('_signature_time')->cannotBeEmpty()->end()
96
                ->end()
97
            ->end()
98
        ;
99
    }
100
}
101