2013-02-12 16:22:14 +00:00
|
|
|
<?php
|
2013-02-13 19:39:43 +00:00
|
|
|
/**
|
|
|
|
* OAuth 2.0 Password grant
|
|
|
|
*
|
2014-01-08 16:15:29 +00:00
|
|
|
* @package league/oauth2-server
|
2013-02-13 19:39:43 +00:00
|
|
|
* @author Alex Bilbie <hello@alexbilbie.com>
|
2014-03-09 19:34:23 +00:00
|
|
|
* @copyright Copyright (c) Alex Bilbie
|
2013-02-13 19:39:43 +00:00
|
|
|
* @license http://mit-license.org/
|
2014-03-09 20:05:38 +00:00
|
|
|
* @link https://github.com/thephpleague/oauth2-server
|
2013-02-13 19:39:43 +00:00
|
|
|
*/
|
2013-02-12 16:22:14 +00:00
|
|
|
|
2013-05-08 11:42:23 -07:00
|
|
|
namespace League\OAuth2\Server\Grant;
|
2013-05-08 11:06:09 -07:00
|
|
|
|
2015-04-05 21:10:41 +01:00
|
|
|
use League\Event\Event;
|
|
|
|
use League\OAuth2\Server\Entities\Interfaces\UserEntityInterface;
|
2015-11-16 12:58:11 +00:00
|
|
|
use League\OAuth2\Server\Exception\OAuthServerException;
|
2016-01-13 00:13:29 +00:00
|
|
|
use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface;
|
2015-04-05 21:10:41 +01:00
|
|
|
use League\OAuth2\Server\Repositories\UserRepositoryInterface;
|
2015-11-16 12:58:11 +00:00
|
|
|
use League\OAuth2\Server\ResponseTypes\ResponseTypeInterface;
|
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
2013-02-12 16:22:14 +00:00
|
|
|
|
2013-02-13 19:39:43 +00:00
|
|
|
/**
|
|
|
|
* Password grant class
|
|
|
|
*/
|
2014-05-02 17:24:55 +01:00
|
|
|
class PasswordGrant extends AbstractGrant
|
2014-01-08 16:15:29 +00:00
|
|
|
{
|
2016-01-17 00:41:55 +01:00
|
|
|
/**
|
|
|
|
* Grant identifier
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $identifier = 'password';
|
|
|
|
|
2013-05-06 15:04:00 -07:00
|
|
|
/**
|
2015-04-05 21:10:41 +01:00
|
|
|
* @var \League\OAuth2\Server\Repositories\UserRepositoryInterface
|
2013-05-06 15:04:00 -07:00
|
|
|
*/
|
2015-11-16 12:58:11 +00:00
|
|
|
private $userRepository;
|
2013-05-06 15:04:00 -07:00
|
|
|
|
2013-02-13 19:39:43 +00:00
|
|
|
/**
|
2016-01-13 00:13:29 +00:00
|
|
|
* @var \League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface
|
|
|
|
*/
|
|
|
|
private $refreshTokenRepository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param \League\OAuth2\Server\Repositories\UserRepositoryInterface $userRepository
|
|
|
|
* @param \League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface $refreshTokenRepository
|
2013-02-13 19:39:43 +00:00
|
|
|
*/
|
2015-04-05 21:10:41 +01:00
|
|
|
public function __construct(
|
2015-11-16 12:58:11 +00:00
|
|
|
UserRepositoryInterface $userRepository,
|
2016-01-13 00:13:29 +00:00
|
|
|
RefreshTokenRepositoryInterface $refreshTokenRepository
|
2015-04-05 21:10:41 +01:00
|
|
|
) {
|
|
|
|
$this->userRepository = $userRepository;
|
2016-01-13 00:13:29 +00:00
|
|
|
$this->refreshTokenRepository = $refreshTokenRepository;
|
2013-02-12 16:22:14 +00:00
|
|
|
}
|
|
|
|
|
2013-02-13 19:39:43 +00:00
|
|
|
/**
|
2015-11-16 12:58:11 +00:00
|
|
|
* @inheritdoc
|
2013-02-13 19:39:43 +00:00
|
|
|
*/
|
2015-11-16 12:58:11 +00:00
|
|
|
public function respondToRequest(
|
|
|
|
ServerRequestInterface $request,
|
|
|
|
ResponseTypeInterface $responseType,
|
2016-01-20 12:21:44 +01:00
|
|
|
\DateInterval $accessTokenTTL,
|
|
|
|
\DateInterval $refreshTokenTTL
|
2015-04-05 21:10:41 +01:00
|
|
|
) {
|
2016-01-17 12:43:20 +00:00
|
|
|
// Validate request
|
2016-01-17 00:41:55 +01:00
|
|
|
$client = $this->validateClient($request);
|
2016-01-17 15:49:55 +01:00
|
|
|
$user = $this->validateUser($request);
|
2016-01-17 16:38:25 +00:00
|
|
|
$scopes = $this->validateScopes($request, $client);
|
2013-02-12 16:22:14 +00:00
|
|
|
|
2016-01-17 12:43:20 +00:00
|
|
|
// Issue and persist new tokens
|
2016-01-20 12:21:44 +01:00
|
|
|
$accessToken = $this->issueAccessToken($accessTokenTTL, $client, $user->getIdentifier(), $scopes);
|
|
|
|
$refreshToken = $this->issueRefreshToken($refreshTokenTTL, $accessToken);
|
2016-01-17 00:41:55 +01:00
|
|
|
$this->accessTokenRepository->persistNewAccessToken($accessToken);
|
|
|
|
$this->refreshTokenRepository->persistNewRefreshToken($refreshToken);
|
2013-02-12 16:22:14 +00:00
|
|
|
|
2016-01-17 00:41:55 +01:00
|
|
|
// Inject tokens into response
|
|
|
|
$responseType->setAccessToken($accessToken);
|
|
|
|
$responseType->setRefreshToken($refreshToken);
|
2013-02-12 16:22:14 +00:00
|
|
|
|
2016-01-17 00:41:55 +01:00
|
|
|
return $responseType;
|
|
|
|
}
|
2015-11-16 12:58:11 +00:00
|
|
|
|
2016-01-17 00:41:55 +01:00
|
|
|
/**
|
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request
|
|
|
|
*
|
|
|
|
* @return \League\OAuth2\Server\Entities\Interfaces\UserEntityInterface
|
|
|
|
*
|
|
|
|
* @throws \League\OAuth2\Server\Exception\OAuthServerException
|
|
|
|
*/
|
|
|
|
protected function validateUser(ServerRequestInterface $request)
|
|
|
|
{
|
2016-01-17 12:43:20 +00:00
|
|
|
$username = $this->getRequestParameter('username', $request);
|
2013-12-24 17:01:56 +00:00
|
|
|
if (is_null($username)) {
|
2015-11-16 12:58:11 +00:00
|
|
|
throw OAuthServerException::invalidRequest('username', null, '`%s` parameter is missing');
|
2013-02-12 16:22:14 +00:00
|
|
|
}
|
|
|
|
|
2016-01-17 12:43:20 +00:00
|
|
|
$password = $this->getRequestParameter('password', $request);
|
2013-12-24 17:01:56 +00:00
|
|
|
if (is_null($password)) {
|
2015-11-16 12:58:11 +00:00
|
|
|
throw OAuthServerException::invalidRequest('password', null, '`%s` parameter is missing');
|
2013-02-12 16:22:14 +00:00
|
|
|
}
|
|
|
|
|
2016-01-17 00:41:55 +01:00
|
|
|
$user = $this->userRepository->getUserEntityByUserCredentials($username, $password);
|
|
|
|
if (!$user instanceof UserEntityInterface) {
|
2015-04-05 21:10:41 +01:00
|
|
|
$this->emitter->emit(new Event('user.authentication.failed', $request));
|
2013-02-12 16:22:14 +00:00
|
|
|
|
2016-01-17 00:41:55 +01:00
|
|
|
throw OAuthServerException::invalidCredentials();
|
2013-03-06 17:18:37 +00:00
|
|
|
}
|
|
|
|
|
2016-01-17 00:41:55 +01:00
|
|
|
return $user;
|
2013-02-12 16:22:14 +00:00
|
|
|
}
|
2013-09-07 18:00:13 +01:00
|
|
|
}
|