Skip to content

Commit

Permalink
Uses riskio/oauth2-auth0 2.x
Browse files Browse the repository at this point in the history
  • Loading branch information
neeckeloo committed Oct 20, 2017
1 parent a4266cf commit 52ba17d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 21 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Zend Authentication adapter for Auth0
## Requirements

* PHP 7.0+
* [riskio/oauth2-auth0 ^0.1](https://github.com/RiskioFr/oauth2-auth0)
* [riskio/oauth2-auth0 ^2.0](https://github.com/RiskioFr/oauth2-auth0)
* [zendframework/zend-authentication ^2.5](https://github.com/zendframework/zend-authentication)

## Installation
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
],
"require": {
"php": "^7.0",
"riskio/oauth2-auth0": "~0.1",
"riskio/oauth2-auth0": "~2.0",
"zendframework/zend-authentication": "~2.5"
},
"require-dev": {
Expand Down
9 changes: 4 additions & 5 deletions src/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@

use Exception;
use League\OAuth2\Client\Grant\AuthorizationCode;
use League\OAuth2\Client\Provider\ProviderInterface;
use League\OAuth2\Client\Provider\AbstractProvider;
use League\OAuth2\Client\Token\AccessToken;
use Zend\Authentication\Adapter\AdapterInterface;

class Adapter implements AdapterInterface
{
/**
* @var ProviderInterface
* @var AbstractProvider
*/
private $oauthProvider;

Expand All @@ -19,7 +19,7 @@ class Adapter implements AdapterInterface
*/
private $code;

public function __construct(ProviderInterface $oauthProvider)
public function __construct(AbstractProvider $oauthProvider)
{
$this->oauthProvider = $oauthProvider;
}
Expand Down Expand Up @@ -47,8 +47,7 @@ public function authenticate() : OAuth2Result
try {
$token = $this->getAccessToken();

/* @var $user \League\OAuth2\Client\Entity\User */
$user = $this->oauthProvider->getUserDetails($token);
$user = $this->oauthProvider->getResourceOwner($token);
if (!$user) {
return new OAuth2Result(
OAuth2Result::FAILURE_IDENTITY_NOT_FOUND,
Expand Down
38 changes: 24 additions & 14 deletions tests/AdapterTest.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php
namespace Riskio\Authentication\Auth0;

use League\OAuth2\Client\Entity\User;
use League\OAuth2\Client\Provider\GenericResourceOwner;
use League\OAuth2\Client\Grant\AuthorizationCode;
use League\OAuth2\Client\Provider\ProviderInterface;
use League\OAuth2\Client\Provider\AbstractProvider;
use League\OAuth2\Client\Token\AccessToken;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
Expand All @@ -15,29 +15,39 @@ class AdapterTest extends TestCase
*/
public function authenticate_GivenCodeAndValidIdentity_ShouldReturnSuccessResult()
{
$code = 123;
$token = $this->prophesize(AccessToken::class);
$user = new User();
$providerMock = $this->getOauthProvider($token, $user);
$accessToken = 'abcdef';
$resourceOwnerId = 123;
$code = 123;

$token = new AccessToken(['access_token' => $accessToken]);
$resourceOwner = $this->createResourceOwner($resourceOwnerId);
$providerMock = $this->getOauthProvider($token, $resourceOwner);
$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());
self::assertInstanceOf(OAuth2Result::class, $result);
self::assertSame(OAuth2Result::SUCCESS, $result->getCode());
self::assertSame($token, $result->getAccessToken());
self::assertSame($resourceOwner, $result->getIdentity());
}

private function getOauthProvider($token, $user)
private function getOauthProvider($token, $resourceOwner)
{
$providerMock = $this->prophesize(ProviderInterface::class);
$providerMock = $this->prophesize(AbstractProvider::class);
$providerMock
->getAccessToken(Argument::type(AuthorizationCode::class), Argument::type('array'))
->willReturn($token->reveal());
$providerMock->getUserDetails(Argument::type(AccessToken::class))->willReturn($user);
->willReturn($token);
$providerMock
->getResourceOwner($token)
->willReturn($resourceOwner);

return $providerMock;
}

private function createResourceOwner(int $resourceOwnerId) : GenericResourceOwner
{
return new GenericResourceOwner([], $resourceOwnerId);
}
}

0 comments on commit 52ba17d

Please sign in to comment.