Permalink
Browse files

Persona login and install support

  • Loading branch information...
1 parent edc7db0 commit 9761623ee48c192f0730165daf4fcac4fafeebe2 @nikhilm committed Apr 3, 2013
Showing with 62 additions and 20 deletions.
  1. +16 −2 app/index.html
  2. +2 −9 app/js/app.js
  3. +44 −9 server/app.py
View
@@ -9,13 +9,27 @@
<body>
<div class="hero-unit span10 offset2" id="container">
<h1 class="offset2">Push to phone</h1>
- <!--<img class="offset4" style="margin-top: 50px" src="/static/img/plain_sign_in_red.png" id="persona-signin">-->
+ {% if email %}
+ <h5 class="offset1">Hi {{ email }}</h5>
<p>
- <form class="form-inline offset2 span4" action="/install" method="POST">
+ <form class="offset1" action="/install" method="POST">
+ <label for="manifest">Application</label>
<input type="text" class="input-small span3" placeholder="Manifest URL" name="manifest">
+ <fieldset>
+ <legend>Devices to install to</legend>
+ {% for device in devices %}
+ <label class="checkbox">
+ <input type="checkbox" value="{{device}}" name="device-{{loop.index0}}">
+ {{ device }}
+ </label>
+ {% endfor %}
+ </fieldset>
<button type="submit" class="btn ">Install</button>
</form>
</p>
+ {% else %}
+ <img class="offset4" style="margin-top: 50px" src="/static/img/plain_sign_in_red.png" id="persona-signin">
+ {% endif %}
</div>
<script src="/static/js/bootstrap.min.js"></script>
<script src="/static/js/app.js"></script>
View
@@ -1,6 +1,6 @@
function finishLogin(assertion) {
- $.post('/login', { assertion: assertion }, function(data, status, xhr) {
- console.log(arguments);
+ $.post('/weblogin', { assertion: assertion }, function(data, status, xhr) {
+ //window.location.reload();
});
}
@@ -16,11 +16,4 @@ $(function() {
$('#persona-signin').click(function() {
navigator.id.request();
});
-
- /*if (navigator.mozApps) {
- $('#container').append('<a id="install-button" class="btn btn-primary offset4">Install</a>');
- $('#install-button').click(function() {
- navigator.mozApps.install("http://localhost:5000/static/manifest.webapp");
- });
- }*/
});
View
@@ -4,33 +4,66 @@
from redis import Redis
app = Flask(__name__, static_url_path='/static', static_folder='../app', template_folder='../app')
+app.secret_key = 'topseekret'
redis = Redis()
+def k(s):
+ return 'pushapptophone:%s'%s
+
@app.route('/')
def index():
- return render_template('index.html')
+ endpointskey = k('%s:endpoints'%(session.get('email', '')))
+ return render_template('index.html', email=session.get('email', None), devices=redis.lrange(endpointskey, 0, 20))
@app.route('/install', methods=['POST'])
def install():
+ print request.form
manifest = request.form['manifest']
- redis.set('pushapptophone:manifest', manifest)
- for endpoint in redis.lrange('pushapptophone:endpoints', 0, 25):
+ redis.set(k('manifest'), manifest)
+ for endpoint in redis.lrange(k('%s:endpoints'%session['email']), 0, 25):
print endpoint
r = requests.put(endpoint)
print r
return ''
@app.route('/manifest')
def get_manifest():
- return Response(redis.get('pushapptophone:manifest'), content_type='text/plain')
+ return Response(redis.get(k('manifest')), content_type='text/plain')
+
+@app.route('/weblogin', methods=['POST'])
+def weblogin():
+ if 'assertion' not in request.form:
+ abort(400)
+
+ # Send the assertion to Mozilla's verifier service.
+ data = {'assertion': request.form['assertion'], 'audience': 'http://pushtophone.nikhilism.com'}
+ resp = requests.post('https://verifier.login.persona.org/verify', data=data, verify=True)
+
+ # Did the verifier respond?
+ if resp.ok:
+ # Parse the response
+ verification_data = json.loads(resp.content)
+
+ # Check if the assertion was valid
+ if verification_data['status'] == 'okay':
+ # Log the user in by setting a secure session cookie
+ session.update({'email': verification_data['email']})
+ return verification_data['email']
+ else:
+ print 'status not okay', verification_data
+ else:
+ print 'resp.ok not true', resp
+
+ # Oops, something failed. Abort.
+ abort(500)
@app.route('/login', methods=['POST'])
def login():
if 'assertion' not in request.form:
abort(400)
# Send the assertion to Mozilla's verifier service.
- data = {'assertion': request.form['assertion'], 'audience': 'http://pushtophone.nikhilism.com'}
+ data = {'assertion': request.form['assertion'], 'audience': 'system.gaiamobile.org'}
resp = requests.post('https://verifier.login.native-persona.org/verify', data=data, verify=True)
# Did the verifier respond?
@@ -42,7 +75,7 @@ def login():
if verification_data['status'] == 'okay':
# Log the user in by setting a secure session cookie
session.update({'email': verification_data['email']})
- return resp.content
+ return verification_data['email']
else:
print 'status not okay', verification_data
else:
@@ -54,11 +87,13 @@ def login():
@app.route('/endpoint', methods=['POST'])
def endpoint():
endpoint = request.form['endpoint']
- print endpoint
- if not endpoint:
+ email = request.form['email']
+
+ print endpoint, email
+ if not endpoint or not email:
abort(400)
- redis.rpush('pushapptophone:endpoints', endpoint)
+ redis.rpush(k('%s:endpoints'%email), endpoint)
return 'OK'
if __name__ == '__main__':

0 comments on commit 9761623

Please sign in to comment.