Code tidy

This commit is contained in:
Alex Bilbie 2016-01-13 00:13:29 +00:00
parent 0efa7cd7ea
commit 79791e5848

View File

@ -20,6 +20,7 @@ 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;
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;
@ -36,19 +37,27 @@ class PasswordGrant extends AbstractGrant
*/ */
private $userRepository; private $userRepository;
/**
* @var \League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface
*/
private $refreshTokenRepository;
/** /**
* @param \League\OAuth2\Server\Repositories\UserRepositoryInterface $userRepository * @param \League\OAuth2\Server\Repositories\UserRepositoryInterface $userRepository
* @param \League\OAuth2\Server\Repositories\ClientRepositoryInterface $clientRepository * @param \League\OAuth2\Server\Repositories\ClientRepositoryInterface $clientRepository
* @param \League\OAuth2\Server\Repositories\ScopeRepositoryInterface $scopeRepository * @param \League\OAuth2\Server\Repositories\ScopeRepositoryInterface $scopeRepository
* @param \League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface $accessTokenRepository * @param \League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface $accessTokenRepository
* @param \League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface $refreshTokenRepository
*/ */
public function __construct( public function __construct(
UserRepositoryInterface $userRepository, UserRepositoryInterface $userRepository,
ClientRepositoryInterface $clientRepository, ClientRepositoryInterface $clientRepository,
ScopeRepositoryInterface $scopeRepository, ScopeRepositoryInterface $scopeRepository,
AccessTokenRepositoryInterface $accessTokenRepository AccessTokenRepositoryInterface $accessTokenRepository,
RefreshTokenRepositoryInterface $refreshTokenRepository
) { ) {
$this->userRepository = $userRepository; $this->userRepository = $userRepository;
$this->refreshTokenRepository = $refreshTokenRepository;
parent::__construct($clientRepository, $scopeRepository, $accessTokenRepository); parent::__construct($clientRepository, $scopeRepository, $accessTokenRepository);
} }
@ -58,7 +67,7 @@ class PasswordGrant 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 // Get the required params
@ -133,7 +142,7 @@ class PasswordGrant extends AbstractGrant
// Generate an access token // Generate an access token
$accessToken = new AccessTokenEntity(); $accessToken = new AccessTokenEntity();
$accessToken->setIdentifier(SecureKey::generate()); $accessToken->setIdentifier(SecureKey::generate());
$accessToken->setExpiryDateTime((new \DateTime())->add($accessTokenTTL)); $accessToken->setExpiryDateTime((new \DateTime())->add($tokenTTL));
$accessToken->setClient($client); $accessToken->setClient($client);
$accessToken->setUserIdentifier($userEntity->getIdentifier()); $accessToken->setUserIdentifier($userEntity->getIdentifier());
@ -142,15 +151,16 @@ class PasswordGrant extends AbstractGrant
$accessToken->addScope($scope); $accessToken->addScope($scope);
} }
// Persist the token
$this->accessTokenRepository->persistNewAccessToken($accessToken);
// Generate a refresh token // Generate a refresh token
$refreshToken = new RefreshTokenEntity(); $refreshToken = new RefreshTokenEntity();
$refreshToken->setIdentifier(SecureKey::generate()); $refreshToken->setIdentifier(SecureKey::generate());
$refreshToken->setExpiryDateTime((new \DateTime())->add(new DateInterval('P1M'))); $refreshToken->setExpiryDateTime((new \DateTime())->add(new DateInterval('P1M')));
$refreshToken->setAccessToken($accessToken); $refreshToken->setAccessToken($accessToken);
// Persist the tokens
$this->accessTokenRepository->persistNewAccessToken($accessToken);
$this->refreshTokenRepository->persistNewRefreshToken($refreshToken);
// Inject tokens into response // Inject tokens into response
$responseType->setAccessToken($accessToken); $responseType->setAccessToken($accessToken);
$responseType->setRefreshToken($refreshToken); $responseType->setRefreshToken($refreshToken);