0

編集:元の問題は自分の愚かさによるものであることに気付きましたが、現在は少し異なる問題があります

コントローラ作成 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_keyapi_secretから単にユーザー名とパスワードに変更すると、すべてが機能することがわかりました。

ユーザー名フィールドを好きなように変更することもできます。

しかし、パスワード フィールド名を変更すると、認証が機能しません。したがって、モード、特にこのビットに問題があるに違いないと思います。

    public function getAuthPassword()
    {
        return $this->api_secret;
    }
4

0 に答える 0