Completed
Pull Request — master (#735)
by Guilherme
03:50
created

ClientCredentials::isPublicClient()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 12
rs 9.4285
c 1
b 1
f 0
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
    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...
22
    {
23
        $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...
24
    }
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.
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...
35
     * @endcode
36
     *
37
     * @see http://tools.ietf.org/html/rfc6749#section-3.1
38
     *
39
     * @ingroup oauth2_section_3
40
     */
41
    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...
42
    {
43
        $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...
44
45
        // If client exists check secret
46
        if ($client) {
47
            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...
48
        }
49
50
        return false;
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
0 ignored issues
show
Documentation introduced by
Should the return type not be false|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...
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
    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...
77
    {
78
        $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...
79
80
        if (!$client) {
81
            return false;
82
        }
83
84
        return [
85
            'redirect_uri' => implode(' ', $client->getRedirectUris()),
86
            'client_id' => $client->getPublicId(),
87
            '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.
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...
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
    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...
107
    {
108
        $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...
109
110
        if (!$client) {
111
            return false;
112
        }
113
114
        $secret = $client->getClientSecret();
115
116
        return empty($secret);
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
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...
123
     */
124
    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...
125
    {
126
        $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...
127
128
        if (!$client) {
129
            return false;
130
        }
131
132
        return implode(' ', $client->getAllowedScopes());
133
    }
134
135
    /**
136
     * @param $client_id mixed
137
     * @return null|ClientInterface
0 ignored issues
show
Documentation introduced by
Should the return type not be object?

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...
138
     */
139
    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...
140
    {
141
        $randomId = null;
142
        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...
143
            $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...
144
            $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...
145
            $randomId = $parts[1];
146
        }
147
148
        $repo = $this->em->getRepository('LoginCidadaoOAuthBundle:Client');
149
150
        if ($randomId) {
151
            $client = $repo->findOneBy([
152
                '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...
153
                'randomId' => $randomId,
154
            ]);
155
        } else {
156
            $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...
157
        }
158
159
        return $client;
160
    }
161
}
162