def render(template_, **kwargs): session = request.get_cookie('t') user = Account.get_user_from_session_id(session) kwargs['user'] = user templ = MakoTemplate(name=template_, lookup=[TEMPLATES_PATH], imports=['from webhelpers.html import escape'], default_filters=['escape']) return templ.render(**kwargs)
def register_account(): username = request.POST.get('username') password = request.POST.get('password') repeat = request.POST.get('repeat') if len(username) < 6 or password != repeat or len(password)< 6: return render('register.html') else: user = Account(username, passhash(password)) # this key is used once on the registration to know who was the user trying to authenticate with Twitter user.key = hashlib.sha256(username + password).hexdigest() user.save() response.set_cookie("k", user.key, path='/') oauth_consumer = oauth.Consumer(key=CONSUMER_KEY, secret=CONSUMER_SECRET) oauth_client = oauth.Client(oauth_consumer) resp, content = oauth_client.request(REQUEST_TOKEN_URL, 'POST', body=urllib.urlencode({'oauth_callback':'http://twittergrafico.ignorelist.com/end_registration'})) request_token = dict(parse_qsl(content)) url = "%s?oauth_token=%s" % (AUTHORIZATION_URL, request_token['oauth_token']) redirect(url)
def get_login_url(): username = request.POST.get('username') password = request.POST.get('password') user = Account.get_user(username) if user: if user.password != passhash(password): redirect('home') else: user.login() response.set_cookie("t", user.session, path='/') redirect('home')
def end_registration(): oauth_token = request.GET.get('oauth_token') oauth_verifier = request.GET.get('oauth_verifier') oauth_consumer = oauth.Consumer(key=CONSUMER_KEY, secret=CONSUMER_SECRET) oauth_client = oauth.Client(oauth_consumer) resp, content = oauth_client.request(ACCESS_TOKEN_URL, 'POST', body=urllib.urlencode({'oauth_token':oauth_token, 'oauth_verifier':oauth_verifier})) access_token = dict(parse_qsl(content)) key = request.get_cookie('k') response.delete_cookie('k') user = Account.get_user_from_key(key) user.oauth_token = access_token.get('oauth_token') user.oauth_token_secret = access_token.get('oauth_token_secret') user.user_id = access_token.get('user_id') user.screen_name = access_token.get('screen_name') user.save() user.login() response.set_cookie("t", user.session, path='/') redirect('/home')
def wrapper(*args, **kwargs): session = request.get_cookie('t') user = Account.get_user_from_session_id(session) if user: return view(user, *args, **kwargs) return redirect('/#login_div')