browserauth.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 
3 """
4 usage: %(progname)s [args]
5 """
6 
7 
8 import os, sys, string, time, getopt
9 from pyclearsilver.log import *
10 
11 from pyclearsilver import odb
12 
13 import config
14 
15 #import fcrypt as crypt
16 import crypt
17 
18 def _createCheckVal(username, issued_at, pw_hash, vcode):
19  checkval = "%s:%s" % (username, now)
20  realcheckval = "%s:%s:%s" % (checkval, pw_hash, vcode)
21  checkval_hash = crypt.crypt(realcheckval,config.gAuthSalt)
22  return checkval, checkval_hash
23 
24 
25 # -------------------------------
26 # issueLoginCookie
27 #
28 # format: "login:issued_at_time_t:hash(pw_hash+issued_at_time_t)"
29 # ex: "V1/jeske:2123123:AS132dd12"
30 
31 def generateBrowserCookie(authdb, ipaddr):
32  now = int(time.time())
33 
34  row = authdb.browserid.newRow()
35  row.creationDate = now
36  row.ipaddr = ipaddr
37  row.save()
38 
39  cookie = "V1/%09d" % row.browserid
40 
41  return cookie, row.browserid
42 
43 def issueBrowserCookie(ncgi, authdb, domain):
44  ipaddr = ncgi.hdf.getValue("CGI.RemoteAddress", "")
45 
46  bcookie, browserid = generateBrowserCookie(authdb, ipaddr)
47  ncgi.cookieSet("MB_B", bcookie, persist=1, path="/", domain=config.gDomain)
48  return browserid
49 
50 
52  ncgi.cookieClear("MB_B", "", "/")
53 
54 def getBrowserCookie(ncgi):
55  bcookie = ncgi.hdf.getValue("Cookie.MB_B","")
56 
57  if not bcookie: return None
58 
59  version, restCookie = string.split(bcookie, "/", 1)
60  browserid = int(restCookie)
61 
62  return browserid
63 
64 
65 def _checkBrowserCookie(authdb, cookie, ipaddr):
66  version, restCookie = string.split(cookie, "/", 1)
67  if version != "V1":
68  warn("browserauth.py", "invalid browser cookie, version", version, cookie)
69  return None
70 
71  browserid = int(restCookie)
72 
73  try:
74  row = authdb.browserid.fetchRow(("browserid", browserid))
75  except odb.eNoMatchingRows:
76  warn("browserauth.py", "invalid browser cookie, browserid not found")
77  return browserid
78 # return None
79 
80  if row.ipaddr != ipaddr:
81  warn("browserauth.py", "ipaddr mismatch", row.ipaddr, ipaddr)
82 
83  debug("browserauth.py", "cookie", browserid)
84 
85  return browserid
86 
87 def checkBrowserCookie(authdb, ncgi):
88  bcookie = ncgi.hdf.getValue("Cookie.MB_B","")
89  if not bcookie: return None
90 
91  ipaddr = ncgi.hdf.getValue("CGI.RemoteAddress", "")
92 
93  browserid = _checkBrowserCookie(authdb, bcookie, ipaddr)
94  return browserid
95 
96 
97 
98 def test():
99  pass
100 
101 def usage(progname):
102  print __doc__ % vars()
103 
104 def main(argv, stdout, environ):
105  progname = argv[0]
106  optlist, args = getopt.getopt(argv[1:], "", ["help", "test", "debug"])
107 
108  testflag = 0
109  if len(args) == 0:
110  usage(progname)
111  return
112  for (field, val) in optlist:
113  if field == "--help":
114  usage(progname)
115  return
116  elif field == "--debug":
117  debugfull()
118  elif field == "--test":
119  testflag = 1
120 
121  if testflag:
122  test()
123  return
124 
125 
126 if __name__ == "__main__":
127  main(sys.argv, sys.stdout, os.environ)
def usage(progname)
Definition: browserauth.py:101
def _checkBrowserCookie(authdb, cookie, ipaddr)
Definition: browserauth.py:65
def clearBrowserCookie(ncgi)
Definition: browserauth.py:51
def getBrowserCookie(ncgi)
Definition: browserauth.py:54
def checkBrowserCookie(authdb, ncgi)
Definition: browserauth.py:87
def generateBrowserCookie(authdb, ipaddr)
Definition: browserauth.py:31
def main(argv, stdout, environ)
Definition: browserauth.py:104
def _createCheckVal(username, issued_at, pw_hash, vcode)
Definition: browserauth.py:18
def issueBrowserCookie(ncgi, authdb, domain)
Definition: browserauth.py:43


webui
Author(s): Scott Hassan
autogenerated on Mon Jun 10 2019 15:51:24