Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
166 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
namespace Chadicus\Slim\OAuth2\Routes; | ||
|
||
use Slim\Slim; | ||
|
||
final class ReceiveCode | ||
{ | ||
const ROUTE = '/receive-code'; | ||
|
||
/** | ||
* The slim framework application instance. | ||
* | ||
* @var Slim $slim | ||
*/ | ||
private $slim; | ||
|
||
/** | ||
* The template for /receive-code | ||
* | ||
* @var string | ||
*/ | ||
private $template; | ||
|
||
/** | ||
* Construct a new instance of ReceiveCode route. | ||
* | ||
* @param Slim $slim The slim framework application instance. | ||
* @param string $template The template for /receive-code | ||
*/ | ||
public function __construct(Slim $slim, $template = 'receive-code.phtml') | ||
{ | ||
$this->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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<h2>The authorization code is <?php echo $this->data['code']; ?></h2> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
<?php | ||
|
||
namespace ChadicusTest\Slim\OAuth2\Routes; | ||
|
||
use Chadicus\Slim\OAuth2\Routes\ReceiveCode; | ||
|
||
/** | ||
* Unit tests for the \Chadicus\Slim\OAuth2\Routes\ReceiveCode class. | ||
* | ||
* @coversDefaultClass \Chadicus\Slim\OAuth2\Routes\ReceiveCode | ||
* @covers ::<private> | ||
* @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 = <<<HTML | ||
<h2>The authorization code is {$code}</h2> | ||
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() | ||
); | ||
} | ||
} |