AuthMemCookie::getAuthSource()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\Module\memcookie;
6
7
use Exception;
8
use Memcached;
9
use SimpleSAML\Configuration;
10
use SimpleSAML\Utils;
11
12
/**
13
 * This is a helper class for the Auth MemCookie module.
14
 * It handles the configuration, and implements the logout handler.
15
 *
16
 * @package SimpleSAMLphp
17
 */
18
class AuthMemCookie
19
{
20
    /**
21
     * @var AuthMemCookie|null This is the singleton instance of this class.
22
     */
23
    private static ?AuthMemCookie $instance = null;
24
25
    /**
26
     * @var \SimpleSAML\Configuration The configuration for Auth MemCookie.
27
     */
28
    private Configuration $config;
29
30
31
    /**
32
     * This function is used to retrieve the singleton instance of this class.
33
     *
34
     * @return \SimpleSAML\Module\memcookie\AuthMemCookie The singleton instance of this class.
35
     */
36
    public static function getInstance(): AuthMemCookie
37
    {
38
        if (self::$instance === null) {
39
            self::$instance = new AuthMemCookie();
40
        }
41
42
        return self::$instance;
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::instance could return the type null which is incompatible with the type-hinted return SimpleSAML\Module\memcookie\AuthMemCookie. Consider adding an additional type-check to rule them out.
Loading history...
43
    }
44
45
46
    /**
47
     * This function implements the constructor for this class. It loads the Auth MemCookie configuration.
48
     */
49
    private function __construct()
50
    {
51
        // load AuthMemCookie configuration
52
        $this->config = Configuration::getConfig('module_authmemcookie.php');
53
    }
54
55
56
    /**
57
     * Retrieve the authentication source that should be used to authenticate the user.
58
     *
59
     * @return string The login type which should be used for Auth MemCookie.
60
     */
61
    public function getAuthSource(): string
62
    {
63
        return $this->config->getString('authsource');
64
    }
65
66
67
    /**
68
     * This function retrieves the name of the cookie from the configuration.
69
     *
70
     * @return string The name of the cookie.
71
     * @throws \Exception If the value of the 'cookiename' configuration option is invalid.
72
     */
73
    public function getCookieName(): string
74
    {
75
        $cookieName = $this->config->getOptionalString('cookiename', 'AuthMemCookie');
76
        if (!is_string($cookieName) || strlen($cookieName) === 0) {
77
            throw new Exception(
78
                "Configuration option 'cookiename' contains an invalid value. This option should be a string.",
79
            );
80
        }
81
82
        return $cookieName;
83
    }
84
85
86
    /**
87
     * This function retrieves the name of the attribute which contains the username from the configuration.
88
     *
89
     * @return string|null The name of the attribute which contains the username.
90
     */
91
    public function getUsernameAttr(): ?string
92
    {
93
        return $this->config->getOptionalString('username', null);
94
    }
95
96
97
    /**
98
     * This function retrieves the name of the attribute which contains the groups from the configuration.
99
     *
100
     * @return string|null The name of the attribute which contains the groups.
101
     */
102
    public function getGroupsAttr(): ?string
103
    {
104
        return $this->config->getOptionalString('groups', null);
105
    }
106
107
108
    /**
109
     * This function creates and initializes a Memcache object from our configuration.
110
     *
111
     * @return \Memcached A Memcache object initialized from our configuration.
112
     */
113
    public function getMemcache(): \Memcached
114
    {
115
        $memcacheHost = $this->config->getOptionalString('memcache.host', '127.0.0.1');
116
        $memcachePort = $this->config->getOptionalInteger('memcache.port', 11211);
117
118
        $class = class_exists('\Memcached') ? '\Memcached' : false;
119
120
        if (!$class) {
121
            throw new Exception('Missing Memcached implementation. You must install either the Memcached extension.');
122
        }
123
124
        $memcache = new Memcached();
125
126
        foreach (explode(',', $memcacheHost) as $memcacheHost) {
0 ignored issues
show
Bug introduced by
It seems like $memcacheHost can also be of type null; however, parameter $string of explode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

126
        foreach (explode(',', /** @scrutinizer ignore-type */ $memcacheHost) as $memcacheHost) {
Loading history...
127
            $memcache->addServer($memcacheHost, $memcachePort);
128
        }
129
130
        return $memcache;
131
    }
132
133
134
    /**
135
     * This function logs the user out by deleting the session information from memcache.
136
     */
137
    private function doLogout(): void
138
    {
139
        $cookieName = $this->getCookieName();
140
141
        // check if we have a valid cookie
142
        if (!array_key_exists($cookieName, $_COOKIE)) {
143
            return;
144
        }
145
146
        $sessionID = $_COOKIE[$cookieName];
147
148
        // delete the session from memcache
149
        $memcache = $this->getMemcache();
150
        $memcache->delete($sessionID);
151
152
        // delete the session cookie
153
        $httpUtils = new Utils\HTTP();
154
        $httpUtils->setCookie($cookieName, null);
155
    }
156
157
158
    /**
159
     * This function implements the logout handler. It deletes the information from Memcache.
160
     */
161
    public static function logoutHandler(): void
162
    {
163
        self::getInstance()->doLogout();
164
    }
165
}
166