Commit | Line | Data |
---|---|---|
88a4c51b FB |
1 | <?php |
2 | /*************************************************************************** | |
e92ecb8c | 3 | * Copyright (C) 2003-2011 Polytechnique.org * |
88a4c51b FB |
4 | * http://opensource.polytechnique.org/ * |
5 | * * | |
6 | * This program is free software; you can redistribute it and/or modify * | |
7 | * it under the terms of the GNU General Public License as published by * | |
8 | * the Free Software Foundation; either version 2 of the License, or * | |
9 | * (at your option) any later version. * | |
10 | * * | |
11 | * This program is distributed in the hope that it will be useful, * | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | |
14 | * GNU General Public License for more details. * | |
15 | * * | |
16 | * You should have received a copy of the GNU General Public License * | |
17 | * along with this program; if not, write to the Free Software * | |
18 | * Foundation, Inc., * | |
19 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * | |
20 | ***************************************************************************/ | |
21 | ||
47fa97fe FB |
22 | /** Authentication level. |
23 | * Only AUTH_PUBLIC is mandatory. The others are defined as useful values, | |
24 | * but can be overwritten by others auth levels definitions. | |
25 | */ | |
5a074c2f | 26 | define('AUTH_SUID', -1); |
47fa97fe | 27 | define('AUTH_PUBLIC', 0); |
8cd8f58b FB |
28 | define('AUTH_COOKIE', 5); |
29 | define('AUTH_MDP', 10); | |
47fa97fe | 30 | |
88a4c51b FB |
31 | |
32 | /** The PlSession is a wrapper around the user session management. | |
33 | */ | |
34 | abstract class PlSession | |
35 | { | |
36 | /** Build a new session object. | |
37 | * If a session is already started, this just wraps the session... if no | |
38 | * session is currently started, this set the base session variables. | |
39 | * * auth contains the current authentication level. The level is | |
40 | * an integer that grows with the security of the authentication | |
41 | * method. | |
42 | * * perms contains the current permission flags of the user. This flags | |
43 | * depends on the category of the user. | |
44 | * * challenge contains a random uniq id for authentication. | |
45 | * * xsrf_token contains a random uniq id for xsrf prevention. | |
46 | * * user contains a reference to the current user. | |
47 | */ | |
48 | public function __construct() | |
49 | { | |
c0799142 | 50 | $this->create(); |
88a4c51b FB |
51 | } |
52 | ||
53 | /** Build the session structure with system fields. | |
54 | */ | |
9007d495 | 55 | protected function fillSession() |
88a4c51b FB |
56 | { |
57 | S::bootstrap('user', null); | |
58 | S::bootstrap('auth', AUTH_PUBLIC); | |
59 | S::bootstrap('challenge', sha1(uniqid(rand(), true))); | |
60 | S::bootstrap('xsrf_token', rand_url_id()); | |
61 | S::bootstrap('perms', new PlFlagSet()); | |
62 | } | |
63 | ||
64 | /** Write current session and close it. | |
65 | */ | |
66 | public function close() | |
67 | { | |
68 | session_write_close(); | |
69 | } | |
70 | ||
c0799142 FB |
71 | /** Create a new session |
72 | */ | |
73 | private function create() | |
74 | { | |
75 | session_start(); | |
76 | $this->fillSession(); | |
77 | } | |
78 | ||
88a4c51b FB |
79 | /** Kill the current session. |
80 | */ | |
81 | public function destroy() | |
82 | { | |
83 | session_destroy(); | |
84 | unset($_SESSION); | |
c0799142 | 85 | $this->create(); |
88a4c51b FB |
86 | } |
87 | ||
88 | /** Check if the user has at least the given authentication level. | |
89 | */ | |
90 | public function checkAuth($level) | |
91 | { | |
92 | return S::i('auth') >= $level; | |
93 | } | |
94 | ||
95 | /** Check if the user has the given permissions. | |
96 | */ | |
97 | public function checkPerms($perms) | |
98 | { | |
99 | return S::v('perms')->hasFlagCombination($perms); | |
100 | } | |
101 | ||
102 | /** Run authentication procedure to reach at least the given level. | |
103 | */ | |
104 | public function start($level) | |
105 | { | |
106 | if ($this->checkAuth($level)) { | |
107 | return true; | |
108 | } | |
109 | $user = $this->doAuth($level); | |
c0799142 FB |
110 | if (is_null($user)) { |
111 | return false; | |
112 | } | |
113 | if (!$this->checkAuth($level)) { | |
114 | $this->destroy(); | |
88a4c51b FB |
115 | return false; |
116 | } | |
117 | if ($this->startSessionAs($user, $level)) { | |
118 | if (is_null(S::v('user'))) { | |
119 | S::set('user', $user); | |
120 | } | |
121 | return true; | |
122 | } else { | |
123 | $this->destroy(); | |
124 | } | |
125 | return false; | |
126 | } | |
127 | ||
128 | ||
129 | /*** Abstract methods ***/ | |
130 | ||
c0799142 FB |
131 | /** Function that check authentication at build time of the session object. |
132 | * This is useful to perform authentication from a cookie or when coming | |
133 | * back from a authentication service. | |
134 | * | |
135 | * This function must NOT try to launch a new authenticatioin procedure. It | |
136 | * just tests if the environment contains sufficient information to start | |
137 | * a user session. | |
138 | * | |
139 | * This function return false if informations are available but lead to an | |
140 | * authentication failure (invalid cookie, invalid service return data...) | |
141 | */ | |
142 | abstract public function startAvailableAuth(); | |
143 | ||
88a4c51b FB |
144 | /** Run the effectively authentication procedure to reach the given user. |
145 | * This method must return a user object (that will be used to fill the | |
146 | * $_SESSION['user'] field). | |
147 | * | |
148 | * If auth failed, the function MUST return null. If auth succeed, the | |
149 | * field $_SESSION['auth'] MUST be filled to the current effective level. | |
150 | */ | |
151 | abstract protected function doAuth($level); | |
152 | ||
153 | /** Set the session environment to the given user and authentication level. | |
154 | * This function MUST return false if a session is already started and the | |
155 | * user mismatch. | |
156 | * | |
157 | * On succes, this function MUST return true. | |
158 | * If $level is set to -1, this means you are building a new SUID session. | |
159 | */ | |
160 | abstract protected function startSessionAs($user, $level); | |
161 | ||
504647c5 VZ |
162 | /** Authenticate the request for the given (method, payload) pair. |
163 | * | |
164 | * Implementations are expected to provide strong authentication. It is | |
165 | * suggested to use an HMAC-based scheme, where the signature validates the | |
166 | * method, url, and payload (to avoid replay of the signature against other | |
167 | * methods), and the timestamp (to avoid replay in time). | |
168 | * | |
169 | * @param method method of the request (GET, POST, PUT, DELETE) | |
170 | * @param resource URL path of the resource (eg. "/api/user") | |
171 | * @param payload binary payload sent with the request (before decoding) | |
172 | * @return a valid PlUser object if authentication is successfull, or null. | |
173 | */ | |
174 | public function apiAuth($method, $resource, $payload) | |
175 | { | |
176 | return null; // Default implementation does nothing | |
177 | } | |
178 | ||
8bdb07ee FB |
179 | /** Check authentication with the given token. |
180 | * | |
181 | * Token authentication is a light-weight authentication based on a user-specific token. | |
182 | * This can be used for protocols that requires a 'cookie'-free authentication, such as | |
183 | * RSS, iCal registration... | |
184 | * | |
185 | * This function returns a valid user object if authentication is successful, or null if | |
186 | * token mismatch. | |
187 | */ | |
188 | abstract public function tokenAuth($login, $token); | |
189 | ||
7adcbe0e FB |
190 | /** Set the permissions to the given flagset. |
191 | * | |
192 | * This function sets S::set('perms') with a flagset represeting the combination of | |
193 | * $perms and $is_admin. | |
194 | * | |
195 | * $perms is an abstract object representing the permissions. | |
196 | * $is_admin is a boolean, true if the current user has site-administration rights. | |
197 | */ | |
198 | abstract protected function makePerms($perms, $is_admin); | |
88a4c51b FB |
199 | |
200 | /*** SUID management ***/ | |
201 | ||
202 | /** Start a new SUID session. | |
203 | */ | |
7adcbe0e | 204 | public function startSUID($user, $perms = null) |
88a4c51b | 205 | { |
f1c8bb75 | 206 | if (S::suid()) { |
88a4c51b FB |
207 | return false; |
208 | } | |
663c7f8e | 209 | $backup = S::changeSession(array()); |
88a4c51b FB |
210 | $this->fillSession(); |
211 | S::set('suid', $backup); | |
5a074c2f | 212 | if (!$this->startSessionAs($user, AUTH_SUID)) { |
88a4c51b FB |
213 | $this->stopSUID(); |
214 | return false; | |
215 | } | |
732e5855 | 216 | S::set('user', $user); |
7adcbe0e FB |
217 | if (!is_null($perms)) { |
218 | $this->makePerms($perms, false); | |
219 | } | |
88a4c51b FB |
220 | return true; |
221 | } | |
222 | ||
223 | /** Stop a SUID session | |
224 | */ | |
225 | public function stopSUID() | |
226 | { | |
f1c8bb75 | 227 | if (!S::suid()) { |
88a4c51b FB |
228 | return false; |
229 | } | |
663c7f8e | 230 | S::changeSession(S::v('suid')); |
88a4c51b FB |
231 | return true; |
232 | } | |
233 | ||
234 | ||
235 | /*** Thresholds ***/ | |
236 | ||
8cd8f58b FB |
237 | /** Minimum level of authentication that is considered as logged. |
238 | */ | |
239 | abstract public function loggedLevel(); | |
240 | ||
88a4c51b FB |
241 | /** Minimum level of authentication that is considered as sure. |
242 | */ | |
243 | abstract public function sureLevel(); | |
244 | } | |
245 | ||
246 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: | |
247 | ?> |