1
|
class Subject:
|
2
|
ATTR_MAP = {"C": "country", "ST": "state", "L": "locality", "CN": "common_name", "O": "organization",
|
3
|
"OU": "organization_unit", "emailAddress": "email_address"}
|
4
|
INVERSE_ATTR_MAP = {k: v for v, k in ATTR_MAP.items()}
|
5
|
|
6
|
def __init__(self, common_name=None, country=None, locality=None, state=None, organization=None,
|
7
|
organization_unit=None, email_address=None):
|
8
|
self.common_name = common_name
|
9
|
self.country = country
|
10
|
self.locality = locality
|
11
|
self.state = state
|
12
|
self.organization = organization
|
13
|
self.organization_unit = organization_unit
|
14
|
self.email_address = email_address
|
15
|
|
16
|
@staticmethod
|
17
|
def from_dict(d):
|
18
|
if not isinstance(d, dict):
|
19
|
return None
|
20
|
|
21
|
s = Subject()
|
22
|
for k, v in Subject.ATTR_MAP.items():
|
23
|
if k in d:
|
24
|
if not isinstance(d[k], str):
|
25
|
return None
|
26
|
s.__setattr__(v, d[k])
|
27
|
|
28
|
return s
|
29
|
|
30
|
def to_dict(self):
|
31
|
return {Subject.INVERSE_ATTR_MAP[k]: v for k, v in vars(self).items()}
|