Projet

Général

Profil

Wiki » Historique » Version 8

Gérald Schwartzmann, 02/05/2011 17:05

1 1 Gaston TJEBBES
h1. Cassification d'une application sur Scribe 
2 1 Gaston TJEBBES
3 1 Gaston TJEBBES
Scribe est fourni avec un serveur d'authentification SSO compatible CAS. Cette page a pour objectif de guider les utilisateurs (administrateurs) de Scribe à utiliser le mode SSO dans leurs applications web php. Les indications fournies permettent de garantir la compatibilité avec les version 0.5 et 1.0.1 de la librairie php-cas sur laquelle se base le paquet php5-cas.
4 1 Gaston TJEBBES
5 2 Gaston TJEBBES
6 1 Gaston TJEBBES
h2. Cas n°1 : authentification simple 
7 1 Gaston TJEBBES
8 6 Joël Cuissinat
Ici, seule l'authentification de l'utilisateur est nécessaire (l'application ne requiert aucune autre information). La cassification de l'accès à une page reste très simple (fonctionnement du Cas classique). On utilisera la configuration centralisée située dans /usr/share/php/configCAS/cas.inc.php. 
9 1 Gaston TJEBBES
10 2 Gaston TJEBBES
11 1 Gaston TJEBBES
h3. Initialisation du client 
12 1 Gaston TJEBBES
13 1 Gaston TJEBBES
<pre>
14 1 Gaston TJEBBES
<?php
15 8 Gérald Schwartzmann
require_once('CAS/eoleCAS.php');
16 1 Gaston TJEBBES
require_once('configCAS/cas.inc.php');
17 1 Gaston TJEBBES
phpCAS::client(__CAS_VERSION, __CAS_SERVER, __CAS_PORT, __CAS_URL, false);
18 1 Gaston TJEBBES
</pre>
19 1 Gaston TJEBBES
Les variables __CAS... sont configurées dans le fichier 'cas.inc.php'. La variable à false signifie qu'une session php a déjà été démarrée avant l'appel au client.
20 7 Gérald Schwartzmann
Si __CAS_URL n'existe pas dans la conf on peut l'ajouter dans cas.inc.php de l'appli et l'initialiser à "" cf webcalendar ou ajaxplorer
21 2 Gaston TJEBBES
22 1 Gaston TJEBBES
h3. Désactivation du contrôle de certificats
23 1 Gaston TJEBBES
24 1 Gaston TJEBBES
En attendant une méthode stable de gestion des certificats, nous utiliserons la méthode setNoCasServerValidation : 
25 1 Gaston TJEBBES
<pre>
26 8 Gérald Schwartzmann
if (method_exists(eolephpCAS, 'setNoCasServerValidation')){
27 8 Gérald Schwartzmann
        eolephpCAS::setNoCasServerValidation();
28 1 Gaston TJEBBES
}
29 1 Gaston TJEBBES
</pre>
30 1 Gaston TJEBBES
31 8 Gérald Schwartzmann
L'utilisation de la fonction method_exists permet d'assurer la compatibilité avec les différentes version du client phpCas (paquet php5-cas) 
32 1 Gaston TJEBBES
33 2 Gaston TJEBBES
34 1 Gaston TJEBBES
h3. Authentification 
35 1 Gaston TJEBBES
36 1 Gaston TJEBBES
<pre>
37 8 Gérald Schwartzmann
eolephpCAS::forceAuthentication();
38 1 Gaston TJEBBES
</pre>
39 6 Joël Cuissinat
La redirection vers le serveur CAS est effectuée si nécessaire, par la suite le code est exécuté dans un contexte authentifié.
40 1 Gaston TJEBBES
41 2 Gaston TJEBBES
42 1 Gaston TJEBBES
h3. Déconnexion 
43 1 Gaston TJEBBES
44 1 Gaston TJEBBES
<pre>
45 1 Gaston TJEBBES
if (isset($_REQUEST['logout'])) {
46 8 Gérald Schwartzmann
    if (method_exists(eolephpCAS, 'logoutWithUrl')){
47 8 Gérald Schwartzmann
        eolephpCAS::logoutWithUrl($_SERVER["SCRIPT_URI"]);
48 1 Gaston TJEBBES
    }else{
49 8 Gérald Schwartzmann
        eolephpCAS::logout($_SERVER["SCRIPT_URI"]);
50 1 Gaston TJEBBES
    }
51 1 Gaston TJEBBES
}
52 1 Gaston TJEBBES
</pre>
53 1 Gaston TJEBBES
54 1 Gaston TJEBBES
La variable de redirection ici : $_SERVER["SCRIPT_URI"]  peut être remplacée par la page de logout prévue au départ dans l'application. 
55 1 Gaston TJEBBES
56 7 Gérald Schwartzmann
Nouvelle méthode : https://wiki.jasig.org/display/CASC/phpCAS+logout
57 1 Gaston TJEBBES
58 8 Gérald Schwartzmann
eolephpCAS::logout(array("url"=>$url));
59 7 Gérald Schwartzmann
60 7 Gérald Schwartzmann
ou
61 7 Gérald Schwartzmann
62 8 Gérald Schwartzmann
eolephpCAS::logout(array("service"=>$service));
63 7 Gérald Schwartzmann
64 7 Gérald Schwartzmann
Dans le premier cas, on est redirigé après déconnexion vers la page $url.
65 7 Gérald Schwartzmann
Dans le deuxième cas, on est redirigé sur la mire d'authentification prête pour se reconnecter sur le service $service.
66 7 Gérald Schwartzmann
67 1 Gaston TJEBBES
h3. Déconnexion centralisée 
68 1 Gaston TJEBBES
69 2 Gaston TJEBBES
Le serveur CAS au moment de la déconnexion d'une application, appelle la déconnexion des différentes applications ayant une session SSO ouverte.
70 2 Gaston TJEBBES
71 1 Gaston TJEBBES
<pre>
72 1 Gaston TJEBBES
if (__CAS_LOGOUT){
73 8 Gérald Schwartzmann
    if (method_exists(eolephpCAS, 'eolelogoutRequests')){
74 8 Gérald Schwartzmann
        eolephpCAS::eolelogoutRequests(false);
75 1 Gaston TJEBBES
    }
76 1 Gaston TJEBBES
}
77 1 Gaston TJEBBES
</pre>
78 3 Gaston TJEBBES
79 1 Gaston TJEBBES
La variable __CAS_LOGOUT est configuré dans le fichier configCAS/cas.inc.php. 
80 1 Gaston TJEBBES
A noter : cette fonction fournit par le client CAS d'origine a un fonctionnement très limité, elle ne fonctionne pas lorsque le nom de la session est forcé par l'application cassifié (appel à session_name(nomdelasession)). 
81 1 Gaston TJEBBES
Dans ce cas, préféré l'utilisation de la librairie eolePhpCAS. 
82 1 Gaston TJEBBES
83 2 Gaston TJEBBES
84 1 Gaston TJEBBES
h3. Forcer l'url de votre application auprès du serveur CAS
85 1 Gaston TJEBBES
86 1 Gaston TJEBBES
Dans certains cas, par exemple : 
87 1 Gaston TJEBBES
Accès au travers d'un reverseproxy d'une application http par le protocole https et port 80 fermé sur le reverseproxy.
88 1 Gaston TJEBBES
89 1 Gaston TJEBBES
Il peut être utile de forcer l'url de l'application.
90 1 Gaston TJEBBES
<pre>
91 8 Gérald Schwartzmann
eolephpCAS::setFixedServiceURL("https://adressedemonapplication");
92 1 Gaston TJEBBES
</pre>
93 1 Gaston TJEBBES
Il est possible de construire l'url depuis les variables php ($_SERVER par exemple).
94 1 Gaston TJEBBES
95 1 Gaston TJEBBES
96 1 Gaston TJEBBES
h3. Récupération du login de l'utilisateur
97 1 Gaston TJEBBES
98 1 Gaston TJEBBES
<pre>
99 8 Gérald Schwartzmann
eolephpCAS::getUser();
100 1 Gaston TJEBBES
</pre>
101 1 Gaston TJEBBES
102 3 Gaston TJEBBES
103 1 Gaston TJEBBES
h3. Le tout dans des fonctions
104 1 Gaston TJEBBES
105 1 Gaston TJEBBES
<pre>
106 1 Gaston TJEBBES
<?php
107 1 Gaston TJEBBES
function cas_auth(){
108 8 Gérald Schwartzmann
   require_once('CAS/eoleCAS.php');
109 1 Gaston TJEBBES
   require_once('configCAS/cas.inc.php');
110 8 Gérald Schwartzmann
   eolephpCAS::client(__CAS_VERSION, __CAS_SERVER, __CAS_PORT, __CAS_URL, false);
111 8 Gérald Schwartzmann
   if (method_exists(eolephpCAS, 'setNoCasServerValidation')){
112 8 Gérald Schwartzmann
        eolephpCAS::setNoCasServerValidation();
113 1 Gaston TJEBBES
   }
114 1 Gaston TJEBBES
   if (__CAS_LOGOUT){
115 8 Gérald Schwartzmann
       if (method_exists(eolephpCAS, 'eolelogoutRequests')){
116 8 Gérald Schwartzmann
          eolephpCAS::logoutRequests(false);
117 1 Gaston TJEBBES
       }
118 1 Gaston TJEBBES
   }
119 8 Gérald Schwartzmann
   eolephpCAS::forceAuthentication();
120 1 Gaston TJEBBES
}
121 7 Gérald Schwartzmann
function cas_logout(){
122 8 Gérald Schwartzmann
   require_once('CAS/eoleCAS.php');
123 1 Gaston TJEBBES
   require_once('configCAS/cas.inc.php');
124 8 Gérald Schwartzmann
   eolephpCAS::client(__CAS_VERSION, __CAS_SERVER, __CAS_PORT, __CAS_URL, false);
125 8 Gérald Schwartzmann
   if (method_exists(eolephpCAS, 'setNoCasServerValidation')){
126 8 Gérald Schwartzmann
        eolephpCAS::setNoCasServerValidation();
127 1 Gaston TJEBBES
   }
128 8 Gérald Schwartzmann
   if (method_exists(eolephpCAS, 'logoutWithUrl')){
129 8 Gérald Schwartzmann
        eolephpCAS::logoutWithUrl($_SERVER["SCRIPT_URI"]);
130 1 Gaston TJEBBES
    }else{
131 8 Gérald Schwartzmann
        eolephpCAS::logout($_SERVER["SCRIPT_URI"]);
132 1 Gaston TJEBBES
    }
133 1 Gaston TJEBBES
}
134 1 Gaston TJEBBES
?>
135 2 Gaston TJEBBES
</pre>
136 2 Gaston TJEBBES
137 2 Gaston TJEBBES
Du coup dans ma page php: 
138 2 Gaston TJEBBES
<pre>
139 8 Gérald Schwartzmann
<?php Ce qui s'exécute ici n'est pas authentifié ?>
140 2 Gaston TJEBBES
<?php 
141 2 Gaston TJEBBES
cas_auth();
142 2 Gaston TJEBBES
?>
143 8 Gérald Schwartzmann
<?php Ici on exécute que du code authentifié avec le user : eolephpCAS::getUser(); ?>
144 2 Gaston TJEBBES
</pre>
145 2 Gaston TJEBBES
146 3 Gaston TJEBBES
147 2 Gaston TJEBBES
h2. Cas n°2 : authentification avec récupération de données utilisateurs 
148 2 Gaston TJEBBES
149 2 Gaston TJEBBES
Lors de l'authentification CAS, pour récupérer des données utilisateurs en parallèle,
150 2 Gaston TJEBBES
il faut utiliser le client eolephpCAS au lieu de phpCAS. Le fonctionnement est identique (au Cas n°1). 
151 3 Gaston TJEBBES
152 2 Gaston TJEBBES
153 2 Gaston TJEBBES
h3. Initialisation du client 
154 2 Gaston TJEBBES
155 2 Gaston TJEBBES
Identique au Cas n°1 (remplacer phpCAS par eolephpCAS).
156 2 Gaston TJEBBES
157 2 Gaston TJEBBES
158 2 Gaston TJEBBES
h3. Désactivation du contrôle de certificats
159 2 Gaston TJEBBES
160 2 Gaston TJEBBES
Identique au Cas n°1 (remplacer phpCAS par eolephpCAS).
161 2 Gaston TJEBBES
162 2 Gaston TJEBBES
163 2 Gaston TJEBBES
h3. Authentification
164 2 Gaston TJEBBES
165 2 Gaston TJEBBES
Identique au Cas n°1 (remplacer phpCAS par eolephpCAS).
166 2 Gaston TJEBBES
167 2 Gaston TJEBBES
168 2 Gaston TJEBBES
h3.Déconnexion
169 2 Gaston TJEBBES
170 2 Gaston TJEBBES
Identique au Cas n°1 (remplacer phpCAS par eolephpCAS).
171 2 Gaston TJEBBES
172 2 Gaston TJEBBES
173 2 Gaston TJEBBES
h3. Déconnexion centralisée 
174 2 Gaston TJEBBES
175 2 Gaston TJEBBES
<pre>
176 8 Gérald Schwartzmann
if (method_exists(eolephpCAS, 'eoleLogoutRequests'){
177 8 Gérald Schwartzmann
    eolephpCAS::eoleLogoutRequests(false)
178 2 Gaston TJEBBES
}
179 2 Gaston TJEBBES
</pre>
180 2 Gaston TJEBBES
181 2 Gaston TJEBBES
Lorsque l'on se déconnecte d'une application, celle-ci supprime généralement sa session et demande sa déconnexion du serveur SSO. Les sessions des autres applications ne sont pas supprimées dans ce processus et (la plupart du temps), l'application conserve les informations de connexion dans sa session et conserve donc la connexion. Lors de l'activation de la fonction de déconnexion centralisé, le serveur sso demande à chaque application de supprimer la session associée à l'utilisateur qui s'est déconnecté. Pour que les applications prennent en compte cet appel, il faut rajouter un appel à la fonction 
182 2 Gaston TJEBBES
183 2 Gaston TJEBBES
<pre>
184 8 Gérald Schwartzmann
eolephpCAS::eoleLogoutRequests(false). 
185 2 Gaston TJEBBES
</pre>
186 2 Gaston TJEBBES
187 2 Gaston TJEBBES
188 2 Gaston TJEBBES
h3. Récupération des informations utilisateurs 
189 2 Gaston TJEBBES
190 2 Gaston TJEBBES
<pre>
191 2 Gaston TJEBBES
$user_login = eolephpCAS::getUser();
192 2 Gaston TJEBBES
$user_details = eolephpCAS::getDetails();
193 2 Gaston TJEBBES
</pre>
194 2 Gaston TJEBBES
195 2 Gaston TJEBBES
Les détails utilisateurs sont renvoyés sous forme de Array php.
196 2 Gaston TJEBBES
Les données qu'ils contiennent dépendent du filtre sso configuré pour l'application.
197 2 Gaston TJEBBES
198 2 Gaston TJEBBES
h4. Configuration du filtre SSO
199 2 Gaston TJEBBES
200 2 Gaston TJEBBES
Le serveur SSO est capable de fournir des données utilisateurs aux applications sous la forme de xml. Un filtre par défaut se contente de fournir l'uid de l'utilisateur qui se connecte, mais tout un ensemble de données peut être fournit.
201 2 Gaston TJEBBES
202 2 Gaston TJEBBES
h5. Première étape : configurer l'application dans un fichier monfiltre_apps.ini que l'on place dans /usr/share/sso/app_filters/
203 2 Gaston TJEBBES
204 2 Gaston TJEBBES
On renseigne "url" et "filtre" qui sont l'url et le filtre associés. 
205 2 Gaston TJEBBES
Quand l'url "http://adresse/lechemin" me demande d'authentifier quelqu'un, une fois authentifiée, je lui renvoie les données renseignées dans le filtre 'monfiltre'.
206 2 Gaston TJEBBES
<pre>
207 2 Gaston TJEBBES
[democas]
208 2 Gaston TJEBBES
baseurl=/lechemin
209 2 Gaston TJEBBES
scheme=http
210 2 Gaston TJEBBES
addr=0/0
211 2 Gaston TJEBBES
typeaddr=ip
212 2 Gaston TJEBBES
filter=monfiltre
213 2 Gaston TJEBBES
</pre>
214 2 Gaston TJEBBES
215 2 Gaston TJEBBES
h5. Deuxième étape : configurer le fichier filtre monfiltre.ini 
216 2 Gaston TJEBBES
217 2 Gaston TJEBBES
<pre>
218 2 Gaston TJEBBES
[user]
219 2 Gaston TJEBBES
user=uid                
220 2 Gaston TJEBBES
[infos] 
221 2 Gaston TJEBBES
user_groups=user_groups
222 2 Gaston TJEBBES
typeadmin=typeadmin
223 2 Gaston TJEBBES
rne=rne
224 4 Gaston TJEBBES
</pre>
225 2 Gaston TJEBBES
	
226 2 Gaston TJEBBES
227 2 Gaston TJEBBES
Autres propriétés que l'on peut récupérer et donc assigner dans monfiltre.ini:
228 2 Gaston TJEBBES
229 2 Gaston TJEBBES
    * cn=cn
230 2 Gaston TJEBBES
    * user_groups=user_groups
231 2 Gaston TJEBBES
    * employeeType=employeeType
232 2 Gaston TJEBBES
    * typeadmin=typeadmin
233 2 Gaston TJEBBES
    * pam=pam
234 2 Gaston TJEBBES
    * user=uid
235 2 Gaston TJEBBES
    * profil=profil
236 2 Gaston TJEBBES
    * classe=Divcod
237 2 Gaston TJEBBES
    * firstname=givenName
238 2 Gaston TJEBBES
    * lastname=sn
239 2 Gaston TJEBBES
    * email=mail
240 2 Gaston TJEBBES
    * ctemail=mail_acad
241 2 Gaston TJEBBES
    * fullname=displayName
242 2 Gaston TJEBBES
    * civil=codecivilite
243 2 Gaston TJEBBES
    * codeRNE=rne
244 2 Gaston TJEBBES
    * nomEtab=nom_etab
245 2 Gaston TJEBBES
    * typeEtab=typeEtab
246 2 Gaston TJEBBES
    * uid=secureid
247 2 Gaston TJEBBES
    * ENTEleveNivFormation=Meflcf
248 2 Gaston TJEBBES
    * ENTPersonProfils=profilcns 
249 2 Gaston TJEBBES
250 2 Gaston TJEBBES
(voir la documentation pour l'accès aux informations ldap)
251 2 Gaston TJEBBES
252 2 Gaston TJEBBES
A cette étape le serveur eole-sso nécessite d'être recharger pour prendre votre nouveau filtre en compte:
253 2 Gaston TJEBBES
254 2 Gaston TJEBBES
<pre>
255 2 Gaston TJEBBES
/etc/init.d/eole-sso restart
256 2 Gaston TJEBBES
</pre>
257 2 Gaston TJEBBES
258 2 Gaston TJEBBES
259 2 Gaston TJEBBES
h5. Troisième étape : récupérer les données dans le script PHP 
260 2 Gaston TJEBBES
261 2 Gaston TJEBBES
*Sous forme de XML*
262 2 Gaston TJEBBES
263 2 Gaston TJEBBES
Dans le script php appeler la méthode getCasXML() de la manièe suivante:
264 2 Gaston TJEBBES
<pre>
265 2 Gaston TJEBBES
eolephpCAS::getCasXML()
266 2 Gaston TJEBBES
</pre>
267 2 Gaston TJEBBES
268 2 Gaston TJEBBES
Ici, les données récupérées pour l'utilisateur courant sont :
269 2 Gaston TJEBBES
270 2 Gaston TJEBBES
<pre>
271 2 Gaston TJEBBES
<?xml version="1.0" encoding="UTF-8"?>
272 2 Gaston TJEBBES
<cas:serviceResponse xmlns:cas="http://www.yale.edu/tp/cas">
273 2 Gaston TJEBBES
    <cas:authenticationSuccess>
274 2 Gaston TJEBBES
        <cas:infos>
275 2 Gaston TJEBBES
            <cas:typeadmin>2</cas:typeadmin>
276 2 Gaston TJEBBES
            <cas:rne>1234567X</cas:rne>
277 2 Gaston TJEBBES
            <cas:user_groups>domainusers</cas:user_groups>
278 2 Gaston TJEBBES
            <cas:user_groups>professeurs</cas:user_groups>
279 2 Gaston TJEBBES
            <cas:user_groups>math</cas:user_groups>
280 2 Gaston TJEBBES
            </cas:infos>
281 2 Gaston TJEBBES
        <cas:user>
282 2 Gaston TJEBBES
            <cas:user>prof5e1</cas:user>
283 2 Gaston TJEBBES
        </cas:user>
284 2 Gaston TJEBBES
        
285 2 Gaston TJEBBES
      </cas:authenticationSuccess>
286 2 Gaston TJEBBES
</cas:serviceResponse>
287 2 Gaston TJEBBES
</pre>
288 2 Gaston TJEBBES
289 2 Gaston TJEBBES
290 2 Gaston TJEBBES
*Sous forme de tableau PHP*
291 2 Gaston TJEBBES
292 2 Gaston TJEBBES
Dans le script php appeler la méthode getDetails() de la manière suivante:
293 2 Gaston TJEBBES
<pre>
294 2 Gaston TJEBBES
eolephpCAS::getDetails()
295 2 Gaston TJEBBES
</pre>
296 2 Gaston TJEBBES
La méthode retourne un tableau associatif à plusieurs dimensions(tableau de tableaux).
297 2 Gaston TJEBBES
298 2 Gaston TJEBBES
Si on suit l'exemple du filtre monfiltre.ini le tableau renvoyé contient deux tableaux dont les clés correspondent aux étiquettes [user] et [infos] définies dans monfiltre.ini.
299 2 Gaston TJEBBES
La clé du tableau contenu à la clé [user] est le nom donné dans notre exemple monfiltre.ini : "user="
300 2 Gaston TJEBBES
[user][user][0] contient l'uid.
301 2 Gaston TJEBBES
302 2 Gaston TJEBBES
Ici les données renvoyées pour l'utilisateur courant sont :
303 2 Gaston TJEBBES
304 2 Gaston TJEBBES
<pre>
305 2 Gaston TJEBBES
Array ( [infos] => Array ( [typeadmin] => Array ( [0] => 2 ) [rne] => Array ( [0] => 1234567X ) [user_groups] => Array ( [0] => domainusers [1] => professeurs [2] => math ) ) [user] => Array ( [user] => Array ( [0] => prof5e1 ) ) 
306 2 Gaston TJEBBES
</pre>
307 2 Gaston TJEBBES
308 2 Gaston TJEBBES
309 2 Gaston TJEBBES
h4. Récupération des informations utilisateurs construites 
310 2 Gaston TJEBBES
311 2 Gaston TJEBBES
Il est possible de renvoyer des données utilisateurs autre que celle du ldap.
312 2 Gaston TJEBBES
Il est possible de fournir un script python au serveur SSO qui fournit des données plus complexes. 
313 2 Gaston TJEBBES
Exemple l'envoi de données sorties de la configuration du serveur dans un fichier rne.py (utilisé dans l'exemple):
314 2 Gaston TJEBBES
<pre>
315 2 Gaston TJEBBES
	def calc_info(user_infos):
316 2 Gaston TJEBBES
	    """ renvoie le rne de l'étab de l'uitilisateur """
317 2 Gaston TJEBBES
	    from creole.parsedico import parse_dico
318 2 Gaston TJEBBES
	    return [parse_dico().get('numero_etab', 'renvide')]
319 1 Gaston TJEBBES
</pre>