New version of black, new whitespace
This commit is contained in:
@ -13,7 +13,7 @@ MAX_SIGNATURE_AGE = 300
|
||||
|
||||
|
||||
def create_key_pair():
|
||||
""" a new public/private key pair, used for creating new users """
|
||||
"""a new public/private key pair, used for creating new users"""
|
||||
random_generator = Random.new().read
|
||||
key = RSA.generate(1024, random_generator)
|
||||
private_key = key.export_key().decode("utf8")
|
||||
@ -23,7 +23,7 @@ def create_key_pair():
|
||||
|
||||
|
||||
def make_signature(sender, destination, date, digest):
|
||||
""" uses a private key to sign an outgoing message """
|
||||
"""uses a private key to sign an outgoing message"""
|
||||
inbox_parts = urlparse(destination)
|
||||
signature_headers = [
|
||||
"(request-target): post %s" % inbox_parts.path,
|
||||
@ -44,14 +44,14 @@ def make_signature(sender, destination, date, digest):
|
||||
|
||||
|
||||
def make_digest(data):
|
||||
""" creates a message digest for signing """
|
||||
"""creates a message digest for signing"""
|
||||
return "SHA-256=" + b64encode(hashlib.sha256(data.encode("utf-8")).digest()).decode(
|
||||
"utf-8"
|
||||
)
|
||||
|
||||
|
||||
def verify_digest(request):
|
||||
""" checks if a digest is syntactically valid and matches the message """
|
||||
"""checks if a digest is syntactically valid and matches the message"""
|
||||
algorithm, digest = request.headers["digest"].split("=", 1)
|
||||
if algorithm == "SHA-256":
|
||||
hash_function = hashlib.sha256
|
||||
@ -66,7 +66,7 @@ def verify_digest(request):
|
||||
|
||||
|
||||
class Signature:
|
||||
""" read and validate incoming signatures """
|
||||
"""read and validate incoming signatures"""
|
||||
|
||||
def __init__(self, key_id, headers, signature):
|
||||
self.key_id = key_id
|
||||
@ -75,7 +75,7 @@ class Signature:
|
||||
|
||||
@classmethod
|
||||
def parse(cls, request):
|
||||
""" extract and parse a signature from an http request """
|
||||
"""extract and parse a signature from an http request"""
|
||||
signature_dict = {}
|
||||
for pair in request.headers["Signature"].split(","):
|
||||
k, v = pair.split("=", 1)
|
||||
@ -92,7 +92,7 @@ class Signature:
|
||||
return cls(key_id, headers, signature)
|
||||
|
||||
def verify(self, public_key, request):
|
||||
""" verify rsa signature """
|
||||
"""verify rsa signature"""
|
||||
if http_date_age(request.headers["date"]) > MAX_SIGNATURE_AGE:
|
||||
raise ValueError("Request too old: %s" % (request.headers["date"],))
|
||||
public_key = RSA.import_key(public_key)
|
||||
@ -118,7 +118,7 @@ class Signature:
|
||||
|
||||
|
||||
def http_date_age(datestr):
|
||||
""" age of a signature in seconds """
|
||||
"""age of a signature in seconds"""
|
||||
parsed = datetime.datetime.strptime(datestr, "%a, %d %b %Y %H:%M:%S GMT")
|
||||
delta = datetime.datetime.utcnow() - parsed
|
||||
return delta.total_seconds()
|
||||
|
Reference in New Issue
Block a user