This repository was archived by the owner on Oct 20, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAuthserver_User_Backend.php
More file actions
113 lines (95 loc) · 4.42 KB
/
Authserver_User_Backend.php
File metadata and controls
113 lines (95 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
/**
* The MIT License (MIT)
*
* Copyright (c) 2016 Studentenraad campus Groep T
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
namespace Studentenraad\Owncloud\AuthserverLogin;
use OCA\user_external\Base;
use OCP\IUserBackend;
class Authserver_User_Backend extends Base implements IUserBackend {
private $requiredGroup;
private $groupPrefix;
private $authserverUrl;
public function __construct($authUrl, $requiredGroup, $groupPrefix = null) {
$this->authserverUrl = $authUrl;
$this->requiredGroup = $requiredGroup;
$this->groupPrefix = $groupPrefix;
parent::__construct($authUrl);
}
public function checkPassword( $uid, $password ) {
$arr = explode('://', $this->authserverUrl, 2);
if( ! isset($arr) OR count($arr) !== 2) {
\OCP\Util::writeLog('OC_USER_Authserver', 'Invalid Url: "'.$this->authserverUrl.'" ', 3);
return false;
}
list($protocol, $path) = $arr;
$url= $protocol.'://'.urlencode($uid).':'.urlencode($password).'@'.$path;
$data = file_get_contents($url);
if($data===false) {
\OCP\Util::writeLog('OC_USER_Authserver', 'Not possible to connect to Authserver Url: "'.$protocol.'://'.$path.'" ', 3);
return false;
}
$decoded_data = @json_decode($data, true);
if($decoded_data === null) {
\OCP\Util::writeLog('OC_USER_Authserver', 'Cannot decode received JSON: '.json_last_error_msg(), 3);
return false;
}
if(isset($decoded_data['error'])) {
\OCP\Util::writeLog('OC_USER_Authserver', 'Authserver returned error: '.$decoded_data['error'], 3);
return false;
}
if(!in_array($this->requiredGroup, $decoded_data['groups'])) {
\OCP\Util::writeLog('OC_USER_Authserver', 'User not in required group '.$this->requiredGroup.' (groups: '.implode(', ', $decoded_data['groups']).')', 3);
return false;
}
$this->storeUser($decoded_data['username']);
$this->setDisplayName($decoded_data['username'], $decoded_data['name']);
$owncloudUser = \OC::$server->getUserManager()->get($decoded_data['username']);
if($owncloudUser->getEMailAddress() !== $decoded_data['primary-email'])
$owncloudUser->setEMailAddress($decoded_data['primary-email']);
$owncloudUser->setEnabled(in_array($this->requiredGroup, $decoded_data['groups']));
$authserverGroups = array_map(function($groupName) {
return substr($groupName, strlen($this->groupPrefix));
}, array_filter($decoded_data['groups'], function($groupName) {
return strpos($groupName, $this->groupPrefix) === 0;
}));
$groupManager = \OC::$server->getGroupManager();
foreach($authserverGroups as $groupName) {
$owncloudGroup = $groupManager->createGroup($groupName);
$owncloudGroup->addUser($owncloudUser);
}
foreach($groupManager->getUserGroups($owncloudUser) as $owncloudGroup) {
if(!in_array($owncloudGroup->getGID(), $authserverGroups))
$owncloudGroup->removeUser($owncloudUser);
}
return $decoded_data['username'];
}
/**
* Backend name to be shown in user management
* @return string the name of the backend to be shown
* @since 8.0.0
*/
public function getBackendName()
{
return 'Authserver';
}
}