oauth2-server/src/Grant/PasswordGrant.php

111 lines
3.7 KiB
PHP
Raw Normal View History

2013-02-12 16:22:14 +00:00
<?php
/**
2016-02-19 18:09:39 -05:00
* OAuth 2.0 Password grant.
*
* @author Alex Bilbie <hello@alexbilbie.com>
2014-03-09 19:34:23 +00:00
* @copyright Copyright (c) Alex Bilbie
* @license http://mit-license.org/
2016-02-19 18:09:39 -05:00
*
2014-03-09 20:05:38 +00:00
* @link https://github.com/thephpleague/oauth2-server
*/
namespace League\OAuth2\Server\Grant;
2013-05-08 11:06:09 -07:00
use League\OAuth2\Server\Entities\Interfaces\ClientEntityInterface;
2015-04-05 21:10:41 +01:00
use League\OAuth2\Server\Entities\Interfaces\UserEntityInterface;
2015-11-16 12:58:11 +00:00
use League\OAuth2\Server\Exception\OAuthServerException;
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\UserRepositoryInterface;
2016-03-23 12:54:17 +00:00
use League\OAuth2\Server\RequestEvent;
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
/**
2016-02-19 18:09:39 -05: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-13 00:13:29 +00:00
/**
* @param \League\OAuth2\Server\Repositories\UserRepositoryInterface $userRepository
* @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,
2016-01-13 00:13:29 +00:00
RefreshTokenRepositoryInterface $refreshTokenRepository
2015-04-05 21:10:41 +01:00
) {
2016-03-15 20:54:59 +01:00
$this->setUserRepository($userRepository);
$this->setRefreshTokenRepository($refreshTokenRepository);
$this->refreshTokenTTL = new \DateInterval('P1M');
2013-02-12 16:22:14 +00:00
}
/**
2016-02-19 18:09:39 -05:00
* {@inheritdoc}
*/
2015-11-16 12:58:11 +00:00
public function respondToRequest(
ServerRequestInterface $request,
ResponseTypeInterface $responseType,
\DateInterval $accessTokenTTL
2015-04-05 21:10:41 +01:00
) {
2016-01-17 12:43:20 +00:00
// Validate request
2016-01-17 00:41:55 +01:00
$client = $this->validateClient($request);
$scopes = $this->validateScopes($this->getRequestParameter('scope', $request), $client);
$user = $this->validateUser($request, $client);
// Finalize the requested scopes
2016-03-24 10:04:15 +00:00
$scopes = $this->scopeRepository->finalizeScopes($scopes, $this->getIdentifier(), $client, $user->getIdentifier());
2013-02-12 16:22:14 +00:00
2016-01-17 12:43:20 +00:00
// Issue and persist new tokens
2016-01-20 12:21:44 +01:00
$accessToken = $this->issueAccessToken($accessTokenTTL, $client, $user->getIdentifier(), $scopes);
$refreshToken = $this->issueRefreshToken($accessToken);
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
* @param \League\OAuth2\Server\Entities\Interfaces\ClientEntityInterface $client
2016-03-22 10:18:21 -04:00
*
* @throws \League\OAuth2\Server\Exception\OAuthServerException
2016-03-22 11:11:39 -04:00
*
* @return \League\OAuth2\Server\Entities\Interfaces\UserEntityInterface
2016-01-17 00:41:55 +01:00
*/
protected function validateUser(ServerRequestInterface $request, ClientEntityInterface $client)
2016-01-17 00:41:55 +01:00
{
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)) {
2016-03-17 11:18:59 +00:00
throw OAuthServerException::invalidRequest('username', '`%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)) {
2016-03-17 11:18:59 +00:00
throw OAuthServerException::invalidRequest('password', '`%s` parameter is missing');
2013-02-12 16:22:14 +00:00
}
$user = $this->userRepository->getUserEntityByUserCredentials(
$username,
$password,
$this->getIdentifier(),
$client
);
2016-01-17 00:41:55 +01:00
if (!$user instanceof UserEntityInterface) {
2016-03-23 12:54:17 +00:00
$this->getEmitter()->emit(new RequestEvent('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
}
/**
2016-02-19 18:09:39 -05:00
* {@inheritdoc}
*/
public function getIdentifier()
{
return 'password';
}
2013-09-07 18:00:13 +01:00
}