<commit_before><?php
namespace Tuum\Slimmed;

use Pimple\Container;
use Pimple\ServiceProviderInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Tuum\Respond\RequestHelper;
use Tuum\Respond\Respond;
use Tuum\Respond\Responder;

class CsRfGuard implements ServiceProviderInterface
{
    /**
     * @var string
     */
    private $token_name = '_token';

    /**
     * @var array
     */
    private $methods = ['POST', 'PUT', 'PATCH', 'DELETE'];

    /**
     * @param null|string $tokenName
     * @param null|array  $methods
     */
    public function __construct($tokenName = null, $methods = null)
    {
        if (!is_null($tokenName)) {
            $this->token_name = $tokenName;
        }
        if (!is_null($methods)) {
            $this->methods = $methods;
        }
    }
    
    /**
     * @param ServerRequestInterface $request
     * @param ResponseInterface      $response
     * @param callable               $next
     * @return ResponseInterface
     */
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
    {
        if (in_array($request->getMethod(), $this->methods)) {
            return $this->validate($request, $response, $next);
        }

        return $next($request, $response);
    }

    /**
     * @param ServerRequestInterface $request
     * @param ResponseInterface      $response
     * @param callable               $next
     * @return ResponseInterface
     */
    public function validate(ServerRequestInterface $request, ResponseInterface $response, callable $next)
    {
        $post = $request->getParsedBody();
        if (!isset($post[$this->token_name]) || !$post[$this->token_name]) {
            return Respond::error($request, $response)->forbidden();
        }
        $value = $post[$this->token_name];

        /** @var Responder $responder */
        $responder = RequestHelper::getService($request, Responder::class);
        if (!$responder->getStorage()->validateToken($value)) {
            return Respond::error($request, $response)->forbidden();
        }

        return $next($request, $response);
    }

    /**
     * Registers services on the given container.
     *
     * This method should only be used to configure services and parameters.
     * It should not get services.
     *
     * @param Container $pimple A container instance
     */<commit_msg>use methods for managing csrf token.<commit_after><?php
namespace Tuum\Slimmed;

use Pimple\Container;
use Pimple\ServiceProviderInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Tuum\Respond\RequestHelper;
use Tuum\Respond\Respond;
use Tuum\Respond\Responder;

class CsRfGuard implements ServiceProviderInterface
{
    /**
     * @var string
     */
    private $token_name = '_token';

    /**
     * @var array
     */
    private $methods = ['POST', 'PUT', 'PATCH', 'DELETE'];

    /**
     * @param null|string $tokenName
     * @param null|array  $methods
     */
    public function __construct($tokenName = null, $methods = null)
    {
        if (!is_null($tokenName)) {
            $this->token_name = $tokenName;
        }
        if (!is_null($methods)) {
            $this->methods = $methods;
        }
    }
    
    /**
     * @param ServerRequestInterface $request
     * @param ResponseInterface      $response
     * @param callable               $next
     * @return ResponseInterface
     */
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
    {
        $token   = RequestHelper::getResponder($request)->getStorage()->getToken();
        $request = $request->withAttribute($this->token_name, $token);
        if (in_array($request->getMethod(), $this->methods)) {
            return $this->validate($request, $response, $next);
        }

        return $next($request, $response);
    }

    /**
     * @param ServerRequestInterface $request
     * @param ResponseInterface      $response
     * @param callable               $next
     * @return ResponseInterface
     */
    public function validate(ServerRequestInterface $request, ResponseInterface $response, callable $next)
    {
        $post = $request->getParsedBody();
        if (!isset($post[$this->token_name]) || !$post[$this->token_name]) {
            return Respond::error($request, $response)->forbidden();
        }
        $value = $post[$this->token_name];

        /** @var Responder $responder */
        $responder = RequestHelper::getResponder($request);
        if (!$responder->getStorage()->validateToken($value)) {
            return Respond::error($request, $response)->forbidden();
        }

        return $next($request, $response);
    }

    /**
     * Registers services on the given container.
     *
     * This method should only be used to configure services and parameters.
     * It should not get services.
     *
     * @param Container $pimple A container instance
     */
{'fraction_non_alphanumeric': 0.13085782366957904, 'fraction_numerical': 0.0, 'mean_word_length': 2.579957356076759, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 33, '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': '1729982', 'n_tokens_mistral': 1401, 'n_tokens_neox': 1368, 'n_words': 317}