【发布时间】: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