Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Token request attribute configurable #89

Merged
merged 3 commits into from
Mar 10, 2017
Merged
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
5 changes: 4 additions & 1 deletion src/Container/ResourceServerMiddlewareFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

use Psr\Container\ContainerInterface;
use ZfrOAuth2\Server\Middleware\ResourceServerMiddleware;
use ZfrOAuth2\Server\Options\ServerOptions;
use ZfrOAuth2\Server\ResourceServerInterface;

/**
Expand All @@ -35,7 +36,9 @@ public function __invoke(ContainerInterface $container): ResourceServerMiddlewar
{
/** @var ResourceServerInterface $resourceServer */
$resourceServer = $container->get(ResourceServerInterface::class);
/** @var ServerOptions $serverOptions */
$serverOptions = $container->get(ServerOptions::class);

return new ResourceServerMiddleware($resourceServer);
return new ResourceServerMiddleware($resourceServer, $serverOptions->getTokenRequestAttribute());
}
}
17 changes: 12 additions & 5 deletions src/Middleware/ResourceServerMiddleware.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

declare(strict_types = 1);
declare(strict_types=1);

/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Expand Down Expand Up @@ -43,12 +43,18 @@ class ResourceServerMiddleware
*/
private $resourceServer;

/**
* @var string
*/
private $tokenRequestAttribute;

/**
* @param ResourceServerInterface $resourceServer
*/
public function __construct(ResourceServerInterface $resourceServer)
public function __construct(ResourceServerInterface $resourceServer, string $tokenRequestAttribute = 'oauth_token')
{
$this->resourceServer = $resourceServer;
$this->resourceServer = $resourceServer;
$this->tokenRequestAttribute = $tokenRequestAttribute;
}

public function __invoke(
Expand All @@ -61,10 +67,11 @@ public function __invoke(
} catch (InvalidAccessTokenException $exception) {
// If we're here, this means that there was an access token, but it's either expired or invalid. If
// that's the case we must immediately return
return new JsonResponse(['error' => $exception->getCode(), 'error_description' => $exception->getMessage()], 401);
return new JsonResponse(['error' => $exception->getCode(), 'error_description' => $exception->getMessage()],
401);
}

// Otherwise, if we actually have a token and set it as part of the request attribute for next step
return $next($request->withAttribute('oauth_token', $token), $response);
return $next($request->withAttribute($this->tokenRequestAttribute, $token), $response);
}
}
22 changes: 20 additions & 2 deletions src/Options/ServerOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ final class ServerOptions
*/
private $ownerRequestAttribute;

/**
* Attribute that the ResourceServerMiddleware uses to access the token
*
* @var string
*/
private $tokenRequestAttribute;

/**
* @param callable|string $ownerCallable either a callable or the name of a container service
*/
Expand All @@ -97,7 +104,8 @@ private function __construct(
bool $revokeRotatedRefreshTokens,
$ownerCallable,
array $grants,
string $ownerRequestAttribute
string $ownerRequestAttribute,
string $tokenRequestAttribute
) {
$this->authorizationCodeTtl = $authorizationCodeTtl;
$this->accessTokenTtl = $accessTokenTtl;
Expand All @@ -107,6 +115,7 @@ private function __construct(
$this->ownerCallable = $ownerCallable;
$this->grants = $grants;
$this->ownerRequestAttribute = $ownerRequestAttribute;
$this->tokenRequestAttribute = $tokenRequestAttribute;
}

/**
Expand All @@ -122,7 +131,8 @@ public static function fromArray(array $options = []): self
$options['revoke_rotated_refresh_tokens'] ?? true,
$options['owner_callable'] ?? null,
$options['grants'] ?? [],
$options['owner_request_attribute'] ?? 'owner'
$options['owner_request_attribute'] ?? 'owner',
$options['token_request_attribute'] ?? 'oauth_token'
);
}

Expand Down Expand Up @@ -192,4 +202,12 @@ public function getOwnerRequestAttribute(): string
{
return $this->ownerRequestAttribute;
}

/**
* Attribute that the ResourceServerMiddleware uses to access the token
*/
public function getTokenRequestAttribute(): string
{
return $this->tokenRequestAttribute;
}
}
6 changes: 6 additions & 0 deletions tests/src/Container/ResourceServerMiddlewareFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Psr\Container\ContainerInterface;
use ZfrOAuth2\Server\Container\ResourceServerMiddlewareFactory;
use ZfrOAuth2\Server\Middleware\ResourceServerMiddleware;
use ZfrOAuth2\Server\Options\ServerOptions;
use ZfrOAuth2\Server\ResourceServerInterface;

/**
Expand All @@ -42,6 +43,11 @@ public function testCanCreateFromFactory()
->with(ResourceServerInterface::class)
->willReturn($this->createMock(ResourceServerInterface::class));

$container->expects($this->at(1))
->method('get')
->with(ServerOptions::class)
->willReturn(ServerOptions::fromArray());

$factory = new ResourceServerMiddlewareFactory();
$service = $factory($container);

Expand Down
6 changes: 3 additions & 3 deletions tests/src/Middleware/ResourceServerMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class ResourceServerMiddlewareTest extends TestCase
public function testWillGetAccessTokenWithAccessTokenAsResult()
{
$resourceServer = $this->createMock(ResourceServer::class);
$middleware = new ResourceServerMiddleware($resourceServer);
$middleware = new ResourceServerMiddleware($resourceServer, 'oauth_token');
$accessToken = $this->createMock(AccessToken::class);
$request = $this->createMock(RequestInterface::class);
$response = $this->createMock(ResponseInterface::class);
Expand All @@ -65,7 +65,7 @@ public function testWillGetAccessTokenWithAccessTokenAsResult()
public function testWillGetAccessTokenWithNullAsResult()
{
$resourceServer = $this->createMock(ResourceServer::class);
$middleware = new ResourceServerMiddleware($resourceServer);
$middleware = new ResourceServerMiddleware($resourceServer, 'oauth_token');
$accessToken = null;
$request = $this->createMock(RequestInterface::class);
$response = $this->createMock(ResponseInterface::class);
Expand All @@ -91,7 +91,7 @@ public function testWillGetAccessTokenWithNullAsResult()
public function testWillCallGetAccessTokenWithException()
{
$resourceServer = $this->createMock(ResourceServer::class);
$middleware = new ResourceServerMiddleware($resourceServer);
$middleware = new ResourceServerMiddleware($resourceServer, 'oauth_token');
$request = $this->createMock(RequestInterface::class);
$response = $this->createMock(ResponseInterface::class);

Expand Down
3 changes: 3 additions & 0 deletions tests/src/Options/ServerOptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public function testDefaults()
$this->assertFalse($options->getRotateRefreshTokens());
$this->assertTrue($options->getRevokeRotatedRefreshTokens());
$this->assertEquals('owner', $options->getOwnerRequestAttribute());
$this->assertEquals('oauth_token', $options->getTokenRequestAttribute());
}

public function testGetters()
Expand All @@ -60,6 +61,7 @@ public function testGetters()
'owner_callable' => $callable,
'grants' => [ClientCredentialsGrant::class],
'owner_request_attribute' => 'something',
'token_request_attribute' => 'else',
]);

$this->assertEquals(300, $options->getAuthorizationCodeTtl());
Expand All @@ -70,5 +72,6 @@ public function testGetters()
$this->assertSame($callable, $options->getOwnerCallable());
$this->assertEquals([ClientCredentialsGrant::class], $options->getGrants());
$this->assertEquals('something', $options->getOwnerRequestAttribute());
$this->assertEquals('else', $options->getTokenRequestAttribute());
}
}