Closed achille-roussel closed 8 months ago
I'm not sure the prefix is the issue. This code also produces the same error (when using real values).
from fastapi import FastAPI
from dispatch.fastapi import Dispatch
app = FastAPI()
dispatch = Dispatch(app,
endpoint="https://420-69-24-7-365.ngrok-free.app",
api_key="apikeygoeshereapikeygoeshereapikeygoeshere",
verification_key="-----BEGIN PUBLIC KEY-----\nYXdlcG9ya2F3ZW9ya3Bva2FwZ29rcGFvc2twYXNlb3JrZmRhc2RmYXNhd3M=\n-----END PUBLIC KEY-----\n",
)
FYI that verification_key
has type Ed25519PublicKey | None
, so mypy or an IDE would catch this issue.
At this stage, the user needs to either parse the PEM key with public_key_from_bytes(bytes) -> Ed25519PublicKey
:
from dispatch.signature import public_key_from_bytes
... or similarly by using base64.b64decode
if the key is in base64 format.
Given that (AFAIK) there are no base64 keys which are also valid PEM keys, and vice versa, we could widen the type of verification_key
to Ed25519PublicKey | bytes | str | None
and automatically detect the type of key the user has provided.
@chriso you suggestion to widen the accepted types seems right to me:
it makes sense to be able to pass the same value to the DISPATCH_VERIFICATION_KEY
environment variable and the verification_key
argument (e.g., a string)
I have gotten tricked by this same problem before, it's unfortunate that Python is so permissive that the problem triggers deep within the abstraction layers
it seems wrong to require users to know about Ed25519PublicKey
which is a bit of an implementation detail
It's true that dispatch.signature
simplifies that, but it seems we could help users a lot more if we weren't asking them to deal with this complexity, what do you think?
I agree, let's make the change :+1: sorry it wasn't clear from the previous comment, but I was trying to highlight what the problem was today and suggest that we should widen the type (as long as it's possible to disambiguate key types).
When setting a string that doesn't contain the
---- BEGIN PUBLIC KEY ----
prefix, the program crashes due to accessing a method that doesn't exist, for example:We should report an error that informs users about what they misconfigured instead of something that looks like an internal bug.