signin.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 import nstart
4 import config
5 import os, sys, string, time
6 
7 from pyclearsilver.log import *
8 
9 from pyclearsilver.CSPage import Context
10 import neo_cgi, neo_cs, neo_util
11 from MBPage import MBPage
12 
13 from auth import browserauth
14 from auth import cookieauth
15 from auth import db_auth
16 from auth import pwauth
17 
18 from pyclearsilver import wordwrap
19 from email import MIMEText, Generator, Parser
20 from cStringIO import StringIO
21 
22 from web_msgs.msg import WebEvent
23 import rospy
24 
25 class SignInPage(MBPage):
26  def setup(self, hdf):
27  self.requestURI = hdf.getValue("Query.request", "")
28  self.authdb = db_auth.initSchema()
29 
30  def display0(self, hdf):
31  q_signout = hdf.getIntValue("Query.signout",0)
32  self.requestURI = hdf.getValue("Query.request", "")
33  if self.requestURI:
34  hdf.setValue("CGI.cur.request", self.requestURI)
35 
36  if q_signout:
37  cookieauth.clearLoginCookie(self.ncgi, self.username)
38 
39  def display(self, hdf):
40  self.redirectUri(self.default_app_path())
41 
43  hdf = self.ncgi.hdf
44  requestURI = hdf.getValue("CGI.RequestURI", "")
45  rurl = config.gBaseURL + "login/changePassword.py"
46  self.redirectUri(rurl + "?q=1&request=" + neo_cgi.urlEscape(config.gBaseURL + "webui/"))
47 
48  def Action_Logout(self, hdf):
49  warn("action logout called")
50 
51  self.ncgi.cookieClear("inactive")
52 
53  if self.is_active_user():
54  warn("removing active user")
55  self.remove_active_user()
56  else:
57  warn("logging out non-active user")
58 
59  # publish a web event that we logged out
60  pub = rospy.Publisher("/webui/events", WebEvent)
61  rospy.init_node("webui_login", anonymous=True)
62  msg = WebEvent()
63  msg.source = "user"
64  msg.type = "logout"
65  msg.data = self.username
66  pub.publish(msg)
67 
68  if config.get_robot_type().startswith("texas"):
69  self.redirectUri(config.gLobbyReturnPage + "?robot_name=" + hdf.getValue('CGI.Robot', ""))
70  else:
71  # don't clear login cookie for texai since the lobby will handle it
72  domain = hdf.getValue("HTTP.Host", "")
73  cookieauth.clearLoginCookie(self.ncgi, self.username, domain)
74  self.redirectUri(self.default_app_path())
75 
76  def Action_Login(self, hdf):
77 
78  q_username = hdf.getValue("Query.username","")
79  q_passwordHash = hdf.getValue("Query.password","")
80  q_persist = hdf.getValue("Query.persist","0")
81 
82  try: q_persist = int(q_persist)
83  except ValueError: q_persist = 0
84 
85  default_requestURI = config.gBaseURL + "%s/" % config.gDefaultModule
86 
87  warn("requestURI", self.requestURI)
88 
89  if not self.requestURI:
90  self.requestURI = default_requestURI
91 
92 
93  wwwhostname = hdf.getValue("HTTP.Host", "")
94 
95  rurl = self.http + wwwhostname + config.gBaseURL + "login/signin0.py"
96 
97  warn("signin.py", rurl)
98 
99  # open login db to get pw
100  userRec = self.authdb.users.lookup(q_username)
101 
102  if not userRec:
103  warn("signin.py", "login failure (%s) unknown user" % q_username)
104  self.redirectUri(rurl + "?err=Invalid+Login&request=%s" % neo_cgi.urlEscape(self.requestURI))
105 
106  q_password = pwauth.unmungePassword(q_passwordHash)
107 
108  ipaddr = hdf.getValue("CGI.RemoteAddress", "Unknown")
109  browserid = browserauth.getBrowserCookie(self.ncgi)
110 
111  now = time.time()
112 
113 
114 
115  loginRow = self.authdb.login.newRow()
116  loginRow.uid = userRec.uid
117  loginRow.username = userRec.username
118  loginRow.ipaddr = ipaddr
119  loginRow.browserid = browserid
120 
121  if userRec.checkPassword(q_password) == 0:
122  warn("signin.py", "login failure (%s) password mismatch" % q_username, q_password)
123  loginRow.loginType = 0
124  loginRow.save()
125 
126 
127  url = rurl + "?err=Invalid+Login&request=%s" % neo_cgi.urlEscape(self.requestURI)
128  warn("redirecting to", url)
129  self.redirectUri(url)
130  return
131 
132  # ----------- success!!! ------------------
133  # generate cookie
134 
135  loginRow.loginType = 1
136  loginRow.save()
137 
138  cookieauth.issueLoginCookie(self.ncgi, self.authdb, q_username, userRec.pw_hash, q_persist)
139 
140  if userRec.changePassword == 1:
141  self.requestChangePassword()
142  return
143 
144  # publish a web event that we logged in
145  pub = rospy.Publisher("/webui/events", WebEvent)
146  rospy.init_node("webui_login", anonymous=True)
147  msg = WebEvent()
148  msg.source = "user"
149  msg.type = "login (local)"
150  msg.data = self.username
151  pub.publish(msg)
152 
153  # redirect to the main page
154  self.redirectUri(self.requestURI)
155 
156 
157 
158  def __del__(self):
159  if self.authdb:
160  self.authdb.close()
161  self.authdb = None
162 
163 def run(context):
164  page = SignInPage(context, pagename="signin",nologin=1)
165  return page
166 
167 def main(context):
168  page = run(context)
169  page.start()
170 
171 
172 if __name__ == "__main__":
173  main(Context())


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