Skip to content

Commit

Permalink
Authorize application page now random user, not scope.
Browse files Browse the repository at this point in the history
  • Loading branch information
hswong3i committed Jul 26, 2014
1 parent 79e7194 commit a58eec8
Show file tree
Hide file tree
Showing 10 changed files with 330 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Tests/TestBundle/Controller/AuthorizeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function authorizeAction(Request $request)
// Save authorization.
$authorize->setClientId($clientId)
->setUsername($username)
->setScope(array_merge($authorize->getScope(), $scope));
->setScope(array_merge((array) $authorize->getScope(), $scope));
$authorizeManager->updateAuthorize($authorize);

// Back to this path, with original GET parameters.
Expand Down
40 changes: 30 additions & 10 deletions Tests/TestBundle/Controller/DemoController.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,26 @@ public function demoAuthorizeCodeAction(Request $request)
{
$session = $request->getSession();

$scopeManager = $this->get('authbucket_oauth2.model_manager.factory')->getModelManager('scope');
$scope = $scopeManager->createScope()
->setScope(substr(md5(uniqid(null, true)), 0, 8));
$scopeManager->updateScope($scope);
$_username = $session->get('_username', substr(md5(uniqid(null, true)), 0, 8));
$_password = $session->get('_password', substr(md5(uniqid(null, true)), 0, 8));

$session->set('_username', $_username);
$session->set('_password', $_password);

$userManager = $this->get('authbucket_oauth2.model_manager.factory')->getModelManager('user');
$user = $userManager->createUser()
->setUsername($_username)
->setPassword($_password)
->setRoles(array(
'ROLE_USER',
));
$userManager->updateUser($user);

$parameters = array(
'response_type' => 'code',
'client_id' => 'authorization_code_grant',
'redirect_uri' => $request->getUriForPath('/demo/response_type/code'),
'scope' => 'demoscope1 ' . $scope->getScope(),
'scope' => 'demoscope1 demoscope2 demoscope3',
'state' => $session->getId(),
);

Expand All @@ -49,16 +59,26 @@ public function demoAuthorizeTokenAction(Request $request)
{
$session = $request->getSession();

$scopeManager = $this->get('authbucket_oauth2.model_manager.factory')->getModelManager('scope');
$scope = $scopeManager->createScope()
->setScope(substr(md5(uniqid(null, true)), 0, 8));
$scopeManager->updateScope($scope);
$_username = $session->get('_username', substr(md5(uniqid(null, true)), 0, 8));
$_password = $session->get('_password', substr(md5(uniqid(null, true)), 0, 8));

$session->set('_username', $_username);
$session->set('_password', $_password);

$userManager = $this->get('authbucket_oauth2.model_manager.factory')->getModelManager('user');
$user = $userManager->createUser()
->setUsername($_username)
->setPassword($_password)
->setRoles(array(
'ROLE_USER',
));
$userManager->updateUser($user);

$parameters = array(
'response_type' => 'token',
'client_id' => 'implicit_grant',
'redirect_uri' => $request->getUriForPath('/demo/response_type/token'),
'scope' => 'demoscope1 ' . $scope->getScope(),
'scope' => 'demoscope1 demoscope2 demoscope3',
'state' => $session->getId(),
);

Expand Down
7 changes: 7 additions & 0 deletions Tests/TestBundle/Controller/OAuth2Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,21 @@ public function oauth2IndexAction(Request $request)

public function oauth2LoginAction(Request $request)
{
$session = $request->getSession();

if ($request->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) {
$error = $request->attributes->get(SecurityContext::AUTHENTICATION_ERROR);
} else {
$error = $request->getSession()->get(SecurityContext::AUTHENTICATION_ERROR);
}

$_username = $session->get('_username');
$_password = $session->get('_password');

return $this->render('TestBundle:oauth2:login.html.twig', array(
'error' => $error,
'_username' => $_username,
'_password' => $_password,
));
}
}
48 changes: 48 additions & 0 deletions Tests/TestBundle/DataFixtures/ORM/UserFixture.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

/**
* This file is part of the authbucket/oauth2-bundle package.
*
* (c) Wong Hoi Sing Edison <hswong3i@pantarei-design.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace AuthBucket\Bundle\OAuth2Bundle\Tests\TestBundle\DataFixtures\ORM;

use AuthBucket\Bundle\OAuth2Bundle\Tests\TestBundle\Entity\User;
use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;

class UserFixture implements FixtureInterface
{
public function load(ObjectManager $manager)
{
$model = new User();
$model->setUsername('demousername1')
->setPassword('demopassword1')
->setRoles(array(
'ROLE_USER',
));
$manager->persist($model);

$model = new User();
$model->setUsername('demousername2')
->setPassword('demopassword2')
->setRoles(array(
'ROLE_USER',
));
$manager->persist($model);

$model = new User();
$model->setUsername('demousername3')
->setPassword('demopassword3')
->setRoles(array(
'ROLE_USER',
));
$manager->persist($model);

$manager->flush();
}
}
145 changes: 145 additions & 0 deletions Tests/TestBundle/Entity/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
<?php

/**
* This file is part of the authbucket/oauth2-bundle package.
*
* (c) Wong Hoi Sing Edison <hswong3i@pantarei-design.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

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

use AuthBucket\OAuth2\Model\ModelInterface;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;

/**
* User
*
* @ORM\Table(name="test_user")
* @ORM\Entity(repositoryClass="AuthBucket\Bundle\OAuth2Bundle\Tests\TestBundle\Entity\UserRepository")
*/
class User implements ModelInterface, UserInterface
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;

/**
* @var string
*
* @ORM\Column(name="username", type="string", length=255)
*/
protected $username;

/**
* @var string
*
* @ORM\Column(name="password", type="string", length=255)
*/
protected $password;

/**
* @var array
*
* @ORM\Column(name="roles", type="array")
*/
protected $roles;

/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}

/**
* Set username
*
* @param string $username
*
* @return User
*/
public function setUsername($username)
{
$this->username = $username;

return $this;
}

/**
* Get username
*
* @return string
*/
public function getUsername()
{
return $this->username;
}

/**
* Set password
*
* @param string $password
*
* @return User
*/
public function setPassword($password)
{
$this->password = $password;

return $this;
}

/**
* Get password
*
* @return string
*/
public function getPassword()
{
return $this->password;
}

/**
* Set roles.
*
* @param array $roles
*
* @return User
*/
public function setRoles($roles)
{
$this->roles = $roles;

return $this;
}

/**
* Get roles.
*
* @return array
*/
public function getRoles()
{
return $this->roles;
}

public function getSalt()
{
}

public function eraseCredentials()
{
}
}
86 changes: 86 additions & 0 deletions Tests/TestBundle/Entity/UserRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

/**
* This file is part of the authbucket/oauth2-bundle package.
*
* (c) Wong Hoi Sing Edison <hswong3i@pantarei-design.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

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

use AuthBucket\OAuth2\Model\ModelManagerInterface;
use Doctrine\ORM\EntityRepository;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;

/**
* UserRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class UserRepository extends EntityRepository implements ModelManagerInterface, UserProviderInterface
{
public function getClass()
{
return $this->getClassName();
}

public function createUser()
{
$class = $this->getClass();

return new $class();
}

public function deleteUser(UserInterface $user)
{
$this->getEntityManager()->remove($user);
$this->getEntityManager()->flush();
}

public function reloadUser(UserInterface $user)
{
$this->getEntityManager()->refresh($user);
}

public function updateUser(UserInterface $user)
{
$this->getEntityManager()->persist($user);
$this->getEntityManager()->flush();
}

public function findUserByUsername($username)
{
return $this->findOneBy(array(
'username' => $username,
));
}

public function loadUserByUsername($username)
{
$user = $this->findOneBy(array(
'username' => $username,
));
if ($user === null) {
throw new UsernameNotFoundException();
}

return $user;
}

public function refreshUser(UserInterface $user)
{
return $this->find($user->getId());
}

public function supportsClass($class)
{
return $this->getEntityName() === $class
|| is_subclass_of($class, $this->getEntityName());
}
}
11 changes: 8 additions & 3 deletions Tests/TestBundle/Resources/views/oauth2/login.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,19 @@

<h2 class="page-header">Login Form</h2>
<form action="{{ path('oauth2_authorize_login_check') }}" method="post" role="form">
{{ error }}
{% if error is not empty %}
<div class="alert alert-danger alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">&times;</span></button>
{{ error }}
</div>
{% endif %}
<div class="form-group">
<label>Username</label>
<input class="form-control" type="text" name="_username" value="demousername1" />
<input class="form-control" type="text" name="_username" value="{{ _username }}" />
</div>
<div class="form-group">
<label>Password</label>
<input class="form-control" type="password" name="_password" value="demopassword1" />
<input class="form-control" type="password" name="_password" value="{{ _password }}" />
</div>
<div class="form-group">
<button class="btn btn-success" type="submit" name="submit">Login</button>
Expand Down
Loading

0 comments on commit a58eec8

Please sign in to comment.