Failed Conditions
Branch master (116909)
by Guilherme
08:28
created

ClientCredentials   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 2 Features 0
Metric Value
dl 0
loc 143
ccs 39
cts 39
cp 1
rs 10
c 2
b 2
f 0
wmc 12

6 Methods

Rating   Name   Duplication   Size   Complexity  
A checkClientCredentials() 0 10 2
A getClientDetails() 0 12 2
A __construct() 0 3 1
A isPublicClient() 0 11 2
A getClient() 0 21 3
A getClientScope() 0 9 2
1
<?php
2
/**
3
 * This file is part of the login-cidadao project or it's bundles.
4
 *
5
 * (c) Guilherme Donato <guilhermednt on github>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace LoginCidadao\OpenIDBundle\Storage;
12
13
use Doctrine\ORM\EntityManagerInterface;
14
use LoginCidadao\OAuthBundle\Model\ClientInterface;
15
use OAuth2\ServerBundle\Storage\ClientCredentials as BaseClass;
16
17
class ClientCredentials extends BaseClass
18
{
19
    private $em;
20
21 9
    public function __construct(EntityManagerInterface $EntityManager)
22
    {
23 9
        $this->em = $EntityManager;
24 9
    }
25
26
    /**
27
     * Make sure that the client credentials is valid.
28
     *
29
     * @param $client_id
30
     * Client identifier to be check with.
31
     * @param $client_secret
32
     * (optional) If a secret is required, check that they've given the right one.
33
     *
34
     * @return TRUE if the client credentials are valid, and MUST return FALSE if it isn't.
35
     * @endcode
36
     *
37
     * @see http://tools.ietf.org/html/rfc6749#section-3.1
38
     *
39
     * @ingroup oauth2_section_3
40
     */
41 3
    public function checkClientCredentials($client_id, $client_secret = null)
42
    {
43 3
        $client = $this->getClient($client_id);
44
45
        // If client exists check secret
46 3
        if ($client) {
47 2
            return $client->getClientSecret() === $client_secret;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $client->getClien...et() === $client_secret returns the type boolean which is incompatible with the documented return type true.
Loading history...
Bug introduced by
The method getClientSecret() does not exist on LoginCidadao\OAuthBundle\Model\ClientInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to LoginCidadao\OAuthBundle\Model\ClientInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

47
            return $client->/** @scrutinizer ignore-call */ getClientSecret() === $client_secret;
Loading history...
48
        }
49
50 1
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type true.
Loading history...
51
    }
52
53
    /**
54
     * Get client details corresponding client_id.
55
     *
56
     * OAuth says we should store request URIs for each registered client.
57
     * Implement this function to grab the stored URI for a given client id.
58
     *
59
     * @param $client_id
60
     * Client identifier to be check with.
61
     *
62
     * @return array
63
     *               Client details. The only mandatory key in the array is "redirect_uri".
64
     *               This function MUST return FALSE if the given client does not exist or is
65
     *               invalid. "redirect_uri" can be space-delimited to allow for multiple valid uris.
66
     * @code
67
     *               return array(
68
     *               "redirect_uri" => REDIRECT_URI,      // REQUIRED redirect_uri registered for the client
69
     *               "client_id"    => CLIENT_ID,         // OPTIONAL the client id
70
     *               "grant_types"  => GRANT_TYPES,       // OPTIONAL an array of restricted grant types
71
     *               );
72
     * @endcode
73
     *
74
     * @ingroup oauth2_section_4
75
     */
76 2
    public function getClientDetails($client_id)
77
    {
78 2
        $client = $this->getClient($client_id);
79
80 2
        if (!$client) {
81 1
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type array.
Loading history...
82
        }
83
84
        return [
85 1
            'redirect_uri' => implode(' ', $client->getRedirectUris()),
86 1
            'client_id' => $client->getPublicId(),
87 1
            'grant_types' => $client->getAllowedGrantTypes(),
88
        ];
89
    }
90
91
    /**
92
     * Determine if the client is a "public" client, and therefore
93
     * does not require passing credentials for certain grant types
94
     *
95
     * @param $client_id
96
     * Client identifier to be check with.
97
     *
98
     * @return TRUE if the client is public, and FALSE if it isn't.
99
     * @endcode
100
     *
101
     * @see http://tools.ietf.org/html/rfc6749#section-2.3
102
     * @see https://github.com/bshaffer/oauth2-server-php/issues/257
103
     *
104
     * @ingroup oauth2_section_2
105
     */
106 2
    public function isPublicClient($client_id)
107
    {
108 2
        $client = $this->getClient($client_id);
109
110 2
        if (!$client) {
111 1
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type true.
Loading history...
112
        }
113
114 1
        $secret = $client->getClientSecret();
115
116 1
        return empty($secret);
0 ignored issues
show
Bug Best Practice introduced by
The expression return empty($secret) returns the type boolean which is incompatible with the documented return type true.
Loading history...
117
    }
118
119
    /**
120
     * Get the scope associated with this client
121
     *
122
     * @return STRING the space-delineated scope list for the specified client_id
123
     */
124 2
    public function getClientScope($client_id)
125
    {
126 2
        $client = $this->getClient($client_id);
127
128 2
        if (!$client) {
129 1
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type string.
Loading history...
130
        }
131
132 1
        return implode(' ', $client->getAllowedScopes());
0 ignored issues
show
Bug introduced by
The method getAllowedScopes() does not exist on LoginCidadao\OAuthBundle\Model\ClientInterface. Did you maybe mean getAllowedGrantTypes()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

132
        return implode(' ', $client->/** @scrutinizer ignore-call */ getAllowedScopes());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
133
    }
134
135
    /**
136
     * @param $client_id mixed
137
     * @return null|ClientInterface
138
     */
139 9
    private function getClient($client_id)
140
    {
141 9
        $randomId = null;
142 9
        if (strstr($client_id, '_') !== false) {
143 7
            $parts = explode('_', $client_id);
144 7
            $client_id = $parts[0];
145 7
            $randomId = $parts[1];
146
        }
147
148 9
        $repo = $this->em->getRepository('LoginCidadaoOAuthBundle:Client');
149
150 9
        if ($randomId) {
151 7
            $client = $repo->findOneBy([
152 7
                'id' => $client_id,
153 7
                'randomId' => $randomId,
154
            ]);
155
        } else {
156 2
            $client = $repo->find($client_id);
157
        }
158
159 9
        return $client;
160
    }
161
}
162