davidearl / webauthn

An implementation of webauthn in PHP on the server side (e.g Yubico 2 and Google Titan keys)
https://webauthn.davidearl.uk
MIT License
129 stars 24 forks source link

couldn't initiate login: SyntaxError: Unexpected end of JSON input: #61

Open GeorgianaCristea opened 1 year ago

GeorgianaCristea commented 1 year ago

Hello, when I ran the code on my localhost, I got the error mentioned in the title, generated by clicking the submit button on user registration. However, after I googled it, nothing that I found seemed to match my problem. (e.g. I have no missing parantheses or anything like this). Do you know what this error might be caused by? Thank you in advance.

davidearl commented 1 year ago

You aren't giving much to go on. The error message is straightforward: a string being passed into json_decode is not valid JSON. What platform are you on (server and client)? What kind of key is it? What is the string that's causing the problem and where in the code? Have you changed the code in any way?

There are two json_decode calls during registration, one near the top of WebAuthn::register where it decodes the information provided by the key, and the other near the end of that function where it decodes the other parameter, the user's existing keys. You should have a line number for which one it was, and I would put some logging just before the relevant call to find out what the string is that is being passed in, the one it is complaining about.

If you haven't changed the code at all, chances are it is the first one, as the second one is only used on registration of additional keys, and the example doesn't allow for adding a key to an existing user. Which rather suggests that what should be passed into the json_decode call is being corrupted somewhere.

Once you've established where the problem is and what the problem string is, if it is indeed the first call, then I'd look at the network request in the browser debugger, to see what is actually being sent. If that's the same as the string that's causing the problem, then the problem is on the client side; and if not it is on the server side, so we'll know where to look next.

davidearl commented 1 year ago

You might also want to check permission where the code writes the user database. If it can't write to that, it will inevitably fail.