python3: upgrade signature.py (#40570)

This commit is contained in:
Nicolas Roche 2020-03-13 09:59:25 +01:00
parent 3936d0e4e8
commit f4e12c2b6e
1 changed files with 11 additions and 6 deletions

View File

@ -22,10 +22,11 @@ import urllib
import random
import logging
from django.utils import six
from django.utils.encoding import force_bytes, smart_bytes
from django.utils.http import quote, urlencode
from django.utils.six.moves.urllib import parse as urlparse
'''Simple signature scheme for query strings'''
# from http://repos.entrouvert.org/portail-citoyen.git/tree/portail_citoyen/apps/data_source_plugin/signature.py
@ -45,12 +46,12 @@ def sign_query(query, key, algo='sha256', timestamp=None, nonce=None):
new_query = query
if new_query:
new_query += '&'
new_query += urllib.urlencode((
new_query += urlencode((
('algo', algo),
('timestamp', timestamp),
('nonce', nonce)))
signature = base64.b64encode(sign_string(new_query, key, algo=algo))
new_query += '&signature=' + urllib.quote(signature)
new_query += '&signature=' + quote(signature)
return new_query
@ -88,7 +89,7 @@ def check_query2(query, key, known_nonce, timedelta):
except Exception as e:
return False, 'could not decode base64 signature (%s)' % e
if algo not in hashlib.algorithms:
if algo not in hashlib.algorithms_guaranteed:
return False, 'hash algorithm %s is not supported' % algo
try:
@ -111,6 +112,10 @@ def check_string(s, signature, key, algo='sha256'):
if len(signature2) != len(signature):
return False
res = 0
for a, b in zip(signature, signature2):
res |= ord(a) ^ ord(b)
if six.PY3:
for a, b in zip(signature, signature2):
res |= a ^ b
else:
for a, b in zip(signature, signature2):
res |= ord(a) ^ ord(b)
return res == 0