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

Commit

Permalink
Merge pull request #39 from TomHAnderson/feature/oauth2server-validate
Browse files Browse the repository at this point in the history
Validate OAuth2 server from Query resources
  • Loading branch information
TomHAnderson committed Jun 12, 2015
2 parents ddb9699 + 0eec774 commit 899de20
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ An example to match ZfcUser `auth_identity_fields` configuration:
'auth_identity_fields' => array('username', 'email'), // defaults to array('username')
```


Validate zf-apigility-doctrine resources
----------------------------------------

To validate the OAuth2 session with Query Create Filters and Query Providers implement `ZF\OAuth2\Doctrine\OAuth2ServerInterface` and use `ZF\OAuth2\Doctrine\OAuth2ServerTrait`. Then call `$result = $this->validateOAuth2($scope);` in the filter function.


Extensions
----------

Expand Down
10 changes: 10 additions & 0 deletions config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,14 @@
'ZF\OAuth2\Doctrine\Factory\DoctrineAdapterFactory',
),
),
'zf-apigility-doctrine-query-create-filter' => array(
'initializers' => array(
'ZF\OAuth2\Doctrine\Query\OAuth2ServerInitializer',
),
),
'zf-apigility-doctrine-query-provider' => array(
'initializers' => array(
'ZF\OAuth2\Doctrine\Query\OAuth2ServerInitializer',
),
),
);
19 changes: 19 additions & 0 deletions src/Query/OAuth2ServerInitializer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace ZF\OAuth2\Doctrine\Query;

use Zend\ServiceManager\InitializerInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use ZF\OAuth2\Doctrine\Query\OAuth2ServerInterface;

class OAuth2ServerInitializer implements InitializerInterface
{
public function initialize($instance, ServiceLocatorInterface $serviceLocator)
{
if ($instance instanceof OAuth2ServerInterface) {
$oAuth2ServerFactory = $serviceLocator->getServiceLocator()->get('ZF\OAuth2\Service\OAuth2Server');
$oAuth2Server = $oAuth2ServerFactory();
$instance->setOAuth2Server($oAuth2Server);
}
}
}
12 changes: 12 additions & 0 deletions src/Query/OAuth2ServerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace ZF\OAuth2\Doctrine\Query;

use OAuth2\Server as OAuth2Server;

interface OAuth2ServerInterface
{
public function setOAuth2Server(OAuth2Server $server);
public function getOAuth2Server();
public function validateOAuth2($scope = null);
}
42 changes: 42 additions & 0 deletions src/Query/OAuth2ServerTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace ZF\OAuth2\Doctrine\Query;

use OAuth2\Server as OAuth2Server;
use OAuth2\Request as OAuth2Request;
use ZF\ApiProblem\ApiProblem;

trait OAuth2ServerTrait
{
protected $oAuth2Server;

public function setOAuth2Server(OAuth2Server $server)
{
$this->oAuth2Server = $server;

return $this;
}

public function getOAuth2Server()
{
return $this->oAuth2Server;
}

public function validateOAuth2($scope = null)
{
if (! $this->getOAuth2Server()->verifyResourceRequest(
OAuth2Request::createFromGlobals(),
$response = null,
$scope
)) {
$error = $this->getOAuth2Server()->getResponse();
$parameters = $error->getParameters();
$detail = isset($parameters['error_description']) ?
$parameters['error_description']: $error->getStatusText();

return new ApiProblem($error->getStatusCode(), $detail);
}

return true;
}
}

0 comments on commit 899de20

Please sign in to comment.