|
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 Doctrine\ORM\EntityManager; |
|
14
|
|
|
use Symfony\Component\HttpFoundation\Cookie; |
|
15
|
|
|
use Symfony\Component\HttpKernel\Event\FilterResponseEvent; |
|
16
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
|
17
|
|
|
|
|
18
|
|
|
class SessionState |
|
19
|
|
|
{ |
|
20
|
|
|
/** @var EntityManager */ |
|
21
|
|
|
protected $em; |
|
22
|
|
|
|
|
23
|
|
|
/** @var TokenStorageInterface */ |
|
24
|
|
|
protected $tokenStorage; |
|
25
|
|
|
|
|
26
|
|
|
public function __construct(EntityManager $em, |
|
|
|
|
|
|
27
|
|
|
TokenStorageInterface $tokenStorage) |
|
|
|
|
|
|
28
|
|
|
{ |
|
|
|
|
|
|
29
|
|
|
$this->em = $em; |
|
30
|
|
|
$this->tokenStorage = $tokenStorage; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function getSessionState($client_id, $sessionId) |
|
34
|
|
|
{ |
|
35
|
|
|
$client = $this->getClient($client_id); |
|
36
|
|
|
|
|
37
|
|
|
$url = $client->getMetadata()->getClientUri(); |
|
38
|
|
|
$salt = bin2hex(random_bytes(15)); |
|
39
|
|
|
|
|
40
|
|
|
$state = $client_id.$url.$sessionId.$salt; |
|
41
|
|
|
|
|
42
|
|
|
return hash('sha256', $state).".$salt"; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function getSessionId() |
|
46
|
|
|
{ |
|
47
|
|
|
$token = $this->tokenStorage->getToken(); |
|
48
|
|
|
if ($token !== null) { |
|
49
|
|
|
return hash('sha256', $token->serialize()); |
|
50
|
|
|
} else { |
|
51
|
|
|
return ''; |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param string $client_id |
|
57
|
|
|
* @return \LoginCidadao\OAuthBundle\Entity\Client |
|
58
|
|
|
*/ |
|
59
|
|
|
private function getClient($client_id) |
|
60
|
|
|
{ |
|
61
|
|
|
$id = explode('_', $client_id); |
|
62
|
|
|
return $this->em->getRepository('LoginCidadaoOAuthBundle:Client')->find($id[0]); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function onKernelResponse(FilterResponseEvent $event) |
|
66
|
|
|
{ |
|
67
|
|
|
if (!$event->isMasterRequest()) { |
|
68
|
|
|
return; |
|
69
|
|
|
} |
|
70
|
|
|
$token = $this->tokenStorage->getToken(); |
|
71
|
|
|
if ($token !== null) { |
|
72
|
|
|
$state = hash('sha256', $token->serialize()); |
|
73
|
|
|
$cookie = new Cookie('session_state', $state, 0, '/', null, false, |
|
74
|
|
|
false); |
|
75
|
|
|
$event->getResponse()->headers->setCookie($cookie); |
|
76
|
|
|
} else { |
|
77
|
|
|
$event->getResponse()->headers->removeCookie('session_state'); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|