-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAuthMemCookie.php
More file actions
167 lines (132 loc) · 4.55 KB
/
AuthMemCookie.php
File metadata and controls
167 lines (132 loc) · 4.55 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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
declare(strict_types=1);
namespace SimpleSAML\Module\memcookie;
use Exception;
use Memcached;
use SimpleSAML\Configuration;
use SimpleSAML\Utils;
use function strlen;
/**
* This is a helper class for the Auth MemCookie module.
* It handles the configuration, and implements the logout handler.
*
* @package SimpleSAMLphp
*/
class AuthMemCookie
{
/**
* @var \SimpleSAML\Module\memcookie\AuthMemCookie|null This is the singleton instance of this class.
*/
private static ?AuthMemCookie $instance = null;
/**
* @var \SimpleSAML\Configuration The configuration for Auth MemCookie.
*/
private Configuration $config;
/**
* This function is used to retrieve the singleton instance of this class.
*
* @return \SimpleSAML\Module\memcookie\AuthMemCookie The singleton instance of this class.
*/
public static function getInstance(): AuthMemCookie
{
if (self::$instance === null) {
self::$instance = new AuthMemCookie();
}
return self::$instance;
}
/**
* This function implements the constructor for this class. It loads the Auth MemCookie configuration.
*/
private function __construct()
{
// load AuthMemCookie configuration
$this->config = Configuration::getConfig('module_authmemcookie.php');
}
/**
* Retrieve the authentication source that should be used to authenticate the user.
*
* @return string The login type which should be used for Auth MemCookie.
*/
public function getAuthSource(): string
{
return $this->config->getString('authsource');
}
/**
* This function retrieves the name of the cookie from the configuration.
*
* @return string The name of the cookie.
* @throws \Exception If the value of the 'cookiename' configuration option is invalid.
*/
public function getCookieName(): string
{
$cookieName = $this->config->getOptionalString('cookiename', 'AuthMemCookie');
if (strlen($cookieName) === 0) {
throw new Exception(
"Configuration option 'cookiename' contains an invalid value. This option should be a string.",
);
}
return $cookieName;
}
/**
* This function retrieves the name of the attribute which contains the username from the configuration.
*
* @return string|null The name of the attribute which contains the username.
*/
public function getUsernameAttr(): ?string
{
return $this->config->getOptionalString('username', null);
}
/**
* This function retrieves the name of the attribute which contains the groups from the configuration.
*
* @return string|null The name of the attribute which contains the groups.
*/
public function getGroupsAttr(): ?string
{
return $this->config->getOptionalString('groups', null);
}
/**
* This function creates and initializes a Memcache object from our configuration.
*
* @return \Memcached A Memcache object initialized from our configuration.
*/
public function getMemcache(): \Memcached
{
$memcacheHost = $this->config->getOptionalString('memcache.host', '127.0.0.1');
$memcachePort = $this->config->getOptionalInteger('memcache.port', 11211);
$class = class_exists('\Memcached') ? '\Memcached' : false;
if (!$class) {
throw new Exception('Missing Memcached implementation. You must install either the Memcached extension.');
}
$memcache = new Memcached();
foreach (explode(',', $memcacheHost) as $memcacheHost) {
$memcache->addServer($memcacheHost, $memcachePort);
}
return $memcache;
}
/**
* This function logs the user out by deleting the session information from memcache.
*/
private function doLogout(): void
{
$cookieName = $this->getCookieName();
// check if we have a valid cookie
if (!array_key_exists($cookieName, $_COOKIE)) {
return;
}
$sessionID = $_COOKIE[$cookieName];
// delete the session from memcache
$memcache = $this->getMemcache();
$memcache->delete($sessionID);
// delete the session cookie
$httpUtils = new Utils\HTTP();
$httpUtils->setCookie($cookieName, null);
}
/**
* This function implements the logout handler. It deletes the information from Memcache.
*/
public static function logoutHandler(): void
{
self::getInstance()->doLogout();
}
}