aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/OAuth2/Bridge/ClientRepository.php
blob: 526e8397ade19c65498f4c4aa5cea25e49274444 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php

namespace Studip\OAuth2\Bridge;

use League\OAuth2\Server\Entities\ClientEntityInterface;
use League\OAuth2\Server\Repositories\ClientRepositoryInterface;
use Studip\OAuth2\Models\Client;

class ClientRepository implements ClientRepositoryInterface
{
    /**
     * Get a client.
     *
     * @param string $clientIdentifier The client's identifier
     */
    public function getClientEntity($clientIdentifier): ?ClientEntityInterface
    {
        $sorm = Client::findActive($clientIdentifier);
        if (!$sorm) {
            return null;
        }

        return new ClientEntity(
            $clientIdentifier,
            $sorm['name'],
            explode(',', $sorm['redirect']),
            $sorm->confidential()
        );
    }

    /**
     * Validate a client's secret.
     *
     * @param string      $clientIdentifier The client's identifier
     * @param string|null $clientSecret     The client's secret (if sent)
     * @param string|null $grantType        The type of grant the client is using (if sent)
     */
    public function validateClient($clientIdentifier, $clientSecret, $grantType): bool
    {
        if (!in_array($grantType, ['authorization_code', 'refresh_token'])) {
            return false;
        }

        $client = Client::findActive($clientIdentifier);
        if (!$client) {
            return false;
        }

        return !$client->confidential() || $this->verifySecret((string) $clientSecret, $client->secret);
    }

    /**
     * @param string $clientSecret
     * @param string $storedHash
     */
    protected function verifySecret($clientSecret, $storedHash): bool
    {
        return password_verify($clientSecret, $storedHash);
    }
}