diff --git a/src/Grant/AbstractGrant.php b/src/Grant/AbstractGrant.php index a51d18e0..ff7aaced 100644 --- a/src/Grant/AbstractGrant.php +++ b/src/Grant/AbstractGrant.php @@ -12,12 +12,16 @@ namespace League\OAuth2\Server\Grant; 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\RefreshTokenEntity; use League\OAuth2\Server\Entities\ScopeEntity; use League\OAuth2\Server\Exception\OAuthServerException; use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface; use League\OAuth2\Server\Repositories\ClientRepositoryInterface; use League\OAuth2\Server\Repositories\ScopeRepositoryInterface; +use League\OAuth2\Server\Utils\SecureKey; use Psr\Http\Message\ServerRequestInterface; /** @@ -95,6 +99,77 @@ abstract class AbstractGrant implements GrantTypeInterface 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 $scopeDelimiterString The delimiter between the scopes in the value string @@ -142,4 +217,57 @@ abstract class AbstractGrant implements GrantTypeInterface { $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 + ); + } } diff --git a/src/Grant/ClientCredentialsGrant.php b/src/Grant/ClientCredentialsGrant.php index 7ae88aa7..1ed22b8d 100644 --- a/src/Grant/ClientCredentialsGrant.php +++ b/src/Grant/ClientCredentialsGrant.php @@ -11,13 +11,9 @@ 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\Exception\OAuthServerException; use League\OAuth2\Server\ResponseTypes\ResponseTypeInterface; -use League\OAuth2\Server\Utils\SecureKey; use Psr\Http\Message\ServerRequestInterface; /** @@ -38,78 +34,20 @@ class ClientCredentialsGrant extends AbstractGrant public function respondToRequest( ServerRequestInterface $request, ResponseTypeInterface $responseType, - DateInterval $accessTokenTTL, + \DateInterval $tokenTTL, $scopeDelimiter = ' ' ) { - // Get the required params - $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); + // Validate request + $client = $this->validateClient($request); + $scopes = $this->validateScopes($this->getRequestParameter('scope', $request), $scopeDelimiter, $client); - if (is_null($clientId)) { - throw OAuthServerException::invalidRequest('client_id', null, '`%s` parameter is missing'); - } - - $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 + // Issue and persist access token + $accessToken = $this->issueAccessToken($tokenTTL, $client, $client->getIdentifier(), $scopes); $this->accessTokenRepository->persistNewAccessToken($accessToken); - // Inject access token into token type + // Inject access token into response type $responseType->setAccessToken($accessToken); return $responseType; } - - /** - * @inheritdoc - */ - public function canRespondToRequest(ServerRequestInterface $request) - { - return ( - array_key_exists('grant_type', $request->getParsedBody()) - && $request->getParsedBody()['grant_type'] === 'client_credentials' - ); - } } diff --git a/src/Grant/PasswordGrant.php b/src/Grant/PasswordGrant.php index fee9d798..30702744 100644 --- a/src/Grant/PasswordGrant.php +++ b/src/Grant/PasswordGrant.php @@ -11,12 +11,9 @@ 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\UserEntityInterface; -use League\OAuth2\Server\Entities\RefreshTokenEntity; use League\OAuth2\Server\Exception\OAuthServerException; use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface; 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\UserRepositoryInterface; use League\OAuth2\Server\ResponseTypes\ResponseTypeInterface; -use League\OAuth2\Server\Utils\SecureKey; use Psr\Http\Message\ServerRequestInterface; /** @@ -32,6 +28,13 @@ use Psr\Http\Message\ServerRequestInterface; */ class PasswordGrant extends AbstractGrant { + /** + * Grant identifier + * + * @var string + */ + protected $identifier = 'password'; + /** * @var \League\OAuth2\Server\Repositories\UserRepositoryInterface */ @@ -56,9 +59,10 @@ class PasswordGrant extends AbstractGrant AccessTokenRepositoryInterface $accessTokenRepository, RefreshTokenRepositoryInterface $refreshTokenRepository ) { + parent::__construct($clientRepository, $scopeRepository, $accessTokenRepository); + $this->userRepository = $userRepository; $this->refreshTokenRepository = $refreshTokenRepository; - parent::__construct($clientRepository, $scopeRepository, $accessTokenRepository); } /** @@ -67,97 +71,17 @@ class PasswordGrant extends AbstractGrant public function respondToRequest( ServerRequestInterface $request, ResponseTypeInterface $responseType, - DateInterval $tokenTTL, + \DateInterval $tokenTTL, $scopeDelimiter = ' ' ) { - // Get the required params - $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); + // Validate request + $client = $this->validateClient($request); + $user = $this->validateUser($request); + $scopes = $this->validateScopes($this->getRequestParameter('scope', $request), $scopeDelimiter, $client); - if (is_null($clientId)) { - throw OAuthServerException::invalidRequest('client_id', null, '`%s` parameter is missing'); - } - - $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 + // Issue and persist new tokens + $accessToken = $this->issueAccessToken($tokenTTL, $client, $user->getIdentifier(), $scopes); + $refreshToken = $this->issueRefreshToken($accessToken); $this->accessTokenRepository->persistNewAccessToken($accessToken); $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 ( - isset($request->getParsedBody()['grant_type']) - && $request->getParsedBody()['grant_type'] === 'password' - ); + $username = $this->getRequestParameter('username', $request); + if (is_null($username)) { + 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; } }