[python] use datetime aware object (#5209)

* python use aware datetime object

* python use aware datetime object

* python use aware datetime object

* python use aware datetime object

* python use aware datetime object

* python use aware datetime object
This commit is contained in:
Sebastien Rosset 2020-02-06 09:48:23 -08:00 committed by GitHub
parent 8d6286dfae
commit cc0fe06d1c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 28 deletions

View File

@ -7,13 +7,12 @@ from Crypto.IO import PEM, PKCS8
from Crypto.Hash import SHA256, SHA512 from Crypto.Hash import SHA256, SHA512
from Crypto.PublicKey import RSA, ECC from Crypto.PublicKey import RSA, ECC
from Crypto.Signature import PKCS1_v1_5, pss, DSS from Crypto.Signature import PKCS1_v1_5, pss, DSS
from datetime import datetime
from email.utils import formatdate from email.utils import formatdate
import json import json
import os import os
import re import re
from six.moves.urllib.parse import urlencode, urlparse from six.moves.urllib.parse import urlencode, urlparse
from time import mktime from time import time
# The constants below define a subset of HTTP headers that can be included in the # The constants below define a subset of HTTP headers that can be included in the
# HTTP signature scheme. Additional headers may be included in the signature. # HTTP signature scheme. Additional headers may be included in the signature.
@ -228,12 +227,6 @@ class HttpSigningConfiguration(object):
"Signing algorithm {0} is not compatible with private key".format( "Signing algorithm {0} is not compatible with private key".format(
self.signing_algorithm)) self.signing_algorithm))
def _get_unix_time(self, ts):
"""Converts and returns a datetime object to UNIX time, the number of seconds
elapsed since January 1, 1970 UTC.
"""
return (ts - datetime(1970, 1, 1)).total_seconds()
def _get_signed_header_info(self, resource_path, method, headers, body, query_params): def _get_signed_header_info(self, resource_path, method, headers, body, query_params):
"""Build the HTTP headers (name, value) that need to be included in """Build the HTTP headers (name, value) that need to be included in
the HTTP signature scheme. the HTTP signature scheme.
@ -262,15 +255,16 @@ class HttpSigningConfiguration(object):
if query_params: if query_params:
request_target += "?" + urlencode(query_params) request_target += "?" + urlencode(query_params)
# Get current time and generate RFC 1123 (HTTP/1.1) date/time string. # Get UNIX time, e.g. seconds since epoch, not including leap seconds.
now = datetime.now() now = time()
stamp = mktime(now.timetuple()) # Format date per RFC 7231 section-7.1.1.2. An example is:
cdate = formatdate(timeval=stamp, localtime=False, usegmt=True) # Date: Wed, 21 Oct 2015 07:28:00 GMT
cdate = formatdate(timeval=now, localtime=False, usegmt=True)
# The '(created)' value MUST be a Unix timestamp integer value. # The '(created)' value MUST be a Unix timestamp integer value.
# Subsecond precision is not supported. # Subsecond precision is not supported.
created = int(self._get_unix_time(now)) created = int(now)
if self.signature_max_validity is not None: if self.signature_max_validity is not None:
expires = self._get_unix_time(now + self.signature_max_validity) expires = now + self.signature_max_validity.total_seconds()
signed_headers_list = [] signed_headers_list = []
request_headers_dict = {} request_headers_dict = {}

View File

@ -15,13 +15,12 @@ from Crypto.IO import PEM, PKCS8
from Crypto.Hash import SHA256, SHA512 from Crypto.Hash import SHA256, SHA512
from Crypto.PublicKey import RSA, ECC from Crypto.PublicKey import RSA, ECC
from Crypto.Signature import PKCS1_v1_5, pss, DSS from Crypto.Signature import PKCS1_v1_5, pss, DSS
from datetime import datetime
from email.utils import formatdate from email.utils import formatdate
import json import json
import os import os
import re import re
from six.moves.urllib.parse import urlencode, urlparse from six.moves.urllib.parse import urlencode, urlparse
from time import mktime from time import time
# The constants below define a subset of HTTP headers that can be included in the # The constants below define a subset of HTTP headers that can be included in the
# HTTP signature scheme. Additional headers may be included in the signature. # HTTP signature scheme. Additional headers may be included in the signature.
@ -236,12 +235,6 @@ class HttpSigningConfiguration(object):
"Signing algorithm {0} is not compatible with private key".format( "Signing algorithm {0} is not compatible with private key".format(
self.signing_algorithm)) self.signing_algorithm))
def _get_unix_time(self, ts):
"""Converts and returns a datetime object to UNIX time, the number of seconds
elapsed since January 1, 1970 UTC.
"""
return (ts - datetime(1970, 1, 1)).total_seconds()
def _get_signed_header_info(self, resource_path, method, headers, body, query_params): def _get_signed_header_info(self, resource_path, method, headers, body, query_params):
"""Build the HTTP headers (name, value) that need to be included in """Build the HTTP headers (name, value) that need to be included in
the HTTP signature scheme. the HTTP signature scheme.
@ -270,15 +263,16 @@ class HttpSigningConfiguration(object):
if query_params: if query_params:
request_target += "?" + urlencode(query_params) request_target += "?" + urlencode(query_params)
# Get current time and generate RFC 1123 (HTTP/1.1) date/time string. # Get UNIX time, e.g. seconds since epoch, not including leap seconds.
now = datetime.now() now = time()
stamp = mktime(now.timetuple()) # Format date per RFC 7231 section-7.1.1.2. An example is:
cdate = formatdate(timeval=stamp, localtime=False, usegmt=True) # Date: Wed, 21 Oct 2015 07:28:00 GMT
cdate = formatdate(timeval=now, localtime=False, usegmt=True)
# The '(created)' value MUST be a Unix timestamp integer value. # The '(created)' value MUST be a Unix timestamp integer value.
# Subsecond precision is not supported. # Subsecond precision is not supported.
created = int(self._get_unix_time(now)) created = int(now)
if self.signature_max_validity is not None: if self.signature_max_validity is not None:
expires = self._get_unix_time(now + self.signature_max_validity) expires = now + self.signature_max_validity.total_seconds()
signed_headers_list = [] signed_headers_list = []
request_headers_dict = {} request_headers_dict = {}