Skip to content

Instantly share code, notes, and snippets.

@davidawad
Created April 1, 2015 04:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidawad/f881a6ec64e8e2be2b86 to your computer and use it in GitHub Desktop.
Save davidawad/f881a6ec64e8e2be2b86 to your computer and use it in GitHub Desktop.
Python Flask O-Auth
# -*- coding: utf-8 -*-
"""
This is a simple Flask app that uses Authomatic to log users in with Facebook Twitter and OpenID.
"""
from flask import Flask, render_template, request, make_response, session
from authomatic.adapters import WerkzeugAdapter
from authomatic import Authomatic
from config import CONFIG
app = Flask(__name__)
# Instantiate Authomatic.
authomatic = Authomatic(CONFIG, 'secretKEY', report_errors=False)
# You need to set a secret string otherwise the session will not work.
app.secret_key = 'randomsecretstring'
@app.route('/')
def index():
"""
Home handler
"""
return render_template('index.html')
@app.route('/login/<provider_name>/', methods=['GET', 'POST'] )
def login(provider_name):
"""
Login handler, must accept both GET and POST to be able to use OpenID.
"""
# We need response object for the WerkzeugAdapter.
response = make_response()
# Log the user in, pass it the adapter and the provider name.
# result = authomatic.login(WerkzeugAdapter(request, response), provider_name)
# save session? ?
result = authomatic.login(
WerkzeugAdapter(request, response),
provider_name,
session=session,
session_saver=lambda: app.save_session(session, response)
)
app.session = session
# If there is no LoginResult object, the login procedure is still pending.
if result:
if result.user:
# We need to update the user to get more info.
result.user.update()
# The rest happens inside the template.
return render_template('login.html', result=result)
# Don't forget to return the response.
return response
@app.route('/content')
def content():
if app.open_session(app):
return render_template('content.html', session=app.open_session() )
return render_template('content.html')
# Run the app.
if __name__ == '__main__':
app.run(debug=True, port=8080)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment