def test_blueprint_factory(): heroku_bp = make_heroku_blueprint( client_id="foo", client_secret="bar", scope="global", redirect_to="index" ) assert isinstance(heroku_bp, OAuth2ConsumerBlueprint) assert heroku_bp.session.scope == "global" assert heroku_bp.session.base_url == "https://api.heroku.com/" assert heroku_bp.session.client_id == "foo" assert heroku_bp.client_secret == "bar" assert heroku_bp.authorization_url == "https://id.heroku.com/oauth/authorize" assert heroku_bp.token_url == "https://id.heroku.com/oauth/token"
from flask import flash from flask_login import current_user, login_user from flask_dance.contrib.heroku import make_heroku_blueprint from flask_dance.consumer import oauth_authorized, oauth_error from flask_dance.consumer.storage.sqla import SQLAlchemyStorage from sqlalchemy.orm.exc import NoResultFound from .models import db, User, OAuth blueprint = make_heroku_blueprint( scope="identity", storage=SQLAlchemyStorage(OAuth, db.session, user=current_user), ) # create/login local user on successful OAuth login @oauth_authorized.connect_via(blueprint) def heroku_logged_in(blueprint, token): if not token: flash("Failed to log in.", category="error") return False resp = blueprint.session.get("/account") if not resp.ok: msg = "Failed to fetch user info." flash(msg, category="error") return False info = resp.json() user_id = info["id"] # Find this OAuth token in the database, or create it
import os from flask import Flask, redirect, url_for from flask_dance.contrib.heroku import make_heroku_blueprint, heroku app = Flask(__name__) app.secret_key = os.environ.get("FLASK_SECRET_KEY", "supersekrit") app.config["HEROKU_OAUTH_CLIENT_ID"] = os.environ.get("HEROKU_OAUTH_CLIENT_ID") app.config["HEROKU_OAUTH_CLIENT_SECRET"] = os.environ.get("HEROKU_OAUTH_CLIENT_SECRET") heroku_bp = make_heroku_blueprint(scope="identity") app.register_blueprint(heroku_bp, url_prefix="/login") @app.route("/") def index(): if not heroku.authorized: return redirect(url_for("heroku.login")) resp = heroku.get("/account") assert resp.ok, resp.text return "You are {email} on Heroku".format(email=resp.json()["email"])
def _make_app(*args, **kwargs): app = Flask(__name__) app.secret_key = "whatever" blueprint = make_heroku_blueprint(*args, **kwargs) app.register_blueprint(blueprint) return app