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 | ||
58 | /* **checkid_immediate is not supported (yet)**, which means that we will | |
59 | * always ask for confirmation before redirecting to a third-party. | |
60 | * A sensible way to implement it would be to add a "Always trust this site" | |
61 | * checkbox to the form, and to store trusted websites per user. This still | |
62 | * raises the question of removing websites from that list. | |
63 | * Another possibility is to maintain a global whitelist. | |
64 | */ | |
65 | ||
24cfa984 AA |
66 | class OpenidModule extends PLModule |
67 | { | |
68 | function handlers() | |
69 | { | |
70 | return array( | |
a1af4a99 AA |
71 | 'openid' => $this->make_hook('openid', AUTH_PUBLIC), |
72 | 'openid/trust' => $this->make_hook('trust', AUTH_COOKIE), | |
ab66bf7f | 73 | 'openid/idp_xrds' => $this->make_hook('idp_xrds', AUTH_PUBLIC), |
b69727b4 | 74 | 'openid/user_xrds' => $this->make_hook('user_xrds', AUTH_PUBLIC), |
829fae6a | 75 | 'openid/melix' => $this->make_hook('melix', AUTH_PUBLIC), |
24cfa984 AA |
76 | ); |
77 | } | |
78 | ||
79 | function handler_openid(&$page, $x = null) | |
80 | { | |
a1af4a99 | 81 | $this->load('openid.inc.php'); |
b69727b4 | 82 | |
33536353 AA |
83 | // Spec ยง4.1.2: if "openid.mode" is absent, whe SHOULD assume that |
84 | // the request is not an OpenId message | |
85 | // Thus, we try to render the discovery page | |
86 | if (!array_key_exists('openid_mode', $_REQUEST)) { | |
b21f0bb5 | 87 | return $this->render_discovery_page($page, get_user($x)); |
24cfa984 AA |
88 | } |
89 | ||
7eaf07e9 | 90 | // Now, deal with the OpenId message |
a1af4a99 AA |
91 | // Create a server and decode the request |
92 | $server = init_openid_server(); | |
93 | $request = $server->decodeRequest(); | |
94 | ||
1bda7469 AA |
95 | // In modes 'checkid_immediate' and 'checkid_setup', the request |
96 | // needs some logic and can not be automatically answered by the server | |
97 | ||
98 | // Immediate mode | |
99 | if ($request->mode == 'checkid_immediate') { | |
100 | ||
101 | // We deny immediate requests, unless: | |
102 | // - the user identifier is known by the RP | |
103 | // - the user is logged in | |
104 | // - the user identifier matches the user logged in | |
105 | // - the user and has whitelisted the site | |
106 | $answer = !$request->idSelect() | |
107 | && S::logged() | |
108 | && $request->identity == S::user()->login() | |
109 | && is_trusted_site(S::user(), $request->trust_root); | |
110 | $response =& $request->answer($answer); | |
111 | ||
112 | // Setup mode | |
113 | } else if ($request->mode == 'checkid_setup') { | |
114 | ||
115 | // We redirect to a page where the user will authenticate | |
116 | // and confirm the use of his/her OpenId | |
117 | // Save request in session before jumping to confirmation page | |
118 | S::set('openid_request', serialize($request)); | |
119 | pl_redirect('openid/trust'); | |
120 | return; | |
a1af4a99 | 121 | |
33536353 AA |
122 | // Other requests can be automatically handled by the server |
123 | } else { | |
a1af4a99 | 124 | $response =& $server->handleRequest($request); |
24cfa984 AA |
125 | } |
126 | ||
a1af4a99 AA |
127 | // Render response |
128 | $webresponse =& $server->encodeResponse($response); | |
33536353 | 129 | $this->render_openid_response($webresponse); |
a1af4a99 | 130 | } |
b69727b4 | 131 | |
a1af4a99 AA |
132 | function handler_trust(&$page, $x = null) |
133 | { | |
134 | $this->load('openid.inc.php'); | |
24cfa984 | 135 | |
a1af4a99 | 136 | // Recover request in session |
087f7ecd | 137 | $request = S::v('openid_request'); |
a1af4a99 AA |
138 | if (is_null($request)) { |
139 | // There is no authentication information, something went wrong | |
140 | pl_redirect('/'); | |
141 | return; | |
a1af4a99 | 142 | } |
24cfa984 | 143 | |
b21f0bb5 AA |
144 | // Unserialize the request |
145 | require_once 'Auth/OpenID/Server.php'; | |
146 | $request = unserialize($request); | |
147 | ||
a1af4a99 AA |
148 | $server = init_openid_server(); |
149 | $user = S::user(); | |
b21f0bb5 AA |
150 | $identity = null; |
151 | $claimed_id = null; | |
152 | ||
153 | // Set the identity to the user currently logged in | |
154 | // if an OP Identifier was initially used | |
155 | if ($request->identity == Auth_OpenID_IDENTIFIER_SELECT) { | |
156 | $identity = $user->hruid; | |
157 | $claimed_id = get_user_openid_url($user); | |
a1af4a99 | 158 | // Check that the identity matches the user currently logged in |
b21f0bb5 AA |
159 | // if an User Identifier was initially used |
160 | } else if ($request->identity != $user->hruid) { | |
a1af4a99 AA |
161 | $response =& $request->answer(false); |
162 | $webresponse =& $server->encodeResponse($response); | |
163 | $this->render_openid_response($webresponse); | |
164 | return; | |
165 | } | |
166 | ||
12d4424c AA |
167 | // Prepare Simple Registration response fields |
168 | require_once 'Auth/OpenID/SReg.php'; | |
169 | $sreg_request = Auth_OpenID_SRegRequest::fromOpenIDRequest($request); | |
170 | $sreg_response = Auth_OpenID_SRegResponse::extractResponse($sreg_request, get_sreg_data($user)); | |
171 | ||
7eaf07e9 AA |
172 | // Check the whitelist |
173 | $whitelisted = is_trusted_site($user, $request->trust_root); | |
12d4424c | 174 | |
33536353 | 175 | // Ask the user for confirmation |
7eaf07e9 | 176 | if (!$whitelisted && $_SERVER['REQUEST_METHOD'] != 'POST') { |
a1af4a99 AA |
177 | $page->changeTpl('openid/trust.tpl'); |
178 | $page->assign('relying_party', $request->trust_root); | |
12d4424c | 179 | $page->assign_by_ref('sreg_data', $sreg_response->data); |
a1af4a99 AA |
180 | return; |
181 | } | |
182 | ||
33536353 | 183 | // At this point $_SERVER['REQUEST_METHOD'] == 'POST' |
b21f0bb5 | 184 | // Answer to the Relying Party |
7eaf07e9 | 185 | if ($whitelisted || isset($_POST['trust'])) { |
087f7ecd | 186 | S::kill('openid_request'); |
b21f0bb5 | 187 | $response =& $request->answer(true, null, $identity, $claimed_id); |
a1af4a99 | 188 | |
a1af4a99 AA |
189 | // Add the simple registration response values to the OpenID |
190 | // response message. | |
a1af4a99 AA |
191 | $sreg_response->toMessage($response->fields); |
192 | ||
7eaf07e9 | 193 | } else { // !$whitelisted && !isset($_POST['trust']) |
087f7ecd | 194 | S::kill('openid_request'); |
33536353 | 195 | $response =& $request->answer(false); |
a1af4a99 | 196 | } |
33536353 AA |
197 | |
198 | // Generate a response to send to the user agent. | |
199 | $webresponse =& $server->encodeResponse($response); | |
200 | $this->render_openid_response($webresponse); | |
b69727b4 AA |
201 | } |
202 | ||
ab66bf7f AA |
203 | function handler_idp_xrds(&$page) |
204 | { | |
ab66bf7f AA |
205 | $this->load('openid.inc.php'); |
206 | ||
207 | // Set XRDS content-type and template | |
208 | header('Content-type: application/xrds+xml'); | |
209 | $page->changeTpl('openid/idp_xrds.tpl', NO_SKIN); | |
210 | ||
211 | // Set variables | |
212 | $page->assign('type2', Auth_OpenID_TYPE_2_0_IDP); | |
213 | $page->assign('sreg', Auth_OpenID_SREG_URI); | |
214 | $page->assign('provider', get_openid_url()); | |
215 | } | |
216 | ||
b69727b4 AA |
217 | function handler_user_xrds(&$page, $x = null) |
218 | { | |
a1af4a99 | 219 | $this->load('openid.inc.php'); |
24cfa984 | 220 | |
b17873a4 AA |
221 | // Make sure the user exists |
222 | $user = get_user($x); | |
223 | if (is_null($user)) { | |
224 | return PL_NOT_FOUND; | |
225 | } | |
226 | ||
b69727b4 AA |
227 | // Set XRDS content-type and template |
228 | header('Content-type: application/xrds+xml'); | |
229 | $page->changeTpl('openid/user_xrds.tpl', NO_SKIN); | |
24cfa984 | 230 | |
b69727b4 | 231 | // Set variables |
b17873a4 AA |
232 | $page->assign('type2', Auth_OpenID_TYPE_2_0); |
233 | $page->assign('type1', Auth_OpenID_TYPE_1_1); | |
ab2a43ac | 234 | $page->assign('sreg', Auth_OpenID_SREG_URI); |
b17873a4 AA |
235 | $page->assign('provider', get_openid_url()); |
236 | $page->assign('local_id', $user->hruid); | |
24cfa984 AA |
237 | } |
238 | ||
2d8779e2 AA |
239 | function handler_melix(&$page, $x = null) |
240 | { | |
241 | $this->load('openid.inc.php'); | |
242 | $user = get_user_by_alias($x); | |
243 | ||
244 | // This will redirect to the canonic URL, which was not used | |
245 | // if this hook was triggered | |
829fae6a | 246 | return $this->render_discovery_page(&$page, $user); |
2d8779e2 AA |
247 | } |
248 | ||
a1af4a99 AA |
249 | //--------------------------------------------------------------------// |
250 | ||
251 | function render_discovery_page(&$page, $user) | |
252 | { | |
253 | ||
33536353 | 254 | // Show the documentation if this is not the OpenId page of an user |
a1af4a99 | 255 | if (is_null($user)) { |
33536353 | 256 | pl_redirect('Xorg/OpenId'); |
a1af4a99 AA |
257 | } |
258 | ||
2d8779e2 AA |
259 | // Redirect to the canonic URL if we are using an alias |
260 | // There might be a risk of redirection loop here | |
261 | // if $_SERVER was not exactly what we expect | |
262 | $current_url = 'http' . (empty($_SERVER['HTTPS']) ? '' : 's') . '://' | |
263 | . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; | |
264 | $canonic_url = get_user_openid_url($user); | |
265 | if ($current_url != $canonic_url) { | |
266 | http_redirect($canonic_url); | |
267 | } | |
268 | ||
a1af4a99 AA |
269 | // Include X-XRDS-Location response-header for Yadis discovery |
270 | header('X-XRDS-Location: ' . get_user_xrds_url($user)); | |
271 | ||
272 | // Select template | |
273 | $page->changeTpl('openid/openid.tpl'); | |
274 | ||
275 | // Sets the title of the html page. | |
276 | $page->setTitle($user->fullName()); | |
277 | ||
278 | // Sets the <link> tags for HTML-Based Discovery | |
279 | $page->addLink('openid.server openid2.provider', get_openid_url()); | |
280 | $page->addLink('openid.delegate openid2.local_id', $user->hruid); | |
281 | ||
282 | // Adds the global user property array to the display. | |
283 | $page->assign_by_ref('user', $user); | |
284 | ||
285 | return; | |
286 | } | |
287 | ||
288 | function render_no_identifier_page($page, $request) | |
289 | { | |
33536353 | 290 | // Select template |
a1af4a99 AA |
291 | $page->changeTpl('openid/no_identifier.tpl'); |
292 | } | |
293 | ||
33536353 | 294 | function render_openid_response($webresponse) |
a1af4a99 | 295 | { |
33536353 | 296 | // Send HTTP response code |
a1af4a99 AA |
297 | if ($webresponse->code != AUTH_OPENID_HTTP_OK) { |
298 | header(sprintf("HTTP/1.1 %d ", $webresponse->code), | |
299 | true, $webresponse->code); | |
300 | } | |
301 | ||
33536353 | 302 | // Send headers |
a1af4a99 AA |
303 | foreach ($webresponse->headers as $k => $v) { |
304 | header("$k: $v"); | |
305 | } | |
33536353 | 306 | header('Connection: close'); |
a1af4a99 | 307 | |
33536353 | 308 | // Send body |
a1af4a99 AA |
309 | print $webresponse->body; |
310 | exit; | |
311 | } | |
24cfa984 AA |
312 | } |
313 | ||
314 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: | |
2d8779e2 | 315 | ?> |