|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright 2016 François Kooman <[email protected]>. |
|
4
|
|
|
* |
|
5
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
6
|
|
|
* you may not use this file except in compliance with the License. |
|
7
|
|
|
* You may obtain a copy of the License at |
|
8
|
|
|
* |
|
9
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
10
|
|
|
* |
|
11
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
12
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
13
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
14
|
|
|
* See the License for the specific language governing permissions and |
|
15
|
|
|
* limitations under the License. |
|
16
|
|
|
*/ |
|
17
|
|
|
|
|
18
|
|
|
namespace fkooman\VPN\Server\Acl; |
|
19
|
|
|
|
|
20
|
|
|
use fkooman\Config\Reader; |
|
21
|
|
|
use GuzzleHttp\Client; |
|
22
|
|
|
use GuzzleHttp\Exception\TransferException; |
|
23
|
|
|
use fkooman\VPN\Server\AclInterface; |
|
24
|
|
|
use fkooman\VPN\Server\VootToken; |
|
25
|
|
|
|
|
26
|
|
|
class VootAcl implements AclInterface |
|
27
|
|
|
{ |
|
28
|
|
|
/** @var \fkooman\Config\Reader */ |
|
29
|
|
|
private $configReader; |
|
30
|
|
|
|
|
31
|
|
|
/** @var \GuzzleHttp\Client */ |
|
32
|
|
|
private $client; |
|
33
|
|
|
|
|
34
|
|
|
public function __construct(Reader $configReader, Client $client = null) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->configReader = $configReader; |
|
37
|
|
|
if (is_null($client)) { |
|
38
|
|
|
$client = new Client(); |
|
39
|
|
|
} |
|
40
|
|
|
$this->client = $client; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function getGroups($userId) |
|
44
|
|
|
{ |
|
45
|
|
|
$tokenDir = $this->configReader->v('VootAcl', 'tokenDir'); |
|
46
|
|
|
$apiUrl = $this->configReader->v('VootAcl', 'apiUrl'); |
|
47
|
|
|
$aclMapping = $this->configReader->v('VootAcl', 'aclMapping', false, []); |
|
|
|
|
|
|
48
|
|
|
|
|
49
|
|
|
$vootToken = new VootToken($tokenDir); |
|
50
|
|
|
$bearerToken = $vootToken->getVootToken($userId); |
|
51
|
|
|
|
|
52
|
|
|
if (false === $bearerToken) { |
|
53
|
|
|
// no Bearer token registered for this user, so assume user is not |
|
54
|
|
|
// a member of any groups |
|
55
|
|
|
return []; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
// fetch the groups and extract the membership data |
|
59
|
|
|
$memberOf = self::extractMembership( |
|
60
|
|
|
$this->fetchGroups($apiUrl, $bearerToken) |
|
61
|
|
|
); |
|
62
|
|
|
|
|
63
|
|
|
return self::applyMapping( |
|
64
|
|
|
$memberOf, |
|
65
|
|
|
$this->configReader->v('VootAcl', 'aclMapping', false, []) |
|
66
|
|
|
); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
private function fetchGroups($apiUrl, $bearerToken) |
|
70
|
|
|
{ |
|
71
|
|
|
try { |
|
72
|
|
|
return $this->client->get( |
|
73
|
|
|
$apiUrl, |
|
74
|
|
|
[ |
|
75
|
|
|
'headers' => [ |
|
76
|
|
|
'Authorization' => sprintf('Bearer %s', $bearerToken), |
|
77
|
|
|
], |
|
78
|
|
|
] |
|
79
|
|
|
)->json(); |
|
80
|
|
|
} catch (TransferException $e) { |
|
81
|
|
|
return []; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
private static function extractMembership(array $responseData) |
|
86
|
|
|
{ |
|
87
|
|
|
$memberOf = []; |
|
88
|
|
|
foreach ($responseData as $groupEntry) { |
|
89
|
|
|
if (!is_array($groupEntry)) { |
|
90
|
|
|
continue; |
|
91
|
|
|
} |
|
92
|
|
|
if (!array_key_exists('id', $groupEntry)) { |
|
93
|
|
|
continue; |
|
94
|
|
|
} |
|
95
|
|
|
if (!is_string($groupEntry['id'])) { |
|
96
|
|
|
continue; |
|
97
|
|
|
} |
|
98
|
|
|
$memberOf[] = $groupEntry['id']; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return $memberOf; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
private static function applyMapping(array $memberOf, array $groupMapping) |
|
105
|
|
|
{ |
|
106
|
|
|
$returnGroups = []; |
|
107
|
|
|
foreach ($memberOf as $groupEntry) { |
|
108
|
|
|
// check if it is available in the mapping |
|
109
|
|
|
if (array_key_exists($groupEntry, $groupMapping)) { |
|
110
|
|
|
$returnGroups = array_merge($returnGroups, $groupMapping[$groupEntry]); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
return $returnGroups; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.