Added AuthServer::getParam() function to reduce repetitive code

This commit is contained in:
Alex Bilbie 2013-02-13 18:25:10 +00:00
parent 0f4546db47
commit 1e2d2b3d25
3 changed files with 25 additions and 22 deletions

View File

@ -374,4 +374,24 @@ class AuthServer
return self::$grantTypes[$grantType];
}
/**
* Get a parameter from passed input parameters or the Request class
* @param string|array $param Requried parameter
* @param string $method Get/put/post/delete
* @param array $inputParams Passed input parameters
* @return mixed 'Null' if parameter is missing
*/
public static function getParam($param = '', $method = 'get', $inputParams = array())
{
if (is_string($param)) {
return (isset($inputParams[$param])) ? $inputParams['client_id'] : self::getRequest()->{$method}($param);
} else {
$response = array();
foreach ($param as $p) {
$response[$p] = self::getParam($p, $method, $inputParams);
}
return $response;
}
}
}

View File

@ -25,31 +25,18 @@ class AuthCode implements GrantTypeInterface {
return $this->responseType;
}
public function completeFlow($inputParams = null, $authParams = array())
public function completeFlow($inputParams = null)
{
// Client ID
$authParams['client_id'] = (isset($inputParams['client_id'])) ?
$inputParams['client_id'] :
AuthServer::getRequest()->post('client_id');
$authParams = AuthServer::getParam(array('client_id', 'client_secret', 'redirect_uri', 'code'), 'post', $inputParams);
if (is_null($authParams['client_id'])) {
if (is_null($authParams['client_id'])) {
throw new Exception\ClientException(sprintf(AuthServer::getExceptionMessage('invalid_request'), 'client_id'), 0);
}
// Client secret
$authParams['client_secret'] = (isset($inputParams['client_secret'])) ?
$inputParams['client_secret'] :
AuthServer::getRequest()->post('client_secret');
if (is_null($authParams['client_secret'])) {
throw new Exception\ClientException(sprintf(AuthServer::getExceptionMessage('invalid_request'), 'client_secret'), 0);
}
// Redirect URI
$authParams['redirect_uri'] = (isset($inputParams['redirect_uri'])) ?
$inputParams['redirect_uri'] :
AuthServer::getRequest()->post('redirect_uri');
if (is_null($authParams['redirect_uri'])) {
throw new Exception\ClientException(sprintf(AuthServer::getExceptionMessage('invalid_request'), 'redirect_uri'), 0);
}
@ -63,11 +50,7 @@ class AuthCode implements GrantTypeInterface {
$authParams['client_details'] = $clientDetails;
// The authorization code
$authParams['code'] = (isset($inputParams['code'])) ?
$inputParams['code'] :
AuthServer::getRequest()->post('code');
// Validate the authorization code
if (is_null($authParams['code'])) {
throw new Exception\ClientException(sprintf(AuthServer::getExceptionMessage('invalid_request'), 'code'), 0);
}

View File

@ -42,5 +42,5 @@ interface GrantTypeInterface
* @param array $authParams The authorisation paramaters that have been set so far in the request
* @return array An array of parameters to be passed back to the client
*/
public function completeFlow($inputParams = null, $authParams = array());
public function completeFlow($inputParams = null);
}