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

SessionStateParameterExtension::processBefore()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 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