forked from Alex014/NessNodeTester
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-auth.py
140 lines (106 loc) · 5.52 KB
/
test-auth.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import json
import sys
import urllib.parse
from lib.NessAuth import NessAuth
from Crypto.Hash import MD5
class AuthTester:
data_user = {}
data_node = {}
def __init__(self):
return
def loadUser(self, username: str):
filename = username + ".key.json"
f = open("out/keys/user/" + filename, "r")
return json.loads(f.read())
def loadNode(self, url: str):
filename = urllib.parse.quote_plus(url) + ".key.json"
f = open("out/keys/node/" + filename, "r")
return json.loads(f.read())
def test(self, username: str, node_url: str):
ness_auth = NessAuth()
user = self.loadUser(username)
node = self.loadNode(node_url)
url = node_url + "/node/test/auth"
user_private_key = user['keys']["private"][user['keys']['current']]
result = ness_auth.get_by_auth_id(url, user_private_key, node_url, node["nonce"], username, username, user["nonce"])
if result['result'] == 'error':
print(" ~~~ TEST #1 Auth ID FAILED ~~~ ")
print(result['error'])
else:
print(" *** TEST #1 Auth ID OK !!! *** ")
print(result['message'])
test_string = 'The state calls its own violence law, but that of the individual, crime.'
result = ness_auth.get_by_two_way_encryption(url, test_string, node['public'], user_private_key, username)
if result['result'] == 'error':
print(" ~~~ TEST #2 Two way encryption FAILED ~~~ ")
print(result['error'])
else:
if ness_auth.verify_two_way_result(node['verify'], result):
print(" *** TEST #2 Two way encryption OK !!! *** ")
print(ness_auth.decrypt_two_way_result(result, user_private_key))
else:
print(" ~~~ TEST #2 Two way encryption FAILED ~~~ ")
print(" Verifying signature failed ")
return False
url = node_url + "/node/joined"
result = ness_auth.get_by_two_way_encryption(url, 'test', node['public'], user_private_key, username)
if result['result'] == 'error':
print(" ~~~ TEST #3 Registration check FAILED ~~~ ")
print(result['error'])
else:
if ness_auth.verify_two_way_result(node['verify'], result):
print(" *** TEST #3 Registration check OK !!! *** ")
result = ness_auth.decrypt_two_way_result(result, user_private_key)
data = json.loads(result)
if data['joined']:
shadowname = data['shadowname']
print(" *** The user:" + username + " is joined with shadowname:" + shadowname + " OK !!! ***")
else:
print("The user:" + username + " is not joined yet.")
url = node_url + "/node/join"
result = ness_auth.get_by_two_way_encryption(url, test_string, node['public'], user_private_key, username)
if result['result'] == 'error':
print(" ~~~ TEST #3.1 Registration FAILED ~~~ ")
print(result['error'])
else:
if ness_auth.verify_two_way_result(node['verify'], result):
print(" *** TEST #3.1 Registration OK *** ")
result = ness_auth.decrypt_two_way_result(result, user_private_key)
data = json.loads(result)
shadowname = data['shadowname']
addr = data['address']
print("User registered with shadowname:" + shadowname + " and addr:" + addr)
else:
print(" ~~~ TEST #3.1 Registration FAILED ~~~ ")
print(" Verifying signature failed ")
url = node_url + "/node/test/auth-shadow"
result = ness_auth.get_by_auth_id(url, user_private_key, node_url, node["nonce"], username, shadowname, user["nonce"])
if result['result'] == 'error':
print(" ~~~ TEST #4 Auth ID with shadowname FAILED ~~~ ")
print(result['error'])
else:
print(" *** TEST #4 Auth ID with shadowname OK !!! *** ")
print(result['message'])
test_string = 'Whoever knows how to take, to defend, the thing, to him belongs property'
result = ness_auth.get_by_two_way_encryption(url, test_string, node['public'], user_private_key, shadowname)
if result['result'] == 'error':
print(" ~~~ TEST #5 Two way encryption FAILED ~~~ ")
print(result['error'])
else:
if ness_auth.verify_two_way_result(node['verify'], result):
print(" *** TEST #5 Two way encryption OK !!! *** ")
print(ness_auth.decrypt_two_way_result(result, user_private_key))
else:
print(" ~~~ TEST #5 Two way encryption FAILED ~~~ ")
print(" Verifying signature failed ")
else:
print(" ~~~ TEST #3 Registration check FAILED ~~~ ")
print(" Verifying signature failed ")
url = node_url + "/node/test/auth-shadow"
return True
print('Test authentication')
if len(sys.argv) == 3:
tester = AuthTester()
tester.test(sys.argv[1], sys.argv[2])
else:
print('Usage: python test-auth.py <username> <node URL>')