summaryrefslogtreecommitdiffstats
path: root/Core/Authentication/Token
diff options
context:
space:
mode:
authorJohannes M. Schmitt <schmittjoh@gmail.com>2011-01-26 21:34:11 +0100
committerFabien Potencier <fabien.potencier@gmail.com>2011-01-26 22:23:20 +0100
commitbebc09870cb0a7720e2c6a8c5c74585e69e8bb24 (patch)
tree0c399647cdbe504be405017e7cc04c70c53482f2 /Core/Authentication/Token
parentc85f3d708d2c9b00d73ca1234ccfaf50336d94b1 (diff)
downloadsymfony-security-bebc09870cb0a7720e2c6a8c5c74585e69e8bb24.zip
symfony-security-bebc09870cb0a7720e2c6a8c5c74585e69e8bb24.tar.gz
symfony-security-bebc09870cb0a7720e2c6a8c5c74585e69e8bb24.tar.bz2
namespace changes
Symfony\Component\Security -> Symfony\Component\Security\Core Symfony\Component\Security\Acl remains unchanged Symfony\Component\HttpKernel\Security -> Symfony\Component\Security\Http
Diffstat (limited to 'Core/Authentication/Token')
-rw-r--r--Core/Authentication/Token/AnonymousToken.php58
-rw-r--r--Core/Authentication/Token/PreAuthenticatedToken.php52
-rw-r--r--Core/Authentication/Token/RememberMeToken.php75
-rw-r--r--Core/Authentication/Token/Token.php199
-rw-r--r--Core/Authentication/Token/TokenInterface.php102
-rw-r--r--Core/Authentication/Token/UsernamePasswordToken.php66
6 files changed, 552 insertions, 0 deletions
diff --git a/Core/Authentication/Token/AnonymousToken.php b/Core/Authentication/Token/AnonymousToken.php
new file mode 100644
index 0000000..7735925
--- /dev/null
+++ b/Core/Authentication/Token/AnonymousToken.php
@@ -0,0 +1,58 @@
+<?php
+
+/*
+ * 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.
+ */
+
+namespace Symfony\Component\Security\Core\Authentication\Token;
+
+/**
+ * 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/Core/Authentication/Token/PreAuthenticatedToken.php b/Core/Authentication/Token/PreAuthenticatedToken.php
new file mode 100644
index 0000000..c84ea10
--- /dev/null
+++ b/Core/Authentication/Token/PreAuthenticatedToken.php
@@ -0,0 +1,52 @@
+<?php
+
+/*
+ * 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.
+ */
+
+namespace Symfony\Component\Security\Core\Authentication\Token;
+
+/**
+ * PreAuthenticatedToken implements a pre-authenticated token.
+ *
+ * @author Fabien Potencier <fabien.potencier@symfony-project.com>
+ */
+class PreAuthenticatedToken extends Token
+{
+ protected $providerKey;
+
+ /**
+ * Constructor.
+ */
+ public function __construct($user, $credentials, $providerKey, array $roles = null)
+ {
+ parent::__construct(null === $roles ? array() : $roles);
+ if (null !== $roles) {
+ $this->setAuthenticated(true);
+ }
+
+ $this->user = $user;
+ $this->credentials = $credentials;
+ $this->providerKey = $providerKey;
+ }
+
+ public function getProviderKey()
+ {
+ return $this->providerKey;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function eraseCredentials()
+ {
+ parent::eraseCredentials();
+
+ $this->credentials = null;
+ }
+}
diff --git a/Core/Authentication/Token/RememberMeToken.php b/Core/Authentication/Token/RememberMeToken.php
new file mode 100644
index 0000000..81bf1e0
--- /dev/null
+++ b/Core/Authentication/Token/RememberMeToken.php
@@ -0,0 +1,75 @@
+<?php
+
+/*
+ * 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.
+ */
+
+namespace Symfony\Component\Security\Core\Authentication\Token;
+
+use Symfony\Component\Security\Core\Authentication\RememberMe\PersistentTokenInterface;
+use Symfony\Component\Security\Core\User\AccountInterface;
+
+/**
+ * Base class for "Remember Me" tokens
+ *
+ * @author Johannes M. Schmitt <schmittjoh@gmail.com>
+ */
+class RememberMeToken extends Token
+{
+ protected $key;
+ protected $providerKey;
+
+ /**
+ * The persistent token which resulted in this authentication token.
+ *
+ * @var PersistentTokenInterface
+ */
+ protected $persistentToken;
+
+ /**
+ * Constructor.
+ *
+ * @param string $username
+ * @param string $key
+ */
+ public function __construct(AccountInterface $user, $providerKey, $key) {
+ parent::__construct($user->getRoles());
+
+ if (empty($key)) {
+ throw new \InvalidArgumentException('$key must not be empty.');
+ }
+ if (empty($providerKey)) {
+ throw new \InvalidArgumentException('$providerKey must not be empty.');
+ }
+
+ $this->setUser($user);
+ $this->providerKey = $providerKey;
+ $this->key = $key;
+ $this->setAuthenticated(true);
+ }
+
+ public function getProviderKey()
+ {
+ return $this->providerKey;
+ }
+
+ public function getKey()
+ {
+ return $this->key;
+ }
+
+ public function getPersistentToken()
+ {
+ return $this->persistentToken;
+ }
+
+ public function setPersistentToken(PersistentTokenInterface $persistentToken)
+ {
+ $this->persistentToken = $persistentToken;
+ }
+} \ No newline at end of file
diff --git a/Core/Authentication/Token/Token.php b/Core/Authentication/Token/Token.php
new file mode 100644
index 0000000..d41bab5
--- /dev/null
+++ b/Core/Authentication/Token/Token.php
@@ -0,0 +1,199 @@
+<?php
+
+/*
+ * 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.
+ */
+
+namespace Symfony\Component\Security\Core\Authentication\Token;
+
+use Symfony\Component\Security\Core\Role\RoleInterface;
+use Symfony\Component\Security\Core\Role\Role;
+use Symfony\Component\Security\Core\User\AccountInterface;
+
+/**
+ * Base class for Token instances.
+ *
+ * @author Fabien Potencier <fabien.potencier@symfony-project.com>
+ * @author Johannes M. Schmitt <schmittjoh@gmail.com>
+ */
+abstract class Token implements TokenInterface
+{
+ protected $roles;
+ protected $authenticated;
+ protected $user;
+ protected $credentials;
+ protected $immutable;
+
+ /**
+ * Constructor.
+ *
+ * @param Role[] $roles An array of roles
+ */
+ public function __construct(array $roles = array())
+ {
+ $this->setRoles($roles);
+ $this->authenticated = false;
+ $this->immutable = false;
+ }
+
+ /**
+ * Adds a Role to the token.
+ *
+ * @param RoleInterface $role A RoleInterface instance
+ */
+ public function addRole(RoleInterface $role)
+ {
+ if ($this->immutable) {
+ throw new \LogicException('This token is considered immutable.');
+ }
+
+ $this->roles[] = $role;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function getRoles()
+ {
+ return $this->roles;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public function setRoles(array $roles)
+ {
+ $this->roles = array();
+
+ foreach ($roles as $role) {
+ if (is_string($role)) {
+ $role = new Role($role);
+ }
+
+ $this->addRole($role);
+ }
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function __toString()
+ {
+ if (!is_object($this->user)) {
+ return (string) $this->user;
+ } elseif ($this->user instanceof AccountInterface) {
+ return $this->user->getUsername();
+ } else {
+ return 'n/a';
+ }
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function isAuthenticated()
+ {
+ return $this->authenticated;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function setAuthenticated($authenticated)
+ {
+ if ($this->immutable) {
+ throw new \LogicException('This token is considered immutable.');
+ }
+
+ $this->authenticated = (Boolean) $authenticated;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function getCredentials()
+ {
+ return $this->credentials;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function getUser()
+ {
+ return $this->user;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public function setUser($user)
+ {
+ if ($this->immutable) {
+ throw new \LogicException('This token is considered immutable.');
+ }
+
+ if (!is_string($user) && !is_object($user)) {
+ throw new \InvalidArgumentException('$user must be an object, or a primitive string.');
+ } else if (is_object($user) && !method_exists($user, '__toString')) {
+ throw new \InvalidArgumentException('If $user is an object, it must implement __toString().');
+ }
+
+ $this->user = $user;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function eraseCredentials()
+ {
+ if ($this->immutable) {
+ throw new \LogicException('This token is considered immutable.');
+ }
+
+ 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()
+ {
+ $this->immutable = true;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function serialize()
+ {
+ 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/Core/Authentication/Token/TokenInterface.php b/Core/Authentication/Token/TokenInterface.php
new file mode 100644
index 0000000..b6ac31c
--- /dev/null
+++ b/Core/Authentication/Token/TokenInterface.php
@@ -0,0 +1,102 @@
+<?php
+
+/*
+ * 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.
+ */
+
+namespace Symfony\Component\Security\Core\Authentication\Token;
+
+use Symfony\Component\Security\Core\User\AccountInterface;
+
+/**
+ * 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
+ */
+ function __toString();
+
+ /**
+ * Returns the user roles.
+ *
+ * @return Role[] An array of Role instances.
+ */
+ function getRoles();
+
+ /**
+ * Sets the user's roles
+ *
+ * @param array $roles
+ * @return void
+ */
+ function setRoles(array $roles);
+
+ /**
+ * Returns the user credentials.
+ *
+ * @return mixed The user credentials
+ */
+ function getCredentials();
+
+ /**
+ * Returns a user representation.
+ *
+ * @return mixed either returns an object which implements __toString(), or
+ * a primitive string is returned.
+ */
+ function getUser();
+
+ /**
+ * Sets the user.
+ *
+ * @param mixed $user can either be an object which implements __toString(), or
+ * only a primitive string
+ */
+ function setUser($user);
+
+ /**
+ * 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 $isAuthenticated The authenticated flag
+ */
+ function setAuthenticated($isAuthenticated);
+
+ /**
+ * Whether this token is considered immutable
+ *
+ * @return Boolean
+ */
+ function isImmutable();
+
+ /**
+ * Marks this token as immutable. This change cannot be reversed.
+ *
+ * You'll need to create a new token if you want a mutable token again.
+ *
+ * @return void
+ */
+ function setImmutable();
+
+ /**
+ * Removes sensitive information from the token.
+ */
+ function eraseCredentials();
+}
diff --git a/Core/Authentication/Token/UsernamePasswordToken.php b/Core/Authentication/Token/UsernamePasswordToken.php
new file mode 100644
index 0000000..a61acd4
--- /dev/null
+++ b/Core/Authentication/Token/UsernamePasswordToken.php
@@ -0,0 +1,66 @@
+<?php
+
+/*
+ * 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.
+ */
+
+namespace Symfony\Component\Security\Core\Authentication\Token;
+
+/**
+ * UsernamePasswordToken implements a username and password token.
+ *
+ * @author Fabien Potencier <fabien.potencier@symfony-project.com>
+ */
+class UsernamePasswordToken extends Token
+{
+ protected $providerKey;
+
+ /**
+ * Constructor.
+ *
+ * @param string $user The username (like a nickname, email address, etc.)
+ * @param string $credentials This usually is the password of the user
+ */
+ public function __construct($user, $credentials, $providerKey, array $roles = array())
+ {
+ parent::__construct($roles);
+
+ $this->setUser($user);
+ $this->credentials = $credentials;
+ $this->providerKey = $providerKey;
+
+ parent::setAuthenticated((Boolean) count($roles));
+ }
+
+ public function getProviderKey()
+ {
+ return $this->providerKey;
+ }
+
+ /**
+ * {@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;
+ }
+}