<commit_before><?php
namespace Ksoft\Tssaa\WebAppBundle\Security\Authentication\Provider;

use Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\Exception\NonceExpiredException;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Ksoft\Tssaa\WebAppBundle\Security\Authentication\Token\WsseUserToken;

require_once('FirePHPCore/FirePHP.class.php');

class WsseProvider implements AuthenticationProviderInterface {
	private $userProvider;
	private $cacheDir;
	private $firephp;

	public function __construct(UserProviderInterface $userProvider, $cacheDir) {
		$this->userProvider = $userProvider;
		$this->cacheDir = $cacheDir;

		// for great logs	
		$this->firePHPLogger = \FirePHP::getInstance(true);
	}

	public function authenticate(TokenInterface $token) {
		$fp = $this->firePHPLogger;
		
		// authenticate() log group
		$fp->group('authenticate()');
		ob_start();

		$user = $this->userProvider->loadUserByUsername($token->getUsername());
		$fp->info($user, "user object");

		try {
			$is_valid = NULL;
			if ($user && $is_valid = $this->validateDigest(
				$token->digest, 
				$token->nonce, 
				$token->created, 
				$user->getPassword()
			)) {
				$authenticatedToken = new WsseUserToken($user->getRoles());
				$authenticatedToken->setUser($user);

				$fp->info($authenticatedToken, "auth'd token");

				return $authenticatedToken;
			}
			$fp->info($is_valid, "digest is valid?");

			throw new AuthenticationException('WSSE failed');

		} catch(AuthenticationException $err) {
			$fp->info("hay man it's the catch block");
			$fp->error($err, "auth exception");
			throw $err;
		}	

		// end authenticate() log group
		$fp->groupEnd(); 
	}

	protected function validateDigest($digest, $nonce, $created, $secret) {
		$fp = $this->firePHPLogger;
		$fp->group('validateDigest()');
		$fp->info($digest, 'digest');
		$fp->info($nonce, 'nonce');
		$fp->info($created, 'created');
		$fp->info($secret, 'secret');

		if (time() - strtotime($created) > 300) {
			return false;
		}

		if (file_exists($this->cacheDir.'/'.$nonce) && file_get_contents($this->cacheDir.'/'.$nonce) + 300 < time()) {
			throw new NonceExpiredException('Previously used nonce detected');
		}
		file_put_contents($this->cacheDir.'/'.$nonce, time());
			<commit_msg>add ob_start<commit_after><?php
namespace Ksoft\Tssaa\WebAppBundle\Security\Authentication\Provider;

use Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\Exception\NonceExpiredException;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Ksoft\Tssaa\WebAppBundle\Security\Authentication\Token\WsseUserToken;

require_once('FirePHPCore/FirePHP.class.php');

class WsseProvider implements AuthenticationProviderInterface {
	private $userProvider;
	private $cacheDir;
	private $firephp;

	public function __construct(UserProviderInterface $userProvider, $cacheDir) {
		$this->userProvider = $userProvider;
		$this->cacheDir = $cacheDir;

		// for great logs	
		$this->firePHPLogger = \FirePHP::getInstance(true);
	}

	public function authenticate(TokenInterface $token) {
		$fp = $this->firePHPLogger;
		
		// authenticate() log group
		$fp->group('authenticate()');
		ob_start();

		$user = $this->userProvider->loadUserByUsername($token->getUsername());
		$fp->info($user, "user object");

		try {
			$is_valid = NULL;
			if ($user && $is_valid = $this->validateDigest(
				$token->digest, 
				$token->nonce, 
				$token->created, 
				$user->getPassword()
			)) {
				$authenticatedToken = new WsseUserToken($user->getRoles());
				$authenticatedToken->setUser($user);

				$fp->info($authenticatedToken, "auth'd token");

				return $authenticatedToken;
			}
			$fp->info($is_valid, "digest is valid?");

			throw new AuthenticationException('WSSE failed');

		} catch(AuthenticationException $err) {
			$fp->info("hay man it's the catch block");
			$fp->error($err, "auth exception");
			throw $err;
		}	

		// end authenticate() log group
		$fp->groupEnd(); 
	}

	protected function validateDigest($digest, $nonce, $created, $secret) {
		$fp = $this->firePHPLogger;
		ob_start();
		$fp->group('validateDigest()');
		$fp->info($digest, 'digest');
		$fp->info($nonce, 'nonce');
		$fp->info($created, 'created');
		$fp->info($secret, 'secret');

		if (time() - strtotime($created) > 300) {
			return false;
		}

		if (file_exists($this->cacheDir.'/'.$nonce) && file_get_contents($this->cacheDir.'/'.$nonce) + 300 < time()) {
			throw new NonceExpiredException('Previously used nonce detected');
		}
		file_put_contents($this->cacheDir.'/'.$nonce, time());
			
{'fraction_non_alphanumeric': 0.15994380895043148, 'fraction_numerical': 0.002408187838651415, 'mean_word_length': 4.983193277310924, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 65, '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': '1466902', 'n_tokens_mistral': 1791, 'n_tokens_neox': 1578, 'n_words': 278}