2
0

apiSessionAuthenticator.class.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. class apiSessionAuthenticator
  3. {
  4. private static $cookieFile;
  5. /**
  6. * Initialiser les configurations pour l'authentification
  7. */
  8. public static function init($loginUrl, $apiBaseUrl, $email, $password)
  9. {
  10. // Fichier temporaire pour stocker les cookies de session
  11. self::$cookieFile = tempnam(sys_get_temp_dir(), 'cookie');
  12. }
  13. public static function checkSession()
  14. {
  15. if (session::isConnect()) {
  16. // L'utilisateur est connecté
  17. return json_encode(['authenticated' => true, 'user' => session::getName()]);
  18. } else {
  19. // L'utilisateur n'est pas connecté
  20. return json_encode(['authenticated' => false]);
  21. }
  22. }
  23. /**
  24. * Authentifier l'utilisateur et maintenir la session PHP via un cookie
  25. */
  26. public static function authenticate(array $_input) {
  27. $ch = curl_init();
  28. $data = [
  29. 'email' => $_input["email"],
  30. 'password' => $_input["password"],
  31. ];
  32. curl_setopt($ch, CURLOPT_URL, API_AUTHENT);
  33. curl_setopt($ch, CURLOPT_POST, true);
  34. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
  35. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  36. // Activer le suivi des redirections
  37. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  38. // Désactiver la vérification SSL (à ne pas utiliser en production)
  39. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  40. // Gérer les cookies pour maintenir la session
  41. curl_setopt($ch, CURLOPT_COOKIEJAR, self::$cookieFile);
  42. curl_setopt($ch, CURLOPT_COOKIEFILE, self::$cookieFile);
  43. // Exécuter la requête cURL
  44. $response = curl_exec($ch);
  45. // Fermer la session cURL
  46. curl_close($ch);
  47. return $response;
  48. }
  49. /**
  50. * Faire une requête API authentifiée avec la session PHP active
  51. */
  52. public static function makeAuthenticatedRequest($endpoint, $method = 'GET', $data = [])
  53. {
  54. $ch = curl_init();
  55. $url = DOMAIN_API . $endpoint;
  56. curl_setopt($ch, CURLOPT_URL, $url . "/");
  57. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  58. // Utiliser les cookies pour maintenir la session
  59. curl_setopt($ch, CURLOPT_COOKIEFILE, self::$cookieFile);
  60. // Configuration des méthodes GET/POST/PUT/DELETE
  61. if ($method === 'POST') {
  62. curl_setopt($ch, CURLOPT_POST, true);
  63. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
  64. curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
  65. } elseif ($method === 'PUT') {
  66. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
  67. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
  68. curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
  69. } elseif ($method === 'DELETE') {
  70. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
  71. }
  72. $response = curl_exec($ch);
  73. curl_close($ch);
  74. if ($response === false) {
  75. throw new Exception('Erreur lors de la requête API.');
  76. }
  77. return json_decode($response, true);
  78. }
  79. /**
  80. * Déconnexion (facultatif)
  81. */
  82. public static function logout()
  83. {
  84. $ch = curl_init();
  85. curl_setopt($ch, CURLOPT_URL, API_LOGOUT);
  86. curl_setopt($ch, CURLOPT_POST, true);
  87. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  88. curl_setopt($ch, CURLOPT_COOKIEFILE, self::$cookieFile);
  89. $response = curl_exec($ch);
  90. curl_close($ch);
  91. // Supprimer le fichier cookie après déconnexion
  92. unlink(self::$cookieFile);
  93. }
  94. }