GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

MellonAuthenticationHook::executeBefore()   B
last analyzed

Complexity

Conditions 5
Paths 8

Size

Total Lines 32
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 8.439
c 0
b 0
f 0
cc 5
eloc 14
nc 8
nop 2
1
<?php
2
3
/**
4
 * eduVPN - End-user friendly VPN.
5
 *
6
 * Copyright: 2016-2017, The Commons Conservancy eduVPN Programme
7
 * SPDX-License-Identifier: AGPL-3.0+
8
 */
9
10
namespace SURFnet\VPN\Common\Http;
11
12
use fkooman\SeCookie\SessionInterface;
13
use SURFnet\VPN\Common\Http\Exception\HttpException;
14
15
/**
16
 * The following mod_auth_mellon configuration flags MUST be set:.
17
 *
18
 *     MellonIdP "IDP"
19
 *     MellonMergeEnvVars On
20
 */
21
class MellonAuthenticationHook implements BeforeHookInterface
22
{
23
    /** @var \fkooman\SeCookie\SessionInterface */
24
    private $session;
25
26
    /** @var string */
27
    private $userIdAttribute;
28
29
    /** @var bool */
30
    private $addEntityId;
31
32
    /** @var array|null */
33
    private $userIdAuthorization = null;
34
35
    /** @var string|null */
36
    private $entitlementAttribute = null;
37
38
    /** @var array|null */
39
    private $entitlementAuthorization = null;
40
41
    public function __construct(SessionInterface $session, $userIdAttribute, $addEntityId)
42
    {
43
        $this->session = $session;
44
        $this->userIdAttribute = $userIdAttribute;
45
        $this->addEntityId = $addEntityId;
46
    }
47
48
    public function enableUserIdAuthorization(array $userIdAuthorization)
49
    {
50
        $this->userIdAuthorization = $userIdAuthorization;
51
    }
52
53
    public function enableEntitlementAuthorization($entitlementAttribute, array $entitlementAuthorization)
54
    {
55
        $this->entitlementAttribute = $entitlementAttribute;
56
        $this->entitlementAuthorization = $entitlementAuthorization;
57
    }
58
59
    public function executeBefore(Request $request, array $hookData)
60
    {
61
        $userId = $request->getHeader($this->userIdAttribute);
62
        if ($this->addEntityId) {
63
            // add the entity ID to the user ID, this is used when we have
64
            // different IdPs that do not guarantee uniqueness among the used
65
            // user identifier attribute, e.g. NAME_ID or uid
66
            $userId = sprintf(
67
                '%s_%s',
68
                // strip out all "special" characters from the entityID, just
69
                // like mod_auth_mellon does
70
                preg_replace('/__*/', '_', preg_replace('/[^A-Za-z.]/', '_', $request->getHeader('MELLON_IDP'))),
71
                $userId
72
            );
73
        }
74
75
        if (!$this->verifyAuthorization($request)) {
76
            throw new HttpException('access forbidden', 403);
77
        }
78
79
        if ($this->session->has('_mellon_auth_user')) {
80
            if ($userId !== $this->session->get('_mellon_auth_user')) {
81
                // if we have an application session where the user_id does not
82
                // match the Mellon user_id we destroy the current session and
83
                // regenerate it below.
84
                $this->session->destroy();
85
            }
86
        }
87
        $this->session->set('_mellon_auth_user', $userId);
88
89
        return $userId;
90
    }
91
92
    private function verifyAuthorization(Request $request)
93
    {
94
        if (is_null($this->userIdAuthorization) && is_null($this->entitlementAuthorization)) {
95
            // authorization disabled, allow user
96
            return true;
97
        }
98
99
        // if either of these succeeds now, we allow the user
100
        if ($this->verifyUserIdAuthorization($request)) {
101
            return true;
102
        }
103
104
        if ($this->verifyEntitlementAuthorization($request)) {
105
            return true;
106
        }
107
108
        return false;
109
    }
110
111
    private function verifyUserIdAuthorization(Request $request)
112
    {
113
        if (is_null($this->userIdAuthorization)) {
114
            return false;
115
        }
116
117
        $userId = sprintf(
118
            '%s|%s',
119
            $request->getHeader('MELLON_IDP'),
120
            $request->getHeader($this->userIdAttribute)
121
        );
122
123
        return in_array($userId, $this->userIdAuthorization);
124
    }
125
126
    private function verifyEntitlementAuthorization(Request $request)
127
    {
128
        if (is_null($this->entitlementAuthorization)) {
129
            return false;
130
        }
131
132
        $entityID = $request->getHeader('MELLON_IDP');
133
        $entitlementValue = $request->getHeader($this->entitlementAttribute, false, 'NO_ENTITLEMENT');
134
        $entitlementList = explode(';', $entitlementValue);
135
        foreach ($entitlementList as $entitlement) {
136
            $entitlementCheck = sprintf('%s|%s', $entityID, $entitlement);
137
            if (in_array($entitlementCheck, $this->entitlementAuthorization)) {
138
                return true;
139
            }
140
        }
141
142
        return false;
143
    }
144
}
145