Projet

Général

Profil

Wiki » Historique » Version 10

Gérald Schwartzmann, 06/05/2011 18:07

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 9 Gérald Schwartzmann
eolephpCAS::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 10 Gérald Schwartzmann
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.
45 1 Gaston TJEBBES
46 1 Gaston TJEBBES
Nouvelle méthode : https://wiki.jasig.org/display/CASC/phpCAS+logout
47 1 Gaston TJEBBES
48 10 Gérald Schwartzmann
<pre>
49 10 Gérald Schwartzmann
eolephpCAS::logout(array("url"=>$_SERVER["SCRIPT_URI"]));
50 10 Gérald Schwartzmann
</pre>
51 1 Gaston TJEBBES
52 8 Gérald Schwartzmann
ou
53 7 Gérald Schwartzmann
54 10 Gérald Schwartzmann
<pre>
55 7 Gérald Schwartzmann
eolephpCAS::logout(array("service"=>$service));
56 10 Gérald Schwartzmann
</pre>
57 7 Gérald Schwartzmann
58 7 Gérald Schwartzmann
Dans le premier cas, on est redirigé après déconnexion vers la page $url.
59 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.
60 7 Gérald Schwartzmann
61 1 Gaston TJEBBES
h3. Déconnexion centralisée 
62 1 Gaston TJEBBES
63 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.
64 2 Gaston TJEBBES
65 1 Gaston TJEBBES
<pre>
66 1 Gaston TJEBBES
if (__CAS_LOGOUT){
67 8 Gérald Schwartzmann
    if (method_exists(eolephpCAS, 'eolelogoutRequests')){
68 8 Gérald Schwartzmann
        eolephpCAS::eolelogoutRequests(false);
69 1 Gaston TJEBBES
    }
70 1 Gaston TJEBBES
}
71 1 Gaston TJEBBES
</pre>
72 3 Gaston TJEBBES
73 1 Gaston TJEBBES
La variable __CAS_LOGOUT est configuré dans le fichier configCAS/cas.inc.php. 
74 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)). 
75 1 Gaston TJEBBES
Dans ce cas, préféré l'utilisation de la librairie eolePhpCAS. 
76 1 Gaston TJEBBES
77 2 Gaston TJEBBES
78 1 Gaston TJEBBES
h3. Forcer l'url de votre application auprès du serveur CAS
79 1 Gaston TJEBBES
80 1 Gaston TJEBBES
Dans certains cas, par exemple : 
81 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.
82 1 Gaston TJEBBES
83 1 Gaston TJEBBES
Il peut être utile de forcer l'url de l'application.
84 1 Gaston TJEBBES
<pre>
85 8 Gérald Schwartzmann
eolephpCAS::setFixedServiceURL("https://adressedemonapplication");
86 1 Gaston TJEBBES
</pre>
87 1 Gaston TJEBBES
Il est possible de construire l'url depuis les variables php ($_SERVER par exemple).
88 1 Gaston TJEBBES
89 1 Gaston TJEBBES
90 1 Gaston TJEBBES
h3. Récupération du login de l'utilisateur
91 1 Gaston TJEBBES
92 1 Gaston TJEBBES
<pre>
93 8 Gérald Schwartzmann
eolephpCAS::getUser();
94 1 Gaston TJEBBES
</pre>
95 1 Gaston TJEBBES
96 3 Gaston TJEBBES
97 1 Gaston TJEBBES
h3. Le tout dans des fonctions
98 1 Gaston TJEBBES
99 1 Gaston TJEBBES
<pre>
100 1 Gaston TJEBBES
<?php
101 1 Gaston TJEBBES
function cas_auth(){
102 8 Gérald Schwartzmann
   require_once('CAS/eoleCAS.php');
103 1 Gaston TJEBBES
   require_once('configCAS/cas.inc.php');
104 8 Gérald Schwartzmann
   eolephpCAS::client(__CAS_VERSION, __CAS_SERVER, __CAS_PORT, __CAS_URL, false);
105 8 Gérald Schwartzmann
   if (method_exists(eolephpCAS, 'setNoCasServerValidation')){
106 8 Gérald Schwartzmann
        eolephpCAS::setNoCasServerValidation();
107 1 Gaston TJEBBES
   }
108 1 Gaston TJEBBES
   if (__CAS_LOGOUT){
109 8 Gérald Schwartzmann
       if (method_exists(eolephpCAS, 'eolelogoutRequests')){
110 1 Gaston TJEBBES
          eolephpCAS::logoutRequests(false);
111 8 Gérald Schwartzmann
       }
112 1 Gaston TJEBBES
   }
113 1 Gaston TJEBBES
   eolephpCAS::forceAuthentication();
114 8 Gérald Schwartzmann
}
115 1 Gaston TJEBBES
function cas_logout(){
116 8 Gérald Schwartzmann
   require_once('CAS/eoleCAS.php');
117 1 Gaston TJEBBES
   require_once('configCAS/cas.inc.php');
118 8 Gérald Schwartzmann
   eolephpCAS::client(__CAS_VERSION, __CAS_SERVER, __CAS_PORT, __CAS_URL, false);
119 8 Gérald Schwartzmann
   if (method_exists(eolephpCAS, 'setNoCasServerValidation')){
120 1 Gaston TJEBBES
        eolephpCAS::setNoCasServerValidation();
121 8 Gérald Schwartzmann
   }
122 10 Gérald Schwartzmann
   eolephpCAS::logout(array("url"=>$_SERVER["SCRIPT_URI"]));
123 1 Gaston TJEBBES
}
124 1 Gaston TJEBBES
?>
125 2 Gaston TJEBBES
</pre>
126 2 Gaston TJEBBES
127 2 Gaston TJEBBES
Du coup dans ma page php: 
128 2 Gaston TJEBBES
<pre>
129 8 Gérald Schwartzmann
<?php Ce qui s'exécute ici n'est pas authentifié ?>
130 2 Gaston TJEBBES
<?php 
131 2 Gaston TJEBBES
cas_auth();
132 2 Gaston TJEBBES
?>
133 8 Gérald Schwartzmann
<?php Ici on exécute que du code authentifié avec le user : eolephpCAS::getUser(); ?>
134 2 Gaston TJEBBES
</pre>
135 2 Gaston TJEBBES
136 3 Gaston TJEBBES
137 2 Gaston TJEBBES
h2. Cas n°2 : authentification avec récupération de données utilisateurs 
138 2 Gaston TJEBBES
139 2 Gaston TJEBBES
Lors de l'authentification CAS, pour récupérer des données utilisateurs en parallèle,
140 2 Gaston TJEBBES
il faut utiliser le client eolephpCAS au lieu de phpCAS. Le fonctionnement est identique (au Cas n°1). 
141 3 Gaston TJEBBES
142 2 Gaston TJEBBES
143 2 Gaston TJEBBES
h3. Initialisation du client 
144 2 Gaston TJEBBES
145 2 Gaston TJEBBES
Identique au Cas n°1 (remplacer phpCAS par eolephpCAS).
146 2 Gaston TJEBBES
147 2 Gaston TJEBBES
148 2 Gaston TJEBBES
h3. Désactivation du contrôle de certificats
149 2 Gaston TJEBBES
150 2 Gaston TJEBBES
Identique au Cas n°1 (remplacer phpCAS par eolephpCAS).
151 2 Gaston TJEBBES
152 2 Gaston TJEBBES
153 2 Gaston TJEBBES
h3. Authentification
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éconnexion
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. Déconnexion centralisée 
164 2 Gaston TJEBBES
165 2 Gaston TJEBBES
<pre>
166 8 Gérald Schwartzmann
if (method_exists(eolephpCAS, 'eoleLogoutRequests'){
167 8 Gérald Schwartzmann
    eolephpCAS::eoleLogoutRequests(false)
168 2 Gaston TJEBBES
}
169 2 Gaston TJEBBES
</pre>
170 2 Gaston TJEBBES
171 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 
172 2 Gaston TJEBBES
173 2 Gaston TJEBBES
<pre>
174 8 Gérald Schwartzmann
eolephpCAS::eoleLogoutRequests(false). 
175 2 Gaston TJEBBES
</pre>
176 2 Gaston TJEBBES
177 2 Gaston TJEBBES
178 2 Gaston TJEBBES
h3. Récupération des informations utilisateurs 
179 2 Gaston TJEBBES
180 2 Gaston TJEBBES
<pre>
181 2 Gaston TJEBBES
$user_login = eolephpCAS::getUser();
182 2 Gaston TJEBBES
$user_details = eolephpCAS::getDetails();
183 2 Gaston TJEBBES
</pre>
184 2 Gaston TJEBBES
185 2 Gaston TJEBBES
Les détails utilisateurs sont renvoyés sous forme de Array php.
186 2 Gaston TJEBBES
Les données qu'ils contiennent dépendent du filtre sso configuré pour l'application.
187 2 Gaston TJEBBES
188 2 Gaston TJEBBES
h4. Configuration du filtre SSO
189 2 Gaston TJEBBES
190 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.
191 2 Gaston TJEBBES
192 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/
193 2 Gaston TJEBBES
194 2 Gaston TJEBBES
On renseigne "url" et "filtre" qui sont l'url et le filtre associés. 
195 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'.
196 2 Gaston TJEBBES
<pre>
197 2 Gaston TJEBBES
[democas]
198 2 Gaston TJEBBES
baseurl=/lechemin
199 2 Gaston TJEBBES
scheme=http
200 2 Gaston TJEBBES
addr=0/0
201 2 Gaston TJEBBES
typeaddr=ip
202 2 Gaston TJEBBES
filter=monfiltre
203 2 Gaston TJEBBES
</pre>
204 2 Gaston TJEBBES
205 2 Gaston TJEBBES
h5. Deuxième étape : configurer le fichier filtre monfiltre.ini 
206 2 Gaston TJEBBES
207 2 Gaston TJEBBES
<pre>
208 2 Gaston TJEBBES
[user]
209 2 Gaston TJEBBES
user=uid                
210 2 Gaston TJEBBES
[infos] 
211 2 Gaston TJEBBES
user_groups=user_groups
212 2 Gaston TJEBBES
typeadmin=typeadmin
213 2 Gaston TJEBBES
rne=rne
214 4 Gaston TJEBBES
</pre>
215 2 Gaston TJEBBES
	
216 2 Gaston TJEBBES
217 2 Gaston TJEBBES
Autres propriétés que l'on peut récupérer et donc assigner dans monfiltre.ini:
218 2 Gaston TJEBBES
219 2 Gaston TJEBBES
    * cn=cn
220 2 Gaston TJEBBES
    * user_groups=user_groups
221 2 Gaston TJEBBES
    * employeeType=employeeType
222 2 Gaston TJEBBES
    * typeadmin=typeadmin
223 2 Gaston TJEBBES
    * pam=pam
224 2 Gaston TJEBBES
    * user=uid
225 2 Gaston TJEBBES
    * profil=profil
226 2 Gaston TJEBBES
    * classe=Divcod
227 2 Gaston TJEBBES
    * firstname=givenName
228 2 Gaston TJEBBES
    * lastname=sn
229 2 Gaston TJEBBES
    * email=mail
230 2 Gaston TJEBBES
    * ctemail=mail_acad
231 2 Gaston TJEBBES
    * fullname=displayName
232 2 Gaston TJEBBES
    * civil=codecivilite
233 2 Gaston TJEBBES
    * codeRNE=rne
234 2 Gaston TJEBBES
    * nomEtab=nom_etab
235 2 Gaston TJEBBES
    * typeEtab=typeEtab
236 2 Gaston TJEBBES
    * uid=secureid
237 2 Gaston TJEBBES
    * ENTEleveNivFormation=Meflcf
238 2 Gaston TJEBBES
    * ENTPersonProfils=profilcns 
239 2 Gaston TJEBBES
240 2 Gaston TJEBBES
(voir la documentation pour l'accès aux informations ldap)
241 2 Gaston TJEBBES
242 2 Gaston TJEBBES
A cette étape le serveur eole-sso nécessite d'être recharger pour prendre votre nouveau filtre en compte:
243 2 Gaston TJEBBES
244 2 Gaston TJEBBES
<pre>
245 2 Gaston TJEBBES
/etc/init.d/eole-sso restart
246 2 Gaston TJEBBES
</pre>
247 2 Gaston TJEBBES
248 2 Gaston TJEBBES
249 2 Gaston TJEBBES
h5. Troisième étape : récupérer les données dans le script PHP 
250 2 Gaston TJEBBES
251 2 Gaston TJEBBES
*Sous forme de XML*
252 2 Gaston TJEBBES
253 2 Gaston TJEBBES
Dans le script php appeler la méthode getCasXML() de la manièe suivante:
254 2 Gaston TJEBBES
<pre>
255 2 Gaston TJEBBES
eolephpCAS::getCasXML()
256 2 Gaston TJEBBES
</pre>
257 2 Gaston TJEBBES
258 2 Gaston TJEBBES
Ici, les données récupérées pour l'utilisateur courant sont :
259 2 Gaston TJEBBES
260 2 Gaston TJEBBES
<pre>
261 2 Gaston TJEBBES
<?xml version="1.0" encoding="UTF-8"?>
262 2 Gaston TJEBBES
<cas:serviceResponse xmlns:cas="http://www.yale.edu/tp/cas">
263 2 Gaston TJEBBES
    <cas:authenticationSuccess>
264 2 Gaston TJEBBES
        <cas:infos>
265 2 Gaston TJEBBES
            <cas:typeadmin>2</cas:typeadmin>
266 2 Gaston TJEBBES
            <cas:rne>1234567X</cas:rne>
267 2 Gaston TJEBBES
            <cas:user_groups>domainusers</cas:user_groups>
268 2 Gaston TJEBBES
            <cas:user_groups>professeurs</cas:user_groups>
269 2 Gaston TJEBBES
            <cas:user_groups>math</cas:user_groups>
270 2 Gaston TJEBBES
            </cas:infos>
271 2 Gaston TJEBBES
        <cas:user>
272 2 Gaston TJEBBES
            <cas:user>prof5e1</cas:user>
273 2 Gaston TJEBBES
        </cas:user>
274 2 Gaston TJEBBES
        
275 2 Gaston TJEBBES
      </cas:authenticationSuccess>
276 2 Gaston TJEBBES
</cas:serviceResponse>
277 2 Gaston TJEBBES
</pre>
278 2 Gaston TJEBBES
279 2 Gaston TJEBBES
280 2 Gaston TJEBBES
*Sous forme de tableau PHP*
281 2 Gaston TJEBBES
282 2 Gaston TJEBBES
Dans le script php appeler la méthode getDetails() de la manière suivante:
283 2 Gaston TJEBBES
<pre>
284 2 Gaston TJEBBES
eolephpCAS::getDetails()
285 2 Gaston TJEBBES
</pre>
286 2 Gaston TJEBBES
La méthode retourne un tableau associatif à plusieurs dimensions(tableau de tableaux).
287 2 Gaston TJEBBES
288 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.
289 2 Gaston TJEBBES
La clé du tableau contenu à la clé [user] est le nom donné dans notre exemple monfiltre.ini : "user="
290 2 Gaston TJEBBES
[user][user][0] contient l'uid.
291 2 Gaston TJEBBES
292 2 Gaston TJEBBES
Ici les données renvoyées pour l'utilisateur courant sont :
293 2 Gaston TJEBBES
294 2 Gaston TJEBBES
<pre>
295 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 ) ) 
296 2 Gaston TJEBBES
</pre>
297 2 Gaston TJEBBES
298 2 Gaston TJEBBES
299 2 Gaston TJEBBES
h4. Récupération des informations utilisateurs construites 
300 2 Gaston TJEBBES
301 2 Gaston TJEBBES
Il est possible de renvoyer des données utilisateurs autre que celle du ldap.
302 2 Gaston TJEBBES
Il est possible de fournir un script python au serveur SSO qui fournit des données plus complexes. 
303 2 Gaston TJEBBES
Exemple l'envoi de données sorties de la configuration du serveur dans un fichier rne.py (utilisé dans l'exemple):
304 2 Gaston TJEBBES
<pre>
305 2 Gaston TJEBBES
	def calc_info(user_infos):
306 2 Gaston TJEBBES
	    """ renvoie le rne de l'étab de l'uitilisateur """
307 2 Gaston TJEBBES
	    from creole.parsedico import parse_dico
308 2 Gaston TJEBBES
	    return [parse_dico().get('numero_etab', 'renvide')]
309 1 Gaston TJEBBES
</pre>