-
Notifications
You must be signed in to change notification settings - Fork 8
/
fastapi_http.py
109 lines (87 loc) · 3.65 KB
/
fastapi_http.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import requests
import os
import json
import base64
SETTINGS_SERTIFICATE = {
# specify the serial number of the license
'SERIAL_NUMBER_LICENSE': '',
# specify the path to the root certificate with the extension (p7b)
'root_certificate': os.path.join('./', 'certificates', 'filename.p7b'),
# specify the path to the user certificate (archive)
'user_certificate': os.path.join('./', 'certificates', 'bundle-no-pin.zip')
}
URL = 'http://localhost:8001' # localhost:8095'
def installation_license():
"""Installing the serial number of the license"""
url_license = f'{URL}/license?serial_number='
response = requests.post(f"{url_license}{SETTINGS_SERTIFICATE['SERIAL_NUMBER_LICENSE']}")
return response
def installation_certificates_root(file):
"""Installing a root certificate"""
url_root = f'{URL}/certificate/root'
files = {
'file': open(file, 'rb'),
'Accept': 'application/json',
'Content-Type': 'multipart/form-data'
}
response = requests.post(url_root, files=files)
return response
def installation_certificates_user(file):
"""Installing a user certificate"""
url_user = f'{URL}/certificate/user'
files = {
'file': open(file, 'rb'),
'Accept': 'application/json',
'Content-Type': 'multipart/form-data'
}
response = requests.post(url_user, files=files)
return response
def document_signing(dir_no_signed, dir_signed: str):
"""Document signing"""
url_sign = f'{URL}/signer'
files = {
'file': open(dir_no_signed, 'rb'),
'Accept': 'application/json',
'Content-Type': 'multipart/form-data'
}
response = requests.post(url_sign, files=files)
with open(os.path.join(dir_signed, json.loads(response.text)['filename']), 'w') as new_file:
new_file.write(json.loads(response.text)['signedContent'])
def decryption_signed_document(dir_signed, dir_unsigned: str):
"""Getting the source file"""
url_unsign = f'{URL}/unsigner'
files = {
'file': open(dir_signed, 'rb'),
'Accept': 'application/json',
'Content-Type': 'multipart/form-data'
}
response = requests.post(url_unsign, files=files)
with open(os.path.join(dir_unsigned, json.loads(response.text)['filename']), 'wb') as new_file:
new_file.write(base64.b64decode(json.loads(response.text)['unsignedContent']))
def signature_verification(original_file, signed_file: str):
"""Verification of a signed document"""
url_verify = f'{URL}/verify'
files = {
'original_file': open(original_file, 'rb'),
'signed_file': open(signed_file, 'rb'),
'Accept': 'application/json',
'Content-Type': 'multipart/form-data'
}
response = requests.post(url_verify, files=files)
print(json.loads(response.text)['verifyContent'])
installation_license()
installation_certificates_root(SETTINGS_SERTIFICATE['root_certificate'])
installation_certificates_user(SETTINGS_SERTIFICATE['user_certificate'])
no_signed_dir = os.path.join('./', 'no_signed')
signed_dir = os.path.join('./', 'signed')
list_name_no_signed_files = os.listdir(no_signed_dir)
for name_file in list_name_no_signed_files:
document_signing(os.path.join(no_signed_dir, name_file), signed_dir)
list_name_signed_files = os.listdir(signed_dir)
list_name_no_signed_files.sort()
list_name_signed_files.sort()
for name_file in list_name_no_signed_files:
signature_verification(os.path.join(no_signed_dir, name_file), os.path.join(signed_dir, f'{name_file}.sig'))
unsigned_dir = os.path.join('./', 'unsigned')
for name_file in list_name_signed_files:
decryption_signed_document(os.path.join(signed_dir, name_file), unsigned_dir)