<commit_before><?php
/**
 * Litus is a project by a group of students from the KU Leuven. The goal is to create
 * various applications to support the IT needs of student unions.
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 *
 * @license http://litus.cc/LICENSE
 */

namespace CommonBundle\Component\Authentication\Service;

use CommonBundle\Component\Authentication\Action,
    CommonBundle\Component\Authentication\Result\Doctrine as Result,
    CommonBundle\Entity\User\Session,
    Doctrine\ORM\EntityManager,
    Zend\Authentication\Adapter\AdapterInterface,
    Zend\Authentication\Storage\StorageInterface as StorageInterface;

/**
 * An authentication service that uses a Doctrine result.
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 */
class Doctrine extends \Zend\Authentication\AuthenticationService
{
    /**
     * @var \Doctrine\ORM\EntityManager The EntityManager instance
     */
    private $_entityManager = null;

    /**
     * @var string The name of the entity that holds the sessions
     */
    private $_entityName = '';

    /**
     * @var int The expiration time for the persistent storage
     */
    private $_expire = -1;

    /**
     * @var string The namespace the storage handlers will use
     */
    private $_namespace = '';

    /**
     * @var string The cookie suffix that is used to store the session cookie
     */
    private $_cookieSuffix = '';

    /**
     * @var \CommonBundle\Component\Authentication\Action The action that should be taken after authentication
     */
    private $_action;

    /**
     * @param \Doctrine\ORM\EntityManager $entityManager The EntityManager instance
     * @param string $entityName The name of the entity that holds the sessions
     * @param int $expire The expiration time for the persistent storage
     * @param \Zend\Authentication\Storage\StorageInterface $storage The persistent storage handler
     * @param string $namespace The namespace the storage handlers will use
     * @param string $cookieSuffix The cookie suffix that is used to store the session cookie
     * @param \CommonBundle\Component\Authentication\Action $action The action that should be taken after authentication
     * @throws \CommonBundle\Component\Authentication\Service\Exception\InvalidArgumentException The entity name cannot have a leading backslash
     */
    public function __construct(
        EntityManager $entityManager, $entityName, $expire, StorageInterface $storage, $namespace, $cookieSuffix, Action $action
    )
    {
        parent::__construct($storage);

        $this->_entityManager = $entityManager;

        $this->_namespace = $namespace;
        $this->_expire = $expire;
        $this->_cookieSuffix = $cookieSuffix;
        $this->_action = $action;

        if ('\\' == substr($entityName, 0, 1)) {
            throw new Exception\InvalidArgumentException(
                'The entity name cannot have a leading backslash'
            );
        }
        $this->_entityName = $entityName;
    }

    /**
     * Authenticates against the supplied adapter
     *
     * @param \Zend\Authentication\Adapter $adapter The supplied adapter
     * @param boolean $rememberMe Remember this authentication session
     * @param boolean $shibboleth Whether or not this is sessions initiated by Shibboleth
     *
     * @return \Zend\Authentication\Result
     */
    public function authenticate(AdapterInterface $adapter = null, $rememberMe = true, $shibboleth = false)
    {
        $result = null;

        if ('' == $this->getIdentity()) {
            $adapterResult = $adapter->authenticate();

            if ($adapterResult->isValid()) {
                $sessionEntity = $this->_entityName;
                $newSession = new $sessionEntity(
                    $adapterResult->getPersonObject(),
                    $_SERVER['HTTP_USER_AGENT'],
                    $_SERVER['REMOTE_ADDR'],
                    $shibboleth,
                    $this->_expire
                );
                $this->_entityManager->persist($newSession);

                $adapterResult->setSessionObject($newSession);

                $this->getStorage()->write($newSession->getId());
                if ($rememberMe) {
                    $this->_setCookie($newSession->getId(), time() + $this->_expire);
                } else {
                    $this->_setCookie('', -1);
                }

                $result = $adapterResult;

                if (isset($this->_action))
                    $this->_action->succeededAction($result);
            } else {
                $result = $adapterResult;
                if (isset($this->_action))
                    $this->_action->failedAction($adapterResult);
            }
        } else {
            $session = $this->_entityManager->getRepository($this->_entityName)->findOneById(
                $this->getIdentity()
            );

            if (null !== $session) {
                $sessionValidation = $session->validate(
                    $this->_entityManager,
                    $_SERVER['HTTP_USER_AGENT'],
                    $_SERVER['REMOTE_ADDR']
                );

                if (true !== $sessionValidation) {
                    $this->getStorage()->write($sessionValidation);
                    if ($rememberMe) {
                        $this->_setCookie($sessionValidation, time() + $this->_expire);
                    } else {
                        $this->_setCookie('', -1);
                    }
                }

                $result = new Result(
                    Result::SUCCESS,
                    $session->getPerson()->getUsername(),
                    array(
                         'Authentication successful'
                    ),
                    $session->getPerson(),
                    $session
                );
            } else {
                $this->clearIdentity();
            }
        }

        $this->_entityManager->flush();

        return $result;
    }

    /**
     * Returns the session identity.
     *
     * @return string
     */
    public function getIdentity()
    {
        return $this->hasIdentity() ? $this->getStorage()->read() : '';
    }
<commit_msg>Fix auth session after receiving new ip<commit_after><?php
/**
 * Litus is a project by a group of students from the KU Leuven. The goal is to create
 * various applications to support the IT needs of student unions.
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 *
 * @license http://litus.cc/LICENSE
 */

namespace CommonBundle\Component\Authentication\Service;

use CommonBundle\Component\Authentication\Action,
    CommonBundle\Component\Authentication\Result\Doctrine as Result,
    CommonBundle\Entity\User\Session,
    Doctrine\ORM\EntityManager,
    Zend\Authentication\Adapter\AdapterInterface,
    Zend\Authentication\Storage\StorageInterface as StorageInterface;

/**
 * An authentication service that uses a Doctrine result.
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 */
class Doctrine extends \Zend\Authentication\AuthenticationService
{
    /**
     * @var \Doctrine\ORM\EntityManager The EntityManager instance
     */
    private $_entityManager = null;

    /**
     * @var string The name of the entity that holds the sessions
     */
    private $_entityName = '';

    /**
     * @var int The expiration time for the persistent storage
     */
    private $_expire = -1;

    /**
     * @var string The namespace the storage handlers will use
     */
    private $_namespace = '';

    /**
     * @var string The cookie suffix that is used to store the session cookie
     */
    private $_cookieSuffix = '';

    /**
     * @var \CommonBundle\Component\Authentication\Action The action that should be taken after authentication
     */
    private $_action;

    /**
     * @param \Doctrine\ORM\EntityManager $entityManager The EntityManager instance
     * @param string $entityName The name of the entity that holds the sessions
     * @param int $expire The expiration time for the persistent storage
     * @param \Zend\Authentication\Storage\StorageInterface $storage The persistent storage handler
     * @param string $namespace The namespace the storage handlers will use
     * @param string $cookieSuffix The cookie suffix that is used to store the session cookie
     * @param \CommonBundle\Component\Authentication\Action $action The action that should be taken after authentication
     * @throws \CommonBundle\Component\Authentication\Service\Exception\InvalidArgumentException The entity name cannot have a leading backslash
     */
    public function __construct(
        EntityManager $entityManager, $entityName, $expire, StorageInterface $storage, $namespace, $cookieSuffix, Action $action
    )
    {
        parent::__construct($storage);

        $this->_entityManager = $entityManager;

        $this->_namespace = $namespace;
        $this->_expire = $expire;
        $this->_cookieSuffix = $cookieSuffix;
        $this->_action = $action;

        if ('\\' == substr($entityName, 0, 1)) {
            throw new Exception\InvalidArgumentException(
                'The entity name cannot have a leading backslash'
            );
        }
        $this->_entityName = $entityName;
    }

    /**
     * Authenticates against the supplied adapter
     *
     * @param \Zend\Authentication\Adapter $adapter The supplied adapter
     * @param boolean $rememberMe Remember this authentication session
     * @param boolean $shibboleth Whether or not this is sessions initiated by Shibboleth
     *
     * @return \Zend\Authentication\Result
     */
    public function authenticate(AdapterInterface $adapter = null, $rememberMe = true, $shibboleth = false)
    {
        $result = null;

        if ('' == $this->getIdentity()) {
            $adapterResult = $adapter->authenticate();

            if ($adapterResult->isValid()) {
                $sessionEntity = $this->_entityName;
                $newSession = new $sessionEntity(
                    $adapterResult->getPersonObject(),
                    $_SERVER['HTTP_USER_AGENT'],
                    $_SERVER['REMOTE_ADDR'],
                    $shibboleth,
                    $this->_expire
                );
                $this->_entityManager->persist($newSession);

                $adapterResult->setSessionObject($newSession);

                $this->getStorage()->write($newSession->getId());
                if ($rememberMe) {
                    $this->_setCookie($newSession->getId(), time() + $this->_expire);
                } else {
                    $this->_setCookie('', -1);
                }

                $result = $adapterResult;

                if (isset($this->_action))
                    $this->_action->succeededAction($result);
            } else {
                $result = $adapterResult;
                if (isset($this->_action))
                    $this->_action->failedAction($adapterResult);
            }
        } else {
            $session = $this->_entityManager->getRepository($this->_entityName)->findOneById(
                $this->getIdentity()
            );

            if (null !== $session) {
                $sessionValidation = $session->validate(
                    $this->_entityManager,
                    $_SERVER['HTTP_USER_AGENT'],
                    $_SERVER['REMOTE_ADDR']
                );

                if (false !== $sessionValidation && true !== $sessionValidation) {
                    $this->getStorage()->write($sessionValidation);
                    if (isset($_COOKIE[$this->_namespace . '_' . $this->_cookieSuffix])) {
                        $this->_setCookie($sessionValidation, time() + $this->_expire);
                    } else {
                        $this->_setCookie('', -1);
                    }
                }

                if (false !== $sessionValidation) {
                    $result = new Result(
                        Result::SUCCESS,
                        $session->getPerson()->getUsername(),
                        array(
                             'Authentication successful'
                        ),
                        $session->getPerson(),
                        $session
                    );
                }
            } else {
                $this->clearIdentity();
            }
        }

        $this->_entityManager->flush();

        return $result;
    }

    /**
     * Returns the session identity.
     *
     * @return string
     */
    public function getIdentity()
    {
        return $this->hasIdentity() ? $this->getStorage()->read() : '';
    }

{'fraction_non_alphanumeric': 0.11384903306300687, 'fraction_numerical': 0.0007797878976918279, 'mean_word_length': 1.940839257051135, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 145, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6647297', 'n_tokens_mistral': 3237, 'n_tokens_neox': 3060, 'n_words': 818}