Allow to customize the login page in handler_groupex.
[platal.git] / modules / auth.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2011 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 class AuthModule extends PLModule
23 {
24 function handlers()
25 {
26 return array(
27 'groupex/donne-chall.php' => $this->make_hook('chall', AUTH_PUBLIC),
28 'groupex/export-econfiance.php' => $this->make_hook('econf', AUTH_PUBLIC, 'user', NO_HTTPS),
29
30 'webservices/manageurs.php' => $this->make_hook('manageurs', AUTH_PUBLIC, 'user', NO_HTTPS),
31
32 'auth-redirect.php' => $this->make_hook('redirect', AUTH_COOKIE, 'user'),
33 'auth-groupex.php' => $this->make_hook('groupex_old', AUTH_COOKIE, 'user'),
34 'auth-groupex' => $this->make_hook('groupex', AUTH_PUBLIC),
35 'admin/auth-groupes-x' => $this->make_hook('admin_authgroupesx', AUTH_MDP, 'admin'),
36 );
37 }
38
39 function handler_chall($page)
40 {
41 $_SESSION["chall"] = uniqid(rand(), 1);
42 echo $_SESSION["chall"] . "\n" . session_id();
43 exit;
44 }
45
46 function handler_econf($page)
47 {
48 global $globals;
49
50 $cle = $globals->core->econfiance;
51
52 $res = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n<membres>\n\n";
53
54 if (S::v('chall') && Get::s('PASS') == md5(S::v('chall').$cle)) {
55 $list = new MMList(User::getWithUID(10154), "x-econfiance.polytechnique.org");
56 $members = $list->get_members('membres');
57 if (is_array($members)) {
58 $membres = Array();
59 foreach($members[1] as $member) {
60 $user = User::getSilent($member[1]);
61 if ($user && $user->hasProfile()) {
62 $profile = $user->profile();
63 $res .= "<membre>\n";
64 $res .= "\t<nom>" . $profile->lastName() . "</nom>\n";
65 $res .= "\t<prenom>" . $profile->firstName() . "</prenom>\n";
66 $res .= "\t<email>" . $user->forlifeEmail() . "</email>\n";
67 $res .= "</membre>\n\n";
68 }
69 }
70 }
71 $res .= "</membres>\n\n";
72
73 pl_content_headers("text/xml");
74 echo $res;
75 }
76 exit;
77 }
78
79 function handler_manageurs($page)
80 {
81 global $globals;
82
83 require_once 'webservices/manageurs.server.inc.php';
84
85 $ips = array_flip(explode(' ', $globals->manageurs->authorized_ips));
86 if ($ips && isset($ips[$_SERVER['REMOTE_ADDR']])) {
87 $server = xmlrpc_server_create();
88
89 xmlrpc_server_register_method($server, 'get_annuaire_infos', 'get_annuaire_infos');
90 xmlrpc_server_register_method($server, 'get_nouveau_infos', 'get_nouveau_infos');
91
92 $request = @$GLOBALS['HTTP_RAW_POST_DATA'];
93 $response = xmlrpc_server_call_method($server, $request, null);
94 pl_content_headers("text/xml");
95 print $response;
96 xmlrpc_server_destroy($server);
97 }
98
99 exit;
100 }
101
102 function handler_redirect($page)
103 {
104 http_redirect(Env::v('dest', '/'));
105 }
106
107 function handler_groupex_old($page)
108 {
109 return $this->handler_groupex($page, 'iso-8859-1');
110 }
111
112 /** Handles the 'auth-groupe-x' authentication.
113 * Expects the following GET parameters:
114 * - pass: the 'password' for the authentication
115 * - challenge: the authentication challenge
116 * - url: the return URL
117 * - session: the remote PHP session ID
118 */
119 function handler_groupex($page, $charset = 'utf8')
120 {
121 if (!S::logged()) {
122 $page->assign('referer', true);
123 return PL_DO_AUTH;
124 }
125
126 $this->load('auth.inc.php');
127
128 $gpex_pass = Get::s('pass');
129 $gpex_url = urldecode(Get::s('url'));
130 if (Get::has('session')) {
131 if (strpos($gpex_url, '?') === false) {
132 $gpex_url .= "?PHPSESSID=" . Get::s('session');
133 } else {
134 $gpex_url .= "&PHPSESSID=" . Get::s('session');
135 }
136 }
137
138 // Normalize the return URL.
139 if (!preg_match("/^(http|https):\/\/.*/",$gpex_url)) {
140 $gpex_url = "http://$gpex_url";
141 }
142 $gpex_challenge = Get::s('challenge');
143
144 // Update the last login information (unless the user is in SUID).
145 $uid = S::i('uid');
146 if (!S::suid()) {
147 global $platal;
148 S::logger($uid)->log('connexion_auth_ext', $platal->path.' '.urldecode($_GET['url']));
149 }
150
151 // Iterate over the auth token to find which one did sign the request.
152 $res = XDB::iterRow('SELECT privkey, name, datafields, returnurls FROM group_auth');
153 while (list($privkey, $name, $datafields, $returnurls) = $res->next()) {
154 if (md5($gpex_challenge.$privkey) == $gpex_pass) {
155 $returnurls = trim($returnurls);
156 // We check that the return url matches a per-key regexp to prevent
157 // replay attacks (more exactly to force replay attacks to redirect
158 // the user to the real GroupeX website, which defeats the attack).
159 if (empty($returnurls) || @preg_match($returnurls, $gpex_url)) {
160 $returl = $gpex_url . gpex_make_params($gpex_challenge, $privkey, $datafields, $charset);
161 XDB::execute('UPDATE group_auth
162 SET last_used = DATE(NOW())
163 WHERE name = {?}',
164 $name);
165 http_redirect($returl);
166 } else if (S::admin()) {
167 $page->kill("La requête d'authentification a échouée (url de retour invalide).");
168 }
169 }
170 }
171
172 // Otherwise (if no valid request was found, or if the return URL is not
173 // acceptable), the user is redirected back to our homepage.
174 pl_redirect('/');
175 }
176
177 function handler_admin_authgroupesx($page, $action = 'list', $id = null)
178 {
179 $page->setTitle('Administration - Auth groupes X');
180 $page->assign('title', 'Gestion de l\'authentification centralisée');
181 $table_editor = new PLTableEditor('admin/auth-groupes-x','group_auth','id');
182 $table_editor->describe('name','nom',true);
183 $table_editor->describe('privkey','clé privée',false, true);
184 $table_editor->describe('datafields','champs renvoyés',true);
185 $table_editor->describe('returnurls','urls de retour',true);
186 $table_editor->describe('last_used', 'dernière utilisation', true);
187 $table_editor->apply($page, $action, $id);
188 }
189 }
190
191 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
192 ?>