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\Scope; |
15
|
|
|
|
16
|
|
|
use OAuth2Framework\Component\AuthorizationEndpoint\AuthorizationRequest\AuthorizationRequest; |
17
|
|
|
use OAuth2Framework\Component\AuthorizationEndpoint\ParameterChecker\ParameterChecker; |
18
|
|
|
use OAuth2Framework\Component\Scope\Policy\ScopePolicyManager; |
19
|
|
|
|
20
|
|
|
class ScopeParameterChecker implements ParameterChecker |
21
|
|
|
{ |
22
|
|
|
private $scopeRepository; |
23
|
|
|
|
24
|
|
|
private $scopePolicyManager; |
25
|
|
|
|
26
|
|
|
public function __construct(ScopeRepository $scopeRepository, ScopePolicyManager $scopePolicyManager) |
27
|
|
|
{ |
28
|
|
|
$this->scopeRepository = $scopeRepository; |
29
|
|
|
$this->scopePolicyManager = $scopePolicyManager; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function check(AuthorizationRequest $authorization): void |
33
|
|
|
{ |
34
|
|
|
$requestedScope = $this->getRequestedScope($authorization); |
35
|
|
|
$requestedScope = $this->scopePolicyManager->apply($requestedScope, $authorization->getClient()); |
36
|
|
|
if (empty($requestedScope)) { |
37
|
|
|
return; |
38
|
|
|
} |
39
|
|
|
$scopes = \explode(' ', $requestedScope); |
40
|
|
|
|
41
|
|
|
$availableScopes = $this->scopeRepository->all(); |
42
|
|
|
if (0 !== \count(\array_diff($scopes, $availableScopes))) { |
43
|
|
|
throw new \InvalidArgumentException(\Safe\sprintf('An unsupported scope was requested. Available scopes are %s.', \implode(', ', $availableScopes))); |
44
|
|
|
} |
45
|
|
|
$authorization->getMetadata()->set('scope', \implode(' ', $scopes)); |
46
|
|
|
$authorization->setResponseParameter('scope', \implode(' ', $scopes)); //TODO: should be done after consent depending on approved scope |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
private function getRequestedScope(AuthorizationRequest $authorization): string |
50
|
|
|
{ |
51
|
|
|
if ($authorization->hasQueryParam('scope')) { |
52
|
|
|
$requestedScope = $authorization->getQueryParam('scope'); |
53
|
|
|
if (1 !== \Safe\preg_match('/^[\x20\x23-\x5B\x5D-\x7E]+$/', $requestedScope)) { |
54
|
|
|
throw new \InvalidArgumentException('Invalid characters found in the "scope" parameter.'); |
55
|
|
|
} |
56
|
|
|
} else { |
57
|
|
|
$requestedScope = ''; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $requestedScope; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|