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 DateInterval;
|
|
|
|
use League\Event\Event;
|
|
|
|
use League\OAuth2\Server\Entities\AccessTokenEntity;
|
|
|
|
use League\OAuth2\Server\Entities\Interfaces\ClientEntityInterface;
|
|
|
|
use League\OAuth2\Server\Entities\Interfaces\UserEntityInterface;
|
2016-01-12 23:05:19 +00:00
|
|
|
use League\OAuth2\Server\Entities\RefreshTokenEntity;
|
2015-11-16 12:58:11 +00:00
|
|
|
use League\OAuth2\Server\Exception\OAuthServerException;
|
2015-04-05 21:10:41 +01:00
|
|
|
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface;
|
|
|
|
use League\OAuth2\Server\Repositories\ClientRepositoryInterface;
|
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\ScopeRepositoryInterface;
|
|
|
|
use League\OAuth2\Server\Repositories\UserRepositoryInterface;
|
2015-11-16 12:58:11 +00:00
|
|
|
use League\OAuth2\Server\ResponseTypes\ResponseTypeInterface;
|
2015-04-05 21:10:41 +01:00
|
|
|
use League\OAuth2\Server\Utils\SecureKey;
|
2015-11-16 12:58:11 +00:00
|
|
|
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
|
|
|
{
|
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\ClientRepositoryInterface $clientRepository
|
|
|
|
* @param \League\OAuth2\Server\Repositories\ScopeRepositoryInterface $scopeRepository
|
|
|
|
* @param \League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface $accessTokenRepository
|
|
|
|
* @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,
|
2015-04-05 21:10:41 +01:00
|
|
|
ClientRepositoryInterface $clientRepository,
|
|
|
|
ScopeRepositoryInterface $scopeRepository,
|
2016-01-13 00:13:29 +00:00
|
|
|
AccessTokenRepositoryInterface $accessTokenRepository,
|
|
|
|
RefreshTokenRepositoryInterface $refreshTokenRepository
|
2015-04-05 21:10:41 +01:00
|
|
|
) {
|
|
|
|
$this->userRepository = $userRepository;
|
2016-01-13 00:13:29 +00:00
|
|
|
$this->refreshTokenRepository = $refreshTokenRepository;
|
2015-11-16 12:58:11 +00:00
|
|
|
parent::__construct($clientRepository, $scopeRepository, $accessTokenRepository);
|
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-13 00:13:29 +00:00
|
|
|
DateInterval $tokenTTL,
|
2015-04-05 21:10:41 +01:00
|
|
|
$scopeDelimiter = ' '
|
|
|
|
) {
|
2013-02-13 19:39:43 +00:00
|
|
|
// Get the required params
|
2015-11-16 12:58:11 +00:00
|
|
|
$clientId = isset($request->getParsedBody()['client_id'])
|
|
|
|
? $request->getParsedBody()['client_id'] // $_POST['client_id']
|
|
|
|
: (isset($request->getServerParams()['PHP_AUTH_USER'])
|
|
|
|
? $request->getServerParams()['PHP_AUTH_USER'] // $_SERVER['PHP_AUTH_USER']
|
|
|
|
: null);
|
|
|
|
|
2013-12-24 17:01:56 +00:00
|
|
|
if (is_null($clientId)) {
|
2015-11-16 12:58:11 +00:00
|
|
|
throw OAuthServerException::invalidRequest('client_id', null, '`%s` parameter is missing');
|
2013-02-12 16:22:14 +00:00
|
|
|
}
|
|
|
|
|
2015-11-16 12:58:11 +00:00
|
|
|
$clientSecret = isset($request->getParsedBody()['client_secret'])
|
|
|
|
? $request->getParsedBody()['client_secret'] // $_POST['client_id']
|
|
|
|
: (isset($request->getServerParams()['PHP_AUTH_PW'])
|
|
|
|
? $request->getServerParams()['PHP_AUTH_PW'] // $_SERVER['PHP_AUTH_USER']
|
|
|
|
: null);
|
|
|
|
|
2013-12-24 17:01:56 +00:00
|
|
|
if (is_null($clientSecret)) {
|
2015-11-16 12:58:11 +00:00
|
|
|
throw OAuthServerException::invalidRequest('client_secret', null, '`%s` parameter is missing');
|
2013-02-12 16:22:14 +00:00
|
|
|
}
|
|
|
|
|
2013-12-24 17:01:56 +00:00
|
|
|
// Validate client ID and client secret
|
2015-11-16 12:58:11 +00:00
|
|
|
$client = $this->clientRepository->getClientEntity(
|
2013-12-24 17:01:56 +00:00
|
|
|
$clientId,
|
|
|
|
$clientSecret,
|
|
|
|
null,
|
|
|
|
$this->getIdentifier()
|
|
|
|
);
|
2013-02-12 16:22:14 +00:00
|
|
|
|
2015-04-05 21:10:41 +01:00
|
|
|
if (($client instanceof ClientEntityInterface) === false) {
|
|
|
|
$this->emitter->emit(new Event('client.authentication.failed', $request));
|
2015-11-16 12:58:11 +00:00
|
|
|
throw OAuthServerException::invalidClient();
|
2013-02-12 16:22:14 +00:00
|
|
|
}
|
|
|
|
|
2015-11-16 12:58:11 +00:00
|
|
|
// Username
|
|
|
|
$username = isset($request->getParsedBody()['username'])
|
|
|
|
? $request->getParsedBody()['username'] // $_POST['username']
|
|
|
|
: (isset($request->getServerParams()['PHP_AUTH_USER'])
|
|
|
|
? $request->getServerParams()['PHP_AUTH_USER'] // $_SERVER['PHP_AUTH_USER']
|
|
|
|
: null);
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-11-16 12:58:11 +00:00
|
|
|
// Password
|
|
|
|
$password = isset($request->getParsedBody()['password'])
|
|
|
|
? $request->getParsedBody()['password'] // $_POST['password']
|
|
|
|
: (isset($request->getServerParams()['PHP_AUTH_USER'])
|
|
|
|
? $request->getServerParams()['PHP_AUTH_USER'] // $_SERVER['PHP_AUTH_USER']
|
|
|
|
: null);
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-11-16 12:58:11 +00:00
|
|
|
// Verify user's username and password
|
|
|
|
$userEntity = $this->userRepository->getUserEntityByUserCredentials($username, $password);
|
|
|
|
if (($userEntity instanceof UserEntityInterface) === false) {
|
2015-04-05 21:10:41 +01:00
|
|
|
$this->emitter->emit(new Event('user.authentication.failed', $request));
|
2015-11-16 12:58:11 +00:00
|
|
|
throw OAuthServerException::invalidCredentials();
|
2013-02-12 16:22:14 +00:00
|
|
|
}
|
|
|
|
|
2013-03-06 17:18:37 +00:00
|
|
|
// Validate any scopes that are in the request
|
2015-11-16 12:58:11 +00:00
|
|
|
$scopeParam = isset($request->getParsedBody()['scope'])
|
|
|
|
? $request->getParsedBody()['scope'] // $_POST['scope']
|
|
|
|
: '';
|
|
|
|
$scopes = $this->validateScopes($scopeParam, $scopeDelimiter, $client);
|
2013-03-22 11:41:04 +00:00
|
|
|
|
2013-12-24 17:01:56 +00:00
|
|
|
// Generate an access token
|
2015-04-05 21:10:41 +01:00
|
|
|
$accessToken = new AccessTokenEntity();
|
|
|
|
$accessToken->setIdentifier(SecureKey::generate());
|
2016-01-13 00:13:29 +00:00
|
|
|
$accessToken->setExpiryDateTime((new \DateTime())->add($tokenTTL));
|
2015-04-05 21:10:41 +01:00
|
|
|
$accessToken->setClient($client);
|
2016-01-12 23:05:19 +00:00
|
|
|
$accessToken->setUserIdentifier($userEntity->getIdentifier());
|
2013-03-06 17:18:37 +00:00
|
|
|
|
2015-11-16 12:58:11 +00:00
|
|
|
// Associate scopes with the session and access token
|
2013-03-06 17:18:37 +00:00
|
|
|
foreach ($scopes as $scope) {
|
2015-04-05 21:10:41 +01:00
|
|
|
$accessToken->addScope($scope);
|
2013-03-06 17:18:37 +00:00
|
|
|
}
|
|
|
|
|
2016-01-12 23:05:19 +00:00
|
|
|
// Generate a refresh token
|
|
|
|
$refreshToken = new RefreshTokenEntity();
|
|
|
|
$refreshToken->setIdentifier(SecureKey::generate());
|
|
|
|
$refreshToken->setExpiryDateTime((new \DateTime())->add(new DateInterval('P1M')));
|
|
|
|
$refreshToken->setAccessToken($accessToken);
|
|
|
|
|
2016-01-13 00:13:29 +00:00
|
|
|
// Persist the tokens
|
|
|
|
$this->accessTokenRepository->persistNewAccessToken($accessToken);
|
|
|
|
$this->refreshTokenRepository->persistNewRefreshToken($refreshToken);
|
|
|
|
|
2016-01-12 23:05:19 +00:00
|
|
|
// Inject tokens into response
|
2015-11-16 12:58:11 +00:00
|
|
|
$responseType->setAccessToken($accessToken);
|
2016-01-12 23:05:19 +00:00
|
|
|
$responseType->setRefreshToken($refreshToken);
|
2013-03-06 17:18:37 +00:00
|
|
|
|
2015-11-16 12:58:11 +00:00
|
|
|
return $responseType;
|
|
|
|
}
|
2013-02-12 16:22:14 +00:00
|
|
|
|
2015-11-16 12:58:11 +00:00
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
public function canRespondToRequest(ServerRequestInterface $request)
|
|
|
|
{
|
2016-01-12 23:03:38 +00:00
|
|
|
return (
|
2015-11-16 12:58:11 +00:00
|
|
|
isset($request->getParsedBody()['grant_type'])
|
|
|
|
&& $request->getParsedBody()['grant_type'] === 'password'
|
2016-01-12 23:03:38 +00:00
|
|
|
);
|
2013-02-12 16:22:14 +00:00
|
|
|
}
|
2013-09-07 18:00:13 +01:00
|
|
|
}
|