Skip to content

Commit

Permalink
[appengine] Add session support
Browse files Browse the repository at this point in the history
  • Loading branch information
fmoo committed Aug 1, 2011
1 parent 6d3767e commit 6c89344
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 9 deletions.
3 changes: 3 additions & 0 deletions site/appengine/app.yaml
Expand Up @@ -10,5 +10,8 @@ handlers:
- url: /oauth/.*
script: oauth.py

- url: /logout/
script: logout.py

- url: /.*
script: not_found.py
10 changes: 10 additions & 0 deletions site/appengine/common.py
@@ -0,0 +1,10 @@
from google.appengine.ext import webapp
from urllib import urlencode

class BaseHandler(webapp.RequestHandler):
def go_home(self, msg=None):
if msg:
encoded = urlencode({'error_msg': msg})
self.redirect('/?' + encoded)
else:
self.redirect('/')
16 changes: 13 additions & 3 deletions site/appengine/home.py
@@ -1,14 +1,24 @@
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp import template
from gaesessions import get_current_session

import logging

class MainPage(webapp.RequestHandler):
def get(self):
template_values = {}
data = {
'error_msg': self.request.get('error_msg'),
}

# Build data from session
session = get_current_session()
if session.is_active():
data['has_token'] = True
data['scope'] = session.get('oauth_scope')

path = 'templates/home.html'
self.response.out.write(template.render(path, template_values))
self.response.out.write('hello world')
self.response.out.write(template.render(path, data))


application = webapp.WSGIApplication([('/.*', MainPage)],
Expand Down
27 changes: 27 additions & 0 deletions site/appengine/logout.py
@@ -0,0 +1,27 @@
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from gaesessions import get_current_session
from common import BaseHandler

import config
import logging


class LogoutHandler(BaseHandler):
def get(self):
session = get_current_session()
if session.is_active():
session.terminate()
self.go_home()


application = webapp.WSGIApplication([('/logout/', LogoutHandler)],
debug=True)


def main():
run_wsgi_app(application)


if __name__ == "__main__":
main()
29 changes: 23 additions & 6 deletions site/appengine/oauth.py
@@ -1,5 +1,7 @@
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from gaesessions import get_current_session
from common import BaseHandler

import config
import requests
Expand All @@ -18,13 +20,19 @@
from urllib import urlencode


class OAuthIndex(webapp.RequestHandler):
class OAuthIndex(BaseHandler):
def get(self):
scope = config.DEFAULT_SCOPE
self.redirect('/oauth/' + scope + '/step1/')

class OAuthStep1(webapp.RequestHandler):
class OAuthStep1(BaseHandler):
def get(self, scope):
session = get_current_session()
if session.is_active():
if session.get('scope') == scope:
self.go_home('You\'re already authenticated!')
return

# For Now, always redirect to glitch
query = {
'response_type': 'code',
Expand All @@ -38,14 +46,14 @@ def get(self, scope):


import pprint
class OAuthStep2(webapp.RequestHandler):
class OAuthStep2(BaseHandler):
def oauth_fail(self, obj):
self.error(400)
description = obj.get('error_description', None)

# XXX- Do something smarter than just printing this error message
if description is not None:
self.response.out.write(description)
self.go_home(description)
return

def get(self):
code = self.request.get('code', None)
Expand Down Expand Up @@ -78,7 +86,16 @@ def get(self):
# - 'token_type' - I get 'bearer' pretty much all the time;
# must be some kind of spec compliance thing
# - 'scope' - should match the scope you sent in stage1
self.response.out.write("It Worked")
session = get_current_session()
session.start()
if not session.is_active():
self.go_home('It worked, but you need a session to continue')
return

session['oauth_access_token'] = data.get('access_token')
session['oauth_scope'] = data.get('scope')
session['ouath_token_type'] = data.get('token_type')
self.go_home()

# XXX- Hey, you should save data['access_token'] for this user

Expand Down
11 changes: 11 additions & 0 deletions site/appengine/templates/home.html
@@ -1,6 +1,17 @@
<!DOCTYPE html>
<html>
{% if error_msg %}
<div class="error_msg">
{{ error_msg }}
</div>
{% endif %}
<h1>You are currently
{% if not has_token %}NOT {% endif %}
logged in{% if scope %} with {{scope}} permission.{%else%}.
{% endif %}
<h2>
<ul>
<li><a href="/oauth/">test oauth</a></li>
<li><a href="/logout/">logout</a></li>
</ul>
</html>

0 comments on commit 6c89344

Please sign in to comment.