Initial commit.
[webdispense.git] / door.py
1 #!/usr/bin/python
2
3 import commands,re
4
5 print "Content-type: text/json"
6 print
7 import cgi,cgitb
8 cgitb.enable ()
9
10 result = {}
11 form = cgi.FieldStorage ()
12
13 def checkdata ():
14         if not (form.has_key("username") and form.has_key("pin")):
15                 result = {"result" : "Missing information!"}
16                 return result
17         expr_name = re.compile ("^\w*$")
18         expr_pin = re.compile ("^\d{4}$")
19         username = form["username"].value
20         pin = form["pin"].value
21         if (not expr_name.match (form["username"].value)) or (not expr_pin.match (form["pin"].value)):
22                  result = {"result" : "Incorrect login!"}
23                  return result
24         output = commands.getoutput ("sudo checkpin " + username + " " + pin)
25         if output == "False":
26                 result = {"result" : "Authentication failed!"}
27                 return result
28         if not output == "True":
29                 result = {"result" : "PIN not set up."}
30                 return result
31         output = commands.getoutput ("dispense -u " + username + " opendoor");
32         expr_bal = re.compile ("(?P<bal>\d*\.\d*)")
33         balance = expr_bal.search (output).groups()[0]
34
35         return {"result" : "success", "balance" : balance}
36         
37
38
39 print str(checkdata ())

UCC git Repository :: git.ucc.asn.au