Skip to content
This repository has been archived by the owner on Dec 6, 2023. It is now read-only.

[WIP]Implement authenticateToken #10

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 44 additions & 6 deletions Authentication/phpBBSessionAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@
namespace phpBB\SessionsAuthBundle\Authentication;


use Doctrine\ORM\EntityManager;
use phpBB\SessionsAuthBundle\Authentication\Provider\phpBBUserProvider;
use phpBB\SessionsAuthBundle\Tokens\phpBBToken;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Authentication\SimplePreAuthenticatorInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface;

Expand All @@ -31,27 +35,61 @@ class phpBBSessionAuthenticator implements SimplePreAuthenticatorInterface, Auth
/** @var string */
private $loginpage;

/** @var RequestStack */
private $requestStack;

/** @var ContainerInterface */
private $container;

/** @var string */
private $dbconnection;

/**
* @param $cookiename string
* @param $boardurl string
* @param $loginpage string
* @param $requestStack RequestStack
* @param ContainerInterface $container
*/
public function __construct($cookiename, $boardurl, $loginpage)
public function __construct($cookiename, $boardurl, $loginpage, $dbconnection, RequestStack $requestStack, ContainerInterface $container)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's be picky and have these alphabetical?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure:

    public function __construct($acookiename, $boardurl, $cloginpage, $dbconnection,
                                RequestStack $erequestStack, ContainerInterface $fcontainer)

{
$this->cookiename = $cookiename;
$this->boardurl = $boardurl;
$this->loginpage = $loginpage;

$this->cookiename = $cookiename;
$this->boardurl = $boardurl;
$this->loginpage = $loginpage;
$this->dbconnection = $dbconnection;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I only just noticed this. All lowercase property names?

$this->requestStack = $requestStack;
$this->container = $container;
}

/**
* @param TokenInterface $token
* @param UserProviderInterface $userProvider
* @param $providerKey
* @return AnonymousToken
*/
public function authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey)
{
// TODO: Implement authenticateToken() method.
if (!$userProvider instanceof phpBBUserProvider)
{
throw new \InvalidArgumentException(
sprintf(
'The user provider must be an instance of phpBBUserProvider (%s was given).',
get_class($userProvider)
)
);
}

$session_id = $this->requestStack->getCurrentRequest()->cookies->get($this->cookiename . '_sid');

if (empty($session_id))
{
return null; // We can't authenticate if no SID is available.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you don't support SID in url?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Nicofuma We might support it, however is there a chance that there is actually a SID in the URL in a symfony page?
I suppose symfony won't keep it after the first page.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's right, so forgot what I said ^

}

/** @var EntityManager $em */
$em = $this->container->get('doctrine')->getManager($this->dbconnection);

$session = $em->getRepository('phpbbSessionsAuthBundle:Session')->findById($session_id);
}

/**
Expand Down
1 change: 1 addition & 0 deletions DependencyInjection/phpbbSessionsAuthExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public function load(array $configs, ContainerBuilder $container)
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);

$container->setParameter('phpbb_sessions_auth.database.connection', $config['database']['connection']);
$container->setParameter('phpbb_sessions_auth.database.prefix', $config['database']['prefix']);
$container->setParameter('phpbb_sessions_auth.database.cookiename', $config['session']['cookiename']);
$container->setParameter('phpbb_sessions_auth.database.boardurl', $config['session']['boardurl']);
Expand Down
71 changes: 71 additions & 0 deletions Entity/Session.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
/**
*
* @package phpBBSessionsAuthBundle
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license MIT
*
*/
namespace phpBB\SessionsAuthBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\Role\Role;
use Symfony\Component\Security\Core\User\UserInterface;

/**
* Class Session
* @package phpbb\SessionsAuthBundle\Entity
* @ORM\Entity(readOnly=true)
*/
class Session
{
/**
* @var string
* @ORM\Column(type="string", name="session_id")
* @ORM\Id
*/
private $id;

/**
* @var User
* @ORM\ManyToOne(targetEntity="User", inversedBy="sessions")
* @ORM\JoinColumn(name="user_id", referencedColumnName="user_id")
*/
private $user;

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

/**
* @param string $id
*/
public function setId($id)
{
$this->id = $id;
}

/**
* @return User
*/
public function getUser()
{
return $this->user;
}

/**
* @param User $user
*/
public function setUser($user)
{
$this->user = $user;
}


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lots of random empty lines?


}

28 changes: 28 additions & 0 deletions Entity/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/
namespace phpBB\SessionsAuthBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\Role\Role;
use Symfony\Component\Security\Core\User\UserInterface;
Expand Down Expand Up @@ -49,6 +50,17 @@ class User implements UserInterface
*/
private $roles;

/**
* @var ArrayCollection
* @ORM\OneToMany(targetEntity="Session", mappedBy="user")
*/
private $sessions;

public function __construct()
{
$this->sessions = new ArrayCollection();
}

/**
* Returns the roles granted to the user.
*
Expand Down Expand Up @@ -152,6 +164,22 @@ public function setEmail($email)
$this->email = $email;
}

/**
* @return ArrayCollection
*/
public function getSessions()
{
return $this->sessions;
}

/**
* @param ArrayCollection $sessions
*/
public function setSessions($sessions)
{
$this->sessions = $sessions;
}


}

3 changes: 3 additions & 0 deletions Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ services:
- %phpbb_sessions_auth.database.cookiename%
- %phpbb_sessions_auth.database.boardurl%
- %phpbb_sessions_auth.database.loginpage%
- %phpbb_sessions_auth.database.connection%
- @request_stack
- @service_container

phpbb.sessionsauthbundle.phpbb_user_provider:
class: phpBB\SessionsAuthBundle\Authentication\Provider\phpBBUserProvider
Expand Down
9 changes: 5 additions & 4 deletions Subscriber/TablePrefixSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ class TablePrefixSubscriber implements EventSubscriber
/**
* Namespace the entity is in
*/
const ENTITY_NAMESPACE = 'phpbb\\SessionAuthBundle\\Entity';
private static $ENTITY_NAMESPACE = 'phpBB\\SessionsAuthBundle\\Entity';
/**
* Entity that will receive the prefix
*/
const ENTITY_NAME = 'User';
private static $ENTITY_NAME;

/**
* @var string
Expand All @@ -44,6 +44,7 @@ class TablePrefixSubscriber implements EventSubscriber
public function __construct($prefix)
{
$this->prefix = (string) $prefix;
self::$ENTITY_NAME = array(self::$ENTITY_NAMESPACE . '\\User', self::$ENTITY_NAMESPACE . '\\Session');
}

/**
Expand Down Expand Up @@ -74,13 +75,13 @@ public function loadClassMetadata(LoadClassMetadataEventArgs $args)
return;
}

if ($classMetadata->namespace == self::ENTITY_NAMESPACE && $classMetadata->name == self::ENTITY_NAME)
if ($classMetadata->namespace == self::$ENTITY_NAMESPACE && in_array($classMetadata->name, self::$ENTITY_NAME))
{
// Do not re-apply the prefix when the table is already prefixed
if (false === strpos($classMetadata->getTableName(), $this->prefix))
{
$tableName = $this->prefix . $classMetadata->getTableName();
$classMetadata->setPrimaryTable(['name' => $tableName]);
$classMetadata->setPrimaryTable(array('name' => $tableName));
}

foreach ($classMetadata->getAssociationMappings() as $fieldName => $mapping)
Expand Down