UserCredentials.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace OAuth2\GrantType;
  3. use OAuth2\Storage\UserCredentialsInterface;
  4. use OAuth2\ResponseType\AccessTokenInterface;
  5. use OAuth2\RequestInterface;
  6. use OAuth2\ResponseInterface;
  7. /**
  8. *
  9. * @author Brent Shaffer <bshafs at gmail dot com>
  10. */
  11. class UserCredentials implements GrantTypeInterface
  12. {
  13. private $userInfo;
  14. protected $storage;
  15. /**
  16. * @param OAuth2\Storage\UserCredentialsInterface $storage REQUIRED Storage class for retrieving user credentials information
  17. */
  18. public function __construct(UserCredentialsInterface $storage)
  19. {
  20. $this->storage = $storage;
  21. }
  22. public function getQuerystringIdentifier()
  23. {
  24. return 'password';
  25. }
  26. public function validateRequest(RequestInterface $request, ResponseInterface $response)
  27. {
  28. if (!$request->request("password") || !$request->request("username")) {
  29. $response->setError(400, 'invalid_request', 'Missing parameters: "username" and "password" required');
  30. return null;
  31. }
  32. if (!$this->storage->checkUserCredentials($request->request("username"), $request->request("password"))) {
  33. $response->setError(401, 'invalid_grant', 'Invalid username and password combination');
  34. return null;
  35. }
  36. $userInfo = $this->storage->getUserDetails($request->request("username"));
  37. if (empty($userInfo)) {
  38. $response->setError(400, 'invalid_grant', 'Unable to retrieve user information');
  39. return null;
  40. }
  41. if (!isset($userInfo['user_id'])) {
  42. throw new \LogicException("you must set the user_id on the array returned by getUserDetails");
  43. }
  44. $this->userInfo = $userInfo;
  45. return true;
  46. }
  47. public function getClientId()
  48. {
  49. return null;
  50. }
  51. public function getUserId()
  52. {
  53. return $this->userInfo['user_id'];
  54. }
  55. public function getScope()
  56. {
  57. return isset($this->userInfo['scope']) ? $this->userInfo['scope'] : null;
  58. }
  59. public function createAccessToken(AccessTokenInterface $accessToken, $client_id, $user_id, $scope)
  60. {
  61. return $accessToken->createAccessToken($client_id, $user_id, $scope);
  62. }
  63. }