• Stars
    star
    1,005
  • Rank 45,700 (Top 1.0 %)
  • Language
    Python
  • License
    MIT License
  • Created about 10 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Doing the OAuth dance with style using Flask, requests, and oauthlib.

Flask-Dance Build status Test coverage Documentation

Doing the OAuth dance with style using Flask, requests, and oauthlib. Currently, only OAuth consumers are supported, but this project could easily support OAuth providers in the future, as well. The full documentation for this project is hosted on ReadTheDocs, including the full list of supported OAuth providers, but this README will give you a taste of the features.

Installation

Just the basics:

$ pip install Flask-Dance

Or if you're planning on using the SQLAlchemy storage:

$ pip install Flask-Dance[sqla]

Quickstart

If you want your users to be able to log in to your app from any of the supported OAuth providers, you've got it easy. Here's an example using GitHub:

from flask import Flask, redirect, url_for
from flask_dance.contrib.github import make_github_blueprint, github

app = Flask(__name__)
app.secret_key = "supersekrit"
blueprint = make_github_blueprint(
    client_id="my-key-here",
    client_secret="my-secret-here",
)
app.register_blueprint(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
    return "You are @{login} on GitHub".format(login=resp.json()["login"])

If you're itching to try it out, check out the flask-dance-github example repository, with detailed instructions for how to run this code.

The github object is a context local, just like flask.request. That means that you can import it in any Python file you want, and use it in the context of an incoming HTTP request. If you've split your Flask app up into multiple different files, feel free to import this object in any of your files, and use it just like you would use the requests module.

You can also use Flask-Dance with any OAuth provider you'd like, not just the pre-set configurations. See the documentation for how to use other OAuth providers.

Storages

By default, OAuth access tokens are stored in Flask's session object. This means that if the user ever clears their browser cookies, they will have to go through the OAuth dance again, which is not good. You're better off storing access tokens in a database or some other persistent store, and Flask-Dance has support for swapping out the token storage. For example, if you're using SQLAlchemy, set it up like this:

from flask_sqlalchemy import SQLAlchemy
from flask_dance.consumer.storage.sqla import OAuthConsumerMixin, SQLAlchemyStorage

db = SQLAlchemy()

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    # ... other columns as needed

class OAuth(OAuthConsumerMixin, db.Model):
    user_id = db.Column(db.Integer, db.ForeignKey(User.id))
    user = db.relationship(User)

# get_current_user() is a function that returns the current logged in user
blueprint.storage = SQLAlchemyStorage(OAuth, db.session, user=get_current_user)

The SQLAlchemy storage seamlessly integrates with Flask-SQLAlchemy, as well as Flask-Login for user management, and Flask-Caching for caching.

Full Documentation

This README provides just a taste of what Flask-Dance is capable of. To see more, read the documentation on ReadTheDocs.

More Repositories

1

flask-sse

Server-Sent Events for Flask
Python
298
star
2

build-a-flask-api

A tutorial for how to build an API using Flask
Python
108
star
3

flask-misaka

Flask interface to Misaka, a markdown parsing library
Python
75
star
4

flask-dance-github

Example app that logs in with GitHub using Flask-Dance
Python
52
star
5

flask-dance-google

Example app that logs in with Google using Flask-Dance
Python
30
star
6

flask-dance-google-sqla

Example app that logs in with Google using Flask-Dance
Python
18
star
7

create-linked-clubhouse-story

Automatically create a story on Clubhouse when a pull request is opened
TypeScript
17
star
8

webhookdb

Replicates GitHub's database via HTTP webhooks
Python
15
star
9

flask-dance-multi-provider

Python
14
star
10

flask-dance-facebook-sqla

Example app that logs in with Facebook using Flask-Dance
Python
13
star
11

davidbaumgold.com

David Baumgold's personal website
HTML
12
star
12

postgres-text-diff

10
star
13

flask-dance-slack

Example Slackbot written using Flask-Dance
Python
7
star
14

flask-dance-facebook

Example app that logs in with Facebook using Flask-Dance
Python
6
star
15

MBTA-GeoJSON

GeoJSON-formatted information about MBTA rapid transit stations
Python
6
star
16

lektor-google-drive

Integrate Google Drive with Lektor
Python
5
star
17

pytest-dynaconf

Python
4
star
18

flask-dance-google-security-sqla

Example app that logs into Google using Flask-Dance
Python
4
star
19

django-with-dropbox

An example Django app with Dropbox integration, via Python Social Auth.
Python
4
star
20

flask-dance-linkedin

Example app that logs in with LinkedIn using Flask-Dance
Python
4
star
21

contactista

Python
3
star
22

seamless-karma

track and manage your Seamless.com corporate allocations
Python
3
star
23

nose-testconfig

Git clone of nose-testconfig (last updated July 27, 2014)
Python
3
star
24

flask-dance-twitter-sqla

Example app that logs in with Twitter using Flask-Dance
Python
3
star
25

csscount

Count the number of CSS rules in a file
JavaScript
3
star
26

utaustin-django-example

Example project for UT Austin Django class
Python
2
star
27

jira-migrate

Script to migrate JIRA issues from one instance to another.
Python
2
star
28

SublimeGymnastics

A simple plugin for Sublime Text 2 to create vertical and horizontal splits in your files.
Python
2
star
29

jinja2-iso8601

Adds `parse_date` and `format_date` filters to Jinja.
Python
2
star
30

nqueens

Solving the N Queens problem
Python
2
star
31

flask-dance-twitter

Example app that logs in with Twitter using Flask-Dance
Python
2
star
32

flask-dance-heroku-sqla

Example app that logs in with Heroku using Flask-Dance
Python
2
star
33

Quicksilver-Logger

super simple Quicksilver module for personal logging
C
1
star
34

relask

A Relay-based web development kit on Flask
Python
1
star
35

gdata-python-client

unofficial git clone the Google Data APIs Python Client Library repository
Python
1
star
36

dotfiles

my personal dotfiles
Vim Script
1
star
37

sentry-wsgi-gunicorn-gevent

Python
1
star
38

git-tutorial-prework

Documentation for how to prepare for your upcoming Git tutorial
1
star
39

buscalling

Check the Nextbus API and notify
Python
1
star
40

flask-dance-heroku

Example app that logs in with Heroku using Flask-Dance
Python
1
star
41

flask-dance-bitbucket

Python
1
star
42

mastodon-oauth-example

Example for using OAuth login with Mastodon
Python
1
star
43

touchstone-notes

Notes to get MIT's Touchstone system working with Django
1
star
44

photoroom-interview

TypeScript
1
star