Test Setup Failed
Push — oauth2 ( 63c070 )
by Herberto
11:43 queued 07:12
created

OauthClientRepository::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Explicit Architecture POC,
7
 * which is created on top of the Symfony Demo application.
8
 *
9
 * (c) Herberto Graça <[email protected]>
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace Acme\App\Infrastructure\Auth\Authentication\Oauth;
16
17
use Acme\App\Core\Port\Persistence\DQL\DqlQueryBuilderInterface;
18
use Acme\App\Core\Port\Persistence\Exception\EmptyQueryResultException;
19
use Acme\App\Core\Port\Persistence\QueryServiceRouterInterface;
20
use League\OAuth2\Server\Entities\ClientEntityInterface;
21
use League\OAuth2\Server\Repositories\ClientRepositoryInterface;
22
23
final class OauthClientRepository implements ClientRepositoryInterface
24
{
25
    /**
26
     * @var DqlQueryBuilderInterface
27
     */
28
    private $dqlQueryBuilder;
29
30
    /**
31
     * @var QueryServiceRouterInterface
32
     */
33
    private $queryService;
34
35
    public function __construct(
36
        DqlQueryBuilderInterface $dqlQueryBuilder,
37
        QueryServiceRouterInterface $queryService
38
    ) {
39
        $this->dqlQueryBuilder = $dqlQueryBuilder;
40
        $this->queryService = $queryService;
41
    }
42
43
    /**
44
     * @param string $clientIdentifier The client's identifier
45
     * @param string|null $grantType The grant type used (if sent)
46
     * @param string|null $clientSecret The client's secret (if sent)
47
     * @param bool $mustValidateSecret If true the client must attempt to validate the secret if the client
48
     *                                        is confidential
49
     */
50
    public function getClientEntity(
51
        $clientIdentifier,
52
        $grantType = null,
53
        $clientSecret = null,
54
        $mustValidateSecret = true
55
    ): ?ClientEntityInterface {
56
        try {
57
            $oauthClient = $this->findActive($clientIdentifier);
58
        } catch (EmptyQueryResultException $e) {
59
            return null;
60
        }
61
62
        if ($mustValidateSecret && !hash_equals($oauthClient->getSecret(), (string) $clientSecret)) {
63
            return null;
64
        }
65
66
        return $oauthClient;
67
    }
68
69
    private function findActive(string $clientIdentifier): OauthClient
70
    {
71
        $dqlQuery = $this->dqlQueryBuilder->create(OauthClient::class)
72
            ->where('OauthClient.identifier = :clientIdentifier')
73
            ->andWhere('OauthClient.active = :active')
74
            ->setParameter('clientIdentifier', $clientIdentifier)
75
            ->setParameter('active', true)
76
            ->build();
77
78
        return $this->queryService->query($dqlQuery)->getSingleResult();
79
    }
80
}
81