|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright (C) 2016 SURFnet. |
|
4
|
|
|
* |
|
5
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
6
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
7
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
8
|
|
|
* License, or (at your option) any later version. |
|
9
|
|
|
* |
|
10
|
|
|
* This program is distributed in the hope that it will be useful, |
|
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13
|
|
|
* GNU Affero General Public License for more details. |
|
14
|
|
|
* |
|
15
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
16
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17
|
|
|
*/ |
|
18
|
|
|
namespace SURFnet\VPN\Server\GroupProvider; |
|
19
|
|
|
|
|
20
|
|
|
use GuzzleHttp\Client; |
|
21
|
|
|
use GuzzleHttp\Exception\TransferException; |
|
22
|
|
|
use SURFnet\VPN\Server\GroupProviderInterface; |
|
23
|
|
|
use SURFnet\VPN\Server\InstanceConfig; |
|
24
|
|
|
|
|
25
|
|
|
class VootProvider implements GroupProviderInterface |
|
26
|
|
|
{ |
|
27
|
|
|
/** @var string */ |
|
28
|
|
|
private $dataDir; |
|
29
|
|
|
|
|
30
|
|
|
/** @var \SURFnet\VPN\Server\InstanceConfig */ |
|
31
|
|
|
private $instanceConfig; |
|
32
|
|
|
|
|
33
|
|
|
public function __construct($dataDir, InstanceConfig $config) |
|
|
|
|
|
|
34
|
|
|
{ |
|
35
|
|
|
$this->dataDir = $dataDir; |
|
36
|
|
|
$this->instanceConfig = $instanceConfig; |
|
|
|
|
|
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function getGroups($userId) |
|
40
|
|
|
{ |
|
41
|
|
|
if (false === $bearerToken = @file_get_contents(sprintf('%s/users/voot_tokens/%s', $dataDir, $userId))) { |
|
|
|
|
|
|
42
|
|
|
return []; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
// fetch the groups and extract the membership data |
|
46
|
|
|
return self::extractMembership( |
|
47
|
|
|
$this->fetchGroups($bearerToken) |
|
48
|
|
|
); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
private function fetchGroups($bearerToken) |
|
52
|
|
|
{ |
|
53
|
|
|
$httpClient = new Client(); |
|
54
|
|
|
try { |
|
55
|
|
|
return $httpClient->get( |
|
56
|
|
|
$this->instanceConfig->v('GroupProviders', 'VootProvider', 'apiUrl'), |
|
57
|
|
|
[ |
|
58
|
|
|
'headers' => [ |
|
59
|
|
|
'Authorization' => sprintf('Bearer %s', $bearerToken), |
|
60
|
|
|
], |
|
61
|
|
|
] |
|
62
|
|
|
)->json(); |
|
63
|
|
|
} catch (TransferException $e) { |
|
64
|
|
|
return []; |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
private static function extractMembership(array $responseData) |
|
69
|
|
|
{ |
|
70
|
|
|
$memberOf = []; |
|
71
|
|
|
foreach ($responseData as $groupEntry) { |
|
72
|
|
|
if (!is_array($groupEntry)) { |
|
73
|
|
|
continue; |
|
74
|
|
|
} |
|
75
|
|
|
if (!array_key_exists('id', $groupEntry)) { |
|
76
|
|
|
continue; |
|
77
|
|
|
} |
|
78
|
|
|
if (!is_string($groupEntry['id'])) { |
|
79
|
|
|
continue; |
|
80
|
|
|
} |
|
81
|
|
|
$displayName = $groupEntry['id']; |
|
82
|
|
|
|
|
83
|
|
|
// override displayName if one is set |
|
84
|
|
|
if (array_key_exists('displayName', $groupEntry)) { |
|
85
|
|
|
// check if it is multilanguage |
|
86
|
|
|
if (is_string($groupEntry['displayName'])) { |
|
87
|
|
|
$displayName = $groupEntry['displayName']; |
|
88
|
|
|
} else { |
|
89
|
|
|
// take english if available, otherwise first |
|
90
|
|
|
if (array_key_exists('en', $groupEntry['displayName'])) { |
|
91
|
|
|
$displayName = $groupEntry['displayName']['en']; |
|
92
|
|
|
} else { |
|
93
|
|
|
$displayName = array_values($groupEntry['displayName'])[0]; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
$memberOf[] = [ |
|
99
|
|
|
'id' => $groupEntry['id'], |
|
100
|
|
|
'displayName' => $displayName, |
|
101
|
|
|
]; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
return $memberOf; |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.