From 8538d9e43e687d5b0ebcae64dd24256590d89468 Mon Sep 17 00:00:00 2001 From: Nicolas Eeckeloo Date: Fri, 22 Jan 2016 11:09:08 +0100 Subject: [PATCH] Initial commit --- .gitignore | 2 + composer.json | 41 +++++++++++++++++ phpunit.xml.dist | 24 ++++++++++ src/Adapter.php | 101 ++++++++++++++++++++++++++++++++++++++++++ src/OAuth2Result.php | 29 ++++++++++++ tests/AdapterTest.php | 42 ++++++++++++++++++ tests/bootstrap.php | 13 ++++++ 7 files changed, 252 insertions(+) create mode 100644 .gitignore create mode 100755 composer.json create mode 100644 phpunit.xml.dist create mode 100644 src/Adapter.php create mode 100644 src/OAuth2Result.php create mode 100644 tests/AdapterTest.php create mode 100644 tests/bootstrap.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3a9875b --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/vendor/ +composer.lock diff --git a/composer.json b/composer.json new file mode 100755 index 0000000..c54ed26 --- /dev/null +++ b/composer.json @@ -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": "neeckeloo@gmail.com" + } + ], + "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/" + } + } +} diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..84647a6 --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,24 @@ + + + + + + ./tests + + + + + + src + + + \ No newline at end of file diff --git a/src/Adapter.php b/src/Adapter.php new file mode 100644 index 0000000..a150bf3 --- /dev/null +++ b/src/Adapter.php @@ -0,0 +1,101 @@ +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, + ]); + } +} diff --git a/src/OAuth2Result.php b/src/OAuth2Result.php new file mode 100644 index 0000000..fe96e1b --- /dev/null +++ b/src/OAuth2Result.php @@ -0,0 +1,29 @@ +accessToken = $accessToken; + } + + /** + * @return AccessToken + */ + public function getAccessToken() + { + return $this->accessToken; + } +} diff --git a/tests/AdapterTest.php b/tests/AdapterTest.php new file mode 100644 index 0000000..f07a89f --- /dev/null +++ b/tests/AdapterTest.php @@ -0,0 +1,42 @@ +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; + } +} diff --git a/tests/bootstrap.php b/tests/bootstrap.php new file mode 100644 index 0000000..2a4c9d2 --- /dev/null +++ b/tests/bootstrap.php @@ -0,0 +1,13 @@ +