Convert Figma logo to code with AI

lepture logoflask-oauthlib

YOU SHOULD USE https://github.com/lepture/authlib

1,451
456
1,451
75

Top Related Projects

4,925

The ultimate Python library in building OAuth, OpenID Connect clients and servers. JWS,JWE,JWK,JWA,JWT included.

OAuthlib support for Python-Requests!

A generic, spec-compliant, thorough implementation of the OAuth request-signing logic

A fully tested, abstract interface to creating OAuth clients and servers.

70,047

The Python micro framework for building web applications.

Quick Overview

Flask-OAuthlib is a Flask extension that provides OAuth 1.0a and OAuth 2.0 support for both client and provider. It integrates with Flask's configuration system and provides a simple way to implement OAuth in Flask applications. The library is designed to be easy to use and flexible.

Pros

  • Easy integration with Flask applications
  • Supports both OAuth 1.0a and OAuth 2.0
  • Can be used for both client-side and provider-side implementations
  • Well-documented with clear examples

Cons

  • No longer actively maintained (last commit was in 2019)
  • May have compatibility issues with newer versions of Flask and its dependencies
  • Some reported issues with certain OAuth providers
  • Limited support for more advanced OAuth features

Code Examples

  1. Creating an OAuth client:
from flask import Flask
from flask_oauthlib.client import OAuth

app = Flask(__name__)
oauth = OAuth(app)

github = oauth.remote_app(
    'github',
    consumer_key='your_client_id',
    consumer_secret='your_client_secret',
    request_token_params={'scope': 'user:email'},
    base_url='https://api.github.com/',
    request_token_url=None,
    access_token_method='POST',
    access_token_url='https://github.com/login/oauth/access_token',
    authorize_url='https://github.com/login/oauth/authorize'
)
  1. Implementing an OAuth provider:
from flask import Flask
from flask_oauthlib.provider import OAuth2Provider

app = Flask(__name__)
oauth = OAuth2Provider(app)

@oauth.clientgetter
def load_client(client_id):
    return Client.query.filter_by(client_id=client_id).first()

@oauth.tokengetter
def load_token(access_token=None, refresh_token=None):
    if access_token:
        return Token.query.filter_by(access_token=access_token).first()
    elif refresh_token:
        return Token.query.filter_by(refresh_token=refresh_token).first()
  1. Handling OAuth callback:
from flask import url_for, session, redirect

@app.route('/login')
def login():
    return github.authorize(callback=url_for('authorized', _external=True))

@app.route('/login/authorized')
def authorized():
    resp = github.authorized_response()
    if resp is None or resp.get('access_token') is None:
        return 'Access denied: reason={} error={}'.format(
            request.args['error_reason'],
            request.args['error_description']
        )
    session['github_token'] = (resp['access_token'], '')
    return redirect(url_for('index'))

Getting Started

  1. Install Flask-OAuthlib:

    pip install Flask-OAuthlib
    
  2. Import and initialize the OAuth object in your Flask app:

    from flask import Flask
    from flask_oauthlib.client import OAuth
    
    app = Flask(__name__)
    oauth = OAuth(app)
    
  3. Configure your OAuth client or provider as shown in the code examples above.

  4. Implement the necessary routes and handlers for authentication and authorization.

  5. Run your Flask application and test the OAuth functionality.

Competitor Comparisons

4,925

The ultimate Python library in building OAuth, OpenID Connect clients and servers. JWS,JWE,JWK,JWA,JWT included.

Pros of Authlib

  • More comprehensive OAuth support, including OAuth 1.0, OAuth 2.0, and OpenID Connect
  • Actively maintained with regular updates and improvements
  • Supports multiple web frameworks beyond Flask, including Django and FastAPI

Cons of Authlib

  • Steeper learning curve due to more extensive features and configurations
  • May be overkill for simple OAuth implementations
  • Slightly more complex setup process compared to Flask-OAuthlib

Code Comparison

Flask-OAuthlib:

from flask_oauthlib.client import OAuth

oauth = OAuth(app)
github = oauth.remote_app(
    'github',
    consumer_key='your_key',
    consumer_secret='your_secret',
    request_token_params={'scope': 'user:email'},
    base_url='https://api.github.com/',
    request_token_url=None,
    access_token_method='POST',
    access_token_url='https://github.com/login/oauth/access_token',
    authorize_url='https://github.com/login/oauth/authorize'
)

Authlib:

from authlib.integrations.flask_client import OAuth

oauth = OAuth(app)
github = oauth.register(
    name='github',
    client_id='your_key',
    client_secret='your_secret',
    access_token_url='https://github.com/login/oauth/access_token',
    access_token_params=None,
    authorize_url='https://github.com/login/oauth/authorize',
    authorize_params=None,
    api_base_url='https://api.github.com/',
    client_kwargs={'scope': 'user:email'},
)

OAuthlib support for Python-Requests!

Pros of requests-oauthlib

  • More flexible and can be used with various web frameworks, not limited to Flask
  • Simpler API for making OAuth requests, integrating well with the popular requests library
  • Better maintained with more recent updates and active community support

Cons of requests-oauthlib

  • Lacks built-in server-side OAuth provider functionality
  • Requires more manual setup for server-side OAuth flows compared to flask-oauthlib
  • May have a steeper learning curve for beginners due to its flexibility

Code Comparison

flask-oauthlib:

from flask_oauthlib.client import OAuth

oauth = OAuth(app)
github = oauth.remote_app(
    'github',
    consumer_key='your_key',
    consumer_secret='your_secret',
    request_token_params={'scope': 'user:email'},
    base_url='https://api.github.com/',
    request_token_url=None,
    access_token_method='POST',
    access_token_url='https://github.com/login/oauth/access_token',
    authorize_url='https://github.com/login/oauth/authorize'
)

requests-oauthlib:

from requests_oauthlib import OAuth2Session

github = OAuth2Session('your_client_id', scope=['user:email'])
authorization_url, state = github.authorization_url('https://github.com/login/oauth/authorize')
token = github.fetch_token('https://github.com/login/oauth/access_token',
                           client_secret='your_client_secret',
                           authorization_response=redirect_response)

A generic, spec-compliant, thorough implementation of the OAuth request-signing logic

Pros of oauthlib

  • More comprehensive OAuth support, covering OAuth 1 and 2
  • Framework-agnostic, allowing integration with various Python web frameworks
  • Actively maintained with regular updates and bug fixes

Cons of oauthlib

  • Steeper learning curve due to its more generic nature
  • Requires additional setup and configuration for specific frameworks like Flask

Code Comparison

flask-oauthlib:

from flask_oauthlib.client import OAuth

oauth = OAuth(app)
github = oauth.remote_app(
    'github',
    consumer_key='your_key',
    consumer_secret='your_secret',
    request_token_params={'scope': 'user:email'},
    base_url='https://api.github.com/',
    request_token_url=None,
    access_token_method='POST',
    access_token_url='https://github.com/login/oauth/access_token',
    authorize_url='https://github.com/login/oauth/authorize'
)

oauthlib:

from oauthlib.oauth2 import WebApplicationClient

client = WebApplicationClient('your_client_id')
authorization_url, state = client.prepare_authorization_request(
    'https://github.com/login/oauth/authorize',
    redirect_url='your_callback_url',
    scope=['user:email']
)

The code comparison shows that flask-oauthlib provides a more Flask-specific and concise setup, while oauthlib offers a more flexible but verbose approach that can be adapted to different frameworks.

A fully tested, abstract interface to creating OAuth clients and servers.

Pros of python-oauth2

  • Standalone OAuth 2.0 library, not tied to a specific web framework
  • Supports both client and server-side OAuth 2.0 implementations
  • More flexible for custom OAuth 2.0 flows and integrations

Cons of python-oauth2

  • Less actively maintained, with fewer recent updates
  • Lacks built-in integration with Flask, requiring more setup for Flask applications
  • May require more manual configuration for common use cases

Code Comparison

python-oauth2:

import oauth2 as oauth

consumer = oauth.Consumer(key="your_key", secret="your_secret")
client = oauth.Client(consumer)
resp, content = client.request("https://api.example.com/endpoint", "GET")

flask-oauthlib:

from flask_oauthlib.client import OAuth

oauth = OAuth(app)
remote = oauth.remote_app('example', 
    consumer_key='your_key',
    consumer_secret='your_secret',
    base_url='https://api.example.com'
)
resp = remote.get('endpoint')

The code comparison shows that flask-oauthlib provides a more Flask-centric approach, integrating directly with the Flask application. python-oauth2 offers a more generic implementation, which can be used in various Python environments but requires more manual setup for Flask applications.

70,047

The Python micro framework for building web applications.

Pros of Flask

  • Larger, more active community with frequent updates and contributions
  • More comprehensive documentation and extensive ecosystem of extensions
  • Flexible and lightweight, allowing for easy customization and integration

Cons of Flask

  • Lacks built-in OAuth functionality, requiring additional extensions or libraries
  • May require more setup and configuration for OAuth-specific use cases
  • Steeper learning curve for OAuth implementation compared to specialized libraries

Code Comparison

Flask (basic route):

from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello, World!'

Flask-OAuthlib (OAuth client setup):

from flask import Flask
from flask_oauthlib.client import OAuth

app = Flask(__name__)
oauth = OAuth(app)

github = oauth.remote_app(
    'github',
    consumer_key='your_key',
    consumer_secret='your_secret',
    request_token_params={'scope': 'user:email'},
    base_url='https://api.github.com/',
    request_token_url=None,
    access_token_method='POST',
    access_token_url='https://github.com/login/oauth/access_token',
    authorize_url='https://github.com/login/oauth/authorize'
)

Flask-OAuthlib provides a more streamlined approach for implementing OAuth functionality, while Flask offers greater flexibility and a broader range of features for general web application development.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Flask-OAuthlib

.. image:: https://img.shields.io/badge/donate-lepture-green.svg :target: https://lepture.com/donate :alt: Donate lepture .. image:: https://img.shields.io/pypi/wheel/flask-oauthlib.svg :target: https://pypi.python.org/pypi/flask-OAuthlib/ :alt: Wheel Status .. image:: https://img.shields.io/pypi/v/flask-oauthlib.svg :target: https://pypi.python.org/pypi/flask-oauthlib/ :alt: Latest Version .. image:: https://travis-ci.org/lepture/flask-oauthlib.svg?branch=master :target: https://travis-ci.org/lepture/flask-oauthlib :alt: Travis CI Status .. image:: https://coveralls.io/repos/lepture/flask-oauthlib/badge.svg?branch=master :target: https://coveralls.io/r/lepture/flask-oauthlib :alt: Coverage Status

Notice

You SHOULD use https://github.com/lepture/authlib instead.

Flask-OAuthlib is an extension to Flask that allows you to interact with remote OAuth enabled applications. On the client site, it is a replacement for Flask-OAuth. But it does more than that, it also helps you to create OAuth providers.

Flask-OAuthlib relies on oauthlib_.

.. _oauthlib: https://github.com/idan/oauthlib

Sponsored by

If you want to quickly add secure authentication to Flask, feel free to check out Auth0's Python API SDK and free plan at auth0.com/developers_ |auth0 image|

.. _auth0.com/developers: https://auth0.com/developers?utm_source=GHsponsor&utm_medium=GHsponsor&utm_campaign=flask-oauthlib&utm_content=auth

.. |auth0 image| image:: https://user-images.githubusercontent.com/290496/31718461-031a6710-b44b-11e7-80f8-7c5920c73b8f.png :target: https://auth0.com/developers?utm_source=GHsponsor&utm_medium=GHsponsor&utm_campaign=flask-oauthlib&utm_content=auth :alt: Coverage Status :width: 18px :height: 18px

Features

  • Support for OAuth 1.0a, 1.0, 1.1, OAuth2 client
  • Friendly API (same as Flask-OAuth)
  • Direct integration with Flask
  • Basic support for remote method invocation of RESTful APIs
  • Support OAuth1 provider with HMAC and RSA signature
  • Support OAuth2 provider with Bearer token

Security Reporting

If you found security bugs which can not be public, send me email at me@lepture.com. Attachment with patch is welcome.

Installation

Installing flask-oauthlib is simple with pip::

$ pip install Flask-OAuthlib

There is also a development version <https://github.com/lepture/flask-oauthlib/archive/master.zip#egg=Flask-OAuthlib-dev>_ on GitHub.

Links