|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Lookyman\NetteOAuth2Server\Storage\Doctrine; |
|
5
|
|
|
|
|
6
|
|
|
use Kdyby\Doctrine\Registry; |
|
7
|
|
|
use League\OAuth2\Server\RequestTypes\AuthorizationRequest; |
|
8
|
|
|
use Lookyman\NetteOAuth2Server\Storage\IAuthorizationRequestSerializer; |
|
9
|
|
|
|
|
10
|
|
|
class AuthorizationRequestSerializer implements IAuthorizationRequestSerializer |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var Registry |
|
14
|
|
|
*/ |
|
15
|
|
|
private $registry; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @param Registry $registry |
|
19
|
|
|
*/ |
|
20
|
|
|
public function __construct(Registry $registry) |
|
21
|
|
|
{ |
|
22
|
|
|
$this->registry = $registry; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param AuthorizationRequest $authorizationRequest |
|
27
|
|
|
* @return string |
|
28
|
|
|
*/ |
|
29
|
|
|
public function serialize(AuthorizationRequest $authorizationRequest): string |
|
30
|
|
|
{ |
|
31
|
|
|
$manager = $this->registry->getManager(); |
|
32
|
|
|
if ($authorizationRequest->getClient()) { |
|
33
|
|
|
$manager->detach($authorizationRequest->getClient()); |
|
34
|
|
|
} |
|
35
|
|
|
foreach ($authorizationRequest->getScopes() as $scope) { |
|
36
|
|
|
$manager->detach($scope); |
|
37
|
|
|
} |
|
38
|
|
|
return serialize($authorizationRequest); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param string $data |
|
43
|
|
|
* @return AuthorizationRequest |
|
44
|
|
|
*/ |
|
45
|
|
|
public function unserialize(string $data): AuthorizationRequest |
|
46
|
|
|
{ |
|
47
|
|
|
$manager = $this->registry->getManager(); |
|
48
|
|
|
/** @var AuthorizationRequest $authorizationRequest */ |
|
49
|
|
|
$authorizationRequest = unserialize($data); |
|
50
|
|
|
if ($client = $authorizationRequest->getClient()) { |
|
51
|
|
|
$authorizationRequest->setClient($manager->merge($client)); |
|
52
|
|
|
} |
|
53
|
|
|
$scopes = []; |
|
54
|
|
|
foreach ($authorizationRequest->getScopes() as $scope) { |
|
55
|
|
|
$scopes[] = $manager->merge($scope); |
|
56
|
|
|
} |
|
57
|
|
|
$authorizationRequest->setScopes($scopes); |
|
58
|
|
|
return $authorizationRequest; |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|