|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* The MIT License (MIT) |
|
7
|
|
|
* |
|
8
|
|
|
* Copyright (c) 2014-2018 Spomky-Labs |
|
9
|
|
|
* |
|
10
|
|
|
* This software may be modified and distributed under the terms |
|
11
|
|
|
* of the MIT license. See the LICENSE file for details. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace OAuth2Framework\Component\Server\Scope; |
|
15
|
|
|
|
|
16
|
|
|
use OAuth2Framework\Component\Server\Core\AccessToken\AccessToken; |
|
17
|
|
|
use OAuth2Framework\Component\Server\Core\Client\Client; |
|
18
|
|
|
use OAuth2Framework\Component\Server\Core\ResourceOwner\ResourceOwner; |
|
19
|
|
|
use OAuth2Framework\Component\Server\TokenEndpoint\Extension\TokenEndpointExtension; |
|
20
|
|
|
use OAuth2Framework\Component\Server\TokenEndpoint\GrantTypeData; |
|
21
|
|
|
use OAuth2Framework\Component\Server\TokenEndpoint\GrantType; |
|
22
|
|
|
use OAuth2Framework\Component\Server\Scope\Policy\ScopePolicyManager; |
|
23
|
|
|
use OAuth2Framework\Component\Server\Core\Response\OAuth2Exception; |
|
24
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
25
|
|
|
|
|
26
|
|
|
final class TokenEndpointScopeExtension implements TokenEndpointExtension |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @var ScopeRepository |
|
30
|
|
|
*/ |
|
31
|
|
|
private $scopeRepository; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var ScopePolicyManager|null |
|
35
|
|
|
*/ |
|
36
|
|
|
private $scopePolicyManager; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* ScopeProcessor constructor. |
|
40
|
|
|
* |
|
41
|
|
|
* @param ScopeRepository $scopeRepository |
|
42
|
|
|
* @param ScopePolicyManager|null $scopePolicyManager |
|
43
|
|
|
*/ |
|
44
|
|
|
public function __construct(ScopeRepository $scopeRepository, ? ScopePolicyManager $scopePolicyManager) |
|
45
|
|
|
{ |
|
46
|
|
|
$this->scopeRepository = $scopeRepository; |
|
47
|
|
|
$this->scopePolicyManager = $scopePolicyManager; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* {@inheritdoc} |
|
52
|
|
|
*/ |
|
53
|
|
|
public function beforeAccessTokenIssuance(ServerRequestInterface $request, GrantTypeData $grantTypeData, GrantType $grantType, callable $next): GrantTypeData |
|
54
|
|
|
{ |
|
55
|
|
|
/** @var GrantTypeData $grantTypeData */ |
|
56
|
|
|
$grantTypeData = $next($request, $grantTypeData, $grantType); |
|
57
|
|
|
$params = $request->getParsedBody() ?? []; |
|
58
|
|
|
if (!array_key_exists('scope', $params)) { |
|
59
|
|
|
$scope = $grantTypeData->getAvailableScopes() ?? []; |
|
|
|
|
|
|
60
|
|
|
} else { |
|
61
|
|
|
$scope = explode(' ', $params['scope']); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
//Modify the scope according to the scope policy |
|
65
|
|
|
try { |
|
66
|
|
|
if (null !== $this->scopePolicyManager) { |
|
67
|
|
|
$scope = $this->scopePolicyManager->apply($scope, $grantTypeData->getClient()); |
|
|
|
|
|
|
68
|
|
|
} |
|
69
|
|
|
} catch (\InvalidArgumentException $e) { |
|
70
|
|
|
throw new OAuth2Exception(400, OAuth2Exception::ERROR_INVALID_SCOPE, $e->getMessage(), [], $e); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$availableScope = $grantTypeData->getAvailableScopes() ? $grantTypeData->getAvailableScopes() : $this->scopeRepository->getAvailableScopesForClient($grantTypeData->getClient()); |
|
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
//Check if requested scope are within the available scope |
|
76
|
|
|
if (!$this->scopeRepository->areRequestedScopesAvailable($scope, $availableScope)) { |
|
77
|
|
|
throw new OAuth2Exception(400, OAuth2Exception::ERROR_INVALID_SCOPE, sprintf('An unsupported scope was requested. Available scopes are %s.', implode(', ', $availableScope))); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$grantTypeData = $grantTypeData->withScopes($scope); |
|
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
return $grantTypeData; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* {@inheritdoc} |
|
87
|
|
|
*/ |
|
88
|
|
|
public function afterAccessTokenIssuance(Client $client, ResourceOwner $resourceOwner, AccessToken $accessToken, callable $next): array |
|
89
|
|
|
{ |
|
90
|
|
|
return $next($client, $resourceOwner, $accessToken); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
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.