1

我有一个 Laravel 4.2 应用程序,我已经使用 oauth2-server-php 成功实现了授权代码。但是,我似乎无法让用户凭据授予工作。

这是我设置 oauth 服务器的代码:

App::singleton(
    'oauth2',
    function () {

        $host = Config::get('database.connections.mongodb.host');
        $hosts = is_array($host) ? $host : [$host];
        $dbName = Config::get('database.connections.mongodb.database');
        $dbOptions =
            empty( Config::get('database.connections.mongodb.options') ) ? [] : Config::get(
                'database.connections.mongodb.options'
            );

        $mongo = new MongoClient('mongodb://' . implode(',', $hosts) . '/' . $dbName, $dbOptions);
        $storage = new OAuth2\Storage\Mongo($mongo->{$dbName});
        $server = new OAuth2\Server(
            $storage, [
            'always_issue_new_refresh_token' => true,
            'refresh_token_lifetime'         => 2419200,
        ]
        );

        $userStorage = new \Medusa\Oauth\Storage\MedusaUserCredentials();

        $server->addStorage($userStorage, 'users');

        $userCredentialGrant = new Oauth2\GrantType\UserCredentials($userStorage);

        $server->addGrantType(new OAuth2\GrantType\AuthorizationCode($storage));
        $server->addGrantType(new OAuth2\GrantType\ClientCredentials($storage));
        $server->addGrantType($userCredentialGrant);
        $server->addGrantType(new OAuth2\GrantType\RefreshToken($storage));

        return $server;
    }
);

MedusaUserCredentials 具有以下代码:

namespace Medusa\Oauth\Storage;

use OAuth2\Storage\UserCredentialsInterface;

class MedusaUserCredentials implements UserCredentialsInterface
{

    public function checkUserCredentials($username, $password)
    {
        return Auth::attempt(['email_address' => strtolower($username), 'password' => $password, 'active' => 1]);
    }

    public function getUserDetails($username)
    {
        return ['user_id' => $username];
    }
}

当我使用原始有效负载将 Web 服务器发布到令牌路由时

grant_type=password&username=<USERNAME>&password=<PASSWORD>

我只是得到登录页面。令牌路线如下所示

Route::post(
    'oauth/token',
    function () {
        $bridgedRequest = OAuth2\HttpFoundationBridge\Request::createFromRequest(Request::instance());
        $bridgedResponse = new OAuth2\HttpFoundationBridge\Response();

        $bridgedResponse = App::make('oauth2')->handleTokenRequest($bridgedRequest, $bridgedResponse);
print_r($bridgedResponse); die();
        return $bridgedResponse;
    }
);

我错过了什么?

谢谢!

4

1 回答 1

0

我找到了问题——我有一个必须解决的命名空间问题。出于某种原因,我的应用返回了 200 OK 响应和正常的登录页面,所以我没想到要检查日志。

我知道,糟糕的开发人员,没有饼干!

于 2016-08-23T22:27:07.150 回答