Passed
Pull Request — master (#56)
by Mark
03:04
created

HmacAuthenticationProvider   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
dl 0
loc 47
ccs 12
cts 12
cp 1
rs 10
c 1
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A authenticate() 0 16 3
A supports() 0 3 1
1
<?php
2
3
namespace Acquia\Hmac\Symfony;
4
5
use Acquia\Hmac\RequestAuthenticatorInterface;
6
use Laminas\Diactoros\ResponseFactory;
7
use Laminas\Diactoros\ServerRequestFactory;
8
use Laminas\Diactoros\StreamFactory;
9
use Laminas\Diactoros\UploadedFileFactory;
10
use Symfony\Bridge\PsrHttpMessage\Factory\DiactorosFactory;
0 ignored issues
show
Bug introduced by
The type Symfony\Bridge\PsrHttpMe...actory\DiactorosFactory 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...
11
use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory;
12
use Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface;
13
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
14
use Symfony\Component\Security\Core\Exception\AuthenticationException;
15
16
/**
17
 * Provides the means to authenticate an HTTP HMAC request.
18
 */
19
class HmacAuthenticationProvider implements AuthenticationProviderInterface
20
{
21
    /**
22
     * @var \Acquia\Hmac\RequestAuthenticatorInterface
23
     *   A HMAC request authenticator service.
24
     */
25
    protected $authenticator;
26
27
    /**
28 3
     * Initializes the authentication provider.
29
     *
30 3
     * @param \Acquia\Hmac\RequestAuthenticatorInterface $authenticator
31 3
     *   The HMAC request authenticator service.
32
     */
33
    public function __construct(RequestAuthenticatorInterface $authenticator)
34
    {
35
        $this->authenticator = $authenticator;
36 2
    }
37
38 2
    /**
39 2
     * {@inheritDoc}
40
     */
41
    public function authenticate(TokenInterface $token)
42 2
    {
43
        if (class_exists(DiactorosFactory::class)) {
44 1
            $httpMessageFactory = new DiactorosFactory();
45 1
        } else {
46 1
            $httpMessageFactory = new PsrHttpFactory(new ServerRequestFactory(), new StreamFactory(), new UploadedFileFactory(), new ResponseFactory());
47
        }
48
49
        $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

49
        $psr7Request = $httpMessageFactory->createRequest($token->/** @scrutinizer ignore-call */ getRequest());
Loading history...
50
51
        try {
52
            $key = $this->authenticator->authenticate($psr7Request);
53 1
54
            return new HmacToken($token->getRequest(), $key);
55 1
        } catch (\Exception $e) {
56
            throw new AuthenticationException($e->getMessage());
57
        }
58
    }
59
60
    /**
61
     * {@inheritDoc}
62
     */
63
    public function supports(TokenInterface $token)
64
    {
65
        return $token instanceof HmacToken;
66
    }
67
}
68