2 /***************************************************************************
3 * Copyright (C) 2003-2008 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
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. *
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. *
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 *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
23 /* Definitions for the OpenId Specification
24 * http://openid.net/specs/openid-authentication-2_0.html
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}
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
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.
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.
48 /* Testing suite is here:
49 * http://openidenabled.com/resources/openid-test/
50 * It only supports User Indentifiers.
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.
59 class OpenidModule
extends PLModule
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
),
72 function handler_openid(&$page, $x = null
)
74 $this->load('openid.inc.php');
76 // Spec ยง4.1.2: if "openid.mode" is absent, we SHOULD assume that
77 // the request is not an OpenId message
78 if (!array_key_exists('openid_mode', $_REQUEST)) {
79 return $this->render_discovery_page($page, get_user($x));
82 // Now, deal with the OpenId message
83 $server = init_openid_server();
84 $request = $server->decodeRequest();
86 // In modes 'checkid_immediate' and 'checkid_setup', the request
87 // needs some logic and can not be automatically answered by the server
90 if ($request->mode
== 'checkid_immediate') {
92 // We deny immediate requests, unless:
93 // - the user identifier is known by the RP
94 // - the user is logged in
95 // - the user identifier matches the user logged in
96 // - the user has whitelisted the site
97 $answer = !$request->idSelect()
99 && $request->identity
== S
::user()->login()
100 && is_trusted_site(S
::user(), $request->trust_root
);
101 $response =& $request->answer($answer);
104 } else if ($request->mode
== 'checkid_setup') {
106 // We redirect to a page where the user will authenticate
107 // and confirm the use of his/her OpenId
108 // The request is saved in session before redirecting
109 S
::set('openid_request', serialize($request));
110 pl_redirect('openid/trust');
113 // Other requests can be automatically handled by the server
115 $response =& $server->handleRequest($request);
118 $webresponse =& $server->encodeResponse($response);
119 $this->render_openid_response($webresponse);
122 function handler_trust(&$page, $x = null
)
124 $this->load('openid.inc.php');
126 // Recover request in session
127 $request = S
::v('openid_request');
128 if (is_null($request)) {
129 // There is no authentication information, something went wrong
134 require_once 'Auth/OpenID/Server.php';
135 $request = unserialize($request);
137 $server = init_openid_server();
142 // Set the identity to the user currently logged in
143 // if an OP Identifier was initially used
144 if ($request->identity
== Auth_OpenID_IDENTIFIER_SELECT
) {
145 $identity = $user->hruid
;
146 $claimed_id = get_user_openid_url($user);
147 // Check that the identity matches the user currently logged in
148 // if an User Identifier was initially used
149 } else if ($request->identity
!= $user->hruid
) {
150 $response =& $request->answer(false
);
151 $webresponse =& $server->encodeResponse($response);
152 $this->render_openid_response($webresponse);
156 // Prepare Simple Registration response fields
157 require_once 'Auth/OpenID/SReg.php';
158 $sreg_request = Auth_OpenID_SRegRequest
::fromOpenIDRequest($request);
159 $sreg_response = Auth_OpenID_SRegResponse
::extractResponse($sreg_request, get_sreg_data($user));
161 $whitelisted = is_trusted_site($user, $request->trust_root
);
163 // Ask the user for confirmation
164 if (!$whitelisted && $_SERVER['REQUEST_METHOD'] != 'POST') {
165 $page->changeTpl('openid/trust.tpl');
166 $page->assign('relying_party', $request->trust_root
);
167 $page->assign_by_ref('sreg_data', $sreg_response->data
);
171 // At this point $_SERVER['REQUEST_METHOD'] == 'POST'
173 // Add 'always trusted' sites to whitelist
174 if (isset($_POST['trust']) && @$_POST['always']) {
175 add_trusted_site($user, $request->trust_root
);
178 // Answer to the Relying Party
179 if ($whitelisted ||
isset($_POST['trust'])) {
180 S
::kill('openid_request');
181 $response =& $request->answer(true
, null
, $identity, $claimed_id);
183 // Add the simple registration response values to the OpenID
185 $sreg_response->toMessage($response->fields
);
187 } else { // !$whitelisted && !isset($_POST['trust'])
188 S
::kill('openid_request');
189 $response =& $request->answer(false
);
192 $webresponse =& $server->encodeResponse($response);
193 $this->render_openid_response($webresponse);
196 function handler_idp_xrds(&$page)
198 $this->load('openid.inc.php');
199 header('Content-type: application/xrds+xml');
200 $page->changeTpl('openid/idp_xrds.tpl', NO_SKIN
);
201 $page->assign('type2', Auth_OpenID_TYPE_2_0_IDP
);
202 $page->assign('sreg', Auth_OpenID_SREG_URI
);
203 $page->assign('provider', get_openid_url());
206 function handler_user_xrds(&$page, $x = null
)
208 $this->load('openid.inc.php');
210 $user = get_user($x);
211 if (is_null($user)) {
215 header('Content-type: application/xrds+xml');
216 $page->changeTpl('openid/user_xrds.tpl', NO_SKIN
);
217 $page->assign('type2', Auth_OpenID_TYPE_2_0
);
218 $page->assign('type1', Auth_OpenID_TYPE_1_1
);
219 $page->assign('sreg', Auth_OpenID_SREG_URI
);
220 $page->assign('provider', get_openid_url());
221 $page->assign('local_id', $user->hruid
);
224 function handler_melix(&$page, $x = null
)
226 $this->load('openid.inc.php');
227 $user = get_user_by_alias($x);
229 // This will redirect to the canonic URL, which was not used
230 // if this hook was triggered
231 return $this->render_discovery_page(&$page, $user);
234 //--------------------------------------------------------------------//
236 function render_discovery_page(&$page, $user)
239 // Show the documentation if this is not the OpenId page of an user
240 if (is_null($user)) {
241 pl_redirect('Xorg/OpenId');
244 // Redirect to the canonic URL if we are using an alias
245 // There might be a risk of redirection loop here
246 // if $_SERVER was not exactly what we expect
247 $current_url = 'http' . (empty($_SERVER['HTTPS']) ?
'' : 's') . '://'
248 . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
249 $canonic_url = get_user_openid_url($user);
250 if ($current_url != $canonic_url) {
251 http_redirect($canonic_url);
254 // Include X-XRDS-Location response-header for Yadis discovery
255 header('X-XRDS-Location: ' . get_user_xrds_url($user));
257 $page->changeTpl('openid/openid.tpl');
258 $page->setTitle($user->fullName());
259 // Set the <link> tags for HTML-Based Discovery
260 $page->addLink('openid.server openid2.provider', get_openid_url());
261 $page->addLink('openid.delegate openid2.local_id', $user->hruid
);
262 $page->assign_by_ref('user', $user);
267 function render_openid_response($webresponse)
269 if ($webresponse->code
!= AUTH_OPENID_HTTP_OK
) {
270 header(sprintf("HTTP/1.1 %d ", $webresponse->code
),
271 true
, $webresponse->code
);
273 foreach ($webresponse->headers
as $k => $v) {
276 header('Connection: close');
277 print $webresponse->body
;
282 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: