php - Securing Rest API with Basic Authentication -


i'm making rest api, called curl using ssl http (example: curl http://domain.com/api/v1/hello -x post -u my@email.com:1x5a6s9x4q1z2 -d post="data").

this @ moment - best , most-secured way it?

i'm concerned "hello" class - needs called if login valid.

<?php require_once '../rest.class.php';  class api extends rest {     protected $user;      public function __construct($request) {         parent::__construct($request);          $this->db = new pdo('mysql:host=******;dbname=******;charset=utf8', '******', '******');         $this->db->setattribute(pdo::attr_errmode, pdo::errmode_warning);         $this->db->setattribute(pdo::attr_emulate_prepares, false);          if(isset($_server['php_auth_user']) && isset($_server['php_auth_pw'])) {             $dbh = $this->db;              $sth = $dbh->prepare("select id user email=:email , api=:api limit 0,1");             $sth->execute(array(                 'email' => $_server['php_auth_user'],                 'api' => $_server['php_auth_pw']             ));              if($sth->rowcount() == 1) {                 while($row = $sth->fetch()) {                     $this->user = $row["id"];                 }             }             else {                 throw new exception('invalid api login credentials.');             }         }         else {             throw new exception('please provide valid api login credentials.');         }     }      protected function hello() {         if($this->method == 'post') {             $response = array();              $response['user'] = $this->user;              $response['method'] = $this->method;              return $response;         }         else {             return "only accepts post requests";         }     } }  try {     $api = new api($_request['request']);      echo $api->processapi(); } catch (exception $e) {     echo json_encode(array(         'error' => $e->getmessage()     )); } ?> 

you can create new function validate user

function validate_user() {  } , call function in hello   protected function hello()  {  if($this->validate_user())  {    if($this->method == 'post') {             $response = array();              $response['user'] = $this->user;              $response['method'] = $this->method;              return $response;         }         else {             return "only accepts post requests";         }  } } 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -