diff options
author | Fabien Potencier <fabien.potencier@gmail.com> | 2010-10-19 13:06:43 +0200 |
---|---|---|
committer | Fabien Potencier <fabien.potencier@gmail.com> | 2010-10-19 13:33:17 +0200 |
commit | 3fec93d3ff1f6a31f078e5558a15a75539bf5185 (patch) | |
tree | 1a6229643289d9d0ca55871bab9497035a6e49f1 /Authentication/Token | |
download | symfony-security-3fec93d3ff1f6a31f078e5558a15a75539bf5185.zip symfony-security-3fec93d3ff1f6a31f078e5558a15a75539bf5185.tar.gz symfony-security-3fec93d3ff1f6a31f078e5558a15a75539bf5185.tar.bz2 |
added the Security Component and its integration into the MVC framework
Happy birthday symfony!
Diffstat (limited to 'Authentication/Token')
-rw-r--r-- | Authentication/Token/AnonymousToken.php | 58 | ||||
-rw-r--r-- | Authentication/Token/PreAuthenticatedToken.php | 44 | ||||
-rw-r--r-- | Authentication/Token/Token.php | 156 | ||||
-rw-r--r-- | Authentication/Token/TokenInterface.php | 69 | ||||
-rw-r--r-- | Authentication/Token/UsernamePasswordToken.php | 56 |
5 files changed, 383 insertions, 0 deletions
diff --git a/Authentication/Token/AnonymousToken.php b/Authentication/Token/AnonymousToken.php new file mode 100644 index 0000000..c8fb1aa --- /dev/null +++ b/Authentication/Token/AnonymousToken.php @@ -0,0 +1,58 @@ +<?php + +namespace Symfony\Component\Security\Authentication\Token; + +/* + * This file is part of the Symfony framework. + * + * (c) Fabien Potencier <fabien.potencier@symfony-project.com> + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +/** + * AnonymousToken represents an anonymous token. + * + * @author Fabien Potencier <fabien.potencier@symfony-project.com> + */ +class AnonymousToken extends Token +{ + protected $user; + protected $key; + + /** + * Constructor. + * + * @param string $key The key shared with the authentication provider + * @param string $user The user + * @param Role[] $roles An array of roles + */ + public function __construct($key, $user, array $roles = array()) + { + parent::__construct($roles); + + $this->key = $key; + $this->user = $user; + + parent::setAuthenticated(true); + } + + /** + * {@inheritdoc} + */ + public function getCredentials() + { + return ''; + } + + /** + * Returns the key. + * + * @return string The Key + */ + public function getKey() + { + return $this->key; + } +} diff --git a/Authentication/Token/PreAuthenticatedToken.php b/Authentication/Token/PreAuthenticatedToken.php new file mode 100644 index 0000000..7466757 --- /dev/null +++ b/Authentication/Token/PreAuthenticatedToken.php @@ -0,0 +1,44 @@ +<?php + +namespace Symfony\Component\Security\Authentication\Token; + +/* + * This file is part of the Symfony package. + * + * (c) Fabien Potencier <fabien.potencier@symfony-project.com> + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +/** + * PreAuthenticatedToken implements a pre-authenticated token. + * + * @author Fabien Potencier <fabien.potencier@symfony-project.com> + */ +class PreAuthenticatedToken extends Token +{ + /** + * Constructor. + */ + public function __construct($user, $credentials, array $roles = null) + { + if (null !== $roles) { + parent::__construct($roles); + $this->setAuthenticated(true); + } + + $this->user = $user; + $this->credentials = $credentials; + } + + /** + * {@inheritdoc} + */ + public function eraseCredentials() + { + parent::eraseCredentials(); + + $this->credentials = null; + } +} diff --git a/Authentication/Token/Token.php b/Authentication/Token/Token.php new file mode 100644 index 0000000..8279363 --- /dev/null +++ b/Authentication/Token/Token.php @@ -0,0 +1,156 @@ +<?php + +namespace Symfony\Component\Security\Authentication\Token; + +use Symfony\Component\Security\Role\RoleInterface; +use Symfony\Component\Security\Role\Role; +use Symfony\Component\Security\User\AccountInterface; + +/* + * This file is part of the Symfony package. + * + * (c) Fabien Potencier <fabien.potencier@symfony-project.com> + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +/** + * Base class for Token instances. + * + * @author Fabien Potencier <fabien.potencier@symfony-project.com> + */ +abstract class Token implements TokenInterface +{ + protected $roles; + protected $authenticated; + protected $user; + protected $credentials; + protected $immutable; + + /** + * Constructor. + * + * @param Role[] An array of roles + */ + public function __construct(array $roles = array()) + { + $this->roles = array(); + foreach ($roles as $role) { + if (is_string($role)) { + $role = new Role((string) $role); + } + $this->addRole($role); + } + } + + /** + * Adds a Role to the token. + * + * @param RoleInterface A RoleInterface instance + */ + public function addRole(RoleInterface $role) + { + $this->roles[] = $role; + } + + /** + * {@inheritdoc} + */ + public function getRoles() + { + return $this->roles; + } + + /** + * {@inheritdoc} + */ + public function __toString() + { + if (!is_object($this->user)) { + return (string) $this->user; + } else { + return $this->user->getUsername(); + } + } + + /** + * {@inheritdoc} + */ + public function isAuthenticated() + { + return $this->authenticated; + } + + /** + * {@inheritdoc} + */ + public function setAuthenticated($authenticated) + { + $this->authenticated = (Boolean) $authenticated; + } + + /** + * {@inheritdoc} + */ + public function getCredentials() + { + return $this->credentials; + } + + /** + * {@inheritdoc} + */ + public function getUser() + { + return $this->user; + } + + /** + * Removes sensitive information from the token. + */ + public function eraseCredentials() + { + if ($this->getCredentials() instanceof AccountInterface) { + $this->getCredentials()->eraseCredentials(); + } + + if ($this->getUser() instanceof AccountInterface) { + $this->getUser()->eraseCredentials(); + } + } + + /** + * {@inheritdoc} + */ + public function isImmutable() + { + return $this->immutable; + } + + /** + * {@inheritdoc} + */ + public function setImmutable($value) + { + $this->immutable = (Boolean) $value; + } + + /** + * {@inheritdoc} + */ + public function serialize() + { + // FIXME: don't serialize the user object, just the username (see ContextListener) + //return serialize(array((string) $this, $this->credentials, $this->authenticated, $this->roles, $this->immutable)); + return serialize(array($this->user, $this->credentials, $this->authenticated, $this->roles, $this->immutable)); + } + + /** + * {@inheritdoc} + */ + public function unserialize($serialized) + { + list($this->user, $this->credentials, $this->authenticated, $this->roles, $this->immutable) = unserialize($serialized); + } +} diff --git a/Authentication/Token/TokenInterface.php b/Authentication/Token/TokenInterface.php new file mode 100644 index 0000000..1300716 --- /dev/null +++ b/Authentication/Token/TokenInterface.php @@ -0,0 +1,69 @@ +<?php + +namespace Symfony\Component\Security\Authentication\Token; + +/* + * This file is part of the Symfony package. + * + * (c) Fabien Potencier <fabien.potencier@symfony-project.com> + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +/** + * TokenInterface is the interface for the user authentication information. + * + * @author Fabien Potencier <fabien.potencier@symfony-project.com> + */ +interface TokenInterface extends \Serializable +{ + /** + * Returns a string representation of the token. + * + * @return string A string representation + */ + public function __toString(); + + /** + * Returns the user roles. + * + * @return Role[] An array of Role instances. + */ + function getRoles(); + + /** + * Returns the user credentials. + * + * @return mixed The user credentials + */ + function getCredentials(); + + /** + * Checks whether the token is immutable or not. + * + * @return Boolean true if the token is immutable, false otherwise + */ + function isImmutable(); + + /** + * Returns a user instance. + * + * @return object The User instance + */ + function getUser(); + + /** + * Checks if the user is authenticated or not. + * + * @return Boolean true if the token has been authenticated, false otherwise + */ + function isAuthenticated(); + + /** + * Sets the authenticated flag. + * + * @param Boolean The authenticated flag + */ + function setAuthenticated($isAuthenticated); +} diff --git a/Authentication/Token/UsernamePasswordToken.php b/Authentication/Token/UsernamePasswordToken.php new file mode 100644 index 0000000..5356f8d --- /dev/null +++ b/Authentication/Token/UsernamePasswordToken.php @@ -0,0 +1,56 @@ +<?php + +namespace Symfony\Component\Security\Authentication\Token; + +/* + * This file is part of the Symfony package. + * + * (c) Fabien Potencier <fabien.potencier@symfony-project.com> + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +/** + * UsernamePasswordToken implements a username and password token. + * + * @author Fabien Potencier <fabien.potencier@symfony-project.com> + */ +class UsernamePasswordToken extends Token +{ + /** + * Constructor. + */ + public function __construct($user, $credentials, array $roles = array()) + { + parent::__construct($roles); + + $this->user = $user; + $this->credentials = $credentials; + + parent::setAuthenticated((Boolean) count($roles)); + } + + /** + * {@inheritdoc} + */ + public function setAuthenticated($isAuthenticated) + { + if ($isAuthenticated) + { + throw new \LogicException('Cannot set this token to trusted after instantiation.'); + } + + parent::setAuthenticated(false); + } + + /** + * {@inheritdoc} + */ + public function eraseCredentials() + { + parent::eraseCredentials(); + + $this->credentials = null; + } +} |