In this assignment, you will add a set of pages and images in private area that are only viewable to logged in users. You will also move user accounts into the datastore and store a record of accesses to the new private area in the datastore.
The steps for this assignment include:
from util.sessions import Session
from google.appengine.ext import db
# A Model for a User
class User(db.Model):
account = db.StringProperty()
password = db.StringProperty()
name = db.StringProperty()
# A helper to do the rendering and to add the necessary
# variables for the _base.htm template
def doRender(handler, tname = 'index.htm', values = { }):
if pw == '' or acct == '':
doRender(
self,
'loginscreen.htm',
{'error' : 'Please specify Account and Password'} )
return
que = db.Query(User)
que = que.filter('account =',acct)
que = que.filter('password = ',pw)
results = que.fetch(limit=1)
if len(results) > 0 :
self.session['username'] = acct
doRender(self,'index.htm',{ } )
else:
doRender(self,'loginscreen.htm',
{'error' : 'Incorrect password'} )
# A Model for a User class User(db.Model): account = db.StringProperty() password = db.StringProperty() name = db.StringProperty() newuser = User(name="Chuck", account="csev", password="secret"); newuser.put();Of course - change the strings to be you. Then press Run Program to run the program. If everything worked, you should not see any errors or output as shown below:
def doRender(handler, tname = 'index.htm', values = { }):
temp = os.path.join(
os.path.dirname(__file__),
'templates/' + tname)
if not os.path.isfile(temp):
return False
# Check to see if we are supposed to be rendering
# A non-html file
binary = False
if temp.endswith(".jpg") or temp.endswith(".jpeg") :
handler.response.headers['Content-Type'] = 'image/jpeg'
binary = True
elif temp.endswith(".png") :
handler.response.headers['Content-Type'] = 'image/png'
binary = True
if binary:
outstr = open(temp, "rb").read()
handler.response.out.write(outstr)
return True
# Make a copy of the dictionary and add the path and session
newval = dict(values)
newval['path'] = handler.request.path
handler.session = Session()
if 'username' in handler.session:
newval['username'] = handler.session['username']
outstr = template.render(temp, newval)
handler.response.out.write(outstr)
return True
http://localhost:8080/private/index.htmYou should have access to the private area even if you are not logged in. So even though we hide the "Private Area" button when the user is not logged in, if they can guess the URL, they can see all of the private material! This is pretty poor security - we call this "Security by Obscurity". We need to fix this security gap.
class PrivateHandler(webapp.RequestHandler):
def get(self):
self.session = Session()
username = self.session.get('username', None)
if username == None:
doRender(self,'noauth.htm')
return
if doRender(self,self.request.path) :
return
doRender(self,'index.htm')
This handler simply checks to see if the user is logged in by checking for
something under the key 'username' in session. If the user is not logged
in, they are given a nasty "No Authorization" screen - otherwise they
are shown the files. Both the HTML and image files will be handled by
this handler.
def main():
application = webapp.WSGIApplication([
('/private/.*', PrivateHandler),
('/login', LoginHandler),
('/logout', LogoutHandler),
('/.*', MainHandler)],
debug=True)
wsgiref.handlers.CGIHandler().run(application)
Make sure that the private routing is before the "everything else" routing.
# A Model for Access class Access(db.Model): account = db.StringProperty() path = db.StringProperty() when = db.DateTimeProperty(auto_now=True)
class PrivateHandler(webapp.RequestHandler):
def get(self):
self.session = Session()
username = self.session.get('username', None)
if username == None:
doRender(self,'noauth.htm')
return
newaccess = Access(account=username, path=self.request.path);
newaccess.put();
if doRender(self,self.request.path) :
return
doRender(self,'index.htm')
Please turn in the following screenshots into CTools.