Commit | Line | Data |
---|---|---|
24cfa984 AA |
1 | <?php |
2 | /*************************************************************************** | |
3 | * Copyright (C) 2003-2008 Polytechnique.org * | |
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 | ||
a1af4a99 AA |
22 | |
23 | /* Definitions for the OpenId Specification | |
24 | * http://openid.net/specs/openid-authentication-2_0.html | |
7eaf07e9 | 25 | * |
a1af4a99 AA |
26 | * OP Endpoint URL: https://www.polytechnique.org/openid |
27 | * OP Identifier: https://www.polytechnique.org/openid | |
b21f0bb5 AA |
28 | * User Identifier: https://www.polytechnique.org/openid/{hruid} |
29 | * OP-Local Identifier: {hruid} | |
30 | */ | |
31 | ||
32 | /* This implementation supports two modes: | |
33 | * - entering the OP Identifier, which can simply be 'polytechnique.org' | |
34 | * - entering the User Identifier, or some URL that resolves there | |
35 | * In both cases, Yadis discovery is made possible through the X-XRDS-Location | |
36 | * header. | |
7eaf07e9 | 37 | * |
b21f0bb5 AA |
38 | * In the former case, Yadis discovery is performed on /, or where it redirects; |
39 | * see platal.php. It resolves to the XRDS for this OP, and User Identifier | |
40 | * selection is performed after forcing the user to log in. This only works for | |
41 | * version 2.0 of the OpenId protocol. | |
42 | * | |
43 | * In the latter cas, Yadis discovery is performed on /openid/{hruid}. It | |
44 | * resolves ta a user-specific XRDS. This page also features HTML-based | |
45 | * discovery. This works with any version of the protocol. | |
a1af4a99 AA |
46 | */ |
47 | ||
33536353 AA |
48 | /* Testing suite is here: |
49 | * http://openidenabled.com/resources/openid-test/ | |
b21f0bb5 AA |
50 | * It only supports User Indentifiers. |
51 | * | |
52 | * To test OP Identifiers, download the JanRain PHP library and use the | |
53 | * consumer provided as an example (although it appears that a failure is | |
54 | * mistakenly reported: 'Server denied check_authentication'). | |
55 | * Reading the source of the server can also help understanding the code below. | |
33536353 AA |
56 | */ |
57 | ||
33536353 | 58 | |
24cfa984 AA |
59 | class OpenidModule extends PLModule |
60 | { | |
61 | function handlers() | |
62 | { | |
63 | return array( | |
a1af4a99 AA |
64 | 'openid' => $this->make_hook('openid', AUTH_PUBLIC), |
65 | 'openid/trust' => $this->make_hook('trust', AUTH_COOKIE), | |
b8f1396f | 66 | 'openid/trusted' => $this->make_hook('trusted', AUTH_MDP), |
51e5bc7d | 67 | 'admin/openid/trusted' => $this->make_hook('admin_trusted', AUTH_MDP), |
ab66bf7f | 68 | 'openid/idp_xrds' => $this->make_hook('idp_xrds', AUTH_PUBLIC), |
b69727b4 | 69 | 'openid/user_xrds' => $this->make_hook('user_xrds', AUTH_PUBLIC), |
829fae6a | 70 | 'openid/melix' => $this->make_hook('melix', AUTH_PUBLIC), |
24cfa984 AA |
71 | ); |
72 | } | |
73 | ||
74 | function handler_openid(&$page, $x = null) | |
75 | { | |
a1af4a99 | 76 | $this->load('openid.inc.php'); |
b69727b4 | 77 | |
a8e858d3 | 78 | // Spec ยง4.1.2: if "openid.mode" is absent, we SHOULD assume that |
33536353 | 79 | // the request is not an OpenId message |
33536353 | 80 | if (!array_key_exists('openid_mode', $_REQUEST)) { |
b21f0bb5 | 81 | return $this->render_discovery_page($page, get_user($x)); |
24cfa984 AA |
82 | } |
83 | ||
7eaf07e9 | 84 | // Now, deal with the OpenId message |
a1af4a99 AA |
85 | $server = init_openid_server(); |
86 | $request = $server->decodeRequest(); | |
87 | ||
1bda7469 AA |
88 | // In modes 'checkid_immediate' and 'checkid_setup', the request |
89 | // needs some logic and can not be automatically answered by the server | |
90 | ||
91 | // Immediate mode | |
92 | if ($request->mode == 'checkid_immediate') { | |
93 | ||
94 | // We deny immediate requests, unless: | |
95 | // - the user identifier is known by the RP | |
96 | // - the user is logged in | |
97 | // - the user identifier matches the user logged in | |
a8e858d3 | 98 | // - the user has whitelisted the site |
1bda7469 AA |
99 | $answer = !$request->idSelect() |
100 | && S::logged() | |
101 | && $request->identity == S::user()->login() | |
102 | && is_trusted_site(S::user(), $request->trust_root); | |
103 | $response =& $request->answer($answer); | |
104 | ||
105 | // Setup mode | |
106 | } else if ($request->mode == 'checkid_setup') { | |
107 | ||
108 | // We redirect to a page where the user will authenticate | |
109 | // and confirm the use of his/her OpenId | |
a8e858d3 | 110 | // The request is saved in session before redirecting |
1bda7469 AA |
111 | S::set('openid_request', serialize($request)); |
112 | pl_redirect('openid/trust'); | |
113 | return; | |
a1af4a99 | 114 | |
33536353 AA |
115 | // Other requests can be automatically handled by the server |
116 | } else { | |
a1af4a99 | 117 | $response =& $server->handleRequest($request); |
24cfa984 AA |
118 | } |
119 | ||
a1af4a99 | 120 | $webresponse =& $server->encodeResponse($response); |
33536353 | 121 | $this->render_openid_response($webresponse); |
a1af4a99 | 122 | } |
b69727b4 | 123 | |
a1af4a99 AA |
124 | function handler_trust(&$page, $x = null) |
125 | { | |
126 | $this->load('openid.inc.php'); | |
24cfa984 | 127 | |
a1af4a99 | 128 | // Recover request in session |
087f7ecd | 129 | $request = S::v('openid_request'); |
a1af4a99 AA |
130 | if (is_null($request)) { |
131 | // There is no authentication information, something went wrong | |
132 | pl_redirect('/'); | |
133 | return; | |
a1af4a99 | 134 | } |
24cfa984 | 135 | |
b21f0bb5 AA |
136 | require_once 'Auth/OpenID/Server.php'; |
137 | $request = unserialize($request); | |
138 | ||
a1af4a99 AA |
139 | $server = init_openid_server(); |
140 | $user = S::user(); | |
b21f0bb5 AA |
141 | $identity = null; |
142 | $claimed_id = null; | |
143 | ||
144 | // Set the identity to the user currently logged in | |
145 | // if an OP Identifier was initially used | |
146 | if ($request->identity == Auth_OpenID_IDENTIFIER_SELECT) { | |
147 | $identity = $user->hruid; | |
148 | $claimed_id = get_user_openid_url($user); | |
a1af4a99 | 149 | // Check that the identity matches the user currently logged in |
b21f0bb5 AA |
150 | // if an User Identifier was initially used |
151 | } else if ($request->identity != $user->hruid) { | |
a1af4a99 AA |
152 | $response =& $request->answer(false); |
153 | $webresponse =& $server->encodeResponse($response); | |
154 | $this->render_openid_response($webresponse); | |
155 | return; | |
156 | } | |
157 | ||
12d4424c AA |
158 | // Prepare Simple Registration response fields |
159 | require_once 'Auth/OpenID/SReg.php'; | |
160 | $sreg_request = Auth_OpenID_SRegRequest::fromOpenIDRequest($request); | |
161 | $sreg_response = Auth_OpenID_SRegResponse::extractResponse($sreg_request, get_sreg_data($user)); | |
162 | ||
f8a567b5 | 163 | $whitelisted = is_trusted_site($user, $request->trust_root); |
12d4424c | 164 | |
33536353 | 165 | // Ask the user for confirmation |
7eaf07e9 | 166 | if (!$whitelisted && $_SERVER['REQUEST_METHOD'] != 'POST') { |
a1af4a99 AA |
167 | $page->changeTpl('openid/trust.tpl'); |
168 | $page->assign('relying_party', $request->trust_root); | |
12d4424c | 169 | $page->assign_by_ref('sreg_data', $sreg_response->data); |
a1af4a99 AA |
170 | return; |
171 | } | |
172 | ||
33536353 | 173 | // At this point $_SERVER['REQUEST_METHOD'] == 'POST' |
2e5fbf5e AA |
174 | |
175 | // Add 'always trusted' sites to whitelist | |
176 | if (isset($_POST['trust']) && @$_POST['always']) { | |
177 | add_trusted_site($user, $request->trust_root); | |
178 | } | |
179 | ||
b21f0bb5 | 180 | // Answer to the Relying Party |
7eaf07e9 | 181 | if ($whitelisted || isset($_POST['trust'])) { |
087f7ecd | 182 | S::kill('openid_request'); |
b21f0bb5 | 183 | $response =& $request->answer(true, null, $identity, $claimed_id); |
a1af4a99 | 184 | |
a1af4a99 AA |
185 | // Add the simple registration response values to the OpenID |
186 | // response message. | |
a1af4a99 AA |
187 | $sreg_response->toMessage($response->fields); |
188 | ||
7eaf07e9 | 189 | } else { // !$whitelisted && !isset($_POST['trust']) |
087f7ecd | 190 | S::kill('openid_request'); |
33536353 | 191 | $response =& $request->answer(false); |
a1af4a99 | 192 | } |
33536353 | 193 | |
33536353 AA |
194 | $webresponse =& $server->encodeResponse($response); |
195 | $this->render_openid_response($webresponse); | |
b69727b4 AA |
196 | } |
197 | ||
b8f1396f AA |
198 | function handler_trusted(&$page, $action = 'list', $id = null) |
199 | { | |
b8f1396f AA |
200 | $page->setTitle('Sites tiers de confiance'); |
201 | $page->assign('title', 'Mes sites tiers de confiance pour OpenId'); | |
202 | $table_editor = new PLTableEditor('openid/trusted', 'openid_trusted', 'id'); | |
203 | $table_editor->set_where_clause('user_id = ' . XDB::escape(S::user()->id())); | |
b8f1396f AA |
204 | $table_editor->vars['user_id']['display'] = false; |
205 | $table_editor->describe('url', 'site tiers', true); | |
206 | $page->assign('deleteonly', true); | |
51e5bc7d AA |
207 | $table_editor->apply($page, $action, $id); |
208 | } | |
209 | ||
210 | function handler_admin_trusted(&$page, $action = 'list', $id = null) | |
211 | { | |
212 | $page->setTitle('Sites tiers de confiance'); | |
213 | $page->assign('title', 'Sites tiers de confiance globaux pour OpenId'); | |
214 | $table_editor = new PLTableEditor('admin/openid/trusted', 'openid_trusted', 'id'); | |
215 | $table_editor->set_where_clause('user_id IS NULL'); | |
216 | $table_editor->vars['user_id']['display'] = false; | |
217 | $table_editor->describe('url', 'site tiers', true); | |
218 | $page->assign('readonly', true); | |
b8f1396f AA |
219 | $table_editor->apply($page, $action, $id); |
220 | } | |
221 | ||
ab66bf7f AA |
222 | function handler_idp_xrds(&$page) |
223 | { | |
ab66bf7f | 224 | $this->load('openid.inc.php'); |
ab66bf7f AA |
225 | header('Content-type: application/xrds+xml'); |
226 | $page->changeTpl('openid/idp_xrds.tpl', NO_SKIN); | |
ab66bf7f AA |
227 | $page->assign('type2', Auth_OpenID_TYPE_2_0_IDP); |
228 | $page->assign('sreg', Auth_OpenID_SREG_URI); | |
229 | $page->assign('provider', get_openid_url()); | |
230 | } | |
231 | ||
b69727b4 AA |
232 | function handler_user_xrds(&$page, $x = null) |
233 | { | |
a1af4a99 | 234 | $this->load('openid.inc.php'); |
24cfa984 | 235 | |
b17873a4 AA |
236 | $user = get_user($x); |
237 | if (is_null($user)) { | |
238 | return PL_NOT_FOUND; | |
239 | } | |
240 | ||
b69727b4 AA |
241 | header('Content-type: application/xrds+xml'); |
242 | $page->changeTpl('openid/user_xrds.tpl', NO_SKIN); | |
b17873a4 AA |
243 | $page->assign('type2', Auth_OpenID_TYPE_2_0); |
244 | $page->assign('type1', Auth_OpenID_TYPE_1_1); | |
ab2a43ac | 245 | $page->assign('sreg', Auth_OpenID_SREG_URI); |
b17873a4 AA |
246 | $page->assign('provider', get_openid_url()); |
247 | $page->assign('local_id', $user->hruid); | |
24cfa984 AA |
248 | } |
249 | ||
2d8779e2 AA |
250 | function handler_melix(&$page, $x = null) |
251 | { | |
252 | $this->load('openid.inc.php'); | |
253 | $user = get_user_by_alias($x); | |
254 | ||
255 | // This will redirect to the canonic URL, which was not used | |
256 | // if this hook was triggered | |
829fae6a | 257 | return $this->render_discovery_page(&$page, $user); |
2d8779e2 AA |
258 | } |
259 | ||
a1af4a99 AA |
260 | //--------------------------------------------------------------------// |
261 | ||
262 | function render_discovery_page(&$page, $user) | |
263 | { | |
264 | ||
33536353 | 265 | // Show the documentation if this is not the OpenId page of an user |
a1af4a99 | 266 | if (is_null($user)) { |
33536353 | 267 | pl_redirect('Xorg/OpenId'); |
a1af4a99 AA |
268 | } |
269 | ||
2d8779e2 AA |
270 | // Redirect to the canonic URL if we are using an alias |
271 | // There might be a risk of redirection loop here | |
272 | // if $_SERVER was not exactly what we expect | |
273 | $current_url = 'http' . (empty($_SERVER['HTTPS']) ? '' : 's') . '://' | |
274 | . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; | |
275 | $canonic_url = get_user_openid_url($user); | |
276 | if ($current_url != $canonic_url) { | |
277 | http_redirect($canonic_url); | |
278 | } | |
279 | ||
a1af4a99 AA |
280 | // Include X-XRDS-Location response-header for Yadis discovery |
281 | header('X-XRDS-Location: ' . get_user_xrds_url($user)); | |
282 | ||
a1af4a99 | 283 | $page->changeTpl('openid/openid.tpl'); |
a1af4a99 | 284 | $page->setTitle($user->fullName()); |
a8e858d3 | 285 | // Set the <link> tags for HTML-Based Discovery |
a1af4a99 AA |
286 | $page->addLink('openid.server openid2.provider', get_openid_url()); |
287 | $page->addLink('openid.delegate openid2.local_id', $user->hruid); | |
a1af4a99 AA |
288 | $page->assign_by_ref('user', $user); |
289 | ||
290 | return; | |
291 | } | |
292 | ||
33536353 | 293 | function render_openid_response($webresponse) |
a1af4a99 AA |
294 | { |
295 | if ($webresponse->code != AUTH_OPENID_HTTP_OK) { | |
296 | header(sprintf("HTTP/1.1 %d ", $webresponse->code), | |
297 | true, $webresponse->code); | |
298 | } | |
a1af4a99 AA |
299 | foreach ($webresponse->headers as $k => $v) { |
300 | header("$k: $v"); | |
301 | } | |
33536353 | 302 | header('Connection: close'); |
a1af4a99 AA |
303 | print $webresponse->body; |
304 | exit; | |
305 | } | |
24cfa984 AA |
306 | } |
307 | ||
308 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: | |
2d8779e2 | 309 | ?> |