Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
neeckeloo committed Jan 22, 2016
0 parents commit 8538d9e
Show file tree
Hide file tree
Showing 7 changed files with 252 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/vendor/
composer.lock
41 changes: 41 additions & 0 deletions composer.json
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/"
}
}
}
24 changes: 24 additions & 0 deletions phpunit.xml.dist
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>
101 changes: 101 additions & 0 deletions src/Adapter.php
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,
]);
}
}
29 changes: 29 additions & 0 deletions src/OAuth2Result.php
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;
}
}
42 changes: 42 additions & 0 deletions tests/AdapterTest.php
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;
}
}
13 changes: 13 additions & 0 deletions tests/bootstrap.php
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';

0 comments on commit 8538d9e

Please sign in to comment.