Skip to content

Commit

Permalink
1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
hswong3i committed Jul 23, 2014
1 parent bee97c2 commit bf43aa2
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 66 deletions.
52 changes: 1 addition & 51 deletions Controller/AuthorizeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,63 +11,13 @@

namespace AuthBucket\Bundle\OAuth2Bundle\Controller;

use AuthBucket\OAuth2\Exception\InvalidScopeException;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

class AuthorizeController extends Controller
{
public function authorizeAction(Request $request)
{
// We only handle non-authorized scope here.
try {
return $this->get('authbucket_oauth2.authorize_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 = $this->get('security.context')->getToken()->getUser()->getUsername();
$scope = preg_split('/\s+/', $request->query->get('scope', ''));

// Create form.
$form = $this->createFormBuilder()->getForm();
$form->handleRequest($request);

// Save authorized scope if submitted by POST.
if ($form->isValid()) {
$modelManager = $this->get('authbucket_oauth2.model_manager.factory');
$authorizeManager = $modelManager->getModelManager('authorize');
$authorize = $authorizeManager->findAuthorizeByClientIdAndUsername($clientId, $username);

// Update existing authorization if possible, else create new.
if ($authorize === null) {
$authorize = $authorizeManager->createAuthorize();
}

// Save authorization.
$authorize->setClientId($clientId)
->setUsername($username)
->setScope(array_merge($authorize->getScope(), $scope));
$authorizeManager->updateAuthorize($authorize);

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

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

return $this->render('TestBundle:oauth2:authorize.html.twig', array(
'client_id' => $clientId,
'username' => $username,
'scopes' => $scope,
'form' => $form->createView(),
'authorization_request' => $authorizationRequest,
));
return $this->get('authbucket_oauth2.authorize_controller')->authorizeAction($request);
}
}
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Here is a minimal example of a `composer.json`:

{
"require": {
"authbucket/oauth2-bundle": "dev-master"
"authbucket/oauth2-bundle": "~1.0"
}
}

Expand Down Expand Up @@ -82,6 +82,7 @@ You have to add `AuthBucketOAuth2Bundle` to your `AppKernel.php`:
$bundles = array(
new AuthBucket\Bundle\OAuth2Bundle\AuthBucketOAuth2Bundle(),
);

return $bundles;
}
}
Expand Down Expand Up @@ -109,7 +110,7 @@ Below is a list of recipes that cover some common use cases.
We don't provide custom firewall for this endpoint, which you should
protect it by yourself, authenticate and capture the user credential,
e.g. by
[SecurityServiceProvider](http://silex.sensiolabs.org/doc/providers/security.html):
[SecurityBundle](http://symfony.com/doc/current/reference/configuration/security.html):

# app/config/security.yml

Expand Down Expand Up @@ -209,8 +210,8 @@ endpoint:
options:
token_path: http://example.com/oauth2/token
debug_path: http://example.com/oauth2/debug
client_id: 'http://democlient1.com/'
client_secret: 'demosecret1'
client_id: http://democlient1.com/
client_secret: demosecret1
cache: true

Demo
Expand Down
52 changes: 51 additions & 1 deletion Tests/TestBundle/Controller/AuthorizeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,63 @@

namespace AuthBucket\Bundle\OAuth2Bundle\Tests\TestBundle\Controller;

use AuthBucket\OAuth2\Exception\InvalidScopeException;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

class AuthorizeController extends Controller
{
public function authorizeAction(Request $request)
{
return $this->get('authbucket_oauth2.authorize_controller')->authorizeAction($request);
// We only handle non-authorized scope here.
try {
return $this->get('authbucket_oauth2.authorize_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 = $this->get('security.context')->getToken()->getUser()->getUsername();
$scope = preg_split('/\s+/', $request->query->get('scope', ''));

// Create form.
$form = $this->createFormBuilder()->getForm();
$form->handleRequest($request);

// Save authorized scope if submitted by POST.
if ($form->isValid()) {
$modelManager = $this->get('authbucket_oauth2.model_manager.factory');
$authorizeManager = $modelManager->getModelManager('authorize');
$authorize = $authorizeManager->findAuthorizeByClientIdAndUsername($clientId, $username);

// Update existing authorization if possible, else create new.
if ($authorize === null) {
$authorize = $authorizeManager->createAuthorize();
}

// Save authorization.
$authorize->setClientId($clientId)
->setUsername($username)
->setScope(array_merge($authorize->getScope(), $scope));
$authorizeManager->updateAuthorize($authorize);

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

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

return $this->render('TestBundle:oauth2:authorize.html.twig', array(
'client_id' => $clientId,
'username' => $username,
'scopes' => $scope,
'form' => $form->createView(),
'authorization_request' => $authorizationRequest,
));
}
}
2 changes: 1 addition & 1 deletion Tests/TestBundle/Controller/DefaultController.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function adminRefreshDatabaseAction(Request $request)
}

$classes = array();
foreach ($this->get('authbucket_oauth2.model') as $class) {
foreach ($this->container->getParameter('authbucket_oauth2.model') as $class) {
$classes[] = $em->getClassMetadata($class);
}

Expand Down
1 change: 1 addition & 0 deletions Tests/TestBundle/Resources/config/routing_client.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
client_index:
path: /client
defaults: { _controller: TestBundle:Client:clientIndex }
methods: [ GET ]
11 changes: 11 additions & 0 deletions Tests/TestBundle/Resources/config/routing_demo.yml
Original file line number Diff line number Diff line change
@@ -1,43 +1,54 @@
demo_index:
path: /demo
defaults: { _controller: TestBundle:Demo:demoIndex }
methods: [ GET ]

demo_authorize_code:
path: /demo/authorize/code
defaults: { _controller: TestBundle:Demo:demoAuthorizeCode }
methods: [ GET ]

demo_authorize_token:
path: /demo/authorize/token
defaults: { _controller: TestBundle:Demo:demoAuthorizeToken }
methods: [ GET ]

demo_response_type_code:
path: /demo/response_type/code
defaults: { _controller: TestBundle:Demo:demoResponseTypeCode }
methods: [ GET ]

demo_response_type_token:
path: /demo/response_type/token
defaults: { _controller: TestBundle:Demo:demoResponseTypeToken }
methods: [ GET ]

demo_grant_type_authorization_code:
path: /demo/grant_type/authorization_code
defaults: { _controller: TestBundle:Demo:demoGrantTypeAuthorizationCode }
methods: [ GET ]

demo_grant_type_password:
path: /demo/grant_type/password
defaults: { _controller: TestBundle:Demo:demoGrantTypePassword }
methods: [ GET ]

demo_grant_type_client_credentials:
path: /demo/grant_type/client_credentials
defaults: { _controller: TestBundle:Demo:demoGrantTypeClientCredentials }
methods: [ GET ]

demo_grant_type_refresh_token:
path: /demo/grant_type/refresh_token
defaults: { _controller: TestBundle:Demo:demoGrantTypeRefreshToken }
methods: [ GET ]

demo_resource_type_model:
path: /demo/resource_type/model
defaults: { _controller: TestBundle:Demo:demoResourceTypeModel }
methods: [ GET ]

demo_resource_type_debug_endpoint:
path: /demo/resource_type/debug_endpoint
defaults: { _controller: TestBundle:Demo:demoResourceTypeDebugEndpoint }
methods: [ GET ]
20 changes: 15 additions & 5 deletions Tests/TestBundle/Resources/config/routing_oauth2.yml
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
authbucketoauth2bundle:
prefix: /oauth2
resource: "@AuthBucketOAuth2Bundle/Resources/config/routing.yml"

oauth2_index:
path: /oauth2
defaults: { _controller: TestBundle:OAuth2:oauth2Index }
methods: [ GET ]

oauth2_login:
path: /oauth2/login
defaults: { _controller: TestBundle:OAuth2:oauth2Login }
methods: [ GET ]

oauth2_authorize_http:
path: /oauth2/authorize/http
defaults: { _controller: AuthBucketOAuth2Bundle:Authorize:authorize }
methods: [ GET ]

oauth2_authorize:
pattern: /oauth2/authorize
defaults: { _controller: TestBundle:Authorize:authorize }
methods: [GET]

oauth2_token:
pattern: /oauth2/token
defaults: { _controller: AuthBucketOAuth2Bundle:Token:token }

oauth2_debug:
pattern: /oauth2/debug
defaults: { _controller: AuthBucketOAuth2Bundle:Debug:debug }

oauth2_authorize_login_check:
path: /oauth2/authorize/login_check
Expand Down
1 change: 1 addition & 0 deletions Tests/TestBundle/Resources/config/routing_resource.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
resource_index:
path: /resource
defaults: { _controller: TestBundle:Resource:resourceIndex }
methods: [ GET ]

resource_resource_type_model:
path: /resource/resource_type/model
Expand Down
5 changes: 3 additions & 2 deletions Tests/TestBundle/Resources/views/index.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<p>Here is a minimal example of a <code>composer.json</code>:</p>
<pre><code class="json">{
&quot;require&quot;: {
&quot;authbucket/oauth2-bundle&quot;: &quot;dev-master&quot;
&quot;authbucket/oauth2-bundle&quot;: &quot;~1.0&quot;
}
}</code></pre>

Expand Down Expand Up @@ -69,6 +69,7 @@ class AppKernel extends Kernel
$bundles = array(
new AuthBucket\Bundle\OAuth2Bundle\AuthBucketOAuth2Bundle(),
);

return $bundles;
}
}</code></pre>
Expand All @@ -84,7 +85,7 @@ authbucketoauth2bundle:
<p>Below is a list of recipes that cover some common use cases.</p>

<h3 id="authorization-endpoint">Authorization Endpoint</h3>
<p>We don't provide custom firewall for this endpoint, which you should protect it by yourself, authenticate and capture the user credential, e.g. by <a href="http://silex.sensiolabs.org/doc/providers/security.html">SecurityServiceProvider</a>:</p>
<p>We don't provide custom firewall for this endpoint, which you should protect it by yourself, authenticate and capture the user credential, e.g. by <a href="http://symfony.com/doc/current/reference/configuration/security.html">SecurityBundle</a>:</p>
<pre><code class="no-highlight"># app/config/security.yml

security:
Expand Down
1 change: 0 additions & 1 deletion Tests/TestBundle/TestBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,5 @@ class TestBundle extends Bundle
{
public function bulid(ContainerBuilder $container)
{

}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"minimum-stability": "dev",
"name": "authbucket/oauth2-bundle",
"require": {
"authbucket/oauth2": "dev-master",
"authbucket/oauth2": "~1.0",
"php": ">=5.3.3",
"symfony/framework-bundle": "~2.3",
"symfony/security-bundle": "~2.3"
Expand Down

0 comments on commit bf43aa2

Please sign in to comment.