【问题标题】:Doctrine 2 and OAuth2.0 Server PHP Client Credentials are invalidDoctrine 2 和 OAuth2.0 服务器 PHP 客户端凭据无效
【发布时间】:2015-06-17 11:51:40
【问题描述】:

我正在尝试使用 Doctrine 作为实体管理器来实施 OAuth2。我完全按照本教程进行操作:

http://bshaffer.github.io/oauth2-server-php-docs/cookbook/doctrine2/

这是我在用户向 API 发出请求时调用的代码:

// obtaining the entity manager
$entityManager = EntityManager::create($conn, $config);

$clientStorage  = $entityManager->getRepository('OAuthClient');
$clients = $clientStorage->findAll();
print_r($clients); // We are getting the clients from the database.
$userStorage = $entityManager->getRepository('OAuthUser');
$accessTokenStorage  = $entityManager->getRepository('OAuthAccessToken');
$authorizationCodeStorage = $entityManager->getRepository('OAuthAuthorizationCode');
$refreshTokenStorage = $entityManager->getRepository('OAuthRefreshToken');

//Pass the doctrine storage objects to the OAuth2 server class
$server = new \OAuth2\Server([
    'client_credentials' => $clientStorage,
    'user_credentials'   => $userStorage,
    'access_token'       => $accessTokenStorage,
    'authorization_code' => $authorizationCodeStorage,
    'refresh_token'      => $refreshTokenStorage,
], [
    'auth_code_lifetime' => 30,
    'refresh_token_lifetime' => 30,
]);

$server->addGrantType(new OAuth2\GrantType\ClientCredentials($clientStorage));

// handle the request
$server->handleTokenRequest(OAuth2\Request::createFromGlobals())->send();

每当使用正确凭据进行呼叫时,我都会收到以下响应:

Array
(
    [0] => OAuthClient Object
        (
            [id:OAuthClient:private] => 1
            [client_identifier:OAuthClient:private] => testclient
            [client_secret:OAuthClient:private] => testpass
            [redirect_uri:OAuthClient:private] => http://fake.com
            [hashOptions:protected] => Array
                (
                    [cost] => 11
                )

        )

    [1] => OAuthClient Object
        (
            [id:OAuthClient:private] => 2
            [client_identifier:OAuthClient:private] => trevor
            [client_secret:OAuthClient:private] => hutto
            [redirect_uri:OAuthClient:private] => https://www.another.com
            [hashOptions:protected] => Array
                (
                    [cost] => 11
                )

        )

)
{"error":"invalid_client","error_description":"The client credentials are invalid"}

所以我们从数据库中获取客户端,我们应该检查它们,并返回它们实际上存在并发出访问令牌。但是,由于某种原因,OAuth2 Server(可以看到here)无法将给定的凭据与存储的凭据匹配。

我认为这不是 Doctrine 问题,因为我可以使用 findAll() 轻松检索结果。

我的问题是:

为什么会发生这种情况,我该如何解决?

【问题讨论】:

    标签: php doctrine-orm oauth-2.0 credentials entitymanager


    【解决方案1】:

    我发现了问题。在教程 (http://bshaffer.github.io/oauth2-server-php-docs/cookbook/doctrine2/) 中,他们没有提到当根据提供的客户端密码的散列版本检查客户端密码时。

    在本教程中,他们将示例客户端密码放入数据库时​​不会对其进行哈希处理。

    如果您在将客户端密码插入数据库时​​对其进行哈希处理,它将按预期工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-16
      • 1970-01-01
      • 2011-02-10
      • 1970-01-01
      相关资源
      最近更新 更多