Skip to content
Myr

Added Persona example

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Armin Ronacher · committed by Armin Ronacher · parent e17024e · Browse files · 6 files+198 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

6 files+198 added−0 removed

  • other:+198 added−0 removed

6 files+198 added−0 removed

added:examples/persona/static/persona.js+50 added
added:examples/persona/static/spinner.pngbinary

A binary file, added.

added:examples/persona/static/style.css+39 added
added:examples/persona/templates/index.html+23 added
added:examples/persona/templates/layout.html+27 added
added:examples/persona/persona.py+59 added
1from flask import Flask, render_template, session, request, json, abort, g23import requests456app = Flask(__name__)7app.config.update(8 DEBUG=True,9 SECRET_KEY='my development key',10 PERSONA_JS='https://login.persona.org/include.js',11 PERSONA_VERIFIER='https://verifier.login.persona.org/verify',12)13app.config.from_envvar('PERSONA_SETTINGS', silent=True)141516@app.before_request17def get_current_user():18 g.user = None19 email = session.get('email')20 if email is not None:21 g.user = email222324@app.route('/')25def index():26 """Just a generic index page to show."""27 return render_template('index.html')282930@app.route('/_auth/login', methods=['GET', 'POST'])31def login_handler():32 """This is used by the persona.js file to kick off the33 verification securely from the server side. If all is okay34 the email address is remembered on the server.35 """36 resp = requests.post(app.config['PERSONA_VERIFIER'], data={37 'assertion': request.form['assertion'],38 'audience': request.host_url,39 }, verify=True)40 if resp.ok:41 verification_data = json.loads(resp.content)42 if verification_data['status'] == 'okay':43 session['email'] = verification_data['email']44 return 'OK'4546 abort(400)474849@app.route('/_auth/logout', methods=['POST'])50def logout_handler():51 """This is what persona.js will call to sign the user52 out again.53 """54 session.clear()55 return 'OK'565758if __name__ == '__main__':59 app.run()