1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Controller; |
4
|
|
|
|
5
|
|
|
use Del\Common\ContainerService; |
6
|
|
|
use League\OAuth2\Server\Exception\OAuthServerException; |
7
|
|
|
use League\OAuth2\Server\ResourceServer; |
8
|
|
|
use OAuth\AccessToken; |
9
|
|
|
use OAuth\Exception\OAuthException; |
10
|
|
|
use OAuth\Repository\AccessTokenRepository; |
11
|
|
|
use Zend\Diactoros\Response; |
12
|
|
|
|
13
|
|
|
class ResourceServerController extends BaseController |
14
|
|
|
{ |
15
|
|
|
/** @var AccessToken $accessToken*/ |
16
|
|
|
protected $accessToken; |
17
|
|
|
|
18
|
|
|
/** @var string $client */ |
19
|
|
|
protected $client; |
20
|
|
|
|
21
|
|
|
/** @var array $scopes */ |
22
|
|
|
protected $scopes; |
23
|
|
|
|
24
|
|
|
/** @var null|int $user */ |
25
|
|
|
protected $user; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @throws OAuthServerException |
29
|
|
|
*/ |
30
|
2 |
|
public function init() |
31
|
|
|
{ |
32
|
2 |
|
$container = ContainerService::getInstance()->getContainer(); |
33
|
2 |
|
$container['repository.Client']; // this is a weird doctrine/pimple bug? |
34
|
|
|
// comment this ^ out and you cant get the repo below! mapping from access token to scope! |
35
|
|
|
/** @var AccessTokenRepository $accessTokenRepository */ |
36
|
2 |
|
$accessTokenRepository = $container['repository.AccessToken']; |
37
|
2 |
|
$publicKeyPath = 'file://' . APPLICATION_PATH . '/data/keys/public.key'; |
38
|
2 |
|
$server = new ResourceServer( |
39
|
2 |
|
$accessTokenRepository, |
40
|
2 |
|
$publicKeyPath |
41
|
|
|
); |
42
|
|
|
try { |
43
|
2 |
|
$request = $server->validateAuthenticatedRequest($this->getRequest()); |
44
|
1 |
|
$this->setRequest($request); |
45
|
1 |
|
$this->accessToken = $accessTokenRepository->findOneBy(['identifier' => $request->getAttribute('oauth_access_token_id')]); |
46
|
1 |
|
$this->client = $request->getAttribute('oauth_client_id'); |
47
|
1 |
|
$this->scopes = $request->getAttribute('oauth_scopes'); |
48
|
1 |
|
$this->user = $request->getAttribute('user'); |
49
|
1 |
|
} catch (OAuthServerException $e) { |
50
|
1 |
|
$response = $e->generateHttpResponse(new Response()); |
51
|
1 |
|
$this->sendResponse($response); |
52
|
|
|
} |
53
|
1 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param array $scopes |
57
|
|
|
* @return bool |
58
|
|
|
* @throws OAuthException |
59
|
|
|
*/ |
60
|
1 |
|
protected function scopeCheck(array $scopes): bool |
61
|
|
|
{ |
62
|
1 |
|
$grantedScopes = $this->scopes; |
63
|
1 |
|
foreach ($scopes as $scope) { |
64
|
1 |
|
if (!in_array($scope, $grantedScopes)) { |
65
|
|
|
throw new OAuthException('Required scope has not been granted.'); |
66
|
|
|
} |
67
|
|
|
} |
68
|
1 |
|
return true; |
69
|
|
|
} |
70
|
|
|
} |