Completed
Push — master ( 592d95...c00565 )
by Abdelrahman
18:11 queued 10s
created

ClientRepository   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 2
dl 0
loc 71
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getClientEntity() 0 18 4
A validateClient() 0 15 6
B handlesGrant() 0 19 8
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Rinvex\Oauth\Repositories;
6
7
use Rinvex\Oauth\Bridge\Client;
8
use League\OAuth2\Server\Repositories\ClientRepositoryInterface;
9
10
class ClientRepository implements ClientRepositoryInterface
11
{
12
    /**
13
     * {@inheritdoc}
14
     */
15
    public function getClientEntity($clientIdentifier)
16
    {
17
        $client = app('rinvex.oauth.client')->resolveRouteBinding($clientIdentifier);
18
19
        $record = $client && ! $client->is_revoked ? $client : null;
20
21
        if (! $record) {
22
            return;
23
        }
24
25
        return new Client(
26
            $clientIdentifier,
27
            $record->name,
28
            $record->redirect,
29
            $record->user_type,
30
            $record->isConfidential()
31
        );
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function validateClient($clientIdentifier, $clientSecret, $grantType)
38
    {
39
        // First, we will verify that the client exists and is authorized to create personal
40
        // access tokens. Generally personal access tokens are only generated by the user
41
        // from the main interface. We'll only let certain clients generate the tokens.
42
        $client = app('rinvex.oauth.client')->resolveRouteBinding($clientIdentifier);
43
44
        $record = $client && ! $client->is_revoked ? $client : null;
45
46
        if (! $record || ! $this->handlesGrant($record, $grantType)) {
47
            return false;
48
        }
49
50
        return ! $record->isConfidential() || password_verify((string) $clientSecret, $record->secret);
51
    }
52
53
    /**
54
     * Determine if the given client can handle the given grant type.
55
     *
56
     * @param \Rinvex\Oauth\Models\Client $record
57
     * @param string                      $grantType
58
     *
59
     * @return bool
60
     */
61
    protected function handlesGrant($record, $grantType)
62
    {
63
        if (is_array($record->grant_types) && ! in_array($grantType, $record->grant_types)) {
64
            return false;
65
        }
66
67
        switch ($grantType) {
68
            case 'authorization_code':
69
                return ! $record->firstParty();
70
            case 'personal_access':
71
                return $record->grant_type === 'personal_access' && $record->isConfidential();
72
            case 'password':
73
                return $record->grant_type === 'password';
74
            case 'client_credentials':
75
                return $record->isConfidential();
76
            default:
77
                return true;
78
        }
79
    }
80
}
81