-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Added a DoctrineTokenProvider in Security/Core/Authentication/RememberMe #6057
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
123 changes: 123 additions & 0 deletions
123
src/Symfony/Bridge/Doctrine/Security/RememberMe/DoctrineTokenProvider.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
<?php | ||
|
||
namespace Symfony\Bridge\Doctrine\Security\RememberMe; | ||
|
||
use Symfony\Component\Security\Core\Authentication\RememberMe\TokenProviderInterface; | ||
use Symfony\Component\Security\Core\Authentication\RememberMe\PersistentTokenInterface; | ||
use Symfony\Component\Security\Core\Authentication\RememberMe\PersistentToken; | ||
use Symfony\Component\Security\Core\Exception\TokenNotFoundException; | ||
use Doctrine\DBAL\Connection; | ||
use Doctrine\DBAL\Types\Type as DoctrineType; | ||
use PDO, DateTime; | ||
|
||
/** | ||
* This class provides storage for the tokens that is set in "remember me" | ||
* cookies. This way no password secrets will be stored in the cookies on | ||
* the client machine, and thus the security is improved. | ||
* | ||
* This depends only on doctrine in order to get a database connection | ||
* and to do the conversion of the datetime column. | ||
* | ||
* In order to use this class, you need the following table in your database: | ||
* CREATE TABLE `rememberme_token` ( | ||
* `series` char(88) UNIQUE PRIMARY KEY NOT NULL, | ||
* `value` char(88) NOT NULL, | ||
* `lastUsed` datetime NOT NULL, | ||
* `class` varchar(100) NOT NULL, | ||
* `username` varchar(200) NOT NULL | ||
* ); | ||
*/ | ||
class DoctrineTokenProvider implements TokenProviderInterface | ||
{ | ||
/** | ||
* Doctrine DBAL database connection | ||
* F.ex. service id: doctrine.dbal.default_connection | ||
* | ||
* @var \Doctrine\DBAL\Connection | ||
*/ | ||
private $conn; | ||
|
||
/** | ||
* new DoctrineTokenProvider for the RemembeMe authentication service | ||
* | ||
* @param \Doctrine\DBAL\Connection $conn | ||
*/ | ||
public function __construct(Connection $conn) | ||
{ | ||
$this->conn = $conn; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function loadTokenBySeries($series) | ||
{ | ||
$sql = 'SELECT class, username, value, lastUsed' | ||
. ' FROM rememberme_token WHERE series=:series'; | ||
$paramValues = array('series' => $series); | ||
$paramTypes = array('series' => PDO::PARAM_STR); | ||
$stmt = $this->conn->executeQuery($sql, $paramValues, $paramTypes); | ||
$row = $stmt->fetch(PDO::FETCH_ASSOC); | ||
if ($row) { | ||
return new PersistentToken($row['class'], | ||
$row['username'], | ||
$series, | ||
$row['value'], | ||
new DateTime($row['lastUsed']) | ||
); | ||
} | ||
|
||
throw new TokenNotFoundException('No token found.'); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function deleteTokenBySeries($series) | ||
{ | ||
$sql = 'DELETE FROM rememberme_token WHERE series=:series'; | ||
$paramValues = array('series' => $series); | ||
$paramTypes = array('series' => PDO::PARAM_STR); | ||
$this->conn->executeUpdate($sql, $paramValues, $paramTypes); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function updateToken($series, $tokenValue, DateTime $lastUsed) | ||
{ | ||
$sql = 'UPDATE rememberme_token SET value=:value, lastUsed=:lastUsed' | ||
. ' WHERE series=:series'; | ||
$paramValues = array('value' => $tokenValue, | ||
'lastUsed' => $lastUsed, | ||
'series' => $series); | ||
$paramTypes = array('value' => PDO::PARAM_STR, | ||
'lastUsed' => DoctrineType::DATETIME, | ||
'series' => PDO::PARAM_STR); | ||
$updated = $this->conn->executeUpdate($sql, $paramValues, $paramTypes); | ||
if ($updated < 1) { | ||
throw new TokenNotFoundException('No token found.'); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function createNewToken(PersistentTokenInterface $token) | ||
{ | ||
$sql = 'INSERT INTO rememberme_token' | ||
. ' (class, username, series, value, lastUsed)' | ||
. ' VALUES (:class, :username, :series, :value, :lastUsed)'; | ||
$paramValues = array('class' => $token->getClass(), | ||
'username' => $token->getUsername(), | ||
'series' => $token->getSeries(), | ||
'value' => $token->getTokenValue(), | ||
'lastUsed' => $token->getLastUsed()); | ||
$paramTypes = array('class' => PDO::PARAM_STR, | ||
'username' => PDO::PARAM_STR, | ||
'series' => PDO::PARAM_STR, | ||
'value' => PDO::PARAM_STR, | ||
'lastUsed' => DoctrineType::DATETIME); | ||
$this->conn->executeUpdate($sql, $paramValues, $paramTypes); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/Symfony/Bridge/Doctrine/Security/RememberMe/SchemaForDoctrineTokenProvider.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
namespace Symfony\Bridge\Doctrine\Security\RememberMe; | ||
|
||
use Doctrine\DBAL\Schema\Schema; | ||
|
||
$schema = new Schema(); | ||
$rememberTable = $schema->createTable('rememberme_token'); | ||
$rememberTable->addColumn('series', "string", array('Length' => 88, | ||
'Notnull' => true)); | ||
$rememberTable->addColumn('value', "string", array('Length' => 88, | ||
'Notnull' => true)); | ||
|
||
$rememberTable->addColumn('lastUsed', 'datetime', array('Notnull' => true)); | ||
|
||
$rememberTable->addColumn('class', 'string', array('Length' => 100, | ||
'Notnull' => true)); | ||
$rememberTable->addColumn('username', 'string', array('Length' => 200, | ||
'Notnull' => true)); | ||
|
||
$rememberTable->setPrimaryKey(array('series')); | ||
$rememberTable->addUniqueIndex(array('series')); | ||
|
||
|
||
$queries = $schema->toSql($myPlatform); // get queries to create this schema. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you should provide a Schema class for this (as done for the ACL system)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is a Schema class? (and what is the ACL system?)
Do you have any URL references where I can read more about it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Security/Acl/Dbal/Schema.php
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@pborreli Just a link to the source code of Schema is very little helpful, when what I obviously need is some kind of introduction to the topic, or some good documentation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sorry then.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/schema-representation.html
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, and where should I put this Schema then?
(What should be the name of the file, and in which directory?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
e809458