This repository has been archived by the owner on Sep 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshibbolethApacheBackend.php
150 lines (132 loc) · 4.6 KB
/
shibbolethApacheBackend.php
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
/**
* @author Mikael Karlsson <[email protected]>
* @copyright 2016 CSC - IT Center for Science Ltd.
* @license AGPLv3
*/
namespace OCA\user_shib;
use OC_User_Backend;
use OCP\Authentication\IApacheBackend;
use OCP\IUserBackend;
use OCP\Util;
/**
* @property string handler
* @property string login
* @property string logout
* @property string basegroup
* @property string quota
* @property string idp
* @property string mail
* @property string pid
* @property string group
* @property string fn
*/
class ShibbolethApacheBackend extends OC_User_Backend implements IApacheBackend, IUserBackend {
public function __construct() {
$config = \OC::$server->getConfig();
$this->handler = $config->getAppValue(APP_NAME, "shib_handler", "/Shibboleth.sso");
$this->login = $config->getAppValue(APP_NAME, "shib_login", "/Login");
$this->logout = $config->getAppValue(APP_NAME, "shib_logout", "/Logout");
$this->basegroup = $config->getAppValue(APP_NAME, "shib_group", "SAML");
$this->quota = $config->getAppValue(APP_NAME, "shib_quota", "");
$this->idp = $config->getAppValue(APP_NAME, "shib_idp", "Shib-Identity-Provider");
$this->mail = $config->getAppValue(APP_NAME, "shib_mail_attr", "mail");
$this->pid = $config->getAppValue(APP_NAME, "shib_pid_attr", "eppn");
$this->group = $config->getAppValue(APP_NAME, "shib_group_attr", "schacHomeOrganization");
$this->fn = $config->getAppValue(APP_NAME, "shib_fn_attr", "cn");
}
/**
* In case the user has been authenticated by Apache true is returned.
*
* @return boolean whether Apache reports a user as currently logged in.
* @since 6.0.0
*/
public function isSessionActive() {
Util::writeLog(APP_NAME, "isSessionActive() called", Util::DEBUG);
return isset($_SERVER[$this->idp]);
}
/**
* Creates an attribute which is added to the logout hyperlink. It can
* supply any attribute(s) which are valid for <a>.
*
* @return string with one or more HTML attributes.
* @since 6.0.0
*/
public function getLogoutAttribute() {
Util::writeLog(APP_NAME, "getLogoutAttribute() called", Util::DEBUG);
Util::callRegister(); // Burn one token, otherwise logout fails
return 'href="' . $this->handler . $this->logout . "?return=" . urlencode(\OC::$server->getConfig()->getSystemValue("overwrite.cli.url") . link_to("", "index.php") . "?logout=true&requesttoken=" . Util::callRegister()) . '"';
}
/**
* Return the id of the current user
* @return string
* @since 6.0.0
*/
public function getCurrentUserId() {
Util::writeLog(APP_NAME, "getCurrentUserId() called", Util::DEBUG);
return hash("md5", $_SERVER[$this->pid]);
}
/**
* 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() {
Util::writeLog(APP_NAME, "getBackendName() called", Util::DEBUG);
return "Shibboleth";
}
}
class Hooks {
static public function pre_login($array) {
Util::writeLog(APP_NAME, "pre_login() called", Util::DEBUG);
$backend = (new ShibbolethApacheBackend());
if (!$backend->isSessionActive())
return;
Util::writeLog(APP_NAME, "Shibboleth user!", Util::DEBUG);
// User
$uid = $array["uid"];
$um = \OC::$server->getUserManager();
if (!($um->userExists($uid))) {
$um->createUser($uid, \OC::$server->getSecureRandom()->getMediumStrengthGenerator()->generate(32));
\OC::$server->getUserFolder($uid); // Trigger files_skeleton, otherwise login fails
}
$user = $um->get($uid);
// Group(s)
$gm = \OC::$server->getGroupManager();
$gids = [$backend->basegroup, $_SERVER[$backend->group]];
foreach ($gids as $gid) {
if (empty($gid)) {
continue;
}
$gid = explode(".", $gid, 2)[0];
$gid = strlen($gid) <= 4 ? strtoupper($gid) : ucfirst($gid);
if (!($gm->groupExists($gid))) {
$gm->createGroup($gid);
}
$group = $gm->get($gid);
if (!$group->inGroup($user)) {
$group->addUser($user);
}
}
}
static public function post_login($array) {
Util::writeLog(APP_NAME, "post_login() called", Util::DEBUG);
$backend = new ShibbolethApacheBackend();
if (!$backend->isSessionActive()) {
return;
}
Util::writeLog(APP_NAME, "Shibboleth user!", Util::DEBUG);
$uid = $array["uid"];
$um = \OC::$server->getUserManager();
$user = $um->get($uid);
$user->setDisplayName($_SERVER[$backend->fn]);
$user->updateLastLoginTimestamp();
$config = \OC::$server->getConfig();
$config->setUserValue($uid, "settings", "email", $_SERVER[$backend->mail]);
if (empty($backend->quota)) {
$config->deleteUserValue($uid, "files", "quota");
} else {
$config->setUserValue($uid, "files", "quota", $backend->quota);
}
}
}