Failed Conditions
Push — master ( 1325ac...02feb2 )
by Florent
19:27
created

SessionStateParameterExtension   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 6
dl 0
loc 70
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A processBefore() 0 4 1
A getBrowserState() 0 13 2
A calculateSessionState() 0 8 1
A getOriginUri() 0 6 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2018 Spomky-Labs
9
 *
10
 * This software may be modified and distributed under the terms
11
 * of the MIT license.  See the LICENSE file for details.
12
 */
13
14
namespace OAuth2Framework\ServerBundle\Service;
15
16
use Base64Url\Base64Url;
17
use OAuth2Framework\Component\AuthorizationEndpoint\Authorization;
18
use Psr\Http\Message\ServerRequestInterface;
19
use Symfony\Component\HttpFoundation\Cookie;
20
use Symfony\Component\HttpFoundation\Session\SessionInterface;
21
22
class SessionStateParameterExtension extends \OAuth2Framework\Component\OpenIdConnect\ConsentScreen\SessionStateParameterExtension
23
{
24
    /**
25
     * @var string
26
     */
27
    private $storageName;
28
29
    /**
30
     * @var SessionInterface
31
     */
32
    private $session;
33
34
    /**
35
     * SessionStateParameterExtension constructor.
36
     *
37
     * @param SessionInterface $session
38
     * @param string           $storageName
39
     */
40
    public function __construct(SessionInterface $session, string $storageName)
41
    {
42
        $this->session = $session;
43
        $this->storageName = $storageName;
44
    }
45
46
    public function processBefore(ServerRequestInterface $request, Authorization $authorization): Authorization
47
    {
48
        return $authorization;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    protected function getBrowserState(ServerRequestInterface $request, Authorization &$authorization): string
55
    {
56
        if ($this->session->has($this->storageName)) {
57
            return $this->session->get($this->storageName);
58
        }
59
60
        $browserState = Base64Url::encode(random_bytes(64));
61
        $this->session->set($this->storageName, $browserState);
62
        $cookie = new Cookie($this->storageName, $browserState);
63
        $authorization = $authorization->withResponseHeader('Set-Cookie', (string) $cookie);
64
65
        return $browserState;
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    protected function calculateSessionState(ServerRequestInterface $request, Authorization $authorization, string $browserState): string
72
    {
73
        $origin = $this->getOriginUri($authorization->getRedirectUri());
74
        $salt = Base64Url::encode(random_bytes(16));
75
        $hash = hash('sha256', sprintf('%s%s%s%s', $authorization->getClient()->getPublicId(), $origin, $browserState, $salt));
76
77
        return sprintf('%s.%s', $hash, $salt);
78
    }
79
80
    /**
81
     * @param string $redirectUri
82
     *
83
     * @return string
84
     */
85
    private function getOriginUri(string $redirectUri): string
86
    {
87
        $url_parts = parse_url($redirectUri);
88
89
        return sprintf('%s://%s%s', $url_parts['scheme'], $url_parts['host'], isset($url_parts['port']) ? ':'.$url_parts['port'] : '');
90
    }
91
}
92