<commit_before><?php
namespace ValuCli\Service;

use Valu\Service\AbstractService;
use Zend\Authentication\Storage\NonPersistent;
use Zend\Mvc\MvcEvent;
use Zend\Console\Request as ConsoleRequest;
use Zend\Console\Prompt;
use Zend\Authentication\Result;
use Zend\Authentication\AuthenticationService;
use Zend\Authentication\Storage\StorageInterface;
use Zend\Console\Adapter\AdapterInterface as Console;

/**
 * Provides console authentication mechanism
 * 
 * @author juhasuni
 *
 */
class Auth extends AbstractService
{
    
    /**
     * Authentication storage instance
     * 
     * @var Zend\Authentication\Storage\StorageInterface
     */
    private $storage;
    
    /**
     * Private copy of user's identity
     * 
     * @var array
     */
    private $identity = null;
    
    /**
     * Console instance
     * 
     * @var Console
     */
    private $console;
    
    public static function version()
    {
        return '0.1';
    }
    
	public function authenticate(MvcEvent $event)
	{
	    // Skip if not console request
	    if (!($event->getRequest() instanceof ConsoleRequest)) {
	        return null;
	    }
	    
	    $routeMatch = $event->getRouteMatch();
	    
	    // Fetch username and password
	    $username = $routeMatch->getParam('user', $routeMatch->getParam('u'));
	    $password = $routeMatch->getParam('password', $routeMatch->getParam('p'));
	    
	    // Prompt username if not given
	    if (!$username) {
	        $confirm = new Prompt\Line('Enter username:');
	        $username = $confirm->show();
	    }
	    
	    if (!$username) {
	        return false;
	    }
	    
	    // Prompt password if not given
	    if (!$password) {
	        $confirm = new Prompt\Line('Enter password:');
	        $password = $confirm->show();
	    }
	    
	    if (!$password) {
	        return false;
	    }
	    
	    // Resolve identity
	    $identity = $this->resolveIdentity($username, $password);

        if ($this->hasIdentity()) {
            $this->clearIdentity();
        }
        
        $this->getStorage()->write($identity);
        
        return new Result(Result::SUCCESS, $identity);
	}
	
	/**
	 * Set storage instance
	 * 
	 * @param StorageInterface $storage
	 * @valu\service\ignore
	 */
	public function setStorage(StorageInterface $storage)
	{
	    $this->storage = $storage;
	}
	
	/**
	 * Retrieve storage instance
	 * 
	 * @return \Zend\Authentication\Storage\StorageInterface
	 * @valu\service\ignore
	 */
	public function getStorage()
	{
	    if(!$this->storage){
	        $this->setStorage(new NonPersistent());
	    }
	    
	    return $this->storage;
	}
    
    /**
     * Returns the identity or null if no identity is available
     *
     * @return mixed|null
     */
    public function getIdentity()
    {
        if($this->hasIdentity()){
            return $this->getStorage()->read();
        }
        else{
            return null;
        }
    }
    
    /**
     * Returns true if and only if an identity is available
     *
     * @return boolean
     */
    public function hasIdentity()
    {
        return !$this->getStorage()->isEmpty();
    }

    /**
     * Clears the identity
     *
     * @return void
     */
    public function clearIdentity()
    {
        $this->getStorage()->clear();
    }
    
	/**
     * @return \Zend\Console\Adapter\AdapterInterface
     */
    public function getConsole()
    {
        if (!$this->console && $this->getServiceLocator()) {
            $this->setConsole($this->getServiceLocator()->get('console'));
        }
        
        return $this->console;
    }

	/**
     * @param \Zend\Console\Adapter\AdapterInterface $console
     */
    public function setConsole(Console $console)
    {
        $this->console = $console;
    }

    /**
     * Resolve identity by username and password
     * 
     * @param string $username
     * @param string $password
     * @return array
     */
    private function resolveIdentity($username, $password)
    {
        $result = false;
        
        if ($this->getServiceBroker()) {
            $result = $this->getServiceBroker()
                ->service('User')
                ->getIdentity($username, $password);
        }
        
        return is_array($result) ? $result : false;
    }
}<commit_msg>Fixes operation name for identity resolving<commit_after><?php
namespace ValuCli\Service;

use Valu\Service\AbstractService;
use Zend\Authentication\Storage\NonPersistent;
use Zend\Mvc\MvcEvent;
use Zend\Console\Request as ConsoleRequest;
use Zend\Console\Prompt;
use Zend\Authentication\Result;
use Zend\Authentication\AuthenticationService;
use Zend\Authentication\Storage\StorageInterface;
use Zend\Console\Adapter\AdapterInterface as Console;

/**
 * Provides console authentication mechanism
 * 
 * @author juhasuni
 *
 */
class Auth extends AbstractService
{
    
    /**
     * Authentication storage instance
     * 
     * @var Zend\Authentication\Storage\StorageInterface
     */
    private $storage;
    
    /**
     * Private copy of user's identity
     * 
     * @var array
     */
    private $identity = null;
    
    /**
     * Console instance
     * 
     * @var Console
     */
    private $console;
    
    public static function version()
    {
        return '0.1';
    }
    
	public function authenticate(MvcEvent $event)
	{
	    // Skip if not console request
	    if (!($event->getRequest() instanceof ConsoleRequest)) {
	        return null;
	    }
	    
	    $routeMatch = $event->getRouteMatch();
	    
	    // Fetch username and password
	    $username = $routeMatch->getParam('user', $routeMatch->getParam('u'));
	    $password = $routeMatch->getParam('password', $routeMatch->getParam('p'));
	    
	    // Prompt username if not given
	    if (!$username) {
	        $confirm = new Prompt\Line('Enter username:');
	        $username = $confirm->show();
	    }
	    
	    if (!$username) {
	        return false;
	    }
	    
	    // Prompt password if not given
	    if (!$password) {
	        $confirm = new Prompt\Line('Enter password:');
	        $password = $confirm->show();
	    }
	    
	    if (!$password) {
	        return false;
	    }
	    
	    // Resolve identity
	    $identity = $this->resolveIdentity($username, $password);

        if ($this->hasIdentity()) {
            $this->clearIdentity();
        }
        
        $this->getStorage()->write($identity);
        
        return new Result(Result::SUCCESS, $identity);
	}
	
	/**
	 * Set storage instance
	 * 
	 * @param StorageInterface $storage
	 * @valu\service\ignore
	 */
	public function setStorage(StorageInterface $storage)
	{
	    $this->storage = $storage;
	}
	
	/**
	 * Retrieve storage instance
	 * 
	 * @return \Zend\Authentication\Storage\StorageInterface
	 * @valu\service\ignore
	 */
	public function getStorage()
	{
	    if(!$this->storage){
	        $this->setStorage(new NonPersistent());
	    }
	    
	    return $this->storage;
	}
    
    /**
     * Returns the identity or null if no identity is available
     *
     * @return mixed|null
     */
    public function getIdentity()
    {
        if($this->hasIdentity()){
            return $this->getStorage()->read();
        }
        else{
            return null;
        }
    }
    
    /**
     * Returns true if and only if an identity is available
     *
     * @return boolean
     */
    public function hasIdentity()
    {
        return !$this->getStorage()->isEmpty();
    }

    /**
     * Clears the identity
     *
     * @return void
     */
    public function clearIdentity()
    {
        $this->getStorage()->clear();
    }
    
	/**
     * @return \Zend\Console\Adapter\AdapterInterface
     */
    public function getConsole()
    {
        if (!$this->console && $this->getServiceLocator()) {
            $this->setConsole($this->getServiceLocator()->get('console'));
        }
        
        return $this->console;
    }

	/**
     * @param \Zend\Console\Adapter\AdapterInterface $console
     */
    public function setConsole(Console $console)
    {
        $this->console = $console;
    }

    /**
     * Resolve identity by username and password
     * 
     * @param string $username
     * @param string $password
     * @return array
     */
    private function resolveIdentity($username, $password)
    {
        $result = false;
        
        if ($this->getServiceBroker()) {
            $result = $this->getServiceBroker()
                ->service('User')
                ->resolveIdentity($username, $password);
        }
        
        return is_array($result) ? $result : false;
    }
}
{'fraction_non_alphanumeric': 0.12927226226458963, 'fraction_numerical': 0.00046500813764240875, 'mean_word_length': 2.042079207920792, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 73, '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': '5690674', 'n_tokens_mistral': 2514, 'n_tokens_neox': 2375, 'n_words': 531}