基本/摘要式身份验证无效 - CI REST服务器(Phil Sturgeon / Chris Kacerguis)

时间:2018-02-05 17:39:17

标签: php rest codeigniter authentication

因此,正如标题所述,我无法使用HTTP身份验证来使用我使用codeigniter-restserver(https://github.com/chriskacerguis/codeigniter-restserver)创建的CI REST服务器。

无论我发送什么用户/通过组合,我总是得到一个回复​​ - 这不应该是这里的情况。我已经尝试了基本和摘要身份验证,使用config auth源和库auth源无济于事。我完全不知道什么是错的,任何帮助都会受到赞赏。

Rest.php

$config['rest_auth'] = 'digest';
$config['auth_source'] = '';
$config['rest_valid_logins'] = ['admin' => '1234', 'hello' => 'world'];

测试控制器

defined('BASEPATH') OR exit('No direct script access allowed');

require(APPPATH.'libraries\REST_Controller.php');
use Restserver\Libraries\REST_Controller;

class LogTest extends REST_Controller{

function __construct(){
    parent::__construct();
}

public function index_get()
{
    // $this->load->view('welcome_message');
    $this->response([
        'status' => TRUE,
        'message' => "Successfully hit API"
    ], 200);
}

}

API客户端

    // create curl resource 
    $ch = curl_init(); 

    // set url 
    curl_setopt($ch, CURLOPT_URL, "http://localhost/POSOrder/index.php/LogTest"); 

    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
    curl_setopt($ch, CURLOPT_MAXREDIRS, 1);
    curl_setopt($ch, CURLOPT_HEADER, TRUE);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
    curl_setopt($ch, CURLOPT_TIMEOUT, 45);

    //Set authentication parameters
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
    curl_setopt($ch, CURLOPT_USERPWD, "admin" . ":" . "123");

    //return the transfer as a string 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

    // $output contains the output string 
    $output = curl_exec($ch); 

    echo "<pre>";
    echo $output;
    echo "</pre>";

    // close curl resource to free up system resources 
    curl_close($ch);          

这是输出

HTTP/1.1 401 Unauthorized
Date: Mon, 05 Feb 2018 17:27:57 GMT
Server: Apache/2.4.23 (Win32) OpenSSL/1.0.2h PHP/5.6.24
X-Powered-By: PHP/5.6.24
WWW-Authenticate: Digest realm="REST API", qop="auth", nonce="5a78941dd9f83", opaque="aba3d4b49c454e1974970e7b5514b001"
Transfer-Encoding: chunked
Content-Type: text/html; charset=UTF-8

HTTP/1.1 200 OK
Date: Mon, 05 Feb 2018 17:27:57 GMT
Server: Apache/2.4.23 (Win32) OpenSSL/1.0.2h PHP/5.6.24
X-Powered-By: PHP/5.6.24
Content-Length: 48
Content-Type: application/json; charset=utf-8

{"status":true,"message":"Successfully hit API"}

就像我说的那样,我使用了错误的凭据,但我还是得到了回应。任何帮助都会非常感激,TIA。

1 个答案:

答案 0 :(得分:0)

我整个上午一直在努力解决同样的问题。 我在github上发现了许多看起来similar to this one的问题。

看起来已在here中解决了。

但是通过作曲家进行更新并没有帮助,我也没有在“master”分支中看到这段代码。

在尝试“干净”的方法之后,我决定实施一个小技巧来完成这个技巧。

如果您在Controller类中重新定义“response”方法,如下所示:

public function response($data = NULL, $http_code = NULL) {
    // send response via REST server library
    parent::response($data, $http_code);

    // display the data and exit execution
    $this->output->_display();
    exit;
}

它确实有效(至少对我而言)。

希望这有帮助。