Skip to content

Commit 3224f5e

Browse files
committed
chore: code linting
1 parent 17211b6 commit 3224f5e

11 files changed

+90
-100
lines changed

linting.sh

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
3+
autopep8 -r --in-place pymdoccbor
4+
autoflake -r --in-place --remove-unused-variables --expand-star-imports --remove-all-unused-imports pymdoccbor
5+
6+
flake8 pymdoccbor --count --select=E9,F63,F7,F82 --show-source --statistics
7+
flake8 pymdoccbor --max-line-length 120 --count --statistics

pymdoccbor/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__="0.2.0"
1+
__version__ = "0.2.0"

pymdoccbor/deprecated/issuerauth.py

+5-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from typing import List
21

32
from pycose.messages.sign1message import Sign1Message
43

@@ -8,34 +7,29 @@
87
class MdocIssuerAuth:
98
"""
109
IssuerAuth is a COSE_Sign1 ; The payload is the MobileSecurityObject, see ISO 18013-5 section 9.2.2.4
11-
10+
1211
A CBOR decoded issuerAuth is a list of [
1312
cbor({1: -7}) # Protected Header, find -7 here https://datatracker.ietf.org/doc/html/rfc8152
1413
cbor({33: bytes}) # Unprotected Header containing X509 certificate
1514
cbor({24: bytes}) # Payload -> Mobile Security Object
1615
]
1716
"""
1817

19-
def parse(self, mso : Sign1Message) -> None:
18+
def parse(self, mso: Sign1Message) -> None:
2019
pass
2120

22-
2321
def read(self, mso_dict: dict) -> None:
2422
"""
2523
Returns a CBOR Endoded tag 24
2624
"""
27-
25+
2826
def verify_signature(self) -> bool:
2927
pass
3028

31-
32-
def load(self, mso :bytes):
29+
def load(self, mso: bytes):
3330
"""
3431
loads a dumped COSE_Sign1
3532
"""
36-
37-
pass
38-
33+
3934
def dump(self) -> bool:
4035
pass
41-

pymdoccbor/exceptions.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class InvalidMdoc(Exception):
22
"""
33
"""
4-
pass
4+
55

66
class UnsupportedMsoDataFormat(Exception):
77
pass

pymdoccbor/issuersigned.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ class IssuerSigned:
88
"""
99
nameSpaces provides the definition within which the data elements of
1010
the document are defined. A document may have multiple nameSpaces.
11-
11+
1212
IssuerAuth is a COSE_Sign1 ; The payload is the MobileSecurityObject, see ISO 18013-5 section 9.2.2.4
13-
13+
1414
issuerAuth is a list of [
1515
cbor({1: -7}) # Protected Header, find -7 here https://datatracker.ietf.org/doc/html/rfc8152
1616
cbor({33: bytes}) # Unprotected Header containing X509 certificate
@@ -20,23 +20,23 @@ class IssuerSigned:
2020
"""
2121
# nameSpaces :dict = {}
2222
# issuerAuth :IssuerAuth = None
23-
24-
def __init__(self, nameSpaces :dict, issuerAuth :Union[dict, bytes]):
25-
self.namespaces :dict = nameSpaces
26-
23+
24+
def __init__(self, nameSpaces: dict, issuerAuth: Union[dict, bytes]):
25+
self.namespaces: dict = nameSpaces
26+
2727
# if isinstance(ia, dict):
2828
self.issuer_auth = MsoParser(issuerAuth)
29-
29+
3030
def dump(self):
3131
return {
3232
'nameSpaces': self.namespaces,
3333
'issuerAuth': self.issuer_auth
3434
}
35-
35+
3636
def dumps(self):
3737
return cbor2.dumps(
3838
{
39-
'nameSpaces': self.namespaces,
40-
'issuerAuth': self.issuer_auth.payload_as_cbor
39+
'nameSpaces': self.namespaces,
40+
'issuerAuth': self.issuer_auth.payload_as_cbor
4141
}
4242
)

pymdoccbor/mdoc.py

+33-38
Original file line numberDiff line numberDiff line change
@@ -15,58 +15,58 @@ class MobileDocument:
1515
True: "valid",
1616
False: "failed",
1717
}
18-
19-
def __init__(self, docType :str, issuerSigned : dict, deviceSigned :dict = {}):
20-
self.doctype :str = docType # eg: 'org.iso.18013.5.1.mDL'
21-
self.issuersigned :List[IssuerSigned] = IssuerSigned(**issuerSigned)
18+
19+
def __init__(self, docType: str, issuerSigned: dict, deviceSigned: dict = {}):
20+
self.doctype: str = docType # eg: 'org.iso.18013.5.1.mDL'
21+
self.issuersigned: List[IssuerSigned] = IssuerSigned(**issuerSigned)
2222
self.is_valid = False
23-
23+
2424
# TODO
25-
self.devicesigned :dict = deviceSigned
26-
27-
25+
self.devicesigned: dict = deviceSigned
26+
2827
def dump(self) -> dict:
2928
return {
3029
'docType': self.doctype,
3130
'issuerSigned': self.issuersigned.dump()
3231
}
33-
32+
3433
def dumps(self) -> dict:
3534
return cbor2.dumps(
36-
cbor2.CBORTag(24, value = {
37-
'docType': self.doctype,
38-
'issuerSigned': self.issuersigned.dumps()
39-
}
35+
cbor2.CBORTag(24, value={
36+
'docType': self.doctype,
37+
'issuerSigned': self.issuersigned.dumps()
38+
}
4039
)
4140
)
42-
41+
4342
def verify(self) -> bool:
4443
self.is_valid = self.issuersigned.issuer_auth.verify_signature()
4544
return self.is_valid
46-
45+
4746
def __repr__(self):
4847
return f"{self.__module__}.{self.__class__.__name__} [{self._states[self.is_valid]}]"
4948

49+
5050
class MdocCbor:
51-
52-
version :str = '1.0'
53-
documents :List[MobileDocument] = []
54-
status :int = 0
55-
51+
52+
version: str = '1.0'
53+
documents: List[MobileDocument] = []
54+
status: int = 0
55+
5656
def __init__(self):
57-
self.data_as_bytes :bytes = b""
58-
self.data_as_cbor_dict :dict = {}
59-
60-
self.documents :List[MobileDocument] = []
61-
self.documents_invalid : list = []
57+
self.data_as_bytes: bytes = b""
58+
self.data_as_cbor_dict: dict = {}
59+
60+
self.documents: List[MobileDocument] = []
61+
self.documents_invalid: list = []
6262

63-
def loads(self, data :str):
63+
def loads(self, data: str):
6464
"""
6565
data is a AF BINARY
6666
"""
6767
if isinstance(data, bytes):
6868
data = binascii.hexlify(data)
69-
69+
7070
self.data_as_bytes = binascii.unhexlify(data)
7171
self.data_as_cbor_dict = cbor2.loads(self.data_as_bytes)
7272

@@ -84,19 +84,19 @@ def data_as_string(self):
8484
return self.dumps().decode()
8585

8686
def verify(self):
87-
87+
8888
cdict = self.data_as_cbor_dict
89-
89+
9090
for i in ('version', 'documents'):
9191
if i not in cdict:
9292
raise InvalidMdoc(
9393
f"Mdoc is invalid since it doesn't contain the '{i}' element"
9494
)
95-
95+
9696
doc_cnt = 1
9797
for doc in cdict['documents']:
9898
mso = MobileDocument(**doc)
99-
99+
100100
try:
101101
mso.verify()
102102
except Exception as e:
@@ -107,11 +107,6 @@ def verify(self):
107107
self.documents_invalid.append(doc)
108108
doc_cnt += 1
109109
continue
110-
110+
111111
self.documents.append(mso)
112-
doc_cnt +=1
113-
114-
115-
116-
117-
112+
doc_cnt += 1

pymdoccbor/mso.py

+22-26
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
import binascii
21
import cbor2
32
import cryptography
43
import logging
54

65
from pycose.keys import CoseKey, EC2Key
76
from pycose.messages import Sign1Message
87

9-
from typing import Optional, Union
8+
from typing import Optional
109

1110
from . exceptions import UnsupportedMsoDataFormat
1211
from . settings import COSEKEY_HAZMAT_CRV_MAP, CRV_LEN_MAP
@@ -24,62 +23,60 @@ class MobileSecurityObject:
2423
only if the authentication signature or MAC is correct.
2524
"""
2625

27-
pass
28-
2926

3027
class MsoParser(MobileSecurityObject):
3128
"""
3229
Parameters
3330
data: CBOR TAG 24
34-
31+
3532
Example:
3633
MsoParser(mdoc['documents'][0]['issuerSigned']['issuerAuth'])
3734
3835
Note
3936
The signature is contained in an untagged COSE_Sign1
4037
structure as defined in RFC 8152.
4138
"""
42-
39+
4340
def __init__(self, data: cbor2.CBORTag):
4441
self._data = data
45-
42+
4643
if isinstance(data, bytes):
47-
self.object :Sign1Message = bytes2CoseSign1(cbor2.dumps(cbor2.CBORTag(18, value=data)))
44+
self.object: Sign1Message = bytes2CoseSign1(
45+
cbor2.dumps(cbor2.CBORTag(18, value=data)))
4846
elif isinstance(data, list):
49-
self.object :Sign1Message = cborlist2CoseSign1(self._data)
47+
self.object: Sign1Message = cborlist2CoseSign1(self._data)
5048
else:
5149
raise UnsupportedMsoDataFormat(
5250
f"MsoParser only supports raw bytes and list, a {type(data)} was provided"
5351
)
54-
55-
56-
self.object.key :Optional[CoseKey, None] = None
57-
self.public_key :cryptography.hazmat.backends.openssl.ec._EllipticCurvePublicKey = None
58-
self.x509_certificates :list = []
59-
52+
53+
self.object.key: Optional[CoseKey, None] = None
54+
self.public_key: cryptography.hazmat.backends.openssl.ec._EllipticCurvePublicKey = None
55+
self.x509_certificates: list = []
56+
6057
@property
6158
def payload_as_cbor(self):
6259
"""
6360
return the decoded payload
6461
"""
6562
return cbor2.loads(self.object.payload)
66-
63+
6764
@property
6865
def payload_as_raw(self):
6966
return self.object.payload
70-
67+
7168
@property
7269
def payload_as_dict(self):
7370
return cbor2.loads(
7471
cbor2.loads(self.object.payload).value
7572
)
76-
73+
7774
@property
7875
def raw_public_keys(self) -> bytes:
7976
return list(self.object.uhdr.values())
80-
77+
8178
def load_public_key(self):
82-
79+
8380
logger.warning(
8481
"TODO: in next releases. "
8582
"The certificate is to be considered as untrusted, this release "
@@ -90,11 +87,11 @@ def load_public_key(self):
9087
self.x509_certificates.append(
9188
cryptography.x509.load_der_x509_certificate(i)
9289
)
93-
90+
9491
self.public_key = self.x509_certificates[0].public_key()
95-
92+
9693
key = EC2Key(
97-
crv=COSEKEY_HAZMAT_CRV_MAP[self.public_key.curve.name],
94+
crv=COSEKEY_HAZMAT_CRV_MAP[self.public_key.curve.name],
9895
x=self.public_key.public_numbers().x.to_bytes(
9996
CRV_LEN_MAP[self.public_key.curve.name], 'big'
10097
)
@@ -105,12 +102,11 @@ def verify_signature(self) -> bool:
105102

106103
if not self.object.key:
107104
self.load_public_key()
108-
105+
109106
return self.object.verify_signature()
110107

111108

112109
class MsoWriter(MobileSecurityObject):
113110
"""
114-
111+
115112
"""
116-
pass

0 commit comments

Comments
 (0)