From 698844370d3e291f33f86a61e597b14a0d56efa6 Mon Sep 17 00:00:00 2001 From: chadicus Date: Mon, 21 Sep 2015 20:33:36 -0400 Subject: [PATCH] Add /receive-code --- src/ReceiveCode.php | 59 +++++++++++++++++++ templates/receive-code.phtml | 1 + tests/ReceiveCodeTest.php | 106 +++++++++++++++++++++++++++++++++++ 3 files changed, 166 insertions(+) create mode 100644 src/ReceiveCode.php create mode 100644 templates/receive-code.phtml create mode 100644 tests/ReceiveCodeTest.php diff --git a/src/ReceiveCode.php b/src/ReceiveCode.php new file mode 100644 index 0000000..6a23d98 --- /dev/null +++ b/src/ReceiveCode.php @@ -0,0 +1,59 @@ +slim = $slim; + $this->template = $template; + } + + /** + * Call this class as a function. + * + * @return void + */ + public function __invoke() + { + $this->slim->render($this->template, ['code' => $this->slim->request()->params('code')]); + } + + /** + * Register this route with the given Slim application and OAuth2 server + * + * @param Slim $slim The slim framework application instance. + * @param string $template The template for /receive-code + * + * @return void + */ + public static function register(Slim $slim, $template = 'receive-code.phtml') + { + $slim->map(self::ROUTE, new self($slim, $template))->via('GET', 'POST')->name('receive-code'); + } +} diff --git a/templates/receive-code.phtml b/templates/receive-code.phtml new file mode 100644 index 0000000..a5ed818 --- /dev/null +++ b/templates/receive-code.phtml @@ -0,0 +1 @@ +

The authorization code is data['code']; ?>

diff --git a/tests/ReceiveCodeTest.php b/tests/ReceiveCodeTest.php new file mode 100644 index 0000000..b17cb56 --- /dev/null +++ b/tests/ReceiveCodeTest.php @@ -0,0 +1,106 @@ + + * @covers ::__construct + */ +final class ReceiveCodeTest extends \PHPUnit_Framework_TestCase +{ + /** + * Verify basic behavior of __invoke() + * + * @test + * @covers ::__invoke + * + * @return void + */ + public function invoke() + { + $storage = new \OAuth2\Storage\Memory( + [ + 'client_credentials' => [ + 'testClientId' => [ + 'client_id' => 'testClientId', + 'client_secret' => 'testClientSecret', + 'redirect_uri' => '/receive-code', + ], + ], + ] + ); + + $server = new \OAuth2\Server( + $storage, + [ + 'access_lifetime' => 3600, + ], + [ + new \OAuth2\GrantType\ClientCredentials($storage), + ] + ); + + $code = md5(time()); + + \Slim\Environment::mock( + [ + 'REQUEST_METHOD' => 'POST', + 'CONTENT_TYPE' => 'application/json', + 'PATH_INFO' => '/receive-code', + 'QUERY_STRING' => "code={$code}&state=xyz", + ] + ); + + $slim = new \Slim\Slim(); + $slim->post('/receive-code', new ReceiveCode($slim)); + + ob_start(); + + $slim->run(); + + ob_get_clean(); + + $this->assertSame(200, $slim->response->status()); + + $expected = <<The authorization code is {$code} + +HTML; + + $this->assertSame($expected, $slim->response->getBody()); + } + + /** + * Verify basic behavior of register + * + * @test + * @covers ::register + * + * @return void + */ + public function register() + { + $storage = new \OAuth2\Storage\Memory([]); + $server = new \OAuth2\Server($storage, [], []); + + \Slim\Environment::mock(); + + $slim = new \Slim\Slim(); + + ReceiveCode::register($slim); + + $route = $slim->router()->getNamedRoute('receive-code'); + + $this->assertInstanceOf('\Slim\Route', $route); + $this->assertInstanceOf('\Chadicus\Slim\OAuth2\Routes\ReceiveCode', $route->getCallable()); + $this->assertSame( + [\Slim\Http\Request::METHOD_GET, \Slim\Http\Request::METHOD_POST], + $route->getHttpMethods() + ); + } +}