Fix signature typechecking

This commit is contained in:
Andrew Godwin 2023-01-11 11:00:18 -07:00
parent e20aea1b9c
commit e68158202e

View file

@ -1,6 +1,6 @@
import base64 import base64
import json import json
from typing import Literal, TypedDict from typing import Literal, TypedDict, cast
from urllib.parse import urlparse from urllib.parse import urlparse
import httpx import httpx
@ -125,8 +125,9 @@ class HttpSignature:
cleartext: str, cleartext: str,
public_key: str, public_key: str,
): ):
public_key_instance = serialization.load_pem_public_key( public_key_instance: rsa.RSAPublicKey = cast(
public_key.encode("ascii") rsa.RSAPublicKey,
serialization.load_pem_public_key(public_key.encode("ascii")),
) )
try: try:
public_key_instance.verify( public_key_instance.verify(
@ -207,9 +208,12 @@ class HttpSignature:
signed_string = "\n".join( signed_string = "\n".join(
f"{name.lower()}: {value}" for name, value in headers.items() f"{name.lower()}: {value}" for name, value in headers.items()
) )
private_key_instance = serialization.load_pem_private_key( private_key_instance: rsa.RSAPrivateKey = cast(
private_key.encode("ascii"), rsa.RSAPrivateKey,
password=None, serialization.load_pem_private_key(
private_key.encode("ascii"),
password=None,
),
) )
signature = private_key_instance.sign( signature = private_key_instance.sign(
signed_string.encode("ascii"), signed_string.encode("ascii"),
@ -283,8 +287,9 @@ class LDSignature:
# Get the normalised hash of each document # Get the normalised hash of each document
final_hash = cls.normalized_hash(options) + cls.normalized_hash(document) final_hash = cls.normalized_hash(options) + cls.normalized_hash(document)
# Verify the signature # Verify the signature
public_key_instance = serialization.load_pem_public_key( public_key_instance: rsa.RSAPublicKey = cast(
public_key.encode("ascii") rsa.RSAPublicKey,
serialization.load_pem_public_key(public_key.encode("ascii")),
) )
try: try:
public_key_instance.verify( public_key_instance.verify(
@ -312,9 +317,12 @@ class LDSignature:
# Get the normalised hash of each document # Get the normalised hash of each document
final_hash = cls.normalized_hash(options) + cls.normalized_hash(document) final_hash = cls.normalized_hash(options) + cls.normalized_hash(document)
# Create the signature # Create the signature
private_key_instance = serialization.load_pem_private_key( private_key_instance: rsa.RSAPrivateKey = cast(
private_key.encode("ascii"), rsa.RSAPrivateKey,
password=None, serialization.load_pem_private_key(
private_key.encode("ascii"),
password=None,
),
) )
signature = base64.b64encode( signature = base64.b64encode(
private_key_instance.sign( private_key_instance.sign(