<commit_before><?php

/**
 * This file is part of the SlimAuthHmac package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace IC\SlimAuthHmac\Middleware\Auth;

use IC\SlimAuthHmac\Auth\HmacManager;
use IC\SlimAuthHmac\Exception\HttpForbiddenException;

/**
 * HMAC Middleware
 *
 * @author <NAME> <<EMAIL>>
 */
class Hmac extends \Slim\Middleware
{
    private $hmacManager = null;

    /**
     * Constructor.
     *
     * @param string $options List of options
     */
    public function __construct(array $options = array())
    {
        $defaults = array(
            'algorithm' => 'sha256',
            'privateKey' => null
        );

        $this->hmacManager = new HmacManager(array_merge($defaults, $options));
    }

    public function call()
    {
        $app = $this->app;
        $hmacManager = $this->hmacManager;

        $checkRequest = function () use ($app, $hmacManager) {
            $headers = $app->request->headers();

            // get api key and hash from headers
            $authString = $headers->get('authentication');

            if (strpos($authString, 'hmac ') !== 0) {
                throw new HttpForbiddenException();
            }
            else {
                $authString = substr($authString, 5);
                $authArray = explode(':', $authString);

                if (count($authArray) !== 2) {
                    throw new HttpForbiddenException();
                }
                else {
                    list($publicKey, $hmacHash) = $authArray;

                    $this->hmacManager->setPublicKey($publicKey);
                    $this->hmacManager->setHmacHash($hmacHash);
                    $payload = '';
                    $payload .= $app->request->getMethod() . "\n";
                    $payload .= $app->request->getResourceUri() . "\n";
                    $payload .= json_encode($app->request()->getBody());
                    $this->hmacManager->setPayload($payload);

                    $hmacValue = $this->hmacManager->generateHmac();
                    $isValid = $this->hmacManager->isValid($this->hmacManager->generateHmac(), $hmacHash);

                    if ($isValid !== true) {
                        throw new HttpForbiddenException();
                    }
                }
            }
        };

        $this->app->hook('slim.before.dispatch', $checkRequest);

        $this->next->call();
    }
}
<commit_msg>fix: check if body is converted to array<commit_after><?php

/**
 * This file is part of the SlimAuthHmac package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace IC\SlimAuthHmac\Middleware\Auth;

use IC\SlimAuthHmac\Auth\HmacManager;
use IC\SlimAuthHmac\Exception\HttpForbiddenException;

/**
 * HMAC Middleware
 *
 * @author <NAME> <<EMAIL>>
 */
class Hmac extends \Slim\Middleware
{
    private $hmacManager = null;

    /**
     * Constructor.
     *
     * @param string $options List of options
     */
    public function __construct(array $options = array())
    {
        $defaults = array(
            'algorithm' => 'sha256',
            'privateKey' => null
        );

        $this->hmacManager = new HmacManager(array_merge($defaults, $options));
    }

    public function call()
    {
        $app = $this->app;
        $hmacManager = $this->hmacManager;

        $checkRequest = function () use ($app, $hmacManager) {
            $headers = $app->request->headers();

            // get api key and hash from headers
            $authString = $headers->get('authentication');

            if (strpos($authString, 'hmac ') !== 0) {
                throw new HttpForbiddenException();
            }
            else {
                $authString = substr($authString, 5);
                $authArray = explode(':', $authString);

                if (count($authArray) !== 2) {
                    throw new HttpForbiddenException();
                }
                else {
                    list($publicKey, $hmacHash) = $authArray;

                    $this->hmacManager->setPublicKey($publicKey);
                    $this->hmacManager->setHmacHash($hmacHash);
                    $payload = '';
                    $payload .= $app->request->getMethod() . "\n";
                    $payload .= $app->request->getResourceUri() . "\n";
                    $payload .= is_array($app->request()->getBody()) ? json_encode($app->request()->getBody()) : $app->request()->getBody();
                    $this->hmacManager->setPayload($payload);

                    $hmacValue = $this->hmacManager->generateHmac();
                    $isValid = $this->hmacManager->isValid($this->hmacManager->generateHmac(), $hmacHash);

                    if ($isValid !== true) {
                        throw new HttpForbiddenException();
                    }
                }
            }
        };

        $this->app->hook('slim.before.dispatch', $checkRequest);

        $this->next->call();
    }
}

{'fraction_non_alphanumeric': 0.1416425120772947, 'fraction_numerical': 0.002318840579710145, 'mean_word_length': 1.7768240343347639, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 79, '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': '1359941', 'n_tokens_mistral': 1450, 'n_tokens_neox': 1393, 'n_words': 297}