Failed Conditions
Push — master ( cccd95...989cb2 )
by Florent
03:57
created

SessionStateParameterExtension::processAfter()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 6
nc 2
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\Component\OpenIdConnect\ConsentScreen;
15
16
use OAuth2Framework\Component\AuthorizationEndpoint\Authorization;
17
use OAuth2Framework\Component\AuthorizationEndpoint\ConsentScreen\Extension;
18
use Psr\Http\Message\ServerRequestInterface;
19
20
abstract class SessionStateParameterExtension implements Extension
21
{
22
    /**
23
     * {@inheritdoc}
24
     */
25
    public function processAfter(ServerRequestInterface $request, Authorization $authorization): Authorization
26
    {
27
        if ($this->hasOpenIdScope($authorization)) {
28
            $browserState = $this->getBrowserState($request, $authorization);
29
            $sessionState = $this->calculateSessionState($request, $authorization, $browserState);
30
            $authorization = $authorization->withResponseParameter('session_state', $sessionState);
31
        }
32
33
        return $authorization;
34
    }
35
36
    /**
37
     * @param Authorization $authorization
38
     *
39
     * @return bool
40
     */
41
    private function hasOpenIdScope(Authorization $authorization): bool
42
    {
43
        if (!$authorization->hasQueryParam('scope')) {
44
            return false;
45
        }
46
47
        $scope = $authorization->hasQueryParam('scope');
48
        $scopes = explode(' ', $scope);
49
50
        return in_array('openid', $scopes);
51
    }
52
53
    /**
54
     * @param ServerRequestInterface $request
55
     * @param Authorization          $authorization
56
     *
57
     * @return string
58
     */
59
    abstract protected function getBrowserState(ServerRequestInterface $request, Authorization &$authorization): string;
60
61
    /**
62
     * @param ServerRequestInterface $request
63
     * @param Authorization          $authorization
64
     * @param string                 $browserState
65
     *
66
     * @return string
67
     */
68
    abstract protected function calculateSessionState(ServerRequestInterface $request, Authorization $authorization, string $browserState): string;
69
}
70