Subversion Repositories Applications.papyrus

Rev

Rev 2154 | Rev 2156 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2154 aurelien 1
<?php
2
 
3
// Remplacement de apache_request_headers pour les vieux serveurs
4
// http://stackoverflow.com/questions/2916232/call-to-undefined-function-apache-request-headers
5
if(!function_exists('apache_request_headers')) {
6
	function apache_request_headers() {
7
		// Based on: http://www.iana.org/assignments/message-headers/message-headers.xml#perm-headers
8
		$arrCasedHeaders = array(
9
				// HTTP
10
				'Dasl'             => 'DASL',
11
				'Dav'              => 'DAV',
12
				'Etag'             => 'ETag',
13
				'Mime-Version'     => 'MIME-Version',
14
				'Slug'             => 'SLUG',
15
				'Te'               => 'TE',
16
				'Www-Authenticate' => 'WWW-Authenticate',
17
				// MIME
18
				'Content-Md5'      => 'Content-MD5',
19
				'Content-Id'       => 'Content-ID',
20
				'Content-Features' => 'Content-features',
21
		);
22
		$arrHttpHeaders = array();
23
 
24
		foreach($_SERVER as $strKey => $mixValue) {
25
			if('HTTP_' !== substr($strKey, 0, 5)) {
26
				continue;
27
			}
28
 
29
			$strHeaderKey = strtolower(substr($strKey, 5));
30
 
31
			if(0 < substr_count($strHeaderKey, '_')) {
32
				$arrHeaderKey = explode('_', $strHeaderKey);
33
				$arrHeaderKey = array_map('ucfirst', $arrHeaderKey);
34
				$strHeaderKey = implode('-', $arrHeaderKey);
35
			}
36
			else {
37
				$strHeaderKey = ucfirst($strHeaderKey);
38
			}
39
 
40
			if(array_key_exists($strHeaderKey, $arrCasedHeaders)) {
41
				$strHeaderKey = $arrCasedHeaders[$strHeaderKey];
42
			}
43
 
44
			$arrHttpHeaders[$strHeaderKey] = $mixValue;
45
		}
46
		return $arrHttpHeaders;
47
	}
48
}
49
 
50
class identificationSso {
51
 
2155 mathias 52
	// Attention bien v�rifier la pr�sence des variables suivantes :
2154 aurelien 53
	// IDEN_UTILISE_SSO, IDEN_URL_SSO, IDEN_HEADER_SSO, IDEN_COOKIE_SSO
54
	// dans le fichier iden_config.inc.php
55
	private $cookie_tentative_identification = "";
2155 mathias 56
	/** Une tentative par minute pour s'identifier suffit largement */
2154 aurelien 57
	private $delai_tentative_identification = 60;
58
 
59
	private $auth_header = 'Authorization';
60
 
61
	private $annuaire_url = '';
62
 
63
	private $bdd_annuaire = '';
64
	private $table_annuaire = '';
65
	private $champ_login = '';
66
	private $champ_mdp = '';
67
 
68
	private $communs_papyrus = null;
69
 
70
	public function __construct() {
71
		$this->communs_papyrus = $GLOBALS['_GEN_commun'];
72
 
73
		$this->cookie_tentative_identification = IDEN_COOKIE_SSO;
74
		$this->auth_header = IDEN_HEADER_SSO;
75
		$this->annuaire_url = IDEN_URL_SSO;
76
 
2155 mathias 77
		// c'est moche mais je n'ai pas trouv� plus simple pour r�cuperer la table annuaire en cours
78
		// d'utilisation pour le site actuel (la bdd annuaire est la derni�re partie du dsn)
2154 aurelien 79
		$dsn_annuaire = $this->communs_papyrus['info_auth_bdd']->gsab_dsn;
80
		$dsn_annuaire = explode('/', $dsn_annuaire);
81
		$this->bdd_annuaire = end($dsn_annuaire);
82
 
83
		$this->table_annuaire = $this->communs_papyrus['info_auth_bdd']->gsab_nom_table;
84
		$this->champ_login = $this->communs_papyrus['info_auth_bdd']->gsab_nom_champ_login;
85
		$this->champ_mdp = $this->communs_papyrus['info_auth_bdd']->gsab_nom_champ_mdp;
86
	}
87
 
88
	function getToken() {
89
		// Premier essai, dans le header
90
		$headers = @apache_request_headers();
91
		$token = !empty($headers['Authorization']) ? $headers['Authorization'] : null;
2155 mathias 92
		// Eventuellement, le jeton a pu �tre pass� dans un header non standard, comme dans
93
		// le cas o� le header Authorization est supprim� par le mod cgi d'apache
94
		// Dans ce cas l� on v�rifie aussi dans un header alternatif si celui ci a �t� renseign�
2154 aurelien 95
		if($token == null && $this->auth_header != 'Authorization') {
96
			$token = !empty($headers[$this->auth_header]) ? $headers[$this->auth_header] : null;
97
		}
98
 
99
		// Sinon dans $_REQUEST ?
100
		if($token == null) {
101
			$token = !empty($_REQUEST['Authorization']) ? $_REQUEST['Authorization'] : null;
102
		}
103
 
104
		// Sinon dans $_COOKIE ?
105
		if($token == null) {
106
			$token = !empty($_COOKIE['tb_auth']) ? $_COOKIE['tb_auth'] : null;
107
		}
108
 
109
		return $token;
110
	}
111
 
112
	public function decoderToken($token) {
113
		$token_parts = explode('.', $token);
114
		return json_decode(base64_decode($token_parts[1]), true);
115
	}
116
 
117
	// http://stackoverflow.com/questions/1251582/beautiful-way-to-remove-get-variables-with-php?lq=1
118
	function supprimerUrlVar($url, $var) {
119
		 return rtrim(preg_replace('/([?&])'.$var.'=[^&]+(&|$)/','$1',$url), '&?');
120
	}
121
 
2155 mathias 122
	function connecterEtRediriger() {
2154 aurelien 123
		$url_redirect = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
124
		$params = 'login='.$_POST['username'].'&password='.$_POST['password'].'&redirect_url='.urlencode($url_redirect);
125
		$connexion_url = $this->annuaire_url."connexion?".$params;
126
 
127
		header('Location: '.$connexion_url);
128
		exit;
129
	}
130
 
131
	function deconnecterEtRediriger() {
132
		$url_redirect = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
133
		$url_redirect = $this->supprimerUrlVar($url_redirect, 'logout');
134
 
135
		$deconnexion_url = $this->annuaire_url."deconnexion?".'redirect_url='.urlencode($url_redirect);
136
 
137
		header('Location: '.$deconnexion_url);
138
		exit;
139
	}
140
 
141
	function verifierIdentiteEtRediriger() {
2155 mathias 142
		// si on fait autre chose qu'un GET, on ne vérifie pas l'identité, car
143
		// cela conduirait à une redirection en GET (avec le jeton), qui
144
		// supprimerait les données du corps de la requête
145
		if ($_SERVER['REQUEST_METHOD'] == "GET") {
146
			if(empty($_COOKIE['sso_tentative_identification'])) {
2154 aurelien 147
 
2155 mathias 148
				if($this->communs_papyrus['pear_auth']->getAuth()) {
2154 aurelien 149
 
2155 mathias 150
					$cookie_persistant_nom = session_name().'-memo';
151
					$cookie_utilisateur_nom = session_name().'-utilisateur';
152
 
153
					// Si l'utilisateur est d�j� connect� par pear
154
					// on fait tout de m�me une op�ration de logout
155
					// pour coordonner la d�connection depuis une autre application
156
					$this->communs_papyrus['pear_auth']->logout();
157
					// Destruction du cookie de session de Papyrus : est ce utile?
158
					setcookie(session_name(), session_id(), time()-3600, '/');
159
					// Destruction du cookie de permanence de l'identitification de Papyrus
160
					setcookie($cookie_persistant_nom, '', time()-3600, '/');
161
					setcookie($cookie_utilisateur_nom, '', time()-3600, '/');
2154 aurelien 162
				}
2155 mathias 163
 
164
				$url_redirect = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
165
				$url = $this->annuaire_url."identite?redirect_url=".urlencode($url_redirect);
166
 
167
				// une tentative toutes les $this->delai_tentative_identification
168
				setcookie($this->cookie_tentative_identification, 1, time() + $this->delai_tentative_identification, '/');
169
 
170
				header('Location: '.$url);
171
 
172
			} else {
173
				$jeton = $this->getToken();
174
 
175
				if($jeton != null) {
176
					// Verification du jeton aupres de l'annuaire
177
					$valide = $this->verifierToken($jeton);
178
					if ($valide === true) {
179
						$jeton_decode = $this->decoderToken($jeton);
180
 
181
						// R�cup�ration du mot de passe pour remplir les infos de l'objet PEAR Auth
182
						$requete =  'SELECT '.$this->champ_mdp.' '.
183
								'FROM '.$this->bdd_annuaire.'.'.$this->table_annuaire.' '.
184
								'WHERE '.$this->champ_login.' = "'.$jeton_decode['sub'].'" ';
185
 
186
						// TODO: normalement �a n'est jamais le cas mais que fait t'on si l'utilisateur n'existe pas
187
						// dans notre base de donn�es ? (au pire il ne sera pas connect�)
188
 
189
						$this->communs_papyrus['pear_auth']->username = $jeton_decode['sub'];
190
						$this->communs_papyrus['pear_auth']->password = $this->communs_papyrus['pear_db']->getOne($requete);
191
 
192
						// Le mot de passe est d�j� crypt� dans la bdd donc il faut indiquer � pear de ne pas le re crytper
193
						if (isset($this->communs_papyrus['pear_auth']->storage_options)) {
194
							$this->communs_papyrus['pear_auth']->storage_options['cryptType'] = 'none';
195
						}
196
						if (isset($this->communs_papyrus['pear_auth']->storage->options)) {
197
							$this->communs_papyrus['pear_auth']->storage->options['cryptType'] = 'none';
198
						}
199
					}
2154 aurelien 200
				}
201
			}
202
		}
203
	}
2155 mathias 204
 
205
	/**
206
	 * Vérifie un jeton auprès de l'annuaire
207
	 */
208
	protected function verifierToken($token) {
209
		$verificationServiceURL = $this->annuaire_url . "verifytoken";
210
		$verificationServiceURL .= "?token=" . $token;
211
 
212
		$info = file_get_contents($verificationServiceURL);
213
		$info = json_decode($info, true);
214
 
215
		return ($info === true);
216
	}
2154 aurelien 217
}
218
?>