Advertisement
carlosalvet

api-apiclient.local-apiclient.php

Sep 27th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.24 KB | None | 0 0
  1. <?php
  2.  
  3. class APIClient {
  4.     function __construct() {
  5.         $this->url = "http://apiserver.local/usuario/autenticar";
  6.     }
  7.  
  8.     function auth() {}
  9.  
  10.     private function autenticar() {
  11.         $ch = curl_init($this->url);
  12.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, True);
  13.         curl_setopt($ch, CURLOPT_HEADER, True);
  14.         $response = curl_exec($ch);
  15.         curl_close($ch);
  16.         return $response;
  17.     }
  18.  
  19.     private function solicitar_autenticacion() {
  20.         $response = $this->autenticar();
  21.         $regex = "/WWW-Authenticate(.)+\n/";
  22.         preg_match($regex, $response, $coincidencias);
  23.         $auth_digest = str_replace("WWW-Authenticate: Digest ", "", $coincidencias[0]);
  24.         $array_auth = explode(",", $auth_digest);
  25.         return $array_auth;
  26.     }
  27.  
  28.     private function parsear_respuesta_autenticacion($array_auth) {
  29.         $this->terms_auth = array();
  30.         foreach($array_auth as $term) {
  31.             list($name, $value) = explode("=", $term);
  32.             $this->terms_auth[$name] = str_replace(['"', "\r\n"], "", $value);
  33.         }
  34.     }
  35.  
  36.     private function solicitar_autorizacion($username, $password) {
  37.         $A1 = hash('md5', "$username:{$this->terms_auth['realm']}:$password");
  38.         $A2 = "POST:/usuario/autenticar";
  39.         $info_extra_header= hash('md5', "$A1:{$this->terms_auth['nonce']}:$A2");
  40.         $string = 'Authorization: Digest username="%s",realm="%s",nonce="%s",opaque="%s",uri="%s",response="%s"';
  41.         $request = sprintf(
  42.             $string,
  43.             $username,
  44.             $this->terms_auth['realm'],
  45.             $this->terms_auth['nonce'],
  46.             $this->terms_auth['opaque'],  
  47.             '/usuario/autenticar',
  48.             $info_extra_header
  49.         );
  50. #        return $this->acceder_a_recurso_restringido($request);  # ¿solucion? Agregada 5/9 carlos. fallo pantalla en blanco
  51.        return $request;
  52.     }
  53.  
  54.     private function acceder_a_recurso_restringido($request) {
  55.         $ch = curl_init($this->url);
  56.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, True);
  57.         curl_setopt($ch, CURLOPT_HTTPHEADER, [$request]);
  58.         $response = curl_exec($ch);
  59.         curl_close($ch);
  60.         return $response;
  61.     }
  62.    
  63.     function autorizar($url, $username, $password) {
  64.         $this->url = $url;
  65.         $array_auth = $this->solicitar_autenticacion();
  66.         $this->parsear_respuesta_autenticacion($array_auth);
  67.         $request = $this->solicitar_autorizacion($username, $password);
  68.     }
  69.  
  70.     function get($url) {
  71.         $str = "nonce=a1b2c3,opaque=abc123";
  72.         $request = "SessionID: $str";
  73.         $ch = curl_init($url);
  74.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, True);
  75.         curl_setopt($ch, CURLOPT_HTTPHEADER, [$request]);
  76.         $response = curl_exec($ch);
  77.         curl_close($ch);
  78.         return $response;
  79.     }
  80.  
  81. # comentado: 6/9 eugenia: por si se llega a necesitar
  82. #    function get_restricted($url) { $this->url = $url;
  83. #        $string = 'SessionID: nonce="%s",opaque="%s"';
  84. #        $request = sprintf(
  85. #            $string,
  86. #            $this->terms_auth['nonce'],
  87. #            $this->terms_auth['opaque']
  88. #        );
  89.  
  90. #        return $this->acceder_a_recurso_restringido($request);
  91. #    }
  92. }
  93.  
  94.  
  95. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement