Flask-Dance
Flask-Dance is a Python library that simplifies OAuth authentication for Flask applications. It enables Flask apps to act as OAuth consumers, allowing users to log in with various third-party providers like GitHub, Google, and more. The library is actively maintained with frequent updates, currently at version 7.1.0.
Warnings
- breaking Flask-Dance v7.0.0 removed the Twitter pre-set configuration and introduced support for Authorization Flow with PKCE. Existing Twitter integrations will break and require manual implementation or updating to a custom blueprint. Dexcom preset was added.
- breaking Version 6.0.0 updated minimum supported versions to Flask 2.0.3 and Werkzeug 2.1. Version 5.0.0 also dropped support for Flask versions below 1.0.4, specifically adding support for Flask 2.0. Ensure your Flask and Werkzeug versions are compatible.
- breaking Flask-Dance v4.0.0 dropped support for Python 2.7. It also added support for SQLAlchemy 1.4. Older Python 2.7 applications must be migrated to Python 3.
- breaking Older versions (pre-v1.0.0, specifically in 0.x releases) had breaking changes in how backends worked, including changes to `OAuthConsumerMixin` columns setting `nullable=False`, which could require database migrations if upgrading from very old versions. Additionally, the attribute to store the backend changed from `backend` to `storage`.
- gotcha For local development over HTTP (non-HTTPS), you must set the `OAUTHLIB_INSECURE_TRANSPORT` environment variable to `1`. However, this should NEVER be used in production environments, as it disables security checks and makes your application vulnerable.
- gotcha An open issue (#438 on GitHub) indicates that `oauthlib` version 3.3.0 breaks the current implementation of Flask-Dance, preventing OAuth flows from working correctly. This is a critical dependency issue.
Install
-
pip install Flask-Dance -
pip install Flask-Dance[sqla]
Imports
- make_github_blueprint
from flask_dance.contrib.github import make_github_blueprint
- github
from flask_dance.contrib.github import github
- OAuth2ConsumerBlueprint
from flask_dance.consumer import OAuth2ConsumerBlueprint
- OAuthConsumerMixin
from flask_dance.consumer.storage.sqla import OAuthConsumerMixin
- SQLAlchemyStorage
from flask_dance.consumer.storage.sqla import SQLAlchemyStorage
Quickstart
import os
from flask import Flask, redirect, url_for, session
from flask_dance.contrib.github import make_github_blueprint, github
app = Flask(__name__)
app.secret_key = os.environ.get("FLASK_SECRET_KEY", "supersekrit")
github_blueprint = make_github_blueprint(
client_id=os.environ.get("GITHUB_OAUTH_CLIENT_ID"),
client_secret=os.environ.get("GITHUB_OAUTH_CLIENT_SECRET"),
)
app.register_blueprint(github_blueprint, url_prefix="/login")
@app.route("/")
def index():
if not github.authorized:
return redirect(url_for("github.login"))
resp = github.get("/user")
assert resp.ok, resp.text
return f"You are @{resp.json()['login']} on GitHub"
if __name__ == "__main__":
# For local development with HTTP, set OAUTHLIB_INSECURE_TRANSPORT=1
# Example: export OAUTHLIB_INSECURE_TRANSPORT=1
app.run(debug=True)