Projet

Général

Profil

Wiki » Historique » Version 17

Lionel Morin, 12/10/2012 14:11

1 16 Lionel Morin
{{toc}}
2 16 Lionel Morin
3 1 Gaston TJEBBES
h1. Cassification d'une application sur Scribe 
4 1 Gaston TJEBBES
5 16 Lionel Morin
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 versions 1.2.0 et 1.3.1 de la librairie php-cas sur lesquelles se base le paquet php5-cas.
6 1 Gaston TJEBBES
7 2 Gaston TJEBBES
8 1 Gaston TJEBBES
h2. Cas n°1 : authentification simple 
9 1 Gaston TJEBBES
10 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. 
11 1 Gaston TJEBBES
12 2 Gaston TJEBBES
13 1 Gaston TJEBBES
h3. Initialisation du client 
14 1 Gaston TJEBBES
15 1 Gaston TJEBBES
<pre>
16 1 Gaston TJEBBES
<?php
17 16 Lionel Morin
// require_once('CAS/eoleCAS.php'); // pointe sur la version 1.2.0
18 16 Lionel Morin
require_once('CAS-1.3.1/eoleCAS.php'); // pointe sur la version 1.3.1
19 1 Gaston TJEBBES
require_once('configCAS/cas.inc.php');
20 1 Gaston TJEBBES
eolephpCAS::client(__CAS_VERSION, __CAS_SERVER, __CAS_PORT, __CAS_URL, false);
21 7 Gérald Schwartzmann
</pre>
22 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.
23 1 Gaston TJEBBES
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
24 1 Gaston TJEBBES
25 1 Gaston TJEBBES
26 16 Lionel Morin
h3. Contrôle des certificats
27 16 Lionel Morin
28 16 Lionel Morin
Pour une meilleure sécurité, la validation par la CA doit être activée.
29 16 Lionel Morin
Pour permettre une compatibilité avec l'ancienne version où elle n'était pas activée : 
30 1 Gaston TJEBBES
<pre>
31 16 Lionel Morin
    if (__CAS_VALIDER_CA) {
32 16 Lionel Morin
        EolephpCAS::setCasServerCACert(__CAS_CA_LOCATION);
33 16 Lionel Morin
    } else {
34 16 Lionel Morin
        if (method_exists("EolephpCAS", "setNoCasServerValidation")){
35 16 Lionel Morin
            EolephpCAS::setNoCasServerValidation();
36 16 Lionel Morin
        }
37 16 Lionel Morin
    }
38 8 Gérald Schwartzmann
</pre>
39 1 Gaston TJEBBES
40 16 Lionel Morin
L'utilisation de la fonction method_exists permet d'assurer la compatibilité avec les différentes versions du client phpCas (paquet php5-cas) 
41 1 Gaston TJEBBES
42 1 Gaston TJEBBES
43 1 Gaston TJEBBES
h3. Authentification 
44 8 Gérald Schwartzmann
45 1 Gaston TJEBBES
<pre>
46 6 Joël Cuissinat
eolephpCAS::forceAuthentication();
47 1 Gaston TJEBBES
</pre>
48 2 Gaston TJEBBES
La redirection vers le serveur CAS est effectuée si nécessaire, par la suite le code est exécuté dans un contexte authentifié.
49 1 Gaston TJEBBES
50 1 Gaston TJEBBES
51 10 Gérald Schwartzmann
h3. Déconnexion 
52 1 Gaston TJEBBES
53 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.
54 1 Gaston TJEBBES
55 10 Gérald Schwartzmann
Nouvelle méthode : https://wiki.jasig.org/display/CASC/phpCAS+logout
56 10 Gérald Schwartzmann
57 10 Gérald Schwartzmann
<pre>
58 1 Gaston TJEBBES
eolephpCAS::logout(array("url"=>$_SERVER["SCRIPT_URI"]));
59 8 Gérald Schwartzmann
</pre>
60 7 Gérald Schwartzmann
61 10 Gérald Schwartzmann
ou
62 7 Gérald Schwartzmann
63 10 Gérald Schwartzmann
<pre>
64 7 Gérald Schwartzmann
eolephpCAS::logout(array("service"=>$service));
65 7 Gérald Schwartzmann
</pre>
66 1 Gaston TJEBBES
67 1 Gaston TJEBBES
Dans le premier cas, on est redirigé après déconnexion vers la page $url.
68 2 Gaston TJEBBES
Dans le deuxième cas, on est redirigé sur la mire d'authentification prête pour se reconnecter sur le service $service.
69 2 Gaston TJEBBES
70 1 Gaston TJEBBES
h3. Déconnexion centralisée 
71 1 Gaston TJEBBES
72 8 Gérald Schwartzmann
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.
73 1 Gaston TJEBBES
74 1 Gaston TJEBBES
<pre>
75 8 Gérald Schwartzmann
if (__CAS_LOGOUT){
76 1 Gaston TJEBBES
    if (method_exists(eolephpCAS, 'eolelogoutRequests')){
77 1 Gaston TJEBBES
        eolephpCAS::eolelogoutRequests(false);
78 1 Gaston TJEBBES
    }
79 3 Gaston TJEBBES
}
80 1 Gaston TJEBBES
</pre>
81 13 Lionel Morin
82 17 Lionel Morin
La variable __CAS_LOGOUT est configurée dans le fichier configCAS/cas.inc.php. 
83 16 Lionel Morin
-A noter : cette fonction fournie 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ée (appel à session_name(nomdelasession)). 
84 16 Lionel Morin
Dans ce cas, préférez l'utilisation de la librairie eolePhpCAS.- 
85 1 Gaston TJEBBES
86 1 Gaston TJEBBES
87 1 Gaston TJEBBES
h3. Forcer l'url de votre application auprès du serveur CAS
88 1 Gaston TJEBBES
89 1 Gaston TJEBBES
Dans certains cas, par exemple : 
90 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.
91 1 Gaston TJEBBES
92 1 Gaston TJEBBES
Il peut être utile de forcer l'url de l'application.
93 1 Gaston TJEBBES
<pre>
94 1 Gaston TJEBBES
eolephpCAS::setFixedServiceURL("https://adressedemonapplication");
95 1 Gaston TJEBBES
</pre>
96 1 Gaston TJEBBES
Il est possible de construire l'url depuis les variables php ($_SERVER par exemple).
97 8 Gérald Schwartzmann
98 1 Gaston TJEBBES
99 1 Gaston TJEBBES
h3. Récupération du login de l'utilisateur
100 1 Gaston TJEBBES
101 1 Gaston TJEBBES
<pre>
102 1 Gaston TJEBBES
eolephpCAS::getUser();
103 1 Gaston TJEBBES
</pre>
104 1 Gaston TJEBBES
105 1 Gaston TJEBBES
106 1 Gaston TJEBBES
h3. Le tout dans des fonctions
107 1 Gaston TJEBBES
108 1 Gaston TJEBBES
<pre>
109 1 Gaston TJEBBES
<?php
110 8 Gérald Schwartzmann
function cas_auth(){
111 16 Lionel Morin
   require_once('CAS-1.3.1/eoleCAS.php');
112 8 Gérald Schwartzmann
   require_once('configCAS/cas.inc.php');
113 8 Gérald Schwartzmann
   eolephpCAS::client(__CAS_VERSION, __CAS_SERVER, __CAS_PORT, __CAS_URL, false);
114 16 Lionel Morin
   if (__CAS_VALIDER_CA) {
115 16 Lionel Morin
       eolephpCAS::setCasServerCACert(__CAS_CA_LOCATION);
116 16 Lionel Morin
   } else {
117 16 Lionel Morin
       if (method_exists(eolephpCAS, 'setNoCasServerValidation')){
118 16 Lionel Morin
           eolephpCAS::setNoCasServerValidation();
119 16 Lionel Morin
       }
120 1 Gaston TJEBBES
   }
121 1 Gaston TJEBBES
   if (__CAS_LOGOUT){
122 1 Gaston TJEBBES
       if (method_exists(eolephpCAS, 'eolelogoutRequests')){
123 1 Gaston TJEBBES
          eolephpCAS::logoutRequests(false);
124 2 Gaston TJEBBES
       }
125 2 Gaston TJEBBES
   }
126 2 Gaston TJEBBES
   eolephpCAS::forceAuthentication();
127 3 Gaston TJEBBES
}
128 2 Gaston TJEBBES
function cas_logout(){
129 16 Lionel Morin
   require_once('CAS-1.3.1/eoleCAS.php');
130 2 Gaston TJEBBES
   require_once('configCAS/cas.inc.php');
131 2 Gaston TJEBBES
   eolephpCAS::client(__CAS_VERSION, __CAS_SERVER, __CAS_PORT, __CAS_URL, false);
132 16 Lionel Morin
   if (__CAS_VALIDER_CA) {
133 16 Lionel Morin
       eolephpCAS::setCasServerCACert(__CAS_CA_LOCATION);
134 16 Lionel Morin
   } else {
135 16 Lionel Morin
       if (method_exists(eolephpCAS, 'setNoCasServerValidation')){
136 16 Lionel Morin
           eolephpCAS::setNoCasServerValidation();
137 16 Lionel Morin
       }
138 1 Gaston TJEBBES
   }
139 2 Gaston TJEBBES
   eolephpCAS::logout(array("url"=>$_SERVER["SCRIPT_URI"]));
140 2 Gaston TJEBBES
}
141 2 Gaston TJEBBES
?>
142 2 Gaston TJEBBES
</pre>
143 2 Gaston TJEBBES
144 2 Gaston TJEBBES
Du coup dans ma page php: 
145 12 Lionel Morin
<pre>
146 2 Gaston TJEBBES
<?php Ce qui s'exécute ici n'est pas authentifié ?>
147 2 Gaston TJEBBES
<?php 
148 2 Gaston TJEBBES
cas_auth();
149 2 Gaston TJEBBES
?>
150 2 Gaston TJEBBES
<?php Ici on exécute que du code authentifié avec le user : eolephpCAS::getUser(); ?>
151 2 Gaston TJEBBES
</pre>
152 2 Gaston TJEBBES
153 8 Gérald Schwartzmann
154 8 Gérald Schwartzmann
h2. Cas n°2 : authentification avec récupération de données utilisateurs 
155 2 Gaston TJEBBES
156 16 Lionel Morin
Lors de l'authentification CAS, pour récupérer des données utilisateurs en parallèle, le fonctionnement est identique (au Cas n°1). 
157 2 Gaston TJEBBES
158 14 Lionel Morin
h3. Déconnexion centralisée 
159 2 Gaston TJEBBES
160 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ée, 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 
161 8 Gérald Schwartzmann
162 2 Gaston TJEBBES
<pre>
163 2 Gaston TJEBBES
eolephpCAS::eoleLogoutRequests(false). 
164 2 Gaston TJEBBES
</pre>
165 2 Gaston TJEBBES
166 2 Gaston TJEBBES
167 2 Gaston TJEBBES
h3. Récupération des informations utilisateurs 
168 2 Gaston TJEBBES
169 2 Gaston TJEBBES
<pre>
170 2 Gaston TJEBBES
$user_login = eolephpCAS::getUser();
171 2 Gaston TJEBBES
$user_details = eolephpCAS::getDetails();
172 2 Gaston TJEBBES
</pre>
173 2 Gaston TJEBBES
174 2 Gaston TJEBBES
Les détails utilisateurs sont renvoyés sous forme de Array php.
175 2 Gaston TJEBBES
Les données qu'ils contiennent dépendent du filtre sso configuré pour l'application.
176 2 Gaston TJEBBES
177 2 Gaston TJEBBES
h4. Configuration du filtre SSO
178 2 Gaston TJEBBES
179 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.
180 2 Gaston TJEBBES
181 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/
182 2 Gaston TJEBBES
183 2 Gaston TJEBBES
On renseigne "url" et "filtre" qui sont l'url et le filtre associés. 
184 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'.
185 2 Gaston TJEBBES
<pre>
186 2 Gaston TJEBBES
[democas]
187 2 Gaston TJEBBES
baseurl=/lechemin
188 2 Gaston TJEBBES
scheme=http
189 2 Gaston TJEBBES
addr=0/0
190 2 Gaston TJEBBES
typeaddr=ip
191 2 Gaston TJEBBES
filter=monfiltre
192 2 Gaston TJEBBES
</pre>
193 2 Gaston TJEBBES
194 2 Gaston TJEBBES
h5. Deuxième étape : configurer le fichier filtre monfiltre.ini 
195 2 Gaston TJEBBES
196 2 Gaston TJEBBES
<pre>
197 2 Gaston TJEBBES
[user]
198 2 Gaston TJEBBES
user=uid                
199 2 Gaston TJEBBES
[infos] 
200 2 Gaston TJEBBES
user_groups=user_groups
201 4 Gaston TJEBBES
typeadmin=typeadmin
202 2 Gaston TJEBBES
rne=rne
203 15 Lionel Morin
</pre>
204 2 Gaston TJEBBES
	
205 2 Gaston TJEBBES
Le filtre "user=uid" est obligatoire.
206 2 Gaston TJEBBES
207 2 Gaston TJEBBES
Autres propriétés que l'on peut récupérer et donc assigner dans monfiltre.ini:
208 2 Gaston TJEBBES
209 2 Gaston TJEBBES
    * cn=cn
210 2 Gaston TJEBBES
    * user_groups=user_groups
211 2 Gaston TJEBBES
    * employeeType=employeeType
212 2 Gaston TJEBBES
    * typeadmin=typeadmin
213 2 Gaston TJEBBES
    * pam=pam
214 2 Gaston TJEBBES
    * user=uid
215 2 Gaston TJEBBES
    * profil=profil
216 2 Gaston TJEBBES
    * classe=Divcod
217 2 Gaston TJEBBES
    * firstname=givenName
218 2 Gaston TJEBBES
    * lastname=sn
219 2 Gaston TJEBBES
    * email=mail
220 2 Gaston TJEBBES
    * ctemail=mail_acad
221 2 Gaston TJEBBES
    * fullname=displayName
222 2 Gaston TJEBBES
    * civil=codecivilite
223 2 Gaston TJEBBES
    * codeRNE=rne
224 2 Gaston TJEBBES
    * nomEtab=nom_etab
225 2 Gaston TJEBBES
    * typeEtab=typeEtab
226 2 Gaston TJEBBES
    * uid=secureid
227 2 Gaston TJEBBES
    * ENTEleveNivFormation=Meflcf
228 2 Gaston TJEBBES
    * ENTPersonProfils=profilcns 
229 2 Gaston TJEBBES
230 2 Gaston TJEBBES
(voir la documentation pour l'accès aux informations ldap)
231 2 Gaston TJEBBES
232 2 Gaston TJEBBES
A cette étape le serveur eole-sso nécessite d'être recharger pour prendre votre nouveau filtre en compte:
233 2 Gaston TJEBBES
234 2 Gaston TJEBBES
<pre>
235 2 Gaston TJEBBES
/etc/init.d/eole-sso restart
236 2 Gaston TJEBBES
</pre>
237 2 Gaston TJEBBES
238 2 Gaston TJEBBES
239 2 Gaston TJEBBES
h5. Troisième étape : récupérer les données dans le script PHP 
240 2 Gaston TJEBBES
241 2 Gaston TJEBBES
*Sous forme de XML*
242 2 Gaston TJEBBES
243 2 Gaston TJEBBES
Dans le script php appeler la méthode getCasXML() de la manièe suivante:
244 2 Gaston TJEBBES
<pre>
245 2 Gaston TJEBBES
eolephpCAS::getCasXML()
246 2 Gaston TJEBBES
</pre>
247 2 Gaston TJEBBES
248 2 Gaston TJEBBES
Ici, les données récupérées pour l'utilisateur courant sont :
249 2 Gaston TJEBBES
250 2 Gaston TJEBBES
<pre>
251 2 Gaston TJEBBES
<?xml version="1.0" encoding="UTF-8"?>
252 2 Gaston TJEBBES
<cas:serviceResponse xmlns:cas="http://www.yale.edu/tp/cas">
253 2 Gaston TJEBBES
    <cas:authenticationSuccess>
254 2 Gaston TJEBBES
        <cas:infos>
255 2 Gaston TJEBBES
            <cas:typeadmin>2</cas:typeadmin>
256 2 Gaston TJEBBES
            <cas:rne>1234567X</cas:rne>
257 2 Gaston TJEBBES
            <cas:user_groups>domainusers</cas:user_groups>
258 2 Gaston TJEBBES
            <cas:user_groups>professeurs</cas:user_groups>
259 2 Gaston TJEBBES
            <cas:user_groups>math</cas:user_groups>
260 2 Gaston TJEBBES
            </cas:infos>
261 2 Gaston TJEBBES
        <cas:user>
262 2 Gaston TJEBBES
            <cas:user>prof5e1</cas:user>
263 2 Gaston TJEBBES
        </cas:user>
264 2 Gaston TJEBBES
        
265 2 Gaston TJEBBES
      </cas:authenticationSuccess>
266 2 Gaston TJEBBES
</cas:serviceResponse>
267 2 Gaston TJEBBES
</pre>
268 2 Gaston TJEBBES
269 2 Gaston TJEBBES
270 2 Gaston TJEBBES
*Sous forme de tableau PHP*
271 2 Gaston TJEBBES
272 2 Gaston TJEBBES
Dans le script php appeler la méthode getDetails() de la manière suivante:
273 2 Gaston TJEBBES
<pre>
274 2 Gaston TJEBBES
eolephpCAS::getDetails()
275 2 Gaston TJEBBES
</pre>
276 2 Gaston TJEBBES
La méthode retourne un tableau associatif à plusieurs dimensions(tableau de tableaux).
277 2 Gaston TJEBBES
278 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.
279 2 Gaston TJEBBES
La clé du tableau contenu à la clé [user] est le nom donné dans notre exemple monfiltre.ini : "user="
280 2 Gaston TJEBBES
[user][user][0] contient l'uid.
281 2 Gaston TJEBBES
282 2 Gaston TJEBBES
Ici les données renvoyées pour l'utilisateur courant sont :
283 2 Gaston TJEBBES
284 2 Gaston TJEBBES
<pre>
285 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 ) ) 
286 2 Gaston TJEBBES
</pre>
287 2 Gaston TJEBBES
288 2 Gaston TJEBBES
289 2 Gaston TJEBBES
h4. Récupération des informations utilisateurs construites 
290 2 Gaston TJEBBES
291 2 Gaston TJEBBES
Il est possible de renvoyer des données utilisateurs autre que celle du ldap.
292 2 Gaston TJEBBES
Il est possible de fournir un script python au serveur SSO qui fournit des données plus complexes. 
293 2 Gaston TJEBBES
Exemple l'envoi de données sorties de la configuration du serveur dans un fichier rne.py (utilisé dans l'exemple):
294 2 Gaston TJEBBES
<pre>
295 2 Gaston TJEBBES
	def calc_info(user_infos):
296 2 Gaston TJEBBES
	    """ renvoie le rne de l'étab de l'uitilisateur """
297 1 Gaston TJEBBES
	    from creole.parsedico import parse_dico
298 11 Gérald Schwartzmann
	    return [parse_dico().get('numero_etab', 'renvide')]
299 11 Gérald Schwartzmann
</pre>
300 11 Gérald Schwartzmann
301 11 Gérald Schwartzmann
h2. Des applications externes
302 11 Gérald Schwartzmann
303 11 Gérald Schwartzmann
h3. Campus
304 11 Gérald Schwartzmann
305 11 Gérald Schwartzmann
<pre>
306 11 Gérald Schwartzmann
/usr/share/sso/app_filters/local_apps.ini
307 11 Gérald Schwartzmann
...
308 11 Gérald Schwartzmann
[campus]
309 11 Gérald Schwartzmann
baseurl=/LaureatsNet
310 11 Gérald Schwartzmann
scheme=https
311 11 Gérald Schwartzmann
addr=scolarite.net
312 11 Gérald Schwartzmann
typeaddr=dns
313 11 Gérald Schwartzmann
filter=attributs_campus
314 11 Gérald Schwartzmann
...
315 11 Gérald Schwartzmann
316 11 Gérald Schwartzmann
/usr/share/sso/app_filters/attributs_campus.ini
317 11 Gérald Schwartzmann
[utilisateur]
318 11 Gérald Schwartzmann
nom=sn
319 11 Gérald Schwartzmann
prenom=givenName
320 11 Gérald Schwartzmann
login=ENTPersonLogin
321 11 Gérald Schwartzmann
categories=ENTPersonProfils
322 11 Gérald Schwartzmann
dateNaissance=ENTPersonDateNaissance
323 11 Gérald Schwartzmann
codePostal=ENTPersonCodePostal
324 11 Gérald Schwartzmann
eleveClasses=ENTEleveClasses
325 11 Gérald Schwartzmann
uid=uid
326 11 Gérald Schwartzmann
rne=rne
327 11 Gérald Schwartzmann
</pre>
328 11 Gérald Schwartzmann
329 1 Gaston TJEBBES
L'Url d'accès est : https://scolarite.net/LaureatsNet/IdentificationCasEOLE<codeRNE>
330 1 Gaston TJEBBES
Doc en ligne : http://campuswiki.fr/index.php/Scolarite.net:CAS