Skip to content

Commit

Permalink
Merge pull request #8 from chadicus/fea/authorize-template
Browse files Browse the repository at this point in the history
Add authorization code routes
  • Loading branch information
chadicus committed Sep 22, 2015
2 parents 37f4a77 + d1c4071 commit fc6a584
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/Authorize.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ class Authorize

private $slim;
private $server;
private $template;

public function __construct(Slim $slim, OAuth2\Server $server)
public function __construct(Slim $slim, OAuth2\Server $server, $template = 'authorize.phtml')
{
$this->slim = $slim;
$this->server = $server;
$this->template = $template;
}

public function __invoke()
Expand All @@ -31,7 +33,7 @@ public function __invoke()

$authorized = $this->slim->request()->params('authorized');
if (empty($authorized)) {
//@TODO send to authorize landing page
$this->slim->render($this->template, ['client_id' => $request->query('client_id', false)]);
return;
}

Expand All @@ -49,8 +51,8 @@ public function __invoke()
*
* @return void
*/
public static function register(Slim $slim, OAuth2\Server $server)
public static function register(Slim $slim, OAuth2\Server $server, $template = 'authorize.phtml')
{
$slim->map(self::ROUTE, new self($slim, $server))->via('GET', 'POST')->name('authorize');
$slim->map(self::ROUTE, new self($slim, $server, $template))->via('GET', 'POST')->name('authorize');
}
}
5 changes: 5 additions & 0 deletions templates/authorize.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<form method="post">
<label>Do You Authorize <?php echo htmlentities($this->data['client_id']); ?>?</label><br />
<input type="submit" name="authorized" value="yes">
<input type="submit" name="authorized" value="no">
</form>
51 changes: 51 additions & 0 deletions tests/AuthorizeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,55 @@ public function register()
$this->assertInstanceOf('\Chadicus\Slim\OAuth2\Routes\Authorize', $route->getCallable());
$this->assertSame([\Slim\Http\Request::METHOD_GET, \Slim\Http\Request::METHOD_POST], $route->getHttpMethods());
}

/**
* Verify bahavior of /authorize route when authorized parameter is empty.
*
* @test
* @covers ::__invoke
*
* @return void
*/
public function invokeEmptyAuthorized()
{
$storage = new \OAuth2\Storage\Memory(
[
'client_credentials' => [
'testClientId' => [
'client_id' => 'testClientId',
'client_secret' => 'testClientSecret',
],
],
]
);
$server = new \OAuth2\Server($storage, [], []);

\Slim\Environment::mock(
[
'REQUEST_METHOD' => 'GET',
'PATH_INFO' => '/authorize',
'QUERY_STRING' => 'client_id=testClientId&redirect_uri=http://example.com&response_type=code&state=test',
]
);

$slim = new \Slim\Slim();
$slim->get('/authorize', new Authorize($slim, $server));

ob_start();

$slim->run();

ob_get_clean();

$expected = <<<HTML
<form method="post">
<label>Do You Authorize testClientId?</label><br />
<input type="submit" name="authorized" value="yes">
<input type="submit" name="authorized" value="no">
</form>
HTML;

$this->assertSame($expected, $slim->response->getBody());
}
}

0 comments on commit fc6a584

Please sign in to comment.