oauth2-server/src/Grant/PasswordGrant.php

181 lines
6.8 KiB
PHP
Raw Normal View History

2013-02-12 16:22:14 +00:00
<?php
/**
* OAuth 2.0 Password grant
*
2014-01-08 16:15:29 +00:00
* @package league/oauth2-server
* @author Alex Bilbie <hello@alexbilbie.com>
2014-03-09 19:34:23 +00:00
* @copyright Copyright (c) Alex Bilbie
* @license http://mit-license.org/
2014-03-09 20:05:38 +00:00
* @link https://github.com/thephpleague/oauth2-server
*/
2013-02-12 16:22:14 +00:00
namespace League\OAuth2\Server\Grant;
2013-05-08 11:06:09 -07:00
2015-04-05 21:10:41 +01:00
use DateInterval;
use League\Event\Emitter;
use League\Event\Event;
use League\OAuth2\Server\Entities\AccessTokenEntity;
use League\OAuth2\Server\Entities\Interfaces\ClientEntityInterface;
use League\OAuth2\Server\Entities\Interfaces\UserEntityInterface;
2014-05-01 14:32:54 +01:00
use League\OAuth2\Server\Exception;
2015-04-05 21:10:41 +01:00
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface;
use League\OAuth2\Server\Repositories\ClientRepositoryInterface;
use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface;
use League\OAuth2\Server\Repositories\ScopeRepositoryInterface;
use League\OAuth2\Server\Repositories\UserRepositoryInterface;
use League\OAuth2\Server\TokenTypes\TokenTypeInterface;
use League\OAuth2\Server\Utils\SecureKey;
use Symfony\Component\HttpFoundation\Request;
2013-02-12 16:22:14 +00:00
/**
* Password grant class
*/
2014-05-02 17:24:55 +01:00
class PasswordGrant extends AbstractGrant
2014-01-08 16:15:29 +00:00
{
/**
* Grant identifier
2014-12-10 13:10:35 +00:00
*
* @var string
*/
2013-02-12 16:22:14 +00:00
protected $identifier = 'password';
/**
* Callback to authenticate a user's name and password
2014-12-10 13:10:35 +00:00
*
2014-11-07 01:36:17 +00:00
* @var callable
*/
2014-01-10 12:30:13 +00:00
protected $callback;
/**
2015-04-05 21:10:41 +01:00
* @var \League\OAuth2\Server\Repositories\UserRepositoryInterface
*/
2015-04-05 21:10:41 +01:00
protected $userRepository;
/**
2015-04-05 21:10:41 +01:00
* @var \League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface
*/
2015-04-05 21:10:41 +01:00
protected $refreshTokenRepository;
2013-02-12 16:22:14 +00:00
/**
2015-04-05 21:10:41 +01:00
* @param \League\Event\Emitter $emitter
* @param \League\OAuth2\Server\Repositories\ClientRepositoryInterface $clientRepository
* @param \League\OAuth2\Server\Repositories\ScopeRepositoryInterface $scopeRepository
* @param \League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface $accessTokenRepository
* @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(
Emitter $emitter,
ClientRepositoryInterface $clientRepository,
ScopeRepositoryInterface $scopeRepository,
AccessTokenRepositoryInterface $accessTokenRepository,
UserRepositoryInterface $userRepository,
RefreshTokenRepositoryInterface $refreshTokenRepository = null
) {
$this->userRepository = $userRepository;
$this->refreshTokenRepository = $refreshTokenRepository;
parent::__construct($emitter, $clientRepository, $scopeRepository, $accessTokenRepository);
2013-02-12 16:22:14 +00:00
}
/**
2015-04-05 21:10:41 +01:00
* Return an access token
2014-12-10 13:10:35 +00:00
*
2015-04-05 21:10:41 +01:00
* @param \Symfony\Component\HttpFoundation\Request $request
* @param \League\OAuth2\Server\TokenTypes\TokenTypeInterface $tokenType
* @param \DateInterval $accessTokenTTL
* @param string $scopeDelimiter
2014-12-10 13:10:35 +00:00
*
2015-04-05 21:10:41 +01:00
* @return \League\OAuth2\Server\TokenTypes\TokenTypeInterface
* @throws \League\OAuth2\Server\Exception\InvalidClientException
* @throws \League\OAuth2\Server\Exception\InvalidCredentialsException
* @throws \League\OAuth2\Server\Exception\InvalidRequestException
* @throws \League\OAuth2\Server\Exception\InvalidScopeException
*/
2015-04-05 21:10:41 +01:00
public function getAccessTokenAsType(
Request $request,
TokenTypeInterface $tokenType,
DateInterval $accessTokenTTL,
$scopeDelimiter = ' '
) {
// Get the required params
2015-04-05 21:10:41 +01:00
$clientId = $request->request->get('client_id', $request->getUser());
2013-12-24 17:01:56 +00:00
if (is_null($clientId)) {
throw new Exception\InvalidRequestException('client_id');
2013-02-12 16:22:14 +00:00
}
2015-04-05 21:10:41 +01:00
$clientSecret = $request->request->get('client_secret',
$request->getPassword());
2013-12-24 17:01:56 +00:00
if (is_null($clientSecret)) {
throw new Exception\InvalidRequestException('client_secret');
2013-02-12 16:22:14 +00:00
}
2013-12-24 17:01:56 +00:00
// Validate client ID and client secret
2015-04-05 21:10:41 +01:00
$client = $this->clientRepository->get(
2013-12-24 17:01:56 +00:00
$clientId,
$clientSecret,
null,
$this->getIdentifier()
);
2013-02-12 16:22:14 +00:00
2015-04-05 21:10:41 +01:00
if (($client instanceof ClientEntityInterface) === false) {
$this->emitter->emit(new Event('client.authentication.failed', $request));
2014-05-01 14:32:54 +01:00
throw new Exception\InvalidClientException();
2013-02-12 16:22:14 +00:00
}
2015-04-05 21:10:41 +01:00
$username = $request->request->get('username', null);
2013-12-24 17:01:56 +00:00
if (is_null($username)) {
2014-05-01 14:32:54 +01:00
throw new Exception\InvalidRequestException('username');
2013-02-12 16:22:14 +00:00
}
2015-04-05 21:10:41 +01:00
$password = $request->request->get('password', null);
2013-12-24 17:01:56 +00:00
if (is_null($password)) {
2014-05-01 14:32:54 +01:00
throw new Exception\InvalidRequestException('password');
2013-02-12 16:22:14 +00:00
}
// Check if user's username and password are correct
2015-04-05 21:10:41 +01:00
$user = $this->userRepository->getByCredentials($username, $password);
2013-02-12 16:22:14 +00:00
2015-04-05 21:10:41 +01:00
if (($user instanceof UserEntityInterface) === false) {
$this->emitter->emit(new Event('user.authentication.failed', $request));
2014-05-01 14:32:54 +01:00
throw new Exception\InvalidCredentialsException();
2013-02-12 16:22:14 +00:00
}
// Validate any scopes that are in the request
2015-04-05 21:10:41 +01:00
$scopeParamValue = $request->request->get('scope', '');
$scopes = $this->validateScopes($scopeParamValue, $scopeDelimiter, $client);
2013-03-22 11:41:04 +00:00
2013-12-24 17:01:56 +00:00
// Generate an access token
2015-04-05 21:10:41 +01:00
$accessToken = new AccessTokenEntity();
$accessToken->setIdentifier(SecureKey::generate());
$expirationDateTime = (new \DateTime())->add($accessTokenTTL);
$accessToken->setExpiryDateTime($expirationDateTime);
2015-04-05 21:10:41 +01:00
$accessToken->setOwner('user', $user->getIdentifier());
$accessToken->setClient($client);
2015-04-05 21:10:41 +01:00
// Associate scopes with the access token
foreach ($scopes as $scope) {
2015-04-05 21:10:41 +01:00
$accessToken->addScope($scope);
}
2015-04-05 21:10:41 +01:00
$tokenType->setAccessToken($accessToken);
2013-02-12 16:22:14 +00:00
2013-12-24 17:01:56 +00:00
// Associate a refresh token if set
2015-04-05 21:10:41 +01:00
if ($this->refreshTokenRepository instanceof RefreshTokenRepositoryInterface) {
// $refreshToken = new RefreshTokenEntity($this->server);
// $refreshToken->setId(SecureKey::generate());
// $refreshToken->setExpireTime($this->server->getGrantType('refresh_token')->getRefreshTokenTTL() + time());
// $refreshToken->setAccessToken($accessToken);
// $this->server->getTokenType()->setParam('refresh_token', $refreshToken->getId());
// $tokenType->setParam('refresh_token', $refreshToken);
}
2015-04-05 21:10:41 +01:00
// Save the access token
$this->accessTokenRepository->create($accessToken);
2013-02-12 16:22:14 +00:00
2015-04-05 21:10:41 +01:00
// Inject the access token into token type
$tokenType->setAccessToken($accessToken);
2013-02-12 16:22:14 +00:00
2015-04-05 21:10:41 +01:00
return $tokenType;
2013-02-12 16:22:14 +00:00
}
2013-09-07 18:00:13 +01:00
}