1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Limoncello\OAuthServer\GrantTraits; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Copyright 2015-2019 [email protected] |
7
|
|
|
* |
8
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
9
|
|
|
* you may not use this file except in compliance with the License. |
10
|
|
|
* You may obtain a copy of the License at |
11
|
|
|
* |
12
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
13
|
|
|
* |
14
|
|
|
* Unless required by applicable law or agreed to in writing, software |
15
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
16
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
17
|
|
|
* See the License for the specific language governing permissions and |
18
|
|
|
* limitations under the License. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
use Limoncello\OAuthServer\Contracts\ClientInterface; |
22
|
|
|
use Limoncello\OAuthServer\Contracts\Integration\ClientIntegrationInterface; |
23
|
|
|
use Limoncello\OAuthServer\Exceptions\OAuthTokenBodyException; |
24
|
|
|
use Psr\Http\Message\ResponseInterface; |
25
|
|
|
use function array_key_exists; |
26
|
|
|
use function explode; |
27
|
|
|
use function is_string; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @package Limoncello\OAuthServer |
31
|
|
|
* |
32
|
|
|
* @link https://tools.ietf.org/html/rfc6749#section-1.3 |
33
|
|
|
* @link https://tools.ietf.org/html/rfc6749#section-4.4 |
34
|
|
|
*/ |
35
|
|
|
trait ClientGrantTrait |
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* @var ClientIntegrationInterface |
39
|
|
|
*/ |
40
|
3 |
|
private $clientIntegration; |
41
|
|
|
|
42
|
3 |
|
/** |
43
|
|
|
* @return ClientIntegrationInterface |
44
|
|
|
*/ |
45
|
|
|
protected function clientGetIntegration(): ClientIntegrationInterface |
46
|
|
|
{ |
47
|
|
|
return $this->clientIntegration; |
48
|
|
|
} |
49
|
|
|
|
50
|
44 |
|
/** |
51
|
|
|
* @param ClientIntegrationInterface $clientIntegration |
52
|
44 |
|
* |
53
|
|
|
* @return void |
54
|
|
|
*/ |
55
|
|
|
public function clientSetIntegration(ClientIntegrationInterface $clientIntegration): void |
56
|
|
|
{ |
57
|
|
|
$this->clientIntegration = $clientIntegration; |
58
|
|
|
} |
59
|
|
|
|
60
|
3 |
|
/** |
61
|
|
|
* @param string[] $parameters |
62
|
3 |
|
* |
63
|
|
|
* @return string[]|null |
|
|
|
|
64
|
3 |
|
*/ |
65
|
|
|
protected function clientGetScope(array $parameters): ?array |
66
|
|
|
{ |
67
|
|
|
$scope = $this->clientReadStringValue($parameters, 'scope'); |
68
|
|
|
|
69
|
|
|
return empty($scope) === false ? explode(' ', $scope) : null; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
4 |
|
* @param string[] $parameters |
74
|
|
|
* @param ClientInterface $determinedClient |
75
|
4 |
|
* |
76
|
1 |
|
* @return ResponseInterface |
77
|
|
|
*/ |
78
|
|
|
protected function clientIssueToken(array $parameters, ClientInterface $determinedClient): ResponseInterface |
79
|
3 |
|
{ |
80
|
|
|
if ($determinedClient->isClientGrantEnabled() === false) { |
81
|
3 |
|
throw new OAuthTokenBodyException(OAuthTokenBodyException::ERROR_UNAUTHORIZED_CLIENT); |
82
|
3 |
|
} |
83
|
1 |
|
|
84
|
|
|
$scope = $this->clientGetScope($parameters); |
85
|
|
|
list ($isScopeValid, $scopeList, $isScopeModified) = |
86
|
2 |
|
$this->clientGetIntegration()->clientValidateScope($determinedClient, $scope); |
87
|
2 |
|
if ($isScopeValid === false) { |
88
|
2 |
|
throw new OAuthTokenBodyException(OAuthTokenBodyException::ERROR_INVALID_SCOPE); |
89
|
2 |
|
} |
90
|
2 |
|
|
91
|
|
|
$response = $this->clientGetIntegration()->clientCreateAccessTokenResponse( |
92
|
|
|
$determinedClient, |
93
|
2 |
|
$isScopeModified, |
94
|
|
|
$scopeList, |
95
|
|
|
$parameters |
96
|
|
|
); |
97
|
|
|
|
98
|
|
|
return $response; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
3 |
|
* @param array $parameters |
103
|
|
|
* @param string $name |
104
|
3 |
|
* |
105
|
3 |
|
* @return null|string |
106
|
|
|
*/ |
107
|
|
|
private function clientReadStringValue(array $parameters, string $name): ?string |
108
|
|
|
{ |
109
|
|
|
return array_key_exists($name, $parameters) === true && is_string($value = $parameters[$name]) === true ? |
110
|
|
|
$value : null; |
|
|
|
|
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
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.If the return type contains the type array, this check recommends the use of a more specific type like
String[]
orarray<String>
.