slim-oauth2无效令牌(Slimframework 3)

时间:2017-07-02 16:21:07

标签: php oauth slim

我使用chadicus / slim-oauth2集合进行slimframework 3。

这是我的代码atm(在Apache2上运行):

<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
use \Slim\Middleware\HttpBasicAuthentication\PdoAuthenticator;
use Chadicus\Slim\OAuth2\Http\RequestBridge;
use Chadicus\Slim\OAuth2\Http\ResponseBridge;
use Chadicus\Slim\OAuth2\Middleware;
use OAuth2;
use OAuth2\GrantType;
use OAuth2\Storage;
use Slim;

require '../vendor/autoload.php';

define(KUNDEN,'kunden');
define(VERTRAEGE,'vertraege');
define(ADRESSE,'adresse');

$config['displayErrorDetails'] = true;
$config['addContentLengthHeader'] = false;

$config['db']['host']   = "localhost";
$config['db']['user']   = "vv";
$config['db']['pass']   = "vv";
$config['db']['dbname'] = "vv";

$storage = new Storage\Memory(
    [
        'client_credentials' => [
            'administrator' => [
                'client_id' => 'administrator',
                'client_secret' => 'password',
                'scope' => 'superUser',
            ],
            'foo-client' => [
                'client_id' => 'foo-client',
                'client_secret' => 'p4ssw0rd',
                'scope' => 'basicUser canViewFoos',
            ],
            'bar-client' => [
                'client_id' => 'foo-client',
                'client_secret' => '!password1',
                'scope' => 'basicUser',
            ],
        ],
    ]
);

$server = new OAuth2\Server(
    $storage,
    [
        'access_lifetime' => 3600,
    ],
    [
        new GrantType\ClientCredentials($storage),
    ]
);

$app = new \Slim\App(["settings"=>$config]);
$authMiddleware = new Middleware\Authorization($server, $app->getContainer());

$container=$app->getContainer();

$container['db'] = function ($c) {
    $db = $c['settings']['db'];
    $pdo = new PDO("mysql:host=" . $db['host'] . ";dbname=" . $db['dbname'],
        $db['user'], $db['pass']);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
    return $pdo;
};

$app->post('/token', function ($psrRequest, $psrResponse, array $args) use ($app, $server) {
    //create an \OAuth2\Request from the current \Slim\Http\Request Object
    $oauth2Request = RequestBridge::toOAuth2($psrRequest);

    //Allow the oauth2 server instance to handle the oauth2 request
    $oauth2Response = $server->handleTokenRequest($oauth2Request);

    //Map the oauth2 response into the slim response
    //print_r($server['storage']);
    return ResponseBridge::fromOAuth2($oauth2Response);
});

$app->get('/'.KUNDEN, function (Request $request, Response $response) {
    $query=$this->db->prepare("Select * from customer");
    $query->execute();
    return $response->withJson($query->fetchAll());
})->add($authMiddleware);

$app->run();

如果我现在请求使用邮递员和管理员/密码凭证访问/ token,我会收到一个令牌。但是,如果我尝试使用此令牌打开/ kunden,我会得到:&#34;无效令牌&#34;。

Get token back from server

Invalid token

我不确定令牌是否正确存储在内存中。而且,说实话,我对oAuth2的经验不多。

任何人都可以向我推进正确的方向。我需要一个提示,我必须在互联网上搜索。因为&#34; Slimframework oauth2无效令牌&#34;不是谷歌的正确关键词: - /

提前致谢!

弗朗兹

2 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,我只是不知道如何使用OAuth2 Server PHP。

您必须在数据库中创建OAuth2表,如下所示:https://bshaffer.github.io/oauth2-server-php-docs/cookbook/

然后,您可以在数据库中创建用户,并使用PDO进行身份验证并保存用户令牌:

$pdo = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);

$storage = new Storage\Pdo($pdo);

$server = new OAuth2\Server(
    $storage,
    [
        'access_lifetime' => 3600,
    ],
    [
        new GrantType\ClientCredentials($storage),
        new GrantType\AuthorizationCode($storage),
    ]
);

答案 1 :(得分:0)

对于在搜索引擎中偶然发现此问题的其他人。

首先,您必须了解OAuth2中的“内存”存储不是持久性的,这意味着在创建令牌时,它将不会保存在任何地方。旨在向您显示该机制的令牌创建部分正在运行。

第二,如果要对路由的客户端进行身份验证,则必须使用其他存储(PDO或Redis)。使用Redis非常简单。您只需在服务器上安装redis-server,对其进行保护,然后在代码中实现它。对于Redis的易于使用的实现,请检查作曲家软件包predis/predis

有关如何使用Redis实施OAuth2的更多信息,请访问:https://bshaffer.github.io/oauth2-server-php-docs/storage/redis/

祝你有美好的一天:)