Failed Conditions
Push — issue#666 ( 4966a1...aff657 )
by Guilherme
08:23
created

ClientCredentials::getClientScope()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 2
eloc 7
c 1
b 1
f 0
nc 2
nop 1
dl 0
loc 17
ccs 7
cts 7
cp 1
crap 2
rs 9.4285
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\Entity\Client;
15
use LoginCidadao\OAuthBundle\Entity\ClientRepository;
16
use LoginCidadao\OAuthBundle\Model\ClientInterface;
17
use LoginCidadao\RemoteClaimsBundle\Entity\RemoteClaim;
18
use LoginCidadao\RemoteClaimsBundle\Entity\RemoteClaimRepository;
19
use LoginCidadao\RemoteClaimsBundle\Model\RemoteClaimInterface;
20
use OAuth2\ServerBundle\Storage\ClientCredentials as BaseClass;
21
22
class ClientCredentials extends BaseClass
23
{
24
    private $em;
25
26 10
    public function __construct(EntityManagerInterface $EntityManager)
0 ignored issues
show
Coding Style introduced by
$EntityManager does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
Coding Style Naming introduced by
The parameter $EntityManager is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
27
    {
28 10
        $this->em = $EntityManager;
0 ignored issues
show
Coding Style introduced by
$EntityManager does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
29 10
    }
30
31
    /**
32
     * Make sure that the client credentials is valid.
33
     *
34
     * @param $client_id
35
     * Client identifier to be check with.
36
     * @param $client_secret
37
     * (optional) If a secret is required, check that they've given the right one.
38
     *
39
     * @return TRUE if the client credentials are valid, and MUST return FALSE if it isn't.
0 ignored issues
show
Documentation introduced by
Should the return type not be boolean?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
40
     * @endcode
41
     *
42
     * @see http://tools.ietf.org/html/rfc6749#section-3.1
43
     *
44
     * @ingroup oauth2_section_3
45
     */
46 3
    public function checkClientCredentials($client_id, $client_secret = null)
0 ignored issues
show
Coding Style Naming introduced by
The parameter $client_id is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style Naming introduced by
The parameter $client_secret is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
47
    {
48 3
        $client = $this->getClient($client_id);
0 ignored issues
show
Coding Style introduced by
$client_id does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
49
50
        // If client exists check secret
51 3
        if ($client) {
52 2
            return $client->getClientSecret() === $client_secret;
0 ignored issues
show
Coding Style introduced by
$client_secret does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
53
        }
54
55 1
        return false;
56
    }
57
58
    /**
59
     * Get client details corresponding client_id.
60
     *
61
     * OAuth says we should store request URIs for each registered client.
62
     * Implement this function to grab the stored URI for a given client id.
63
     *
64
     * @param $client_id
65
     * Client identifier to be check with.
66
     *
67
     * @return array
0 ignored issues
show
Documentation introduced by
Should the return type not be false|array<string,string|array>?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
68
     *               Client details. The only mandatory key in the array is "redirect_uri".
69
     *               This function MUST return FALSE if the given client does not exist or is
70
     *               invalid. "redirect_uri" can be space-delimited to allow for multiple valid uris.
71
     * @code
72
     *               return array(
73
     *               "redirect_uri" => REDIRECT_URI,      // REQUIRED redirect_uri registered for the client
74
     *               "client_id"    => CLIENT_ID,         // OPTIONAL the client id
75
     *               "grant_types"  => GRANT_TYPES,       // OPTIONAL an array of restricted grant types
76
     *               );
77
     * @endcode
78
     *
79
     * @ingroup oauth2_section_4
80
     */
81 2
    public function getClientDetails($client_id)
0 ignored issues
show
Coding Style Naming introduced by
The parameter $client_id is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
82
    {
83 2
        $client = $this->getClient($client_id);
0 ignored issues
show
Coding Style introduced by
$client_id does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
84
85 2
        if (!$client) {
86 1
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type declared by the interface OAuth2\Storage\ClientInterface::getClientDetails of type array.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
87
        }
88
89
        return [
90 1
            'redirect_uri' => implode(' ', $client->getRedirectUris()),
91 1
            'client_id' => $client->getPublicId(),
92 1
            'grant_types' => $client->getAllowedGrantTypes(),
93
        ];
94
    }
95
96
    /**
97
     * Determine if the client is a "public" client, and therefore
98
     * does not require passing credentials for certain grant types
99
     *
100
     * @param $client_id
101
     * Client identifier to be check with.
102
     *
103
     * @return TRUE if the client is public, and FALSE if it isn't.
0 ignored issues
show
Documentation introduced by
Should the return type not be boolean?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
104
     * @endcode
105
     *
106
     * @see http://tools.ietf.org/html/rfc6749#section-2.3
107
     * @see https://github.com/bshaffer/oauth2-server-php/issues/257
108
     *
109
     * @ingroup oauth2_section_2
110
     */
111 2
    public function isPublicClient($client_id)
0 ignored issues
show
Coding Style Naming introduced by
The parameter $client_id is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
112
    {
113 2
        $client = $this->getClient($client_id);
0 ignored issues
show
Coding Style introduced by
$client_id does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
114
115 2
        if (!$client) {
116 1
            return false;
117
        }
118
119 1
        $secret = $client->getClientSecret();
120
121 1
        return empty($secret);
122
    }
123
124
    /**
125
     * Get the scope associated with this client
126
     *
127
     * @return string the space-delineated scope list for the specified client_id
0 ignored issues
show
Documentation introduced by
Should the return type not be false|string?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
128
     */
129 3
    public function getClientScope($client_id)
0 ignored issues
show
Coding Style Naming introduced by
The parameter $client_id is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
130
    {
131
        /** @var Client $client */
132 3
        $client = $this->getClient($client_id);
0 ignored issues
show
Coding Style introduced by
$client_id does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
133
134 3
        if (!$client instanceof ClientInterface) {
135 1
            return false;
136
        }
137
138
        /*
139
         * TODO: performance issue: if there are too many Remote Claims listing all of them might be an issue
140
         */
141 2
        $remoteClaims = $this->getRemoteClaimsTags($this->getAllRemoteClaims());
142 2
        $allowedScopes = array_merge($client->getAllowedScopes(), $remoteClaims);
143
144 2
        return implode(' ', $allowedScopes);
145
    }
146
147
    /**
148
     * @param $client_id mixed
149
     * @return null|ClientInterface
150
     */
151 10
    private function getClient($client_id)
0 ignored issues
show
Coding Style introduced by
$client_id does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
Coding Style Naming introduced by
The parameter $client_id is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
152
    {
153 10
        $randomId = null;
154 10
        if (strstr($client_id, '_') !== false) {
0 ignored issues
show
Coding Style introduced by
$client_id does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
155 8
            $parts = explode('_', $client_id);
0 ignored issues
show
Coding Style introduced by
$client_id does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
156 8
            $client_id = $parts[0];
0 ignored issues
show
Coding Style introduced by
$client_id does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
157 8
            $randomId = $parts[1];
158
        }
159
160
        /** @var ClientRepository $repo */
161 10
        $repo = $this->em->getRepository('LoginCidadaoOAuthBundle:Client');
162
163 10
        if ($randomId) {
164
            /** @var ClientInterface $client */
165 8
            $client = $repo->findOneBy([
166 8
                'id' => $client_id,
0 ignored issues
show
Coding Style introduced by
$client_id does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
167 8
                'randomId' => $randomId,
168
            ]);
169
        } else {
170
            /** @var ClientInterface $client */
171 2
            $client = $repo->find($client_id);
0 ignored issues
show
Coding Style introduced by
$client_id does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
172
        }
173
174 10
        return $client;
175
    }
176
177
    /**
178
     * @return array|RemoteClaimInterface[]
179
     */
180 2
    private function getAllRemoteClaims()
181
    {
182
        /** @var RemoteClaimRepository $repo */
183 2
        $repo = $this->em->getRepository('LoginCidadaoRemoteClaimsBundle:RemoteClaim');
184
185 2
        $remoteClaims = $repo->findAll();
186
187 2
        return $remoteClaims;
188
    }
189
190 2
    private function getRemoteClaimsTags(array $remoteClaims)
191
    {
192 2
        if (count($remoteClaims) > 0) {
193 1
            return array_map(function (RemoteClaimInterface $claim) {
194 1
                return $claim->getName();
195 1
            }, $remoteClaims);
196
        }
197
198 1
        return [];
199
    }
200
}
201