mirror of
https://github.com/elyby/oauth2-server.git
synced 2024-12-23 05:29:52 +05:30
Tidy up
This commit is contained in:
commit
0744d8e926
@ -12,12 +12,16 @@
|
|||||||
namespace League\OAuth2\Server\Grant;
|
namespace League\OAuth2\Server\Grant;
|
||||||
|
|
||||||
use League\Event\EmitterInterface;
|
use League\Event\EmitterInterface;
|
||||||
|
use League\Event\Event;
|
||||||
|
use League\OAuth2\Server\Entities\AccessTokenEntity;
|
||||||
use League\OAuth2\Server\Entities\Interfaces\ClientEntityInterface;
|
use League\OAuth2\Server\Entities\Interfaces\ClientEntityInterface;
|
||||||
|
use League\OAuth2\Server\Entities\RefreshTokenEntity;
|
||||||
use League\OAuth2\Server\Entities\ScopeEntity;
|
use League\OAuth2\Server\Entities\ScopeEntity;
|
||||||
use League\OAuth2\Server\Exception\OAuthServerException;
|
use League\OAuth2\Server\Exception\OAuthServerException;
|
||||||
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface;
|
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface;
|
||||||
use League\OAuth2\Server\Repositories\ClientRepositoryInterface;
|
use League\OAuth2\Server\Repositories\ClientRepositoryInterface;
|
||||||
use League\OAuth2\Server\Repositories\ScopeRepositoryInterface;
|
use League\OAuth2\Server\Repositories\ScopeRepositoryInterface;
|
||||||
|
use League\OAuth2\Server\Utils\SecureKey;
|
||||||
use Psr\Http\Message\ServerRequestInterface;
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -95,6 +99,77 @@ abstract class AbstractGrant implements GrantTypeInterface
|
|||||||
return $this->respondsWith;
|
return $this->respondsWith;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param \Psr\Http\Message\ServerRequestInterface $request
|
||||||
|
*
|
||||||
|
* @return \League\OAuth2\Server\Entities\Interfaces\ClientEntityInterface
|
||||||
|
*
|
||||||
|
* @throws \League\OAuth2\Server\Exception\OAuthServerException
|
||||||
|
*/
|
||||||
|
protected function validateClient(ServerRequestInterface $request)
|
||||||
|
{
|
||||||
|
$clientId = $this->getRequestParameter(
|
||||||
|
'client_id',
|
||||||
|
$request,
|
||||||
|
$this->getServerParameter('PHP_AUTH_USER', $request)
|
||||||
|
);
|
||||||
|
if (is_null($clientId)) {
|
||||||
|
throw OAuthServerException::invalidRequest('client_id', null, '`%s` parameter is missing');
|
||||||
|
}
|
||||||
|
|
||||||
|
$clientSecret = $this->getRequestParameter(
|
||||||
|
'client_secret',
|
||||||
|
$request,
|
||||||
|
$this->getServerParameter('PHP_AUTH_PW', $request)
|
||||||
|
);
|
||||||
|
if (is_null($clientSecret)) {
|
||||||
|
throw OAuthServerException::invalidRequest('client_secret', null, '`%s` parameter is missing');
|
||||||
|
}
|
||||||
|
|
||||||
|
$client = $this->clientRepository->getClientEntity(
|
||||||
|
$clientId,
|
||||||
|
$clientSecret,
|
||||||
|
null,
|
||||||
|
$this->getIdentifier()
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!$client instanceof ClientEntityInterface) {
|
||||||
|
$this->emitter->emit(new Event('client.authentication.failed', $request));
|
||||||
|
|
||||||
|
throw OAuthServerException::invalidClient();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $client;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve request parameter.
|
||||||
|
*
|
||||||
|
* @param string $parameter
|
||||||
|
* @param \Psr\Http\Message\ServerRequestInterface $request
|
||||||
|
* @param mixed $default
|
||||||
|
*
|
||||||
|
* @return null|string
|
||||||
|
*/
|
||||||
|
protected function getRequestParameter($parameter, ServerRequestInterface $request, $default = null)
|
||||||
|
{
|
||||||
|
return (isset($request->getParsedBody()[$parameter])) ? $request->getParsedBody()[$parameter] : $default;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve server parameter.
|
||||||
|
*
|
||||||
|
* @param string|array $parameter
|
||||||
|
* @param \Psr\Http\Message\ServerRequestInterface $request
|
||||||
|
* @param mixed $default
|
||||||
|
*
|
||||||
|
* @return null|string
|
||||||
|
*/
|
||||||
|
protected function getServerParameter($parameter, ServerRequestInterface $request, $default = null)
|
||||||
|
{
|
||||||
|
return (isset($request->getServerParams()[$parameter])) ? $request->getServerParams()[$parameter] : $default;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $scopeParamValue A string containing a delimited set of scope identifiers
|
* @param string $scopeParamValue A string containing a delimited set of scope identifiers
|
||||||
* @param string $scopeDelimiterString The delimiter between the scopes in the value string
|
* @param string $scopeDelimiterString The delimiter between the scopes in the value string
|
||||||
@ -142,4 +217,57 @@ abstract class AbstractGrant implements GrantTypeInterface
|
|||||||
{
|
{
|
||||||
$this->emitter = $emitter;
|
$this->emitter = $emitter;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param \DateInterval $tokenTTL
|
||||||
|
* @param \League\OAuth2\Server\Entities\Interfaces\ClientEntityInterface $client
|
||||||
|
* @param string $userIdentifier
|
||||||
|
* @param array $scopes
|
||||||
|
*
|
||||||
|
* @return \League\OAuth2\Server\Entities\AccessTokenEntity
|
||||||
|
*/
|
||||||
|
protected function issueAccessToken(
|
||||||
|
\DateInterval $tokenTTL,
|
||||||
|
ClientEntityInterface $client,
|
||||||
|
$userIdentifier,
|
||||||
|
array $scopes = []
|
||||||
|
) {
|
||||||
|
$accessToken = new AccessTokenEntity();
|
||||||
|
$accessToken->setIdentifier(SecureKey::generate());
|
||||||
|
$accessToken->setExpiryDateTime((new \DateTime())->add($tokenTTL));
|
||||||
|
$accessToken->setClient($client);
|
||||||
|
$accessToken->setUserIdentifier($userIdentifier);
|
||||||
|
|
||||||
|
foreach ($scopes as $scope) {
|
||||||
|
$accessToken->addScope($scope);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $accessToken;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param \League\OAuth2\Server\Entities\AccessTokenEntity $accessToken
|
||||||
|
*
|
||||||
|
* @return \League\OAuth2\Server\Entities\RefreshTokenEntity
|
||||||
|
*/
|
||||||
|
protected function issueRefreshToken(AccessTokenEntity $accessToken)
|
||||||
|
{
|
||||||
|
$refreshToken = new RefreshTokenEntity();
|
||||||
|
$refreshToken->setIdentifier(SecureKey::generate());
|
||||||
|
$refreshToken->setExpiryDateTime((new \DateTime())->add(new \DateInterval('P1M')));
|
||||||
|
$refreshToken->setAccessToken($accessToken);
|
||||||
|
|
||||||
|
return $refreshToken;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritdoc
|
||||||
|
*/
|
||||||
|
public function canRespondToRequest(ServerRequestInterface $request)
|
||||||
|
{
|
||||||
|
return (
|
||||||
|
isset($request->getParsedBody()['grant_type'])
|
||||||
|
&& $request->getParsedBody()['grant_type'] === $this->identifier
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,13 +11,9 @@
|
|||||||
|
|
||||||
namespace League\OAuth2\Server\Grant;
|
namespace League\OAuth2\Server\Grant;
|
||||||
|
|
||||||
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\ClientEntityInterface;
|
||||||
use League\OAuth2\Server\Exception\OAuthServerException;
|
use League\OAuth2\Server\Exception\OAuthServerException;
|
||||||
use League\OAuth2\Server\ResponseTypes\ResponseTypeInterface;
|
use League\OAuth2\Server\ResponseTypes\ResponseTypeInterface;
|
||||||
use League\OAuth2\Server\Utils\SecureKey;
|
|
||||||
use Psr\Http\Message\ServerRequestInterface;
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -38,78 +34,20 @@ class ClientCredentialsGrant extends AbstractGrant
|
|||||||
public function respondToRequest(
|
public function respondToRequest(
|
||||||
ServerRequestInterface $request,
|
ServerRequestInterface $request,
|
||||||
ResponseTypeInterface $responseType,
|
ResponseTypeInterface $responseType,
|
||||||
DateInterval $accessTokenTTL,
|
\DateInterval $tokenTTL,
|
||||||
$scopeDelimiter = ' '
|
$scopeDelimiter = ' '
|
||||||
) {
|
) {
|
||||||
// Get the required params
|
// Validate request
|
||||||
$clientId = isset($request->getParsedBody()['client_id'])
|
$client = $this->validateClient($request);
|
||||||
? $request->getParsedBody()['client_id'] // $_POST['client_id']
|
$scopes = $this->validateScopes($this->getRequestParameter('scope', $request), $scopeDelimiter, $client);
|
||||||
: (isset($request->getServerParams()['PHP_AUTH_USER'])
|
|
||||||
? $request->getServerParams()['PHP_AUTH_USER'] // $_SERVER['PHP_AUTH_USER']
|
|
||||||
: null);
|
|
||||||
|
|
||||||
if (is_null($clientId)) {
|
// Issue and persist access token
|
||||||
throw OAuthServerException::invalidRequest('client_id', null, '`%s` parameter is missing');
|
$accessToken = $this->issueAccessToken($tokenTTL, $client, $client->getIdentifier(), $scopes);
|
||||||
}
|
|
||||||
|
|
||||||
$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);
|
|
||||||
|
|
||||||
if (is_null($clientSecret)) {
|
|
||||||
throw OAuthServerException::invalidRequest('client_secret', null, '`%s` parameter is missing');
|
|
||||||
}
|
|
||||||
|
|
||||||
// Validate client ID and client secret
|
|
||||||
$client = $this->clientRepository->getClientEntity(
|
|
||||||
$clientId,
|
|
||||||
$clientSecret,
|
|
||||||
null,
|
|
||||||
$this->getIdentifier()
|
|
||||||
);
|
|
||||||
|
|
||||||
if (($client instanceof ClientEntityInterface) === false) {
|
|
||||||
$this->emitter->emit(new Event('client.authentication.failed', $request));
|
|
||||||
throw OAuthServerException::invalidClient();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Validate any scopes that are in the request
|
|
||||||
$scopeParam = isset($request->getParsedBody()['scope'])
|
|
||||||
? $request->getParsedBody()['scope'] // $_POST['scope']
|
|
||||||
: '';
|
|
||||||
$scopes = $this->validateScopes($scopeParam, $scopeDelimiter, $client);
|
|
||||||
|
|
||||||
// Generate an access token
|
|
||||||
$accessToken = new AccessTokenEntity();
|
|
||||||
$accessToken->setIdentifier(SecureKey::generate());
|
|
||||||
$accessToken->setExpiryDateTime((new \DateTime())->add($accessTokenTTL));
|
|
||||||
$accessToken->setClient($client);
|
|
||||||
$accessToken->setUserIdentifier($client->getIdentifier());
|
|
||||||
|
|
||||||
// Associate scopes with the session and access token
|
|
||||||
foreach ($scopes as $scope) {
|
|
||||||
$accessToken->addScope($scope);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Save the token
|
|
||||||
$this->accessTokenRepository->persistNewAccessToken($accessToken);
|
$this->accessTokenRepository->persistNewAccessToken($accessToken);
|
||||||
|
|
||||||
// Inject access token into token type
|
// Inject access token into response type
|
||||||
$responseType->setAccessToken($accessToken);
|
$responseType->setAccessToken($accessToken);
|
||||||
|
|
||||||
return $responseType;
|
return $responseType;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @inheritdoc
|
|
||||||
*/
|
|
||||||
public function canRespondToRequest(ServerRequestInterface $request)
|
|
||||||
{
|
|
||||||
return (
|
|
||||||
array_key_exists('grant_type', $request->getParsedBody())
|
|
||||||
&& $request->getParsedBody()['grant_type'] === 'client_credentials'
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -11,12 +11,9 @@
|
|||||||
|
|
||||||
namespace League\OAuth2\Server\Grant;
|
namespace League\OAuth2\Server\Grant;
|
||||||
|
|
||||||
use DateInterval;
|
|
||||||
use League\Event\Event;
|
use League\Event\Event;
|
||||||
use League\OAuth2\Server\Entities\AccessTokenEntity;
|
|
||||||
use League\OAuth2\Server\Entities\Interfaces\ClientEntityInterface;
|
use League\OAuth2\Server\Entities\Interfaces\ClientEntityInterface;
|
||||||
use League\OAuth2\Server\Entities\Interfaces\UserEntityInterface;
|
use League\OAuth2\Server\Entities\Interfaces\UserEntityInterface;
|
||||||
use League\OAuth2\Server\Entities\RefreshTokenEntity;
|
|
||||||
use League\OAuth2\Server\Exception\OAuthServerException;
|
use League\OAuth2\Server\Exception\OAuthServerException;
|
||||||
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface;
|
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface;
|
||||||
use League\OAuth2\Server\Repositories\ClientRepositoryInterface;
|
use League\OAuth2\Server\Repositories\ClientRepositoryInterface;
|
||||||
@ -24,7 +21,6 @@ use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface;
|
|||||||
use League\OAuth2\Server\Repositories\ScopeRepositoryInterface;
|
use League\OAuth2\Server\Repositories\ScopeRepositoryInterface;
|
||||||
use League\OAuth2\Server\Repositories\UserRepositoryInterface;
|
use League\OAuth2\Server\Repositories\UserRepositoryInterface;
|
||||||
use League\OAuth2\Server\ResponseTypes\ResponseTypeInterface;
|
use League\OAuth2\Server\ResponseTypes\ResponseTypeInterface;
|
||||||
use League\OAuth2\Server\Utils\SecureKey;
|
|
||||||
use Psr\Http\Message\ServerRequestInterface;
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -32,6 +28,13 @@ use Psr\Http\Message\ServerRequestInterface;
|
|||||||
*/
|
*/
|
||||||
class PasswordGrant extends AbstractGrant
|
class PasswordGrant extends AbstractGrant
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* Grant identifier
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $identifier = 'password';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var \League\OAuth2\Server\Repositories\UserRepositoryInterface
|
* @var \League\OAuth2\Server\Repositories\UserRepositoryInterface
|
||||||
*/
|
*/
|
||||||
@ -56,9 +59,10 @@ class PasswordGrant extends AbstractGrant
|
|||||||
AccessTokenRepositoryInterface $accessTokenRepository,
|
AccessTokenRepositoryInterface $accessTokenRepository,
|
||||||
RefreshTokenRepositoryInterface $refreshTokenRepository
|
RefreshTokenRepositoryInterface $refreshTokenRepository
|
||||||
) {
|
) {
|
||||||
|
parent::__construct($clientRepository, $scopeRepository, $accessTokenRepository);
|
||||||
|
|
||||||
$this->userRepository = $userRepository;
|
$this->userRepository = $userRepository;
|
||||||
$this->refreshTokenRepository = $refreshTokenRepository;
|
$this->refreshTokenRepository = $refreshTokenRepository;
|
||||||
parent::__construct($clientRepository, $scopeRepository, $accessTokenRepository);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -67,97 +71,17 @@ class PasswordGrant extends AbstractGrant
|
|||||||
public function respondToRequest(
|
public function respondToRequest(
|
||||||
ServerRequestInterface $request,
|
ServerRequestInterface $request,
|
||||||
ResponseTypeInterface $responseType,
|
ResponseTypeInterface $responseType,
|
||||||
DateInterval $tokenTTL,
|
\DateInterval $tokenTTL,
|
||||||
$scopeDelimiter = ' '
|
$scopeDelimiter = ' '
|
||||||
) {
|
) {
|
||||||
// Get the required params
|
// Validate request
|
||||||
$clientId = isset($request->getParsedBody()['client_id'])
|
$client = $this->validateClient($request);
|
||||||
? $request->getParsedBody()['client_id'] // $_POST['client_id']
|
$user = $this->validateUser($request);
|
||||||
: (isset($request->getServerParams()['PHP_AUTH_USER'])
|
$scopes = $this->validateScopes($this->getRequestParameter('scope', $request), $scopeDelimiter, $client);
|
||||||
? $request->getServerParams()['PHP_AUTH_USER'] // $_SERVER['PHP_AUTH_USER']
|
|
||||||
: null);
|
|
||||||
|
|
||||||
if (is_null($clientId)) {
|
// Issue and persist new tokens
|
||||||
throw OAuthServerException::invalidRequest('client_id', null, '`%s` parameter is missing');
|
$accessToken = $this->issueAccessToken($tokenTTL, $client, $user->getIdentifier(), $scopes);
|
||||||
}
|
$refreshToken = $this->issueRefreshToken($accessToken);
|
||||||
|
|
||||||
$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);
|
|
||||||
|
|
||||||
if (is_null($clientSecret)) {
|
|
||||||
throw OAuthServerException::invalidRequest('client_secret', null, '`%s` parameter is missing');
|
|
||||||
}
|
|
||||||
|
|
||||||
// Validate client ID and client secret
|
|
||||||
$client = $this->clientRepository->getClientEntity(
|
|
||||||
$clientId,
|
|
||||||
$clientSecret,
|
|
||||||
null,
|
|
||||||
$this->getIdentifier()
|
|
||||||
);
|
|
||||||
|
|
||||||
if (($client instanceof ClientEntityInterface) === false) {
|
|
||||||
$this->emitter->emit(new Event('client.authentication.failed', $request));
|
|
||||||
throw OAuthServerException::invalidClient();
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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);
|
|
||||||
|
|
||||||
if (is_null($username)) {
|
|
||||||
throw OAuthServerException::invalidRequest('username', null, '`%s` parameter is missing');
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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);
|
|
||||||
|
|
||||||
if (is_null($password)) {
|
|
||||||
throw OAuthServerException::invalidRequest('password', null, '`%s` parameter is missing');
|
|
||||||
}
|
|
||||||
|
|
||||||
// Verify user's username and password
|
|
||||||
$userEntity = $this->userRepository->getUserEntityByUserCredentials($username, $password);
|
|
||||||
if (($userEntity instanceof UserEntityInterface) === false) {
|
|
||||||
$this->emitter->emit(new Event('user.authentication.failed', $request));
|
|
||||||
throw OAuthServerException::invalidCredentials();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Validate any scopes that are in the request
|
|
||||||
$scopeParam = isset($request->getParsedBody()['scope'])
|
|
||||||
? $request->getParsedBody()['scope'] // $_POST['scope']
|
|
||||||
: '';
|
|
||||||
$scopes = $this->validateScopes($scopeParam, $scopeDelimiter, $client);
|
|
||||||
|
|
||||||
// Generate an access token
|
|
||||||
$accessToken = new AccessTokenEntity();
|
|
||||||
$accessToken->setIdentifier(SecureKey::generate());
|
|
||||||
$accessToken->setExpiryDateTime((new \DateTime())->add($tokenTTL));
|
|
||||||
$accessToken->setClient($client);
|
|
||||||
$accessToken->setUserIdentifier($userEntity->getIdentifier());
|
|
||||||
|
|
||||||
// Associate scopes with the session and access token
|
|
||||||
foreach ($scopes as $scope) {
|
|
||||||
$accessToken->addScope($scope);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Generate a refresh token
|
|
||||||
$refreshToken = new RefreshTokenEntity();
|
|
||||||
$refreshToken->setIdentifier(SecureKey::generate());
|
|
||||||
$refreshToken->setExpiryDateTime((new \DateTime())->add(new DateInterval('P1M')));
|
|
||||||
$refreshToken->setAccessToken($accessToken);
|
|
||||||
|
|
||||||
// Persist the tokens
|
|
||||||
$this->accessTokenRepository->persistNewAccessToken($accessToken);
|
$this->accessTokenRepository->persistNewAccessToken($accessToken);
|
||||||
$this->refreshTokenRepository->persistNewRefreshToken($refreshToken);
|
$this->refreshTokenRepository->persistNewRefreshToken($refreshToken);
|
||||||
|
|
||||||
@ -169,13 +93,31 @@ class PasswordGrant extends AbstractGrant
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @inheritdoc
|
* @param \Psr\Http\Message\ServerRequestInterface $request
|
||||||
|
*
|
||||||
|
* @return \League\OAuth2\Server\Entities\Interfaces\UserEntityInterface
|
||||||
|
*
|
||||||
|
* @throws \League\OAuth2\Server\Exception\OAuthServerException
|
||||||
*/
|
*/
|
||||||
public function canRespondToRequest(ServerRequestInterface $request)
|
protected function validateUser(ServerRequestInterface $request)
|
||||||
{
|
{
|
||||||
return (
|
$username = $this->getRequestParameter('username', $request);
|
||||||
isset($request->getParsedBody()['grant_type'])
|
if (is_null($username)) {
|
||||||
&& $request->getParsedBody()['grant_type'] === 'password'
|
throw OAuthServerException::invalidRequest('username', null, '`%s` parameter is missing');
|
||||||
);
|
}
|
||||||
|
|
||||||
|
$password = $this->getRequestParameter('password', $request);
|
||||||
|
if (is_null($password)) {
|
||||||
|
throw OAuthServerException::invalidRequest('password', null, '`%s` parameter is missing');
|
||||||
|
}
|
||||||
|
|
||||||
|
$user = $this->userRepository->getUserEntityByUserCredentials($username, $password);
|
||||||
|
if (!$user instanceof UserEntityInterface) {
|
||||||
|
$this->emitter->emit(new Event('user.authentication.failed', $request));
|
||||||
|
|
||||||
|
throw OAuthServerException::invalidCredentials();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $user;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user