oauth2-server/src/Grant/PasswordGrant.php

124 lines
4.5 KiB
PHP
Raw Normal View History

2013-02-12 16:22:14 +00:00
<?php
/**
* OAuth 2.0 Password grant
*
2014-01-08 16:15:29 +00:00
* @package league/oauth2-server
* @author Alex Bilbie <hello@alexbilbie.com>
2014-03-09 19:34:23 +00:00
* @copyright Copyright (c) Alex Bilbie
* @license http://mit-license.org/
2014-03-09 20:05:38 +00:00
* @link https://github.com/thephpleague/oauth2-server
*/
2013-02-12 16:22:14 +00: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\ClientEntityInterface;
use League\OAuth2\Server\Entities\Interfaces\UserEntityInterface;
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;
use Psr\Http\Message\ServerRequestInterface;
2013-02-12 16:22:14 +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';
/**
2015-04-05 21:10:41 +01:00
* @var \League\OAuth2\Server\Repositories\UserRepositoryInterface
*/
2015-11-16 12:58:11 +00:00
private $userRepository;
/**
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
*/
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
) {
2016-01-17 00:41:55 +01:00
parent::__construct($clientRepository, $scopeRepository, $accessTokenRepository);
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
}
/**
2015-11-16 12:58:11 +00:00
* @inheritdoc
*/
2015-11-16 12:58:11 +00:00
public function respondToRequest(
ServerRequestInterface $request,
ResponseTypeInterface $responseType,
2016-01-17 00:41:55 +01:00
\DateInterval $tokenTTL,
2015-04-05 21:10:41 +01:00
$scopeDelimiter = ' '
) {
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 12:43:20 +00:00
$user = $this->validateUser($request);
2016-01-17 00:41:55 +01:00
$scopes = $this->validateScopes($this->getRequestParameter('scope', $request), $scopeDelimiter, $client);
2013-02-12 16:22:14 +00:00
2016-01-17 12:43:20 +00:00
// Issue and persist new tokens
2016-01-17 00:41:55 +01:00
$accessToken = $this->issueAccessToken($tokenTTL, $client, $user->getIdentifier(), $scopes);
$refreshToken = $this->issueRefreshToken($accessToken);
$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();
}
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
}