Passed
Pull Request — master (#56)
by Mark
01:55
created

HmacAuthenticationProvider::authenticate()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 8
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 12
ccs 6
cts 6
cp 1
crap 2
rs 10
1
<?php
2
3
namespace Acquia\Hmac\Symfony;
4
5
use Acquia\Hmac\RequestAuthenticatorInterface;
6
use Nyholm\Psr7\Factory\Psr17Factory;
7
use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory;
0 ignored issues
show
Bug introduced by
The type Symfony\Bridge\PsrHttpMe...\Factory\PsrHttpFactory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface;
9
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
10
use Symfony\Component\Security\Core\Exception\AuthenticationException;
11
12
/**
13
 * Provides the means to authenticate an HTTP HMAC request.
14
 */
15
class HmacAuthenticationProvider implements AuthenticationProviderInterface
16
{
17
    /**
18
     * @var \Acquia\Hmac\RequestAuthenticatorInterface
19
     *   A HMAC request authenticator service.
20
     */
21
    protected $authenticator;
22
23
    /**
24
     * Initializes the authentication provider.
25
     *
26
     * @param \Acquia\Hmac\RequestAuthenticatorInterface $authenticator
27
     *   The HMAC request authenticator service.
28 3
     */
29
    public function __construct(RequestAuthenticatorInterface $authenticator)
30 3
    {
31 3
        $this->authenticator = $authenticator;
32
    }
33
34
    /**
35
     * {@inheritDoc}
36 2
     */
37
    public function authenticate(TokenInterface $token)
38 2
    {
39 2
        $psr17Factory = new Psr17Factory();
40
        $httpMessageFactory = new PsrHttpFactory($psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory);
41
        $psr7Request = $httpMessageFactory->createRequest($token->getRequest());
0 ignored issues
show
Bug introduced by
The method getRequest() does not exist on Symfony\Component\Securi...on\Token\TokenInterface. It seems like you code against a sub-type of Symfony\Component\Securi...on\Token\TokenInterface such as Acquia\Hmac\Symfony\HmacToken. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

41
        $psr7Request = $httpMessageFactory->createRequest($token->/** @scrutinizer ignore-call */ getRequest());
Loading history...
42 2
43
        try {
44 1
            $key = $this->authenticator->authenticate($psr7Request);
45 1
46 1
            return new HmacToken($token->getRequest(), $key);
47
        } catch (\Exception $e) {
48
            throw new AuthenticationException($e->getMessage());
49
        }
50
    }
51
52
    /**
53 1
     * {@inheritDoc}
54
     */
55 1
    public function supports(TokenInterface $token)
56
    {
57
        return $token instanceof HmacToken;
58
    }
59
}
60