Projet

Général

Profil

Wiki » Historique » Version 19

Lionel Morin, 12/10/2012 14:18

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 18 Lionel Morin
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 18 Lionel Morin
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 18 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 19 Lionel Morin
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é. 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 2 Gaston TJEBBES
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 2 Gaston TJEBBES
159 2 Gaston TJEBBES
h3. Récupération des informations utilisateurs 
160 2 Gaston TJEBBES
161 2 Gaston TJEBBES
<pre>
162 2 Gaston TJEBBES
$user_login = eolephpCAS::getUser();
163 2 Gaston TJEBBES
$user_details = eolephpCAS::getDetails();
164 2 Gaston TJEBBES
</pre>
165 2 Gaston TJEBBES
166 2 Gaston TJEBBES
Les détails utilisateurs sont renvoyés sous forme de Array php.
167 2 Gaston TJEBBES
Les données qu'ils contiennent dépendent du filtre sso configuré pour l'application.
168 2 Gaston TJEBBES
169 2 Gaston TJEBBES
h4. Configuration du filtre SSO
170 2 Gaston TJEBBES
171 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.
172 2 Gaston TJEBBES
173 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/
174 2 Gaston TJEBBES
175 2 Gaston TJEBBES
On renseigne "url" et "filtre" qui sont l'url et le filtre associés. 
176 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'.
177 2 Gaston TJEBBES
<pre>
178 2 Gaston TJEBBES
[democas]
179 2 Gaston TJEBBES
baseurl=/lechemin
180 2 Gaston TJEBBES
scheme=http
181 2 Gaston TJEBBES
addr=0/0
182 2 Gaston TJEBBES
typeaddr=ip
183 2 Gaston TJEBBES
filter=monfiltre
184 2 Gaston TJEBBES
</pre>
185 2 Gaston TJEBBES
186 2 Gaston TJEBBES
h5. Deuxième étape : configurer le fichier filtre monfiltre.ini 
187 2 Gaston TJEBBES
188 2 Gaston TJEBBES
<pre>
189 2 Gaston TJEBBES
[user]
190 2 Gaston TJEBBES
user=uid                
191 2 Gaston TJEBBES
[infos] 
192 2 Gaston TJEBBES
user_groups=user_groups
193 4 Gaston TJEBBES
typeadmin=typeadmin
194 2 Gaston TJEBBES
rne=rne
195 15 Lionel Morin
</pre>
196 2 Gaston TJEBBES
	
197 2 Gaston TJEBBES
Le filtre "user=uid" est obligatoire.
198 2 Gaston TJEBBES
199 2 Gaston TJEBBES
Autres propriétés que l'on peut récupérer et donc assigner dans monfiltre.ini:
200 2 Gaston TJEBBES
201 2 Gaston TJEBBES
    * cn=cn
202 2 Gaston TJEBBES
    * user_groups=user_groups
203 2 Gaston TJEBBES
    * employeeType=employeeType
204 2 Gaston TJEBBES
    * typeadmin=typeadmin
205 2 Gaston TJEBBES
    * pam=pam
206 2 Gaston TJEBBES
    * user=uid
207 2 Gaston TJEBBES
    * profil=profil
208 2 Gaston TJEBBES
    * classe=Divcod
209 2 Gaston TJEBBES
    * firstname=givenName
210 2 Gaston TJEBBES
    * lastname=sn
211 2 Gaston TJEBBES
    * email=mail
212 2 Gaston TJEBBES
    * ctemail=mail_acad
213 2 Gaston TJEBBES
    * fullname=displayName
214 2 Gaston TJEBBES
    * civil=codecivilite
215 2 Gaston TJEBBES
    * codeRNE=rne
216 2 Gaston TJEBBES
    * nomEtab=nom_etab
217 2 Gaston TJEBBES
    * typeEtab=typeEtab
218 2 Gaston TJEBBES
    * uid=secureid
219 2 Gaston TJEBBES
    * ENTEleveNivFormation=Meflcf
220 2 Gaston TJEBBES
    * ENTPersonProfils=profilcns 
221 2 Gaston TJEBBES
222 2 Gaston TJEBBES
(voir la documentation pour l'accès aux informations ldap)
223 2 Gaston TJEBBES
224 2 Gaston TJEBBES
A cette étape le serveur eole-sso nécessite d'être recharger pour prendre votre nouveau filtre en compte:
225 2 Gaston TJEBBES
226 2 Gaston TJEBBES
<pre>
227 2 Gaston TJEBBES
/etc/init.d/eole-sso restart
228 2 Gaston TJEBBES
</pre>
229 2 Gaston TJEBBES
230 2 Gaston TJEBBES
231 2 Gaston TJEBBES
h5. Troisième étape : récupérer les données dans le script PHP 
232 2 Gaston TJEBBES
233 2 Gaston TJEBBES
*Sous forme de XML*
234 2 Gaston TJEBBES
235 2 Gaston TJEBBES
Dans le script php appeler la méthode getCasXML() de la manièe suivante:
236 2 Gaston TJEBBES
<pre>
237 2 Gaston TJEBBES
eolephpCAS::getCasXML()
238 2 Gaston TJEBBES
</pre>
239 2 Gaston TJEBBES
240 2 Gaston TJEBBES
Ici, les données récupérées pour l'utilisateur courant sont :
241 2 Gaston TJEBBES
242 2 Gaston TJEBBES
<pre>
243 2 Gaston TJEBBES
<?xml version="1.0" encoding="UTF-8"?>
244 2 Gaston TJEBBES
<cas:serviceResponse xmlns:cas="http://www.yale.edu/tp/cas">
245 2 Gaston TJEBBES
    <cas:authenticationSuccess>
246 2 Gaston TJEBBES
        <cas:infos>
247 2 Gaston TJEBBES
            <cas:typeadmin>2</cas:typeadmin>
248 2 Gaston TJEBBES
            <cas:rne>1234567X</cas:rne>
249 2 Gaston TJEBBES
            <cas:user_groups>domainusers</cas:user_groups>
250 2 Gaston TJEBBES
            <cas:user_groups>professeurs</cas:user_groups>
251 2 Gaston TJEBBES
            <cas:user_groups>math</cas:user_groups>
252 2 Gaston TJEBBES
            </cas:infos>
253 2 Gaston TJEBBES
        <cas:user>
254 2 Gaston TJEBBES
            <cas:user>prof5e1</cas:user>
255 2 Gaston TJEBBES
        </cas:user>
256 2 Gaston TJEBBES
        
257 2 Gaston TJEBBES
      </cas:authenticationSuccess>
258 2 Gaston TJEBBES
</cas:serviceResponse>
259 2 Gaston TJEBBES
</pre>
260 2 Gaston TJEBBES
261 2 Gaston TJEBBES
262 2 Gaston TJEBBES
*Sous forme de tableau PHP*
263 2 Gaston TJEBBES
264 2 Gaston TJEBBES
Dans le script php appeler la méthode getDetails() de la manière suivante:
265 2 Gaston TJEBBES
<pre>
266 2 Gaston TJEBBES
eolephpCAS::getDetails()
267 2 Gaston TJEBBES
</pre>
268 2 Gaston TJEBBES
La méthode retourne un tableau associatif à plusieurs dimensions(tableau de tableaux).
269 2 Gaston TJEBBES
270 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.
271 2 Gaston TJEBBES
La clé du tableau contenu à la clé [user] est le nom donné dans notre exemple monfiltre.ini : "user="
272 2 Gaston TJEBBES
[user][user][0] contient l'uid.
273 2 Gaston TJEBBES
274 2 Gaston TJEBBES
Ici les données renvoyées pour l'utilisateur courant sont :
275 2 Gaston TJEBBES
276 2 Gaston TJEBBES
<pre>
277 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 ) ) 
278 2 Gaston TJEBBES
</pre>
279 2 Gaston TJEBBES
280 2 Gaston TJEBBES
281 2 Gaston TJEBBES
h4. Récupération des informations utilisateurs construites 
282 2 Gaston TJEBBES
283 2 Gaston TJEBBES
Il est possible de renvoyer des données utilisateurs autre que celle du ldap.
284 2 Gaston TJEBBES
Il est possible de fournir un script python au serveur SSO qui fournit des données plus complexes. 
285 2 Gaston TJEBBES
Exemple l'envoi de données sorties de la configuration du serveur dans un fichier rne.py (utilisé dans l'exemple):
286 2 Gaston TJEBBES
<pre>
287 2 Gaston TJEBBES
	def calc_info(user_infos):
288 2 Gaston TJEBBES
	    """ renvoie le rne de l'étab de l'uitilisateur """
289 1 Gaston TJEBBES
	    from creole.parsedico import parse_dico
290 11 Gérald Schwartzmann
	    return [parse_dico().get('numero_etab', 'renvide')]
291 11 Gérald Schwartzmann
</pre>
292 11 Gérald Schwartzmann
293 11 Gérald Schwartzmann
h2. Des applications externes
294 11 Gérald Schwartzmann
295 11 Gérald Schwartzmann
h3. Campus
296 11 Gérald Schwartzmann
297 11 Gérald Schwartzmann
<pre>
298 11 Gérald Schwartzmann
/usr/share/sso/app_filters/local_apps.ini
299 11 Gérald Schwartzmann
...
300 11 Gérald Schwartzmann
[campus]
301 11 Gérald Schwartzmann
baseurl=/LaureatsNet
302 11 Gérald Schwartzmann
scheme=https
303 11 Gérald Schwartzmann
addr=scolarite.net
304 11 Gérald Schwartzmann
typeaddr=dns
305 11 Gérald Schwartzmann
filter=attributs_campus
306 11 Gérald Schwartzmann
...
307 11 Gérald Schwartzmann
308 11 Gérald Schwartzmann
/usr/share/sso/app_filters/attributs_campus.ini
309 11 Gérald Schwartzmann
[utilisateur]
310 11 Gérald Schwartzmann
nom=sn
311 11 Gérald Schwartzmann
prenom=givenName
312 11 Gérald Schwartzmann
login=ENTPersonLogin
313 11 Gérald Schwartzmann
categories=ENTPersonProfils
314 11 Gérald Schwartzmann
dateNaissance=ENTPersonDateNaissance
315 11 Gérald Schwartzmann
codePostal=ENTPersonCodePostal
316 11 Gérald Schwartzmann
eleveClasses=ENTEleveClasses
317 11 Gérald Schwartzmann
uid=uid
318 11 Gérald Schwartzmann
rne=rne
319 11 Gérald Schwartzmann
</pre>
320 11 Gérald Schwartzmann
321 1 Gaston TJEBBES
L'Url d'accès est : https://scolarite.net/LaureatsNet/IdentificationCasEOLE<codeRNE>
322 1 Gaston TJEBBES
Doc en ligne : http://campuswiki.fr/index.php/Scolarite.net:CAS