2013-02-12 16:22:14 +00:00
|
|
|
<?php
|
2013-02-13 19:39:43 +00:00
|
|
|
/**
|
|
|
|
* OAuth 2.0 Password grant
|
|
|
|
*
|
|
|
|
* @package lncd/oauth2
|
|
|
|
* @author Alex Bilbie <hello@alexbilbie.com>
|
|
|
|
* @copyright Copyright (c) 2013 University of Lincoln
|
|
|
|
* @license http://mit-license.org/
|
|
|
|
* @link http://github.com/lncd/oauth2
|
|
|
|
*/
|
2013-02-12 16:22:14 +00:00
|
|
|
|
|
|
|
namespace OAuth2\Grant;
|
|
|
|
|
|
|
|
use OAuth2\Request;
|
|
|
|
use OAuth2\AuthServer;
|
|
|
|
use OAuth2\Exception;
|
|
|
|
use OAuth2\Util\SecureKey;
|
|
|
|
use OAuth2\Storage\SessionInterface;
|
|
|
|
use OAuth2\Storage\ClientInterface;
|
|
|
|
use OAuth2\Storage\ScopeInterface;
|
|
|
|
|
2013-02-13 19:39:43 +00:00
|
|
|
/**
|
|
|
|
* Password grant class
|
|
|
|
*/
|
2013-02-12 16:22:14 +00:00
|
|
|
class Password implements GrantTypeInterface {
|
|
|
|
|
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
|
|
|
|
*/
|
2013-02-12 16:22:14 +00:00
|
|
|
protected $responseType = null;
|
2013-02-13 19:39:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback to authenticate a user's name and password
|
|
|
|
* @var function
|
|
|
|
*/
|
2013-02-12 16:22:14 +00:00
|
|
|
protected $callback = null;
|
|
|
|
|
2013-03-06 16:59:18 +00:00
|
|
|
/**
|
|
|
|
* AuthServer instance
|
|
|
|
* @var AuthServer
|
|
|
|
*/
|
|
|
|
protected $authServer = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
* @param AuthServer $authServer AuthServer instance
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct(AuthServer $authServer)
|
|
|
|
{
|
|
|
|
$this->authServer = $authServer;
|
|
|
|
}
|
|
|
|
|
2013-02-13 19:39:43 +00:00
|
|
|
/**
|
|
|
|
* Return the identifier
|
|
|
|
* @return string
|
|
|
|
*/
|
2013-02-12 16:22:14 +00:00
|
|
|
public function getIdentifier()
|
|
|
|
{
|
|
|
|
return $this->identifier;
|
|
|
|
}
|
|
|
|
|
2013-02-13 19:39:43 +00:00
|
|
|
/**
|
|
|
|
* Return the response type
|
|
|
|
* @return string
|
|
|
|
*/
|
2013-02-12 16:22:14 +00:00
|
|
|
public function getResponseType()
|
|
|
|
{
|
|
|
|
return $this->responseType;
|
|
|
|
}
|
|
|
|
|
2013-02-13 19:39:43 +00:00
|
|
|
/**
|
|
|
|
* Set the callback to verify a user's username and password
|
|
|
|
* @param function $callback The callback function
|
|
|
|
*/
|
2013-02-12 16:22:14 +00:00
|
|
|
public function setVerifyCredentialsCallback($callback)
|
|
|
|
{
|
|
|
|
$this->callback = $callback;
|
|
|
|
}
|
|
|
|
|
2013-02-13 19:39:43 +00:00
|
|
|
/**
|
|
|
|
* Return the callback function
|
|
|
|
* @return function
|
|
|
|
*/
|
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)) {
|
|
|
|
throw new Exception\InvalidGrantTypeException('Null or non-callable callback set');
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
* @param null|array $inputParams
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function completeFlow($inputParams = null)
|
2013-02-12 16:22:14 +00:00
|
|
|
{
|
2013-02-13 19:39:43 +00:00
|
|
|
// Get the required params
|
2013-03-06 16:59:18 +00:00
|
|
|
$authParams = $this->authServer->getParam(array('client_id', 'client_secret', 'username', 'password'), 'post', $inputParams);
|
2013-02-12 16:22:14 +00:00
|
|
|
|
|
|
|
if (is_null($authParams['client_id'])) {
|
2013-03-06 16:59:18 +00:00
|
|
|
throw new Exception\ClientException(sprintf($this->authServer->getExceptionMessage('invalid_request'), 'client_id'), 0);
|
2013-02-12 16:22:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (is_null($authParams['client_secret'])) {
|
2013-03-06 16:59:18 +00:00
|
|
|
throw new Exception\ClientException(sprintf($this->authServer->getExceptionMessage('invalid_request'), 'client_secret'), 0);
|
2013-02-12 16:22:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validate client ID and redirect URI
|
2013-03-06 16:59:18 +00:00
|
|
|
$clientDetails = $this->authServer->getStorage('client')->getClient($authParams['client_id'], $authParams['client_secret']);
|
2013-02-12 16:22:14 +00:00
|
|
|
|
|
|
|
if ($clientDetails === false) {
|
2013-03-06 16:59:18 +00:00
|
|
|
throw new Exception\ClientException($this->authServer->getExceptionMessage('invalid_client'), 8);
|
2013-02-12 16:22:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$authParams['client_details'] = $clientDetails;
|
|
|
|
|
|
|
|
if (is_null($authParams['username'])) {
|
2013-03-06 16:59:18 +00:00
|
|
|
throw new Exception\ClientException(sprintf($this->authServer->getExceptionMessage('invalid_request'), 'username'), 0);
|
2013-02-12 16:22:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (is_null($authParams['password'])) {
|
2013-03-06 16:59:18 +00:00
|
|
|
throw new Exception\ClientException(sprintf($this->authServer->getExceptionMessage('invalid_request'), 'password'), 0);
|
2013-02-12 16:22:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if user's username and password are correct
|
2013-02-12 16:45:33 +00:00
|
|
|
$userId = call_user_func($this->getVerifyCredentialsCallback(), $authParams['username'], $authParams['password']);
|
2013-02-12 16:22:14 +00:00
|
|
|
|
|
|
|
if ($userId === false) {
|
2013-03-06 16:59:18 +00:00
|
|
|
throw new Exception\ClientException($this->authServer->getExceptionMessage('invalid_credentials'), 0);
|
2013-02-12 16:22:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Generate an access token
|
|
|
|
$accessToken = SecureKey::make();
|
2013-03-06 16:59:18 +00:00
|
|
|
$refreshToken = ($this->authServer->hasGrantType('refresh_token')) ? SecureKey::make() : null;
|
2013-02-12 16:22:14 +00:00
|
|
|
|
2013-03-06 16:59:18 +00:00
|
|
|
$accessTokenExpires = time() + $this->authServer->getExpiresIn();
|
|
|
|
$accessTokenExpiresIn = $this->authServer->getExpiresIn();
|
2013-02-12 16:22:14 +00:00
|
|
|
|
|
|
|
// Delete any existing sessions just to be sure
|
2013-03-06 16:59:18 +00:00
|
|
|
$this->authServer->getStorage('session')->deleteSession($authParams['client_id'], 'user', $userId);
|
2013-02-12 16:22:14 +00:00
|
|
|
|
|
|
|
// Create a new session
|
2013-03-06 16:59:18 +00:00
|
|
|
$this->authServer->getStorage('session')->createSession(
|
2013-02-12 16:22:14 +00:00
|
|
|
$authParams['client_id'],
|
|
|
|
null,
|
|
|
|
'user',
|
|
|
|
$userId,
|
|
|
|
null,
|
|
|
|
$accessToken,
|
|
|
|
$refreshToken,
|
|
|
|
$accessTokenExpires,
|
|
|
|
'granted'
|
|
|
|
);
|
|
|
|
|
|
|
|
$response = array(
|
|
|
|
'access_token' => $accessToken,
|
|
|
|
'token_type' => 'bearer',
|
|
|
|
'expires' => $accessTokenExpires,
|
|
|
|
'expires_in' => $accessTokenExpiresIn
|
|
|
|
);
|
|
|
|
|
2013-03-06 16:59:18 +00:00
|
|
|
if ($this->authServer->hasGrantType('refresh_token')) {
|
2013-02-12 16:22:14 +00:00
|
|
|
$response['refresh_token'] = $refreshToken;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|