Skip to content

Commit

Permalink
Finally getting account login/logout to work
Browse files Browse the repository at this point in the history
  • Loading branch information
arfrank committed Nov 4, 2010
1 parent 2af2f3a commit b60e89d
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 13 deletions.
3 changes: 1 addition & 2 deletions app.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ handlers:
- url: /(.*)/accounts/.*
script: handlers/accounts.py



- url: .*
script: handlers/main.py
secure: always
5 changes: 5 additions & 0 deletions appengine_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from libraries.gaesessions import SessionMiddleware

def webapp_add_wsgi_middleware(app):
app = SessionMiddleware(app, cookie_key="asfakjsdljnljksdbcjhabschjbasjkdbcjkhasbcjkabs")
return app
Binary file added appengine_config.pyc
Binary file not shown.
43 changes: 35 additions & 8 deletions handlers/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from google.appengine.ext.webapp import template
import os

from libraries import gaesessions
from libraries.gaesessions import get_current_session
from models import accounts
from helpers import application

Expand All @@ -34,10 +34,19 @@ def get(self):
self.response.out.write(template.render(path,{}))

def post(self):
from hashlib import sha256
required = ['email','password','password_confirm']
if application.required(required,self.request) and self.request.get('password') == self.request.get('password_confirm'):
account = accounts.Account()

exist_account = accounts.Account.get_by_key_name(self.request.get('email'))
if exist_account is None:
account = accounts.Account.new(key_name = self.request.get('email'), email=self.request.get('email'),password=self.request.get('password'))
account.put()
session = get_current_session()
session.regenerate_id()
session['account'] = account
self.redirect('/account')
else:
Register.get(self)
else:
Register.get(self)

Expand All @@ -48,18 +57,36 @@ def get(self):
self.response.out.write(template.render(path,{}))

def post(self):
pass

required = ['email','password']
if application.required(required,self.request):
account = accounts.Account.get_by_key_name(self.request.get('email'))
if account is not None and account.check_password(self.request.get('password')):
session = get_current_session()
session.regenerate_id()
session['account'] = account
self.redirect('/account')
else:
Login.get(self)
class Account(webapp.RequestHandler):
def get(self):
path = os.path.join(os.path.dirname(__file__), '../templates/account.html')
self.response.out.write(template.render(path,{}))

session = get_current_session()
if session['account'] is None:
self.redirect('/login')
else:
path = os.path.join(os.path.dirname(__file__), '../templates/account.html')
self.response.out.write(template.render(path,{'data':{'account':session['account']}}))

class Logout(webapp.RequestHandler):
def get(self):
session = get_current_session()
session.terminate()
self.redirect('/')
def main():
application = webapp.WSGIApplication([
('/', MainHandler),
('/register',Register),
('/login',Login),
('/logout',Logout),
('/account',Account)
],
debug=True)
Expand Down
2 changes: 0 additions & 2 deletions helpers/sid.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
def compute_sid(params_dictionary):
hash_string = ''
keys = params_dictionary.items()#.sort()
print keys
print 'asdfasdf'
for key in keys:
pass
# hash_string += key + '=' + params_dictionary[key]
Expand Down
Binary file added libraries/__init__.pyc
Binary file not shown.
Binary file added libraries/gaesessions/__init__.pyc
Binary file not shown.
20 changes: 19 additions & 1 deletion models/accounts.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,26 @@
from google.appengine.ext import db
from models import base

from hashlib import sha256
import random
import string
class Account(base.CommonModel):
Sid = db.StringProperty()
FriendlyName = db.StringProperty()
Status = db.StringProperty()
AuthToken = db.StringProperty()
Salt = db.StringProperty()
Email = db.EmailProperty()
Password = db.StringProperty()

@classmethod
def new(cls, key_name, email, password):
Salt = ''.join(random.choice(string.digits) for x in range(32))
Sid = 'AC'+sha256(email).hexdigest()
Password = sha256(Sid+password+Salt).hexdigest()
AuthToken = sha256(Sid+Password).hexdigest()
return cls(key_name=email,Email = email, FriendlyName = email,
Sid=Sid,Status='Active',Salt=Salt,
Password=Password,AuthToken=AuthToken)

def check_password(self,password):
return self.Password == sha256(self.Sid+password+self.Salt).hexdigest()
Binary file modified models/accounts.pyc
Binary file not shown.
12 changes: 12 additions & 0 deletions templates/account.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
{% extends "../templates/base.html" %}
{% block 'content' %}
<h1 id="account_information">Account Information</h1>
<h2>Account Information</h2>
<p>
Account Sid: {{data.account.Sid}}<br />
Account Token: {{data.account.AuthToken}}<br />
Email: {{data.account.Email}}<br />
Friendly Name: {{data.account.FriendlyName}}<br />
</p>
<h2>Links</h2>
<p>
<a href="/phone-numbers">Phone Numbers</a><br>
<a href="/logout">Logout</a>
</p>
{% endblock %}

0 comments on commit b60e89d

Please sign in to comment.