编辑:我意识到我原来的问题是由于我自己的愚蠢但现在问题略有不同
控制器创建api密钥:
$apiKey = new ApiKey;
// Make some random strings for the api key / secret
$key = 'test';
$secret = 'test';
$apiKey->api_key = $key;
$apiKey->api_secret = Hash::make($secret);
$apiKey->save();
过滤进行身份验证:
// Set the auth model to use the API keys table
Config::set('auth.model', 'App\Models\ApiKey');
$credentials = array('api_key' => $_SERVER['PHP_AUTH_USER'], 'api_secret' => $_SERVER['PHP_AUTH_PW']);
// Attempt to authenticate
if (!Auth::attempt($credentials)) {
return Response::make('Invalid Credentials.', 401);
}
模型ApiKey.php
<?php
namespace App\Models;
use Hash;
use Illuminate\Auth\UserInterface;
class ApiKey extends \Eloquent implements UserInterface {
protected $table = 'api_keys';
protected $fillable = array('name', 'api_key', 'api_secret', 'active', 'allowed_ip');
public function getAuthIdentifier()
{
return $this->getKey();
}
public function getAuthPassword()
{
return $this->api_secret;
}
}
我的数据库有一个表'api_keys',在该表中有一个字段'api_key'和'api_secret'。
身份验证总是失败,我没有错误。
修改2
我已经部分解决了我的问题。
我发现通过将我的数据库字段从 api_key 和 api_secret 重命名为用户名和密码,一切正常。
我也可以将用户名字段更改为我喜欢的内容。
但是,如果我更改密码字段名称,则auth不起作用。所以我认为模式肯定存在一些问题,特别是这一点:
public function getAuthPassword()
{
return $this->api_secret;
}