|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the login-cidadao project or it's bundles. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Guilherme Donato <guilhermednt on github> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace LoginCidadao\OpenIDBundle\Storage; |
|
12
|
|
|
|
|
13
|
|
|
use LoginCidadao\OAuthBundle\Model\ClientInterface; |
|
14
|
|
|
use LoginCidadao\OpenIDBundle\Manager\ClientManager; |
|
15
|
|
|
use Symfony\Component\HttpFoundation\Cookie; |
|
16
|
|
|
use Symfony\Component\HttpKernel\Event\FilterResponseEvent; |
|
17
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
|
18
|
|
|
|
|
19
|
|
|
class SessionState |
|
20
|
|
|
{ |
|
21
|
|
|
/** @var ClientManager */ |
|
22
|
|
|
private $clientManager; |
|
23
|
|
|
|
|
24
|
|
|
/** @var TokenStorageInterface */ |
|
25
|
|
|
protected $tokenStorage; |
|
26
|
|
|
|
|
27
|
3 |
|
public function __construct(ClientManager $clientManager, TokenStorageInterface $tokenStorage) |
|
28
|
|
|
{ |
|
29
|
3 |
|
$this->clientManager = $clientManager; |
|
30
|
3 |
|
$this->tokenStorage = $tokenStorage; |
|
31
|
3 |
|
} |
|
32
|
|
|
|
|
33
|
1 |
|
public function getSessionState($client_id, $sessionId) |
|
34
|
|
|
{ |
|
35
|
1 |
|
$client = $this->getClient($client_id); |
|
36
|
|
|
|
|
37
|
1 |
|
$url = $client->getMetadata()->getClientUri(); |
|
38
|
1 |
|
$salt = bin2hex(random_bytes(15)); |
|
39
|
|
|
|
|
40
|
1 |
|
$state = $client_id.$url.$sessionId.$salt; |
|
41
|
|
|
|
|
42
|
1 |
|
return hash('sha256', $state).".$salt"; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
2 |
|
public function getSessionId() |
|
46
|
|
|
{ |
|
47
|
2 |
|
$token = $this->tokenStorage->getToken(); |
|
48
|
2 |
|
if ($token !== null) { |
|
49
|
1 |
|
return hash('sha256', $token->serialize()); |
|
50
|
|
|
} else { |
|
51
|
1 |
|
return ''; |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param string $client_id |
|
57
|
|
|
* @return ClientInterface |
|
58
|
|
|
*/ |
|
59
|
1 |
|
private function getClient($client_id) |
|
60
|
|
|
{ |
|
61
|
1 |
|
return $this->clientManager->getClientById($client_id); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
2 |
|
public function onKernelResponse(FilterResponseEvent $event) |
|
65
|
|
|
{ |
|
66
|
2 |
|
if ($event->isMasterRequest()) { |
|
67
|
2 |
|
$sessionId = $this->getSessionId(); |
|
68
|
2 |
|
if ($sessionId !== '') { |
|
69
|
1 |
|
$cookie = new Cookie('session_state', $sessionId, 0, '/', null, false, false); |
|
70
|
1 |
|
$event->getResponse()->headers->setCookie($cookie); |
|
71
|
|
|
} else { |
|
72
|
1 |
|
$event->getResponse()->headers->removeCookie('session_state'); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
2 |
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|