Skip to content

Commit

Permalink
Move all demo related stuff to DemoController.
Browse files Browse the repository at this point in the history
  • Loading branch information
hswong3i committed Jul 7, 2015
1 parent 036a78f commit 84ccadd
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 83 deletions.
76 changes: 76 additions & 0 deletions tests/TestBundle/Controller/DemoController.php
Expand Up @@ -11,6 +11,7 @@

namespace AuthBucket\OAuth2\Tests\TestBundle\Controller;

use AuthBucket\OAuth2\Exception\InvalidScopeException;
use Silex\Application;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Client;
Expand All @@ -21,6 +22,81 @@ public function indexAction(Request $request, Application $app)
{
return $app['twig']->render('demo/index.html.twig');
}
public function loginAction(Request $request, Application $app)
{
$session = $request->getSession();

$error = $app['security.last_error']($request);
$_username = $session->get('_username');
$_password = $session->get('_password');

return $app['twig']->render('demo/login.html.twig', array(
'error' => $error,
'_username' => $_username,
'_password' => $_password,
));
}

public function authorizeAction(Request $request, Application $app)
{
// We only handle non-authorized scope here.
try {
return $app['authbucket_oauth2.oauth2_controller']->authorizeAction($request);
} catch (InvalidScopeException $exception) {
$message = unserialize($exception->getMessage());
if ($message['error_description'] !== 'The requested scope is invalid.') {
throw $exception;
}
}

// Fetch parameters, which already checked.
$clientId = $request->query->get('client_id');
$username = $app['security']->getToken()->getUser()->getUsername();
$scope = preg_split('/\s+/', $request->query->get('scope', ''));

// Create form.
$form = $app['form.factory']->createBuilder('form')->getForm();
$form->handleRequest($request);

// Save authorized scope if submitted by POST.
if ($form->isValid()) {
$modelManagerFactory = $app['authbucket_oauth2.model_manager.factory'];
$authorizeManager = $modelManagerFactory->getModelManager('authorize');

// Update existing authorization if possible, else create new.
$authorize = $authorizeManager->readModelOneBy(array(
'clientId' => $clientId,
'username' => $username,
));
if ($authorize === null) {
$class = $authorizeManager->getClassName();
$authorize = new $class();
$authorize->setClientId($clientId)
->setUsername($username)
->setScope((array) $scope);
$authorize = $authorizeManager->createModel($authorize);
} else {
$authorize->setClientId($clientId)
->setUsername($username)
->setScope(array_merge((array) $authorize->getScope(), $scope));
$authorizeManager->updateAuthorize($authorize);
}

// Back to this path, with original GET parameters.
return $app->redirect($request->getRequestUri());
}

// Display the form.
$authorizationRequest = $request->query->all();

return $app['twig']->render('demo/authorize.html.twig', array(
'client_id' => $clientId,
'username' => $username,
'scopes' => $scope,
'form' => $form->createView(),
'authorization_request' => $authorizationRequest,
));
}

public function requestCodeAction(Request $request, Application $app)
{
Expand Down
77 changes: 0 additions & 77 deletions tests/TestBundle/Controller/OAuth2Controller.php
Expand Up @@ -11,7 +11,6 @@

namespace AuthBucket\OAuth2\Tests\TestBundle\Controller;

use AuthBucket\OAuth2\Exception\InvalidScopeException;
use Silex\Application;
use Symfony\Component\HttpFoundation\Request;

Expand All @@ -21,80 +20,4 @@ public function indexAction(Request $request, Application $app)
{
return $app['twig']->render('oauth2/index.html.twig');
}

public function loginAction(Request $request, Application $app)
{
$session = $request->getSession();

$error = $app['security.last_error']($request);
$_username = $session->get('_username');
$_password = $session->get('_password');

return $app['twig']->render('oauth2/login.html.twig', array(
'error' => $error,
'_username' => $_username,
'_password' => $_password,
));
}

public function authorizeAction(Request $request, Application $app)
{
// We only handle non-authorized scope here.
try {
return $app['authbucket_oauth2.oauth2_controller']->authorizeAction($request);
} catch (InvalidScopeException $exception) {
$message = unserialize($exception->getMessage());
if ($message['error_description'] !== 'The requested scope is invalid.') {
throw $exception;
}
}

// Fetch parameters, which already checked.
$clientId = $request->query->get('client_id');
$username = $app['security']->getToken()->getUser()->getUsername();
$scope = preg_split('/\s+/', $request->query->get('scope', ''));

// Create form.
$form = $app['form.factory']->createBuilder('form')->getForm();
$form->handleRequest($request);

// Save authorized scope if submitted by POST.
if ($form->isValid()) {
$modelManagerFactory = $app['authbucket_oauth2.model_manager.factory'];
$authorizeManager = $modelManagerFactory->getModelManager('authorize');

// Update existing authorization if possible, else create new.
$authorize = $authorizeManager->readModelOneBy(array(
'clientId' => $clientId,
'username' => $username,
));
if ($authorize === null) {
$class = $authorizeManager->getClassName();
$authorize = new $class();
$authorize->setClientId($clientId)
->setUsername($username)
->setScope((array) $scope);
$authorize = $authorizeManager->createModel($authorize);
} else {
$authorize->setClientId($clientId)
->setUsername($username)
->setScope(array_merge((array) $authorize->getScope(), $scope));
$authorizeManager->updateAuthorize($authorize);
}

// Back to this path, with original GET parameters.
return $app->redirect($request->getRequestUri());
}

// Display the form.
$authorizationRequest = $request->query->all();

return $app['twig']->render('oauth2/authorize.html.twig', array(
'client_id' => $clientId,
'username' => $username,
'scopes' => $scope,
'form' => $form->createView(),
'authorization_request' => $authorizationRequest,
));
}
}
4 changes: 2 additions & 2 deletions tests/TestBundle/Resources/config/routing.php
Expand Up @@ -21,10 +21,10 @@
$app->get('/demo', 'authbucket_oauth2.tests.demo_controller:indexAction')
->bind('demo');

$app->get('/demo/login', 'authbucket_oauth2.tests.oauth2_controller:loginAction')
$app->get('/demo/login', 'authbucket_oauth2.tests.demo_controller:loginAction')
->bind('demo_login');

$app->match('/demo/authorize', 'authbucket_oauth2.tests.oauth2_controller:authorizeAction')
$app->match('/demo/authorize', 'authbucket_oauth2.tests.demo_controller:authorizeAction')
->bind('demo_authorize');

$app->get('/demo/request/code', 'authbucket_oauth2.tests.demo_controller:requestCodeAction')
Expand Down
8 changes: 4 additions & 4 deletions tests/TestBundle/Resources/views/oauth2/index.html.twig
Expand Up @@ -19,12 +19,12 @@
<footer><a href="http://tools.ietf.org/html/rfc6749#section-3">3. Protocol Endpoints</a></footer>
</blockquote>

<h3 id="authorization-endpoint-oauth2authorize-and-oauth2authorizehttp">Authorization Endpoint <small>(<code>/api/oauth2/authorize</code> and <code>/oauth2/authorize</code>)</small></h3>
<h3 id="authorization-endpoint-oauth2authorize-and-oauth2authorizehttp">Authorization Endpoint <small>(<code>/api/oauth2/authorize</code> and <code>/demo/authorize</code>)</small></h3>
<blockquote>
<p>The authorization endpoint is used to interact with the resource owner and obtain an authorization grant.</p>
<footer><a href="http://tools.ietf.org/html/rfc6749#section-3.1">3.1. Authorization Endpoint</a></footer>
</blockquote>
<p class="lead">Authorization endpoint (<a href="{{ path('api_oauth2_authorize') }}">HTTP Basic Authentication</a> and <a href="{{ path('oauth2_authorize') }}">Form-based Authentication</a>) are protected by Silex's <a href="http://silex.sensiolabs.org/doc/providers/security.html">SecurityServiceProvider</a> in this example. Read though <a href="https://github.com/authbucket/oauth2-php/blob/master/app/config/security.php">security.php</a> to see how we implement it.</p>
<p class="lead">Authorization endpoint (<a href="{{ path('api_oauth2_authorize') }}">HTTP Basic Authentication</a> and <a href="{{ path('demo_authorize') }}">Form-based Authentication</a>) are protected by Silex's <a href="http://silex.sensiolabs.org/doc/providers/security.html">SecurityServiceProvider</a> in this example. Read though <a href="https://github.com/authbucket/oauth2-php/blob/master/app/config/security.php">security.php</a> to see how we implement it.</p>
<p>Direct browser access is possible, authentication request will therefore triggered, and able to login with following testing account:</p>
<ul>
<li>Username: <code>demousername1</code></li>
Expand All @@ -44,8 +44,8 @@
<h2 id="additional-endpoints" class="page-header">Additional Endpoints</h2>
<p class="lead">Following endpoints are excluded from <a href="http://tools.ietf.org/html/rfc6749">RFC6749</a>, but live implementation should consider it.</p>

<h3 id="form-based-authentication-oauth2login">Form-based Authentication <small>(<code>/oauth2/login</code>)</small></h3>
<p class="lead"><a href="{{ path('oauth2_login') }}">Form-based Authentication</a> implemented by Silex's <a href="http://silex.sensiolabs.org/doc/providers/security.html">SecurityServiceProvider</a> in this example. Read though <a href="https://github.com/authbucket/oauth2-php/blob/master/tests/AuthBucket/OAuth2/Tests/TestBundle/Resources/config/routing_oauth2.php">routing_oauth2.php</a> and <a href="https://github.com/authbucket/oauth2-php/blob/master/tests/AuthBucket/OAuth2/Tests/TestBundle/Resources/views/oauth2/login.html.twig">login.html.twig</a> for more information.</p>
<h3 id="form-based-authentication-demologin">Form-based Authentication <small>(<code>/demo/login</code>)</small></h3>
<p class="lead"><a href="{{ path('demo_login') }}">Form-based Authentication</a> implemented by Silex's <a href="http://silex.sensiolabs.org/doc/providers/security.html">SecurityServiceProvider</a> in this example. Read though <a href="https://github.com/authbucket/oauth2-php/blob/master/tests/AuthBucket/OAuth2/Tests/TestBundle/Resources/config/routing_oauth2.php">routing_oauth2.php</a> and <a href="https://github.com/authbucket/oauth2-php/blob/master/tests/AuthBucket/OAuth2/Tests/TestBundle/Resources/views/demo/login.html.twig">login.html.twig</a> for more information.</p>
<p>This is used for protect above Authorization Endpoints.</p>

<h3 id="debug-endpoint-oauth2debug">Debug Endpoint <small>(<code>/api/oauth2/debug</code>)</small></h3>
Expand Down

0 comments on commit 84ccadd

Please sign in to comment.