• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

thorgate / django-esteid / 4354524044

pending completion
4354524044

push

github-actions

GitHub
Merge pull request #67 from thorgate/updates

404 of 571 branches covered (70.75%)

Branch coverage included in aggregate %.

12 of 12 new or added lines in 6 files covered. (100.0%)

1594 of 2012 relevant lines covered (79.22%)

0.79 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

66.39
/esteid/util.py
1
import hashlib
1✔
2
import os
1✔
3
import re
1✔
4
from collections import OrderedDict
1✔
5
from datetime import datetime
1✔
6

7
from django.utils import timezone
1✔
8

9
from esteid.constants import HASH_ALGORITHMS
1✔
10

11

12
def secure_random(n):
1✔
13
    return os.urandom(n)
1✔
14

15

16
def generate_hash(algorithm, data):
1✔
17
    """Hash the data with the supplied algorithm
18

19
    https://github.com/SK-EID/smart-id-documentation#33-hash-algorithms
20
    https://github.com/SK-EID/MID#231-supported-hashing-algorithms
21
    """
22
    if algorithm.upper() not in HASH_ALGORITHMS:
1✔
23
        raise ValueError(f"Unsupported algorithm {algorithm}")
1✔
24

25
    hash_method = getattr(hashlib, algorithm.lower())
1✔
26
    digest = hash_method(data).digest()
1✔
27

28
    return digest
1✔
29

30

31
def convert_status(status):
1✔
32
    if isinstance(status, bool):
1!
33
        return status
1✔
34

35
    return status.lower() == "ok"
×
36

37

38
def camel_2_py(the_dict):
1✔
39
    new_dict = {}
1✔
40
    for key, val in the_dict.items():
1✔
41
        if len(re.sub(r"([A-Z])", r" \1", key).split()) == len(key):
1!
42
            parts = [key.lower()]
×
43

44
        else:
45
            key = key.replace("ID", "Id").replace("CRL", "Crl")
1✔
46
            parts = re.sub(r"([A-Z])", r" \1", key).split()
1✔
47

48
        new_dict["_".join([x.lower() for x in parts])] = val
1✔
49

50
    return new_dict
1✔
51

52

53
def convert_time(timestamp):
1✔
54
    if not timestamp:
1!
55
        return None
×
56

57
    if isinstance(timestamp, datetime):
1✔
58
        if timezone.is_naive(timestamp):
1!
59
            timestamp = timezone.make_aware(timestamp, timezone.utc)
×
60

61
        return timestamp
1✔
62

63
    return timezone.make_aware(datetime.strptime(timestamp, "%Y-%m-%dT%H:%M:%SZ"), timezone.utc)
1✔
64

65

66
def get_instance_converter(cls, prepare_kwargs=camel_2_py):
1✔
67
    def _convert_instance(value):
1✔
68
        if isinstance(value, (dict, OrderedDict)):
1✔
69
            return cls(**prepare_kwargs(value))
1✔
70

71
        return value
1✔
72

73
    return _convert_instance
1✔
74

75

76
def get_typed_list_validator(klass):
1✔
77
    def _get_typed_list_validator(inst, attr, value):
1✔
78
        if not isinstance(value, list):
1!
79
            raise TypeError("Value MUST be a list")
×
80

81
        if not all(isinstance(x, klass) for x in value):
1!
82
            raise TypeError(f"Value MUST be a list of {klass}")
×
83

84
    return _get_typed_list_validator
1✔
85

86

87
def get_typed_list_converter(klass):
1✔
88
    converter = get_instance_converter(klass)
1✔
89

90
    def _get_typed_list_converter(value):
1✔
91
        return [converter(x) for x in value]
1✔
92

93
    return _get_typed_list_converter
1✔
94

95

96
def get_name_from_legacy_common_name(common_name):
1✔
97
    common_name = common_name.replace("\\,", ",")
1✔
98
    common_name = common_name.strip().rstrip(",")
1✔
99

100
    parts = common_name.split(",")[:-1]
1✔
101
    parts.reverse()
1✔
102

103
    return " ".join(parts).title()
1✔
104

105

106
def get_id_from_legacy_common_name(common_name):
1✔
107
    common_name = common_name.strip().rstrip(",")
1✔
108

109
    return common_name.split(",")[-1]
1✔
110

111

112
def ucs_to_utf8(val):
1✔
113
    return bytes([ord(x) for x in re.sub(r"\\([0-9ABCDEF]{1,2})", lambda x: chr(int(x.group(1), 16)), val)]).decode(
×
114
        "utf-8"
115
    )
116

117

118
def parse_legacy_dn(dn):
1✔
119
    x_client = dn.strip().strip("/").split("/")
×
120

121
    res = {}
×
122

123
    for part in x_client:
×
124
        part = ucs_to_utf8(part).split("=")
×
125
        res[part[0]] = part[1]
×
126

127
    return res
×
128

129

130
def parse_rfc_dn(dn):
1✔
131
    dn = ucs_to_utf8(dn).replace("\\,", ",")
×
132
    res = {}
×
133
    c_key = None
×
134

135
    for part in dn.strip().split(","):
×
136
        if "=" in part:
×
137
            part = part.split("=")
×
138

139
            c_key = part[0]
×
140
            res[c_key] = part[1]
×
141

142
        elif c_key:
×
143
            res[c_key] = f"{res[c_key]},{part}"
×
144

145
    return res
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc