203 lines
6.2 KiB
PHP
Raw Normal View History

2013-02-12 16:22:14 +00:00
<?php
/**
* OAuth 2.0 Password grant
*
* @package php-loep/oauth2-server
* @author Alex Bilbie <hello@alexbilbie.com>
* @copyright Copyright (c) 2013 PHP League of Extraordinary Packages
* @license http://mit-license.org/
2013-05-08 11:30:53 -07:00
* @link http://github.com/php-loep/oauth2-server
*/
2013-02-12 16:22:14 +00:00
namespace League\OAuth2\Server\Grant;
2013-05-08 11:06:09 -07:00
use League\OAuth2\Server\Authorization;
2013-12-24 17:01:56 +00:00
use League\OAuth2\Server\Entities\AccessToken;
use League\OAuth2\Server\Entities\Client;
use League\OAuth2\Server\Entities\RefreshToken;
use League\OAuth2\Server\Entities\Session;
use League\OAuth2\Server\Entities\Scope;
use League\OAuth2\Server\Exception\ClientException;
use League\OAuth2\Server\Exception\InvalidGrantTypeException;
use League\OAuth2\Server\Util\SecureKey;
use League\OAuth2\Server\Storage\SessionInterface;
use League\OAuth2\Server\Storage\ClientInterface;
use League\OAuth2\Server\Storage\ScopeInterface;
2013-02-12 16:22:14 +00:00
/**
* Password grant class
*/
2013-02-12 16:22:14 +00:00
class Password implements GrantTypeInterface {
use GrantTrait;
/**
* Grant identifier
* @var string
*/
2013-02-12 16:22:14 +00:00
protected $identifier = 'password';
/**
* Response type
* @var string
*/
2013-02-12 16:22:14 +00:00
protected $responseType = null;
/**
* Callback to authenticate a user's name and password
* @var function
*/
2013-02-12 16:22:14 +00:00
protected $callback = null;
/**
* AuthServer instance
* @var AuthServer
*/
protected $authServer = null;
/**
* Access token expires in override
* @var int
*/
protected $accessTokenTTL = null;
/**
* Set the callback to verify a user's username and password
2013-05-08 14:10:58 -07:00
* @param callable $callback The callback function
* @return void
*/
2013-12-24 17:01:56 +00:00
public function setVerifyCredentialsCallback(callable $callback)
2013-02-12 16:22:14 +00:00
{
$this->callback = $callback;
}
/**
* Return the callback function
2013-05-08 14:10:58 -07:00
* @return callable
*/
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)) {
2013-12-24 17:01:56 +00:00
throw new InvalidGrantTypeException('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
}
/**
* Complete the password grant
* @param null|array $inputParams
* @return array
*/
public function completeFlow($inputParams = null)
2013-02-12 16:22:14 +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)) {
throw new ClientException(
sprintf(Authorization::getExceptionMessage('invalid_request'), 'client_id'),
0
);
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)) {
throw new ClientException(
sprintf(Authorization::getExceptionMessage('invalid_request'), 'client_secret'),
0
);
2013-02-12 16:22:14 +00:00
}
2013-12-24 17:01:56 +00:00
// Validate client ID and client secret
$clientDetails = $this->server->getStorage('client')->getClient(
$clientId,
$clientSecret,
null,
$this->getIdentifier()
);
2013-02-12 16:22:14 +00:00
if ($clientDetails === false) {
2013-12-24 17:01:56 +00:00
throw new ClientException(Authorization::getExceptionMessage('invalid_client'), 8);
2013-02-12 16:22:14 +00:00
}
2013-12-24 17:01:56 +00:00
$client = new Client;
$client->setId($clientDetails['id']);
$client->setSecret($clientDetails['secret']);
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)) {
throw new ClientException(
sprintf(Authorization::getExceptionMessage('invalid_request'), 'username'),
0
);
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)) {
throw new ClientException(
sprintf(Authorization::getExceptionMessage('invalid_request'), 'password'),
0
);
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) {
throw new Exception\ClientException($this->authServer->getExceptionMessage('invalid_credentials'), 0);
2013-02-12 16:22:14 +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', '');
$scopes = $this->validateScopes($scopeParam);
2013-12-24 17:01:56 +00:00
// Create a new session
$session = new Session($this->server->getStorage('session'));
$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
$accessToken = new AccessToken($this->server->getStorage('access_token'));
$accessToken->setId(SecureKey::make());
$accessToken->setTimestamp(time());
$accessToken->setTTL($this->server->getAccessTokenTTL());
2013-12-24 17:01:56 +00:00
// Associate scopes with the session and access token
foreach ($scopes as $scope) {
2013-12-24 17:01:56 +00:00
$accessToken->associateScope($scope);
$session->associateScope($scope);
}
2013-12-24 17:01:56 +00:00
$response = [
'access_token' => $accessToken->getId(),
'token_type' => 'Bearer',
'expires' => $accessToken->getExpireTime(),
'expires_in' => $accessToken->getTTL()
];
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')) {
$refreshToken = new RefreshToken($this->server->getStorage('refresh_token'));
$refreshToken->setId(SecureKey::make());
$refreshToken->setTimestamp(time());
$refreshToken->setTTL($this->server->getGrantType('refresh_token')->getRefreshTokenTTL());
$response['refresh_token'] = $refreshToken->getId();
}
2013-12-24 17:01:56 +00:00
// Save everything
$session->save();
$accessToken->setSession($session);
$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);
$refreshToken->save();
2013-02-12 16:22:14 +00:00
}
return $response;
}
2013-09-07 18:00:13 +01:00
}