2013-02-12 16:22:14 +00:00
|
|
|
<?php
|
2013-02-13 19:39:43 +00:00
|
|
|
/**
|
|
|
|
* OAuth 2.0 Password grant
|
|
|
|
*
|
2014-01-08 16:15:29 +00:00
|
|
|
* @package league/oauth2-server
|
2013-02-13 19:39:43 +00:00
|
|
|
* @author Alex Bilbie <hello@alexbilbie.com>
|
2014-03-09 19:34:23 +00:00
|
|
|
* @copyright Copyright (c) Alex Bilbie
|
2013-02-13 19:39:43 +00:00
|
|
|
* @license http://mit-license.org/
|
2014-03-09 20:05:38 +00:00
|
|
|
* @link https://github.com/thephpleague/oauth2-server
|
2013-02-13 19:39:43 +00:00
|
|
|
*/
|
2013-02-12 16:22:14 +00:00
|
|
|
|
2013-05-08 11:42:23 -07:00
|
|
|
namespace League\OAuth2\Server\Grant;
|
2013-05-08 11:06:09 -07:00
|
|
|
|
2014-05-02 17:21:53 +01:00
|
|
|
use League\OAuth2\Server\Entity\ClientEntity;
|
|
|
|
use League\OAuth2\Server\Entity\AccessTokenEntity;
|
|
|
|
use League\OAuth2\Server\Entity\RefreshTokenEntity;
|
|
|
|
use League\OAuth2\Server\Entity\SessionEntity;
|
2014-05-01 14:32:54 +01:00
|
|
|
use League\OAuth2\Server\Exception;
|
2013-05-08 11:42:23 -07:00
|
|
|
use League\OAuth2\Server\Util\SecureKey;
|
2014-09-30 22:16:26 +01:00
|
|
|
use League\OAuth2\Server\Event;
|
2013-02-12 16:22:14 +00:00
|
|
|
|
2013-02-13 19:39:43 +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
|
|
|
{
|
2013-02-13 19:39:43 +00:00
|
|
|
/**
|
|
|
|
* Grant identifier
|
|
|
|
* @var string
|
|
|
|
*/
|
2013-02-12 16:22:14 +00:00
|
|
|
protected $identifier = 'password';
|
2013-02-13 19:39:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Response type
|
|
|
|
* @var string
|
|
|
|
*/
|
2014-01-10 12:30:13 +00:00
|
|
|
protected $responseType;
|
2013-02-13 19:39:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback to authenticate a user's name and password
|
|
|
|
* @var function
|
|
|
|
*/
|
2014-01-10 12:30:13 +00:00
|
|
|
protected $callback;
|
2013-03-06 16:59:18 +00:00
|
|
|
|
2013-05-06 15:04:00 -07:00
|
|
|
/**
|
|
|
|
* Access token expires in override
|
|
|
|
* @var int
|
|
|
|
*/
|
2014-01-10 12:30:13 +00:00
|
|
|
protected $accessTokenTTL;
|
2013-05-06 15:04:00 -07:00
|
|
|
|
2013-02-13 19:39:43 +00:00
|
|
|
/**
|
|
|
|
* Set the callback to verify a user's username and password
|
2014-05-03 10:53:57 +01:00
|
|
|
* @param callable $callback The callback function
|
2013-05-08 14:10:58 -07:00
|
|
|
* @return void
|
2013-02-13 19:39:43 +00:00
|
|
|
*/
|
2013-12-24 17:01:56 +00:00
|
|
|
public function setVerifyCredentialsCallback(callable $callback)
|
2013-02-12 16:22:14 +00:00
|
|
|
{
|
|
|
|
$this->callback = $callback;
|
|
|
|
}
|
|
|
|
|
2013-02-13 19:39:43 +00:00
|
|
|
/**
|
|
|
|
* Return the callback function
|
2013-05-08 14:10:58 -07:00
|
|
|
* @return callable
|
2013-02-13 19:39:43 +00:00
|
|
|
*/
|
2013-02-12 16:45:33 +00:00
|
|
|
protected function getVerifyCredentialsCallback()
|
2013-02-12 16:22:14 +00:00
|
|
|
{
|
|
|
|
if (is_null($this->callback) || ! is_callable($this->callback)) {
|
2014-05-01 14:32:54 +01:00
|
|
|
throw new Exception\ServerErrorException('Null or non-callable callback set on Password grant');
|
2013-02-12 16:22:14 +00:00
|
|
|
}
|
|
|
|
|
2013-02-12 16:45:33 +00:00
|
|
|
return $this->callback;
|
2013-02-12 16:22:14 +00:00
|
|
|
}
|
|
|
|
|
2013-02-13 19:39:43 +00:00
|
|
|
/**
|
|
|
|
* Complete the password grant
|
2014-11-07 01:25:13 +00:00
|
|
|
* @return \League\OAuth2\Server\TokenType\TokenTypeInterface
|
2013-02-13 19:39:43 +00:00
|
|
|
*/
|
2014-06-23 08:20:34 +01:00
|
|
|
public function completeFlow()
|
2013-02-12 16:22:14 +00:00
|
|
|
{
|
2013-02-13 19:39:43 +00:00
|
|
|
// Get the required params
|
2013-12-24 17:01:56 +00:00
|
|
|
$clientId = $this->server->getRequest()->request->get('client_id', null);
|
|
|
|
if (is_null($clientId)) {
|
2014-07-03 14:58:13 +07:00
|
|
|
$clientId = $this->server->getRequest()->getUser();
|
|
|
|
if (is_null($clientId)) {
|
|
|
|
throw new Exception\InvalidRequestException('client_id');
|
|
|
|
}
|
2013-02-12 16:22:14 +00:00
|
|
|
}
|
|
|
|
|
2013-12-24 17:01:56 +00:00
|
|
|
$clientSecret = $this->server->getRequest()->request->get('client_secret', null);
|
|
|
|
if (is_null($clientSecret)) {
|
2014-07-03 15:03:58 +07:00
|
|
|
$clientSecret = $this->server->getRequest()->getPassword();
|
2014-07-03 14:58:13 +07: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
|
2014-01-10 12:30:13 +00:00
|
|
|
$client = $this->server->getStorage('client')->get(
|
2013-12-24 17:01:56 +00:00
|
|
|
$clientId,
|
|
|
|
$clientSecret,
|
|
|
|
null,
|
|
|
|
$this->getIdentifier()
|
|
|
|
);
|
2013-02-12 16:22:14 +00:00
|
|
|
|
2014-05-02 17:21:53 +01:00
|
|
|
if (($client instanceof ClientEntity) === false) {
|
2014-09-30 22:16:26 +01:00
|
|
|
$this->server->getEventEmitter()->emit(new Event\ClientAuthenticationFailedEvent($this->server->getRequest()));
|
2014-05-01 14:32:54 +01:00
|
|
|
throw new Exception\InvalidClientException();
|
2013-02-12 16:22:14 +00:00
|
|
|
}
|
|
|
|
|
2013-12-24 17:01:56 +00:00
|
|
|
$username = $this->server->getRequest()->request->get('username', null);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-12-24 17:01:56 +00:00
|
|
|
$password = $this->server->getRequest()->request->get('password', null);
|
|
|
|
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
|
2013-12-24 17:01:56 +00:00
|
|
|
$userId = call_user_func($this->getVerifyCredentialsCallback(), $username, $password);
|
2013-02-12 16:22:14 +00:00
|
|
|
|
|
|
|
if ($userId === false) {
|
2014-09-30 22:16:26 +01:00
|
|
|
$this->server->getEventEmitter()->emit(new Event\UserAuthenticationFailedEvent($this->server->getRequest()));
|
2014-05-01 14:32:54 +01:00
|
|
|
throw new Exception\InvalidCredentialsException();
|
2013-02-12 16:22:14 +00:00
|
|
|
}
|
|
|
|
|
2013-03-06 17:18:37 +00:00
|
|
|
// Validate any scopes that are in the request
|
2013-12-24 17:01:56 +00:00
|
|
|
$scopeParam = $this->server->getRequest()->request->get('scope', '');
|
2014-09-30 23:55:21 +01:00
|
|
|
$scopes = $this->validateScopes($scopeParam, $client);
|
2013-03-06 17:18:37 +00:00
|
|
|
|
2013-12-24 17:01:56 +00:00
|
|
|
// Create a new session
|
2014-05-02 17:21:53 +01:00
|
|
|
$session = new SessionEntity($this->server);
|
2013-12-24 17:01:56 +00:00
|
|
|
$session->setOwner('user', $userId);
|
|
|
|
$session->associateClient($client);
|
2013-03-22 11:41:04 +00:00
|
|
|
|
2013-12-24 17:01:56 +00:00
|
|
|
// Generate an access token
|
2014-05-02 17:21:53 +01:00
|
|
|
$accessToken = new AccessTokenEntity($this->server);
|
2014-07-11 18:27:03 +01:00
|
|
|
$accessToken->setId(SecureKey::generate());
|
2014-09-11 13:39:50 +01:00
|
|
|
$accessToken->setExpireTime($this->getAccessTokenTTL() + time());
|
2013-03-06 17:18:37 +00:00
|
|
|
|
2013-12-24 17:01:56 +00:00
|
|
|
// Associate scopes with the session and access token
|
2013-03-06 17:18:37 +00:00
|
|
|
foreach ($scopes as $scope) {
|
2014-08-18 16:47:36 +01:00
|
|
|
$session->associateScope($scope);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($session->getScopes() as $scope) {
|
|
|
|
$accessToken->associateScope($scope);
|
2013-03-06 17:18:37 +00:00
|
|
|
}
|
|
|
|
|
2014-09-30 22:28:49 +01:00
|
|
|
$this->server->getTokenType()->setSession($session);
|
2014-09-30 22:28:38 +01:00
|
|
|
$this->server->getTokenType()->setParam('access_token', $accessToken->getId());
|
|
|
|
$this->server->getTokenType()->setParam('expires_in', $this->getAccessTokenTTL());
|
2013-02-12 16:22:14 +00:00
|
|
|
|
2013-12-24 17:01:56 +00:00
|
|
|
// Associate a refresh token if set
|
|
|
|
if ($this->server->hasGrantType('refresh_token')) {
|
2014-05-02 17:21:53 +01:00
|
|
|
$refreshToken = new RefreshTokenEntity($this->server);
|
2014-07-11 18:27:03 +01:00
|
|
|
$refreshToken->setId(SecureKey::generate());
|
2014-01-08 16:15:29 +00:00
|
|
|
$refreshToken->setExpireTime($this->server->getGrantType('refresh_token')->getRefreshTokenTTL() + time());
|
2014-09-30 22:28:38 +01:00
|
|
|
$this->server->getTokenType()->setParam('refresh_token', $refreshToken->getId());
|
2013-03-06 17:18:37 +00:00
|
|
|
}
|
|
|
|
|
2013-12-24 17:01:56 +00:00
|
|
|
// Save everything
|
2014-01-10 12:30:13 +00:00
|
|
|
$session->save();
|
2013-12-24 17:01:56 +00:00
|
|
|
$accessToken->setSession($session);
|
2014-01-10 12:30:13 +00:00
|
|
|
$accessToken->save();
|
2013-02-12 16:22:14 +00:00
|
|
|
|
2013-12-24 17:01:56 +00:00
|
|
|
if ($this->server->hasGrantType('refresh_token')) {
|
|
|
|
$refreshToken->setAccessToken($accessToken);
|
2014-01-10 12:30:13 +00:00
|
|
|
$refreshToken->save();
|
2013-02-12 16:22:14 +00:00
|
|
|
}
|
|
|
|
|
2014-04-23 17:02:50 +01:00
|
|
|
return $this->server->getTokenType()->generateResponse();
|
2013-02-12 16:22:14 +00:00
|
|
|
}
|
2013-09-07 18:00:13 +01:00
|
|
|
}
|