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