Failed Conditions
Push — issue#767 ( 50d9b1...25878f )
by Guilherme
07:31
created

SessionState::getSessionState()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 2
dl 0
loc 10
ccs 0
cts 6
cp 0
crap 2
rs 9.4285
c 0
b 0
f 0
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 3
    public function __construct(EntityManager $em,
0 ignored issues
show
Coding Style introduced by
The first parameter of a multi-line function declaration must be on the line after the opening bracket
Loading history...
27
                                TokenStorageInterface $tokenStorage)
0 ignored issues
show
Coding Style introduced by
Multi-line function declaration not indented correctly; expected 8 spaces but found 32
Loading history...
Coding Style introduced by
The closing parenthesis of a multi-line function declaration must be on a new line
Loading history...
28
    {
0 ignored issues
show
Coding Style introduced by
The closing parenthesis and the opening brace of a multi-line function declaration must be on the same line
Loading history...
29 3
        $this->em           = $em;
30 3
        $this->tokenStorage = $tokenStorage;
31 3
    }
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 3
    public function onKernelResponse(FilterResponseEvent $event)
66
    {
67 3
        if (!$event->isMasterRequest()) {
68
            return;
69
        }
70 3
        $token = $this->tokenStorage->getToken();
71 3
        if ($token !== null) {
72 3
            $state  = hash('sha256', $token->serialize());
73 3
            $cookie = new Cookie('session_state', $state, 0, '/', null, false,
74 3
                false);
75 3
            $event->getResponse()->headers->setCookie($cookie);
76
        } else {
77
            $event->getResponse()->headers->removeCookie('session_state');
78
        }
79 3
    }
80
}
81