Subversion Repositories Applications.annuaire

Rev

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

Rev Author Line No. Line
579 mathias 1
<?php
2
 
3
require "AuthPartner.php";
4
 
5
/**
599 mathias 6
 * Permet de se connecter à l'annuaire de Tela Botanica à l'aide d'un compte eRecolnat
7
 * à travers le CAS de Brice (https://cas.recolnat.org)
8
 *
9
 * // POST https://cas.recolnat.org/v1/tickets -H 'Content-Type: application/x-www-form-urlencoded' --data 'username=mchouet&password=mat87cho'
10
 * // => 400 ou 201
11
 * => GET https://api.recolnat.org/erecolnat/v1/users/login/mchouet
579 mathias 12
 */
599 mathias 13
class AuthPartnerRecolnat extends AuthPartner {
579 mathias 14
 
15
	public function verifierAcces($login, $password) {
583 mathias 16
		$login = urlencode($login); // pour les espaces dans le nom d'utilisateur
17
		$password = urlencode($password);
599 mathias 18
		$url = "https://cas.recolnat.org/v1/tickets";
579 mathias 19
 
20
		$curl = curl_init();
21
		curl_setopt($curl, CURLOPT_URL, $url);
22
		curl_setopt($curl, CURLOPT_POST, true);
599 mathias 23
		curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
580 mathias 24
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
599 mathias 25
		curl_setopt($curl, CURLOPT_POSTFIELDS, "username=$login&password=$password");
579 mathias 26
		$res = curl_exec($curl);
599 mathias 27
		$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
579 mathias 28
		curl_close($curl);
29
 
599 mathias 30
		//var_dump($httpCode); exit;
31
		if ($httpCode == 201) { // un ticket a été créé, l'utilisateur existe
32
			// récupération des infos utilisateur
33
			// (attention, répond même si l'authentification a échoué !)
34
			$url = "https://api.recolnat.org/erecolnat/v1/users/login/$login";
35
			$curl = curl_init();
36
			curl_setopt($curl, CURLOPT_URL, $url);
37
			curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
38
			$res = curl_exec($curl);
39
			curl_close($curl);
40
 
41
			$res = json_decode($res, true);
42
			//var_dump($res); exit;
43
			if ($res != null) {
44
				$this->jetonPartenaire = $res; // pas vraiment un jeton...
45
				// stockage pour traitement dans les autres méthodes
46
				$this->data = $res;
47
				//var_dump($this->data); exit;
48
				if ( !empty($this->data['email'])) {
49
					//var_dump($this->data['email']);
50
					return true;
51
				}
579 mathias 52
			}
53
		}
54
		return false;
55
	}
56
 
580 mathias 57
	protected function getNomPartenaire() {
599 mathias 58
		return "recolnat";
580 mathias 59
	}
579 mathias 60
 
583 mathias 61
	public function getCourriel() {
580 mathias 62
		return $this->data['email'];
579 mathias 63
	}
64
 
580 mathias 65
	protected function getId() {
599 mathias 66
		// le "login" est le "username" dans eRecolnat, mais il y a aussi
67
		// un "user_id" et un "user_uuid"... @TODO valider cette stratégie
68
		return $this->data['login'];
580 mathias 69
	}
579 mathias 70
 
580 mathias 71
	protected function getValeursProfilPartenaire() {
72
		return array(
599 mathias 73
			// @WARNING "firstname" et "lastname" semblent inversés
74
			'nom' => $this->data['firstname'],
75
			'prenom' => $this->data['lastname'],
580 mathias 76
			'email' => $this->data['email'],
599 mathias 77
			'pseudo' => $this->data['login'] // @TODO valider cette stratégie
580 mathias 78
		);
579 mathias 79
	}
80
 
580 mathias 81
	/*public function getTimestampMajPartenaire() {
82
		return 420000000000;
83
	}*/
579 mathias 84
}