Passed
Push — master ( c97e91...9a636e )
by Alexandre
01:52
created

authenticate()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 9
nc 4
nop 2
dl 0
loc 14
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: GCC-MED
5
 * Date: 09/03/2018
6
 * Time: 16:58
7
 */
8
9
namespace OAuth2\ClientAuthentication;
10
11
12
use OAuth2\Roles\ClientInterface;
13
use OAuth2\Roles\Clients\ConfidentialClient;
14
use OAuth2\Storages\ClientStorageInterface;
15
use Psr\Http\Message\ServerRequestInterface;
16
17
class ClientSecretBasicAuthenticationMethod implements ClientAuthenticationMethodInterface
18
{
19
    /**
20
     * @var ClientStorageInterface
21
     */
22
    private $clientStorage;
23
24
    public function __construct(ClientStorageInterface $clientStorage)
25
    {
26
        $this->clientStorage = $clientStorage;
27
    }
28
29
    function support(ServerRequestInterface $request, array $requestData): bool
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
30
    {
31
        $header = $request->getHeader('Authorization')[0] ?? null;
32
        return strpos($header, 'Basic') === 0;
33
    }
34
35
    function authenticate(ServerRequestInterface $request, array $requestData): ?ClientInterface
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
36
    {
37
        $header = $request->getHeader('Authorization')[0];
38
        $token = explode(' ', $header)[1] ?? null;
39
        if ($token) {
40
            $credentials = explode(':', base64_decode($token));
41
            if (count($credentials) == 2) {
42
                $client = $this->clientStorage->get($credentials[0]);
43
                if ($client instanceof ConfidentialClient && $client->getPassword() === $credentials[1]) {
44
                    return $client;
45
                }
46
            }
47
        }
48
        return null;
49
    }
50
}