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

HmacAuthenticationProvider   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 42
ccs 12
cts 12
cp 1
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A authenticate() 0 11 2
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\PsrHttpFactory;
11
use Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface;
12
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
13
use Symfony\Component\Security\Core\Exception\AuthenticationException;
14
15
/**
16
 * Provides the means to authenticate an HTTP HMAC request.
17
 */
18
class HmacAuthenticationProvider implements AuthenticationProviderInterface
19
{
20
    /**
21
     * @var \Acquia\Hmac\RequestAuthenticatorInterface
22
     *   A HMAC request authenticator service.
23
     */
24
    protected $authenticator;
25
26
    /**
27
     * Initializes the authentication provider.
28 3
     *
29
     * @param \Acquia\Hmac\RequestAuthenticatorInterface $authenticator
30 3
     *   The HMAC request authenticator service.
31 3
     */
32
    public function __construct(RequestAuthenticatorInterface $authenticator)
33
    {
34
        $this->authenticator = $authenticator;
35
    }
36 2
37
    /**
38 2
     * {@inheritDoc}
39 2
     */
40
    public function authenticate(TokenInterface $token)
41
    {
42 2
        $httpMessageFactory = new PsrHttpFactory(new ServerRequestFactory(), new StreamFactory(), new UploadedFileFactory(), new ResponseFactory());
43
        $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

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