Skip to content

Commit

Permalink
bump version to 1.1.0 + lint and formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Jyrno42 committed Feb 28, 2024
1 parent ff8d020 commit 0006d6d
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 44 deletions.
2 changes: 1 addition & 1 deletion pyasice/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@
# simply: except pyasice.Error
Error = PyAsiceError

__version__ = "1.0.5"
__version__ = "1.1.0"
14 changes: 4 additions & 10 deletions pyasice/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ def add_signature(self, signature: XmlSignature):

if embedded_signatures:
last_n = re.match(self.SIGNATURE_FILES_REGEX, embedded_signatures[-1]).group(1)
next_n = int(last_n) + 1 if last_n.isnumeric() else 1 # even with alphabetic file sorting, this gives valid next number
next_n = int(last_n) + 1 if last_n.isnumeric() else 1 # even with alphabetic file sorting, this gives valid next number
else:
next_n = 1

Expand Down Expand Up @@ -271,9 +271,7 @@ def verify_container(self):
raise self.Error(f"Failed to read manifest for container '{self}'") from e

toc_data_files = [
file_name
for file_name in toc[1:] # the first one is MIME_TYPE_FILE, can be skipped
if not file_name.startswith(self.META_DIR)
file_name for file_name in toc[1:] if not file_name.startswith(self.META_DIR) # the first one is MIME_TYPE_FILE, can be skipped
]

manifest_data_files = [name for name, _ in self._enumerate_data_files()]
Expand All @@ -287,9 +285,7 @@ def _write_signature(self, signature_xml, file_name):
self._delete_files(file_name)

with self.zip_writer as zip_file:
zip_file.writestr(
file_name, signature_xml
)
zip_file.writestr(file_name, signature_xml)

def _write_manifest(self):
"""Create/update the manifest"""
Expand All @@ -302,9 +298,7 @@ def _write_manifest(self):
self._delete_files(self.MANIFEST_PATH)

with self.zip_writer as zip_file:
zip_file.writestr(
self.MANIFEST_PATH, b'<?xml version="1.0" encoding="UTF-8"?>' + etree.tostring(manifest_xml)
)
zip_file.writestr(self.MANIFEST_PATH, b'<?xml version="1.0" encoding="UTF-8"?>' + etree.tostring(manifest_xml))

@classmethod
def _create_container(cls):
Expand Down
2 changes: 1 addition & 1 deletion pyasice/tests/test_ocsp.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from cryptography.hazmat.primitives.serialization import Encoding
from oscrypto.asymmetric import load_certificate

from pyasice.ocsp import OCSP, requests
from pyasice.ocsp import OCSP

from .conftest import cert_builder

Expand Down
4 changes: 2 additions & 2 deletions pyasice/tests/test_tsa.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import hashlib
from unittest.mock import Mock, patch, ANY
from unittest.mock import Mock, patch

from asn1crypto.cms import ContentInfo
from asn1crypto.tsp import PKIStatus, PKIStatusInfo, TimeStampResp

from pyasice.tsa import requests, TSA
from pyasice.tsa import TSA


class MockResponse(Mock):
Expand Down
19 changes: 4 additions & 15 deletions pyasice/tests/test_xml_signature.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,20 +75,15 @@ def test_xmlsig_add_cert_without_attrib(certificate_rsa_bytes):

cert = load_certificate(certificate_rsa_bytes)
xml_signature.add_cert(cert)
assert xml_signature._get_node("xades:EncapsulatedX509Certificate").text == base64.b64encode(
cert.asn1.dump()
).decode("ascii")
assert xml_signature._get_node("xades:EncapsulatedX509Certificate").text == base64.b64encode(cert.asn1.dump()).decode("ascii")


def test_xmlsig_set_root_cert(certificate_rsa_bytes):
xml_signature = XmlSignature.create()

cert = load_certificate(certificate_rsa_bytes)
xml_signature.set_root_ca_cert(cert)
assert (
xml_signature._get_node("xades:EncapsulatedX509Certificate").attrib["Id"]
== f"{xml_signature.NEW_SIGNATURE_ID}-ROOT-CA-CERT"
)
assert xml_signature._get_node("xades:EncapsulatedX509Certificate").attrib["Id"] == f"{xml_signature.NEW_SIGNATURE_ID}-ROOT-CA-CERT"


def test_xmlsig_certificate(certificate_rsa_bytes):
Expand All @@ -115,21 +110,15 @@ def test_xmlsig_documents():
assert xml_signature.doc_ids == ["r-id-1"]
assert xml_signature._get_node('ds:Reference[@Id="r-id-1"]').attrib["URI"] == "test.txt"
assert xml_signature._get_node('ds:Reference[@Id="r-id-1"]/ds:DigestValue').text == digest
assert (
xml_signature._get_node('xades:DataObjectFormat[@ObjectReference="#r-id-1"]/xades:MimeType').text
== "text/plain"
)
assert xml_signature._get_node('xades:DataObjectFormat[@ObjectReference="#r-id-1"]/xades:MimeType').text == "text/plain"

xml_signature.add_document("test2.pdf", b"test PDF", "application/pdf")
digest = base64.b64encode(hashlib.sha256(b"test PDF").digest()).decode("ascii")

assert xml_signature.doc_ids == ["r-id-1", "r-id-2"]
assert xml_signature._get_node('ds:Reference[@Id="r-id-2"]').attrib["URI"] == "test2.pdf"
assert xml_signature._get_node('ds:Reference[@Id="r-id-2"]/ds:DigestValue').text == digest
assert (
xml_signature._get_node('xades:DataObjectFormat[@ObjectReference="#r-id-2"]/xades:MimeType').text
== "application/pdf"
)
assert xml_signature._get_node('xades:DataObjectFormat[@ObjectReference="#r-id-2"]/xades:MimeType').text == "application/pdf"


@pytest.mark.parametrize("signature_algo", [None, "rsa-sha512", "ecdsa-sha256", "ecdsa-sha512"])
Expand Down
4 changes: 1 addition & 3 deletions pyasice/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
from .xmlsig import XmlSignature


def finalize_signature(
xml_signature: XmlSignature, issuer_cert: bytes, lt_ts=True, *, ocsp_url, tsa_url=None, get_session=None
):
def finalize_signature(xml_signature: XmlSignature, issuer_cert: bytes, lt_ts=True, *, ocsp_url, tsa_url=None, get_session=None):
"""Finalize the XAdES signature in accordance with LT-TM profile, or LT-TS profile if `lt_ts` is True
:param XmlSignature xml_signature:
Expand Down
16 changes: 8 additions & 8 deletions pyasice/xmlsig.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,10 @@ def set_timestamp_response(self, tsr) -> "XmlSignature":
if self._get_node("xades:EncapsulatedTimeStamp") is None:
usprp = self._get_unsigned_properties_node()
sigts = etree.Element("{http://uri.etsi.org/01903/v1.3.2#}SignatureTimeStamp", Id="TS")
cmeth = etree.Element("{http://www.w3.org/2000/09/xmldsig#}CanonicalizationMethod", Algorithm="http://www.w3.org/2006/12/xml-c14n11")
cmeth = etree.Element(
"{http://www.w3.org/2000/09/xmldsig#}CanonicalizationMethod",
Algorithm="http://www.w3.org/2006/12/xml-c14n11",
)
encts = etree.Element("{http://uri.etsi.org/01903/v1.3.2#}EncapsulatedTimeStamp", Id="ETS")
sigts.append(cmeth)
sigts.append(encts)
Expand Down Expand Up @@ -484,9 +487,7 @@ def _get_node(self, tag_name) -> etree._Element:
return self.xml.find(".//{}".format(tag_name), namespaces=self.NAMESPACES)

def _get_signed_properties_node(self):
return self.xml.find(
"./ds:Signature/ds:Object/xades:QualifyingProperties/xades:SignedProperties", self.NAMESPACES
)
return self.xml.find("./ds:Signature/ds:Object/xades:QualifyingProperties/xades:SignedProperties", self.NAMESPACES)

def _get_signing_time_node(self):
return self.xml.find(
Expand Down Expand Up @@ -548,12 +549,11 @@ def _calc_signed_properties_hash(self, update=False) -> tuple:

def _get_unsigned_properties_node(self):
res = self.xml.find(
"./ds:Signature/ds:Object/xades:QualifyingProperties/xades:UnsignedProperties/xades:UnsignedSignatureProperties", self.NAMESPACES
"./ds:Signature/ds:Object/xades:QualifyingProperties/xades:UnsignedProperties/xades:UnsignedSignatureProperties",
self.NAMESPACES,
)
if res is None:
qprop = self.xml.find(
"./ds:Signature/ds:Object/xades:QualifyingProperties", self.NAMESPACES
)
qprop = self.xml.find("./ds:Signature/ds:Object/xades:QualifyingProperties", self.NAMESPACES)
usp = etree.Element("{http://uri.etsi.org/01903/v1.3.2#}UnsignedProperties")
res = etree.Element("{http://uri.etsi.org/01903/v1.3.2#}UnsignedSignatureProperties")
usp.append(res)
Expand Down
6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pyasice"
version = "1.0.5"
version = "1.1.0"
description = "Manipulate ASiC-E containers and XAdES/eIDAS signatures for Estonian e-identity services"
readme = "README.md"
license = "ISC"
Expand Down Expand Up @@ -73,7 +73,7 @@ isort = "5.6.*"


[tool.black]
line-length = 120
line-length = 140
target-version = ['py38']
include = '\.pyi?$'
exclude = '''
Expand All @@ -95,7 +95,7 @@ exclude = '''

[tool.isort]
skip_glob = "venv*,.venv*,migrations"
line_length = '120'
line_length = '140'
atomic = 'true'
multi_line_output = '3'
include_trailing_comma = 'true'
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
universal = 1

[flake8]
max-line-length = 120
max-line-length = 140
exclude =
.git,
__pycache__,
Expand Down

0 comments on commit 0006d6d

Please sign in to comment.