-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_stub.py
executable file
·118 lines (90 loc) · 3.32 KB
/
server_stub.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
#! /usr/bin/env python
# This file is part of FoodPuter.
# Foobar is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# Foobar is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with Foobar. If not, see <http://www.gnu.org/licenses/>.
#this could be used to mock connections to hal...
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
import hashlib
import cgi
import json
import Hal
import putil
idtoken = "TokenTOKEN"
PIN = "p1"
def gen_idresponse(rfid):
global idtoken
print rfid
#the magic
if rfid == "r2":
return None
resp = { 'user' : "Linda nielsen", 'token' : idtoken}
return json.dumps(resp)
def validate_order(data):
"""Checks if the signature is correct
the encoding part is in Hal.py
"""
#verify its a valid token
#assert(data['token'] == get_token(data['name']))
#pin = get_token(data['name']) BUT THIS IS NOT STORED IN HAL...
msg = "{}{}{}{}".format(data['name'], data['total'],idtoken, PIN)
print "MSG: ", msg
digest = hashlib.sha512(msg).hexdigest()
print digest
return data['signature'] == digest
def validate_accountbalance(data):
amount = float(data['total'])
putil.trace("Amount to see is {}".format(amount))
return amount < 42;
class HalMock(BaseHTTPRequestHandler):
def do_GET(self):
rfid = self.path[1:] #remove first /
resp = gen_idresponse(rfid)
#the magic
if not resp:
self.send_error(404,'User not Found: %s' % rfid)
else:
self.send_response(200)
self.send_header('Content-type', 'text/json')
self.end_headers()
self.wfile.write(resp)
def do_POST(self):
print "got a POST request"
length = int(self.headers.getheader('content-length'))
indata = self.rfile.read(length)
data = json.loads(indata)
# You now have a dictionary of the post data
putil.trace(data)
ret = {} #return value
if not validate_order(data):
putil.trace("status: HAL.DENY")
ret['status'] = "{}".format(Hal.DENY)
elif not validate_accountbalance(data):
putil.trace("nofounds")
putil.trace("status: HAL.NOFOUNDS")
ret['status'] = "{}".format(Hal.NOFUNDS)
else:
putil.trace("status: HAL.ACCEPT")
ret['status'] = Hal.ACCEPT
self.send_response(200)
self.send_header('Content-type', 'text/json')
self.end_headers()
self.wfile.write(json.dumps(ret));
self.wfile.close()
def main():
try:
server = HTTPServer(('localhost', 8080), HalMock)
print 'started httpserver...'
server.serve_forever()
except KeyboardInterrupt:
print '^C received, shutting down server'
server.socket.close()
if __name__ == '__main__':
main()