Projet

Général

Profil

Wiki » Historique » Version 2

Gaston TJEBBES, 01/06/2010 16:18

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