Skip to content
This repository has been archived by the owner on Jan 21, 2020. It is now read-only.

Commit

Permalink
Do not use a user_id provided in a query string
Browse files Browse the repository at this point in the history
The practice leads to the ability for an attacker to authenticate as one user,
but then spoof as another user on later, authenticated requests.
  • Loading branch information
ezimuel authored and weierophinney committed Jul 23, 2015
1 parent e55b1d1 commit e09c4bf
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 36 deletions.
7 changes: 2 additions & 5 deletions config/module.config.php
Expand Up @@ -51,10 +51,7 @@
),
'service_manager' => array(
'aliases' => array(
'ZF\OAuth2\Provider\UserId' => 'ZF\OAuth2\Provider\UserId\Request',
),
'invokables' => array(
'ZF\OAuth2\Provider\UserId\Request' => 'ZF\OAuth2\Provider\UserId\Request',
'ZF\OAuth2\Provider\UserId' => 'ZF\OAuth2\Provider\UserId\AuthenticationService',
),
'factories' => array(
'ZF\OAuth2\Adapter\PdoAdapter' => 'ZF\OAuth2\Factory\PdoAdapterFactory',
Expand Down Expand Up @@ -98,7 +95,7 @@
*
* If true, client errors are returned using the
* application/problem+json content type,
* otherwise in the format described in the oauth2 specification
* otherwise in the format described in the oauth2 specification
* (default: true)
*/
'api_problem_error_response' => true,
Expand Down
39 changes: 36 additions & 3 deletions src/Provider/UserId/AuthenticationService.php
Expand Up @@ -16,24 +16,57 @@ class AuthenticationService implements UserIdProviderInterface
*/
private $authenticationService;

/**
* @var string
*/
private $userId = 'id';

/**
* Set authentication service
*
* @param ZendAuthenticationService $service
* @param array $config
*/
public function __construct(ZendAuthenticationService $service)
public function __construct(ZendAuthenticationService $service = null, $config = array())
{
$this->authenticationService = $service;

if (isset($config['zf-oauth2']['user_id'])) {
$this->userId = $config['zf-oauth2']['user_id'];
}
}

/**
* Use Zend\Authentication\AuthenticationService to fetch the identity.
*
* @param RequestInterface $request
* @param RequestInterface $request
* @return mixed
*/
public function __invoke(RequestInterface $request)
{
return $this->authenticationService->getIdentity();
if (empty($this->authenticationService)) {
return null;
}

$identity = $this->authenticationService->getIdentity();

if (is_object($identity)) {
if (property_exists($identity, $this->userId)) {
return $identity->{$this->$userId};
}

$method = "get" . ucfirst($this->userId);
if (method_exists($identity, $method)) {
return $identity->$method();
}

return null;
}

if (is_array($identity) && isset($identity[$this->userId])) {
return $identity[$this->userId];
}

return null;
}
}
11 changes: 10 additions & 1 deletion src/Provider/UserId/AuthenticationServiceFactory.php
Expand Up @@ -17,6 +17,15 @@ class AuthenticationServiceFactory implements FactoryInterface
*/
public function createService(ServiceLocatorInterface $services)
{
return new AuthenticationService($services->get('Zend\Authentication\AuthenticationService'));
$config = $services->get('Config');

if ($services->has('Zend\Authentication\AuthenticationService')) {
return new AuthenticationService(
$services->get('Zend\Authentication\AuthenticationService'),
$config
);
}

return new AuthenticationService(null, $config);
}
}
24 changes: 0 additions & 24 deletions src/Provider/UserId/Request.php

This file was deleted.

3 changes: 1 addition & 2 deletions test/Controller/AuthControllerTest.php
Expand Up @@ -172,7 +172,6 @@ public function testAuthorizeCode()
'response_type' => 'code',
'client_id' => 'testclient',
'state' => 'xyz',
'user_id' => 123,
'redirect_uri' => '/oauth/receivecode',
)));
$request->setPost(new Parameters(array(
Expand All @@ -199,7 +198,7 @@ public function testAuthorizeCode()

$selectString = $sql->getSqlStringForSqlObject($select);
$results = $adapter->query($selectString, $adapter::QUERY_MODE_EXECUTE)->toArray();
$this->assertEquals('123', $results[0]['user_id']);
$this->assertEquals(null, $results[0]['user_id']);

// test get token from authorized code
$request = $this->getRequest();
Expand Down
Expand Up @@ -97,7 +97,7 @@ public function testAuthorizeCode()
->query($query)
->fetch();

$this->assertEquals('123', $row['user_id']);
$this->assertEquals(null, $row['user_id']);

// test get token from authorized code
$request = $this->getRequest();
Expand Down

0 comments on commit e09c4bf

Please sign in to comment.