Skip to content
This repository has been archived by the owner on May 4, 2024. It is now read-only.

Commit

Permalink
Added an event triggered before redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
lookyman committed Jun 16, 2017
1 parent 738fa70 commit 1d2f9b0
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/UI/OAuth2Presenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,21 @@
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;

/**
* @method void onBeforeRedirect()
*/
class OAuth2Presenter extends Presenter implements LoggerAwareInterface
{
use LoggerAwareTrait;
use Psr7Trait;

const SESSION_NAMESPACE = 'nette-oauth2-server';

/**
* @var callable[]
*/
public $onBeforeRedirect = [];

/**
* @var IAuthorizationRequestSerializer
* @inject
Expand Down Expand Up @@ -86,6 +94,7 @@ public function actionAuthorize()
$this->getSession(self::SESSION_NAMESPACE)->authorizationRequest = $this->authorizationRequestSerializer->serialize(
$this->authorizationServer->validateAuthorizationRequest($this->createServerRequest())
);
$this->onBeforeRedirect();
if (!$this->getUser()->isLoggedIn()) {
$this->redirect(...$this->redirectConfig->getLoginDestination());
}
Expand Down
36 changes: 36 additions & 0 deletions tests/UI/OAuth2PresenterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,14 @@ public function testActionAuthorizeUnlogged()
$presenter->authorizationServer = $authorizationServer;
$presenter->redirectConfig = $config;

$redirectEventTriggered = false;
$presenter->onBeforeRedirect[] = function () use (&$redirectEventTriggered) {
$redirectEventTriggered = true;
};

$response = $presenter->run(new Request('', null, ['action' => 'authorize']));
self::assertInstanceOf(RedirectResponse::class, $response);
self::assertTrue($redirectEventTriggered);
}

public function testActionAuthorizeLogged()
Expand Down Expand Up @@ -185,8 +191,14 @@ public function testActionAuthorizeLogged()
$presenter->authorizationServer = $authorizationServer;
$presenter->redirectConfig = $config;

$redirectEventTriggered = false;
$presenter->onBeforeRedirect[] = function () use (&$redirectEventTriggered) {
$redirectEventTriggered = true;
};

$response = $presenter->run(new Request('', null, ['action' => 'authorize']));
self::assertInstanceOf(RedirectResponse::class, $response);
self::assertTrue($redirectEventTriggered);
}

public function testActionAuthorizeWrongMethod()
Expand All @@ -200,8 +212,14 @@ public function testActionAuthorizeWrongMethod()
$presenter = new OAuth2Presenter();
$presenter->injectPrimary(null, null, null, $httpRequest, $httpResponse);

$redirectEventTriggered = false;
$presenter->onBeforeRedirect[] = function () use (&$redirectEventTriggered) {
$redirectEventTriggered = true;
};

$response = $presenter->run(new Request('', null, ['action' => 'authorize']));
self::assertInstanceOf(Response::class, $response);
self::assertFalse($redirectEventTriggered);
$this->expectOutputString('Method not allowed');
$response->send($httpRequest, $httpResponse);
}
Expand All @@ -222,8 +240,14 @@ public function testActionAuthorizeOAuthException()
$presenter->authorizationRequestSerializer = $this->getMockBuilder(IAuthorizationRequestSerializer::class)->disableOriginalConstructor()->getMock();
$presenter->authorizationServer = $authorizationServer;

$redirectEventTriggered = false;
$presenter->onBeforeRedirect[] = function () use (&$redirectEventTriggered) {
$redirectEventTriggered = true;
};

$response = $presenter->run(new Request('', null, ['action' => 'authorize']));
self::assertInstanceOf(Response::class, $response);
self::assertFalse($redirectEventTriggered);
ob_start();
$response->send($httpRequest, $httpResponse);
ob_end_clean();
Expand All @@ -245,8 +269,14 @@ public function testActionAuthorizeException()
$presenter->authorizationRequestSerializer = $this->getMockBuilder(IAuthorizationRequestSerializer::class)->disableOriginalConstructor()->getMock();
$presenter->authorizationServer = $authorizationServer;

$redirectEventTriggered = false;
$presenter->onBeforeRedirect[] = function () use (&$redirectEventTriggered) {
$redirectEventTriggered = true;
};

$response = $presenter->run(new Request('', null, ['action' => 'authorize']));
self::assertInstanceOf(Response::class, $response);
self::assertFalse($redirectEventTriggered);
$this->expectOutputString('Unknown error');
$response->send($httpRequest, $httpResponse);
}
Expand All @@ -273,8 +303,14 @@ public function testActionAuthorizeExceptionWithLogger()
$presenter->authorizationServer = $authorizationServer;
$presenter->setLogger($logger);

$redirectEventTriggered = false;
$presenter->onBeforeRedirect[] = function () use (&$redirectEventTriggered) {
$redirectEventTriggered = true;
};

$response = $presenter->run(new Request('', null, ['action' => 'authorize']));
self::assertInstanceOf(Response::class, $response);
self::assertFalse($redirectEventTriggered);
$this->expectOutputString('Unknown error');
$response->send($httpRequest, $httpResponse);
}
Expand Down

0 comments on commit 1d2f9b0

Please sign in to comment.