Added grant and scopes to UserRepository getUserEntityByUserCredentials method

This commit is contained in:
Alex Bilbie 2016-03-22 14:18:02 +00:00
parent c880d5c1ec
commit 400eae153b
4 changed files with 24 additions and 9 deletions

View File

@ -123,7 +123,9 @@ class AuthCodeGrant extends AbstractAuthorizeGrant
if ($userId === null && $usernameParameter !== null && $passwordParameter !== null) { if ($userId === null && $usernameParameter !== null && $passwordParameter !== null) {
$userEntity = $this->userRepository->getUserEntityByUserCredentials( $userEntity = $this->userRepository->getUserEntityByUserCredentials(
$usernameParameter, $usernameParameter,
$passwordParameter $passwordParameter,
$this->getIdentifier(),
$scopes
); );
if ($userEntity instanceof UserEntityInterface) { if ($userEntity instanceof UserEntityInterface) {

View File

@ -121,7 +121,9 @@ class ImplicitGrant extends AbstractAuthorizeGrant
if ($userId === null && $usernameParameter !== null && $passwordParameter !== null) { if ($userId === null && $usernameParameter !== null && $passwordParameter !== null) {
$userEntity = $this->userRepository->getUserEntityByUserCredentials( $userEntity = $this->userRepository->getUserEntityByUserCredentials(
$usernameParameter, $usernameParameter,
$passwordParameter $passwordParameter,
$this->getIdentifier(),
$scopes
); );
if ($userEntity instanceof UserEntityInterface) { if ($userEntity instanceof UserEntityInterface) {

View File

@ -11,6 +11,7 @@
namespace League\OAuth2\Server\Grant; namespace League\OAuth2\Server\Grant;
use League\Event\Event; use League\Event\Event;
use League\OAuth2\Server\Entities\Interfaces\ScopeEntityInterface;
use League\OAuth2\Server\Entities\Interfaces\UserEntityInterface; use League\OAuth2\Server\Entities\Interfaces\UserEntityInterface;
use League\OAuth2\Server\Exception\OAuthServerException; use League\OAuth2\Server\Exception\OAuthServerException;
use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface; use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface;
@ -47,8 +48,8 @@ class PasswordGrant extends AbstractGrant
) { ) {
// Validate request // Validate request
$client = $this->validateClient($request); $client = $this->validateClient($request);
$user = $this->validateUser($request);
$scopes = $this->validateScopes($this->getRequestParameter('scope', $request), $client); $scopes = $this->validateScopes($this->getRequestParameter('scope', $request), $client);
$user = $this->validateUser($request, $scopes);
// Issue and persist new tokens // Issue and persist new tokens
$accessToken = $this->issueAccessToken($accessTokenTTL, $client, $user->getIdentifier(), $scopes); $accessToken = $this->issueAccessToken($accessTokenTTL, $client, $user->getIdentifier(), $scopes);
@ -64,11 +65,12 @@ class PasswordGrant extends AbstractGrant
/** /**
* @param \Psr\Http\Message\ServerRequestInterface $request * @param \Psr\Http\Message\ServerRequestInterface $request
* *
* @throws \League\OAuth2\Server\Exception\OAuthServerException * @param ScopeEntityInterface[] $scopes
* *
* @return \League\OAuth2\Server\Entities\Interfaces\UserEntityInterface * @return \League\OAuth2\Server\Entities\Interfaces\UserEntityInterface
* @throws \League\OAuth2\Server\Exception\OAuthServerException
*/ */
protected function validateUser(ServerRequestInterface $request) protected function validateUser(ServerRequestInterface $request, array $scopes)
{ {
$username = $this->getRequestParameter('username', $request); $username = $this->getRequestParameter('username', $request);
if (is_null($username)) { if (is_null($username)) {
@ -80,7 +82,12 @@ class PasswordGrant extends AbstractGrant
throw OAuthServerException::invalidRequest('password', '`%s` parameter is missing'); throw OAuthServerException::invalidRequest('password', '`%s` parameter is missing');
} }
$user = $this->userRepository->getUserEntityByUserCredentials($username, $password); $user = $this->userRepository->getUserEntityByUserCredentials(
$username,
$password,
$this->getIdentifier(),
$scopes
);
if (!$user instanceof UserEntityInterface) { if (!$user instanceof UserEntityInterface) {
$this->getEmitter()->emit(new Event('user.authentication.failed', $request)); $this->getEmitter()->emit(new Event('user.authentication.failed', $request));

View File

@ -2,6 +2,8 @@
namespace League\OAuth2\Server\Repositories; namespace League\OAuth2\Server\Repositories;
use League\OAuth2\Server\Entities\Interfaces\ScopeEntityInterface;
interface UserRepositoryInterface extends RepositoryInterface interface UserRepositoryInterface extends RepositoryInterface
{ {
/** /**
@ -9,8 +11,10 @@ interface UserRepositoryInterface extends RepositoryInterface
* *
* @param string $username * @param string $username
* @param string $password * @param string $password
* @param string $grantType The grant type used
* @param ScopeEntityInterface[] $scopes
* *
* @return \League\OAuth2\Server\Entities\Interfaces\UserEntityInterface * @return \League\OAuth2\Server\Entities\Interfaces\UserEntityInterface
*/ */
public function getUserEntityByUserCredentials($username, $password); public function getUserEntityByUserCredentials($username, $password, $grantType, array &$scopes);
} }