phplaravelunicode-string

Using symfony UnicodeString on laravel


I want to use unicodeString from symfoni in laravel controller

protected function validateWaWebhook($known_token, Request $request)
    {
        if (($signature = $request->headers->get('X-Hub-Signature-256')) == null) {
            // throw new BadRequestHttpException('Header not set');
            return "header not set";
        }

        $signature_parts = explode('=', $signature);

        if (count($signature_parts) != 2) {
            // throw new BadRequestHttpException('signature has invalid format');
            return "Signature invalid format";
        }

        $type = UnicodeString($request->getContent())->normalize(UnicodeString::NFC);
        
        $known_signature = hash_hmac('sha256', $type, $known_token,false);

        if (! hash_equals($known_signature, $signature_parts[1])) {
            // throw new UnauthorizedException('Could not verify request signature ' . $signature_parts[1]);
            return "Could not verify signature";
        }
        return "Valid signature";
    }

When i try it it show

Error: Call to undefined function App\Http\Controllers\UnicodeString()

I've called the symphony

use Symfony\Component\String\UnicodeString;

Solution

  • You need to instantiate the class first and then call normalize method like this.

    $unicode = new UnicodeString($request->getContent());
    $type = $unicode->normalize(UnicodeString::NFC);
    

    or shorter

    $type = (new UnicodeString($request->getContent()))->normalize(UnicodeString::NFC);