Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

An OAuth2 client is not a user #55

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/OAuth2Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ class OAuth2Adapter implements AuthenticationInterface
*/
protected $responseFactory;

/**
* @var callable
*/
protected $userFactory;

public function __construct(
ResourceServer $resourceServer,
callable $responseFactory,
Expand Down Expand Up @@ -56,9 +61,9 @@ public function authenticate(ServerRequestInterface $request) : ?UserInterface
$result = $this->resourceServer->validateAuthenticatedRequest($request);
$userId = $result->getAttribute('oauth_user_id', null);
$clientId = $result->getAttribute('oauth_client_id', null);
if (isset($userId) || isset($clientId)) {
if (isset($userId)) {
return ($this->userFactory)(
$userId ?? $clientId,
$userId,
[],
[
'oauth_user_id' => $userId,
Expand Down
11 changes: 6 additions & 5 deletions test/OAuth2AdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use League\OAuth2\Server\ResourceServer;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\Prophecy\ObjectProphecy;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Zend\Expressive\Authentication\AuthenticationInterface;
Expand All @@ -32,6 +33,9 @@ class OAuth2AdapterTest extends TestCase
/** @var callable */
private $responseFactory;

/** @var callable */
private $userFactory;

public function setUp()
{
$this->resourceServer = $this->prophesize(ResourceServer::class);
Expand Down Expand Up @@ -122,7 +126,7 @@ public function testAuthenticateReturnsAUserIfTheResourceServerProducesAUserId()
$this->assertSame([], $user->getRoles());
}

public function testAuthenticateReturnsAClientIfTheResourceServerProducesAClientId()
public function testAuthenticateReturnNullIfTheResourceServerProducesAClientIdOnly()
{
$request = $this->prophesize(ServerRequestInterface::class);
$request->getAttribute('oauth_user_id', null)->willReturn(null);
Expand All @@ -141,10 +145,7 @@ public function testAuthenticateReturnsAClientIfTheResourceServerProducesAClient
);

$user = $adapter->authenticate($request->reveal());

$this->assertInstanceOf(UserInterface::class, $user);
$this->assertSame('some-identifier', $user->getIdentity());
$this->assertSame([], $user->getRoles());
$this->assertNull($user);
}

public function testUnauthorizedResponseProducesAResponseWithAWwwAuthenticateHeader()
Expand Down