Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
ClientRepository
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 3
56
0.00% covered (danger)
0.00%
0 / 1
 getClientEntity
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
 getClientEntityByDBId
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 validateClient
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3namespace MediaWiki\Extension\OAuth\Repository;
4
5use InvalidArgumentException;
6use League\OAuth2\Server\Repositories\ClientRepositoryInterface;
7use MediaWiki\Extension\OAuth\Backend\Utils;
8use MediaWiki\Extension\OAuth\Entity\ClientEntity;
9
10class ClientRepository implements ClientRepositoryInterface {
11
12    /**
13     * Get a client.
14     *
15     * @param string $clientIdentifier The client's identifier
16     *
17     * @return ClientEntity|bool
18     */
19    public function getClientEntity( $clientIdentifier ) {
20        $client = ClientEntity::newFromKey(
21            Utils::getCentralDB( DB_REPLICA ),
22            $clientIdentifier
23        );
24        if ( !$client instanceof ClientEntity ) {
25            return false;
26        }
27
28        return $client;
29    }
30
31    /**
32     * @param int $clientId
33     * @return ClientEntity|bool
34     */
35    public function getClientEntityByDBId( $clientId ) {
36        $client = ClientEntity::newFromId( Utils::getCentralDB( DB_REPLICA ), $clientId );
37        if ( !$client instanceof ClientEntity ) {
38            return false;
39        }
40
41        return $client;
42    }
43
44    /**
45     * Validate a client's secret.
46     *
47     * @param string $clientIdentifier The client's identifier
48     * @param null|string $clientSecret The client's secret (if sent)
49     * @param null|string $grantType The type of grant the client is using (if sent)
50     *
51     * @return bool
52     * @throws InvalidArgumentException
53     */
54    public function validateClient( $clientIdentifier, $clientSecret, $grantType ) {
55        $client = $this->getClientEntity( $clientIdentifier );
56        if ( !$client || !$client instanceof ClientEntity ) {
57            throw new InvalidArgumentException(
58                "Client with identifier $clientIdentifier does not exist!"
59            );
60        }
61
62        return $client->validate( $clientSecret, $grantType );
63    }
64}