-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAuthentification.py
68 lines (57 loc) · 2.48 KB
/
Authentification.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
from Crypto.Cipher import PKCS1_OAEP
from Crypto.PublicKey import RSA
# from server.encryptionDES import checkDES_Key
import ARappServer.DBinterface as DBi
RSA_PRIVATE_KEY = 'privatekey.pem'
# Обработка ошибки неправильного ключа DES KEY
class WrongRSA_Key(Exception):
def __init__(self, arg):
self.message = f"The file '{arg}' with the private encryption key was not found" \
"\nPrivate encryption key not found."
def __str__(self):
if self.message:
return 'WrongRSA_Key, {0} '.format(self.message)
else:
return 'WrongRSA_Key has been raised'
def checkRSA_PrivateKey():
'''
Проверка на наличие ключа DES KEY
:return:
'''
try:
if not open(RSA_PRIVATE_KEY).read():
raise WrongRSA_Key(RSA_PRIVATE_KEY) # TODO обработать отсутствие данных в ключе
except:
raise WrongRSA_Key(RSA_PRIVATE_KEY)
##########################################################
def authen(login, password):
'''
Производится аутентификация пользователя по логину и паролю.
Шифрование производится алгоритмом RSA-OAEP
RFC 2437 (https://tools.ietf.org/html/rfc2437)
:param login: <class 'str'> Логин пользователя
:param password: <class 'bytes'> зашифрованный пароль в представлении
последовательности байтов
:return: <class 'str'> Возвращается резщультат аутентификации:
"Successful authentication." or "Incorrect login or password."
'''
key = RSA.importKey(open(RSA_PRIVATE_KEY).read())
cipher = PKCS1_OAEP.new(key)
password_decrypt = cipher.decrypt(password)
password_decode = password_decrypt.decode()
result = DBi.User_DB.getNamefromlogin(login, password_decode)
return result
# if __name__ == '__main__':
# # collection_names = "radiator"
# # # while True:
# # aa = getMany(collection_names, {}, 110)
# # print(aa)
# # # if not aa['n']: break
# # print(getLastOne("radiator"))
# # print(help(deleteOne(collection_names,{"id":4})))
#
# # DB = BaseDBinterface(client.ARdb)
# DB1 = User_DB(client.UserDB)
#
# print(DBi.User_DB.getNamefromlogin("TEST", "TEST0912375981237059812730"))
# print(DB1.getLastId('users'))