-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 8538d9e
Showing
7 changed files
with
252 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/vendor/ | ||
composer.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
{ | ||
"name": "riskio/zf-authentication-auth0", | ||
"description": "Auth0 authentication for Zend Framework 2", | ||
"type": "library", | ||
"keywords": [ | ||
"zf2", | ||
"authentication", | ||
"oauth" | ||
], | ||
"homepage": "https://github.com/riskio/zf-authentication-auth0", | ||
"authors": [ | ||
{ | ||
"name": "Nicolas Eeckeloo", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"repositories": [ | ||
{ | ||
"type": "composer", | ||
"url": "http://packages.riskio.fr" | ||
} | ||
], | ||
"require": { | ||
"php": ">=5.5", | ||
"riskio/oauth2-auth0": "~0.1", | ||
"zendframework/zend-authentication": "~2.5" | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "~4.7" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Riskio\\Authentication\\Auth0\\": "src/" | ||
} | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"Riskio\\Authentication\\Auth0\\": "tests/" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<phpunit backupGlobals="false" | ||
backupStaticAttributes="false" | ||
colors="true" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
processIsolation="false" | ||
stopOnFailure="false" | ||
syntaxCheck="false" | ||
bootstrap="./tests/bootstrap.php"> | ||
<testsuites> | ||
<testsuite name="Auth0 Authentication Test Suite"> | ||
<directory>./tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
|
||
<filter> | ||
<whitelist> | ||
<directory suffix=".php">src</directory> | ||
</whitelist> | ||
</filter> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?php | ||
namespace Riskio\Authentication\Auth0; | ||
|
||
use Exception; | ||
use League\OAuth2\Client\Grant\AuthorizationCode; | ||
use League\OAuth2\Client\Provider\ProviderInterface; | ||
use Riskio\Authentication\Auth0\OAuth2Result; | ||
use Zend\Authentication\Adapter\AdapterInterface; | ||
|
||
class Adapter implements AdapterInterface | ||
{ | ||
/** | ||
* @var ProviderInterface | ||
*/ | ||
private $oauthProvider; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $code; | ||
|
||
/** | ||
* @param ProviderInterface $oauthProvider | ||
*/ | ||
public function __construct(ProviderInterface $oauthProvider) | ||
{ | ||
$this->oauthProvider = $oauthProvider; | ||
} | ||
|
||
/** | ||
* @param string $code | ||
*/ | ||
public function setCode($code) | ||
{ | ||
$this->code = (string) $code; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getCode() | ||
{ | ||
return $this->code; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function authenticate() | ||
{ | ||
if (empty($this->code)) { | ||
return new OAuth2Result( | ||
OAuth2Result::FAILURE_CREDENTIAL_INVALID, | ||
null, | ||
['No code specified'] | ||
); | ||
} | ||
|
||
try { | ||
$token = $this->getAccessToken(); | ||
|
||
/* @var $user \League\OAuth2\Client\Entity\User */ | ||
$user = $this->oauthProvider->getUserDetails($token); | ||
if (!$user) { | ||
return new OAuth2Result( | ||
OAuth2Result::FAILURE_IDENTITY_NOT_FOUND, | ||
$this->code, | ||
[ | ||
sprintf( | ||
'Failed to retrieve user related to access token "%s"', | ||
$token | ||
) | ||
] | ||
); | ||
} | ||
|
||
$result = new OAuth2Result(OAuth2Result::SUCCESS, $user); | ||
$result->setAccessToken($token); | ||
|
||
return $result; | ||
} catch (Exception $e) { | ||
return new OAuth2Result( | ||
OAuth2Result::FAILURE_CREDENTIAL_INVALID, | ||
$this->code, | ||
[$e->getMessage()] | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* @return \League\OAuth2\Client\Token\AccessToken | ||
*/ | ||
private function getAccessToken() | ||
{ | ||
$grant = new AuthorizationCode(); | ||
|
||
return $this->oauthProvider->getAccessToken($grant, [ | ||
'code' => $this->code, | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
namespace Riskio\Authentication\Auth0; | ||
|
||
use League\OAuth2\Client\Token\AccessToken; | ||
use Zend\Authentication\Result; | ||
|
||
class OAuth2Result extends Result | ||
{ | ||
/** | ||
* @var AccessToken | ||
*/ | ||
protected $accessToken; | ||
|
||
/** | ||
* @param AccessToken $accessToken | ||
*/ | ||
public function setAccessToken(AccessToken $accessToken) | ||
{ | ||
$this->accessToken = $accessToken; | ||
} | ||
|
||
/** | ||
* @return AccessToken | ||
*/ | ||
public function getAccessToken() | ||
{ | ||
return $this->accessToken; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
namespace Riskio\Authentication\Auth0; | ||
|
||
use League\OAuth2\Client\Entity\User; | ||
use League\OAuth2\Client\Grant\AuthorizationCode; | ||
use League\OAuth2\Client\Provider\ProviderInterface; | ||
use League\OAuth2\Client\Token\AccessToken; | ||
use Prophecy\Argument; | ||
|
||
class AdapterTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @test | ||
*/ | ||
public function authenticate_GivenCodeAndValidIdentity_ShouldReturnSuccessResult() | ||
{ | ||
$code = 'foo'; | ||
$token = $this->prophesize(AccessToken::class); | ||
$user = new User(); | ||
$providerMock = $this->getOauthProvider($token, $user); | ||
$adapter = new Adapter($providerMock->reveal()); | ||
$adapter->setCode($code); | ||
|
||
$result = $adapter->authenticate(); | ||
|
||
$this->assertInstanceOf(OAuth2Result::class, $result); | ||
$this->assertEquals(OAuth2Result::SUCCESS, $result->getCode()); | ||
$this->assertInstanceOf(AccessToken::class, $result->getAccessToken()); | ||
$this->assertEquals($user, $result->getIdentity()); | ||
} | ||
|
||
private function getOauthProvider($token, $user) | ||
{ | ||
$providerMock = $this->prophesize(ProviderInterface::class); | ||
$providerMock | ||
->getAccessToken(Argument::type(AuthorizationCode::class), Argument::type('array')) | ||
->willReturn($token->reveal()); | ||
$providerMock->getUserDetails(Argument::type(AccessToken::class))->willReturn($user); | ||
|
||
return $providerMock; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
chdir(__DIR__); | ||
$previousDir = '.'; | ||
while (!is_dir($previousDir . DIRECTORY_SEPARATOR . 'vendor')) { | ||
$appRoot = dirname(getcwd()); | ||
if ($previousDir === $appRoot) { | ||
throw new RuntimeException('Unable to locate application root'); | ||
} | ||
$previousDir = $appRoot; | ||
chdir($appRoot); | ||
} | ||
// Load composer autoloader | ||
require_once $appRoot . '/vendor/autoload.php'; |