Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix collections import deprecation on Python 3.9 #246

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions flanker/addresslib/drivers/redis_driver.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import collections
import os
import redis

try:
import collections.abc as c
except ImportError:
import collections as c

class RedisCache(collections.MutableMapping):

class RedisCache(c.MutableMapping):
"""
RedisCache has the same interface as a dict, but talks to a redis server.
"""
Expand Down
34 changes: 19 additions & 15 deletions flanker/dkim.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ def __init__(self, key, selector, domain, signed_headers=None):

def sign(self, message):
canonicalization = NoFWSCanonicalization()
signer = self._key.signer(padding.PKCS1v15(), hashes.SHA1())

if six.PY3 and isinstance(message, six.text_type):
message = message.encode('utf-8')

raw_message = b""
headers, body = _rfc822_parse(message)

h_field = []
Expand All @@ -102,20 +102,22 @@ def sign(self, message):

header, value = canonicalization.canonicalize_header(
header, value)
signer.update(header)
signer.update(b":")
signer.update(value)
raw_message += header
raw_message += b":"
raw_message += value
body = canonicalization.canonicalize_body(body)
if body:
signer.update(b"\r\n")
signer.update(body)
raw_message += b"\r\n"
raw_message += body

signed_message = self._key.sign(raw_message, padding.PKCS1v15(), hashes.SHA1())

return _fold(b"DomainKey-Signature: a=rsa-sha1; c=nofws; d=%s; s=%s;"
b" q=dns; h=%s; b=%s"
% (self._domain,
self._selector,
b": ".join(h_field),
base64.b64encode(signer.finalize()))) + b"\r\n"
base64.b64encode(signed_message))) + b"\r\n"


class DKIMSigner(object):
Expand All @@ -141,7 +143,7 @@ def sign(self, message, current_time=None):
if current_time is None:
current_time = int(time.time())

signer = self._key.signer(padding.PKCS1v15(), hashes.SHA256())
raw_message = b""

headers, body = _rfc822_parse(message)
h_field = []
Expand All @@ -151,9 +153,9 @@ def sign(self, message, current_time=None):

h, v = self._header_canonicalization.canonicalize_header(
header, value)
signer.update(h)
signer.update(b":")
signer.update(v)
raw_message += h
raw_message += b":"
raw_message += v

h = hashes.Hash(hashes.SHA256(), backend=default_backend())
h.update(self._body_canonicalization.canonicalize_body(body))
Expand All @@ -169,11 +171,13 @@ def sign(self, message, current_time=None):

h, v = self._header_canonicalization.canonicalize_header(
b"DKIM-Signature", dkim_header_value)
signer.update(h)
signer.update(b":")
signer.update(v)
raw_message += h
raw_message += b":"
raw_message += v

signed_message = self._key.sign(raw_message, padding.PKCS1v15(), hashes.SHA256())
return b"DKIM-Signature:%s%s\r\n" % (
v, _fold(base64.b64encode(signer.finalize())))
v, _fold(base64.b64encode(signed_message)))

_RFC822_NEWLINE_RE = re.compile(br"\r?\n")
_RFC822_WS_RE = re.compile(br"[\t ]")
Expand Down