<?php
 
namespace Helpers\Securite;
 
use GuzzleHttp\Psr7\HttpFactory;
use Slim\Csrf\Guard;
 
class CSRF
{
    /**
     * @return array<string>
     */
    public static function generateCSRFTokenPair()
    {
        $httpfactory = new HttpFactory;
        $guard       = new Guard($httpfactory, 'tokenCSRF', $_SESSION["tokenCSRF"], null, 200, 32);
 
        
        $token = $guard->generateToken();
        
        // On fait bin2hex pour modsecu
        return [
            'tokenCSRF_value' => \bin2hex($token['tokenCSRF_value'])
        ] + $token;
    }
 
    /**
     * @param array<string> $array
     * @return bool
     */
    public static function checkCSRFTokenPair($array)
    {
        if ($array === []) {
            return false;
        }
 
        $httpfactory = new HttpFactory;
        $guard       = new Guard($httpfactory, 'tokenCSRF', $_SESSION["tokenCSRF"], null, 200, 32);
 
        foreach ($array as $key => $value) {
            // On fait hex2bin ça pour modsecu
            if ($guard->validateToken($key, \hex2bin($value) ?: '')) {
                return true;
            }
        }
 
        return false;
    }
}
