json_encode() of PublicKeyCredentialRequestOptions fails

Open Beginner friendly
#22 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
1/5
Estimated time
Under an hour
Newbie friendliness
82/100
Issue type
Documentation
Clarity
Clearly specified
Activity status
Active
Tech stack
php
Domain
documentation

Research direction

Read the authentication guide linked in the issue and compare its serialization instructions with the maintainer’s reply and the referenced serializer page. Update the guide to explain that json_encode() no longer works for these objects in 5.0 and direct readers to the Serializer; done when the example matches the supported approach.

Written by the indexing model from the issue text.

Description

Version(s) affected

5.0

Description

Works in 4.9 but not 5.0: json_encode(\Webauthn\PublicKeyCredentialRequestOptions::create(random_bytes(32), allowCredentials: []))

I need to json_encode() it to submit the request to my site via a JSON-RPC endpoint.

The docs still say:

The PublicKeyCredentialRequestOptions object is designed to be easily serialized into a JSON object. This will ease the integration into an HTML page or through an API endpoint.

How to reproduce

With 4.9, I was using this code:

      $public_key_credential_request_options = PublicKeyCredentialRequestOptions::create(
        $this->webauthn->createChallenge(),
        allowCredentials: $allowed_credentials,
      );
      $this->tempstore->set(self::TEMP_STORE_REQUEST_OPTIONS, $public_key_credential_request_options);

      $stringified_json = json_encode($public_key_credential_request_options);
      if (is_string($stringified_json)) {
      }

After upgrading to 5.0, this fails because the value of json_encode() is false.

Possible Solution

No response

Additional Context

No response


From the original thread

albanx — 2024-10-28

I had the same issue today, the only way to make it work is to serialize it and send as JSON string:

This is the example in the docs:

use Symfony\Component\Serializer\Encoder\JsonEncode;
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;

// The serializer is the same as the one created in the previous pages
$jsonObject = $serializer->serialize(
    $publicKeyCredentialCreationOptions,
    'json',
    [ // Optional
        AbstractObjectNormalizer::SKIP_NULL_VALUES => true,
        JsonEncode::OPTIONS => JSON_THROW_ON_ERROR,
    ]
);

Spomky — 2024-10-28

After upgrading to 5.0, this fails because the value of json_encode() is false.

Indeed, json_encode should not be used as in 5.0.+ the objects do not implement the JsonSerializable interface.
Please use the Serializer instead.

ptmkenny — 2024-10-29

Thank you for the updates. Also, Webauthn\PublicKeyCredentialSource::jsonSerialize() was removed, but this isn't listed in the migration docs.


Copied from web-auth/doc#32, opened by @ptmkenny on 2024-10-25. The documentations of Slabs now share one site and one repository, and the former one is being retired — nothing was lost, and this is where the thread continues.

Dominant language
Python
Stars
0
Forks
0
Avg merge
34m
Merged PRs (30d)
40

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Spomky-Labs/docs

All issues in Spomky-Labs/docs

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.