-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.py
99 lines (83 loc) · 3 KB
/
helper.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
# -*- coding: utf-8 -*-
import re
import os
import binascii
import db as database
class InputParser(object):
regex = {
'hostname': r'''^[-a-zA-Z0-9_]{1,32}$''',
'key': r'''^([a-fA-F0-9]{64})?$''',
'email': r'''^[A-Za-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[A-Za-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?\.)+[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?$''',
'nickname': r'''^[-a-zA-Z0-9_ äöüÄÖÜß]{1,64}$''',
'mac': r'''^([a-fA-F0-9]{12}|([a-fA-F0-9]{2}:){5}[a-fA-F0-9]{2})$''',
'coords': r'''^(-?[0-9]{1,3}(\.[0-9]{1,15})? -?[0-9]{1,3}(\.[0-9]{1,15})?)?$'''
}
regex_token = {'token': r'''^[a-f0-9]{30}$'''}
def __init__(self):
pass
def getNodeRegex(self):
return self.regex
def getTokenRegex(self):
return self.regex_token
def getNodeWithTokenRegex(self):
x = self.regex.copy()
y = self.regex_token.copy()
x.update(y)
return x
def __normalizeStrings(self, struct):
return dict((k.lower(), v) for k, v in struct.items())
def getData(self, req):
res = self.__normalizeStrings(req.form.to_dict())
return res
def __validationError(self, vres):
res = {}
res['status'] = 'error'
res['type'] = 'ValidationError'
res['validationResult'] = vres
return res
def validate(self, regex, data):
invalid = []
missing = []
unknown = []
errors = False
res = {}
for key in regex.keys():
if key not in data.keys():
missing.append(key)
errors = True
for key in data.keys():
value = data[key]
if key not in regex.keys():
unknown.append(key)
errors = True
elif not re.search(regex[key], value, re.IGNORECASE):
invalid.append(key)
errors = True
res['invalid'] = invalid
res['missing'] = missing
res['unknown'] = unknown
res['hasErrors'] = errors
if errors:
return self.__validationError(res)
else:
return None
class Token(object):
db = database.DB()
def getToken(self):
return binascii.b2a_hex(os.urandom(15)).decode('utf-8')
def checkToken(self, token):
if self.db.checkToken(token):
return None
else:
return {'status':'error','type':'NodeNotFoundError','token': token}
class Dedup(object):
db = database.DB()
def checkDups(self, hostname, mac, key, token):
if self.db.checkHostname(hostname, token):
return {'status':'error', 'type':'NodeEntryAlreadyExistsError','hostname': hostname}
elif self.db.checkMAC(mac, token):
return {'status':'error', 'type':'MacEntryAlreadyExistsError','mac': mac}
elif self.db.checkKey(key, token):
return {'status':'error', 'type':'KeyEntryAlreadyExistsError','key': key}
else:
return None