Convert Figma logo to code with AI

litl logorauth

A Python library for OAuth 1.0/a, 2.0, and Ofly.

1,602
174
1,602
39

Top Related Projects

OAuthlib support for Python-Requests!

4,878

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

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

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

Flask user session management.

Social auth made simple

Quick Overview

Rauth is a Python library that provides OAuth 1.0/a, OAuth 2.0, and Ofly consumer support. It's designed to be a simple and easy-to-use alternative to other OAuth libraries, with a focus on simplicity and consistency across different service providers.

Pros

  • Simple and intuitive API for OAuth authentication
  • Supports multiple OAuth versions (1.0/a, 2.0) and Ofly
  • Consistent interface across different service providers
  • Well-documented with examples for various popular services

Cons

  • No longer actively maintained (last commit in 2015)
  • May lack support for newer OAuth features or service-specific updates
  • Limited to consumer-side OAuth implementation
  • Potential security vulnerabilities due to lack of updates

Code Examples

  1. Creating an OAuth1Service:
from rauth import OAuth1Service

twitter = OAuth1Service(
    name='twitter',
    consumer_key='your_consumer_key',
    consumer_secret='your_consumer_secret',
    request_token_url='https://api.twitter.com/oauth/request_token',
    access_token_url='https://api.twitter.com/oauth/access_token',
    authorize_url='https://api.twitter.com/oauth/authorize',
    base_url='https://api.twitter.com/1.1/'
)
  1. Obtaining an OAuth2 access token:
from rauth import OAuth2Service

github = OAuth2Service(
    client_id='your_client_id',
    client_secret='your_client_secret',
    name='github',
    authorize_url='https://github.com/login/oauth/authorize',
    access_token_url='https://github.com/login/oauth/access_token',
    base_url='https://api.github.com/'
)

params = {'scope': 'user:email',
          'response_type': 'code'}

authorize_url = github.get_authorize_url(**params)

# Redirect user to authorize_url, then get the code from the redirect URL
code = 'obtained_code_from_redirect'

session = github.get_auth_session(data={'code': code})
  1. Making an authenticated request:
response = session.get('user')
print(response.json())

Getting Started

To get started with Rauth, first install it using pip:

pip install rauth

Then, import the necessary classes and create a service object:

from rauth import OAuth2Service

service = OAuth2Service(
    name='example',
    client_id='your_client_id',
    client_secret='your_client_secret',
    authorize_url='https://example.com/oauth/authorize',
    access_token_url='https://example.com/oauth/token',
    base_url='https://api.example.com/'
)

# Follow the OAuth flow as shown in the code examples above

Competitor Comparisons

OAuthlib support for Python-Requests!

Pros of requests-oauthlib

  • Seamless integration with the popular requests library
  • More active development and maintenance
  • Supports a wider range of OAuth providers and flows

Cons of requests-oauthlib

  • Slightly more complex setup for basic OAuth operations
  • Requires additional dependencies (oauthlib)

Code Comparison

requests-oauthlib:

from requests_oauthlib import OAuth1Session

oauth = OAuth1Session(client_key,
                      client_secret=client_secret,
                      resource_owner_key=resource_owner_key,
                      resource_owner_secret=resource_owner_secret)
r = oauth.get('https://api.example.com/endpoint')

rauth:

from rauth import OAuth1Service

service = OAuth1Service(
    consumer_key=client_key,
    consumer_secret=client_secret,
    name='example',
    access_token_url='https://api.example.com/oauth/access_token',
    authorize_url='https://api.example.com/oauth/authorize',
    base_url='https://api.example.com/')

session = service.get_session((resource_owner_key, resource_owner_secret))
r = session.get('endpoint')

Both libraries provide similar functionality for OAuth authentication, but requests-oauthlib offers tighter integration with the requests library and more extensive OAuth support. rauth, on the other hand, has a simpler API for basic OAuth operations but lacks some advanced features and active development.

4,878

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

Pros of Authlib

  • More actively maintained with recent updates
  • Supports a wider range of authentication protocols (OAuth 1, OAuth 2, OpenID Connect)
  • Comprehensive documentation and examples

Cons of Authlib

  • Steeper learning curve due to more complex API
  • Larger codebase, potentially leading to increased overhead

Code Comparison

Authlib example:

from authlib.integrations.requests_client import OAuth2Session

client = OAuth2Session(client_id, client_secret, scope='email')
token = client.fetch_token(token_url, authorization_response=redirect_uri)

Rauth example:

from rauth import OAuth2Service

service = OAuth2Service(client_id=client_id, client_secret=client_secret)
session = service.get_auth_session(data={'code': code, 'redirect_uri': redirect_uri})

Summary

Authlib offers a more comprehensive and up-to-date solution for authentication, supporting various protocols and providing detailed documentation. However, it may be more complex to implement and have a larger footprint. Rauth, while simpler, has not been actively maintained recently and supports fewer authentication methods. The choice between the two depends on the specific project requirements and the developer's familiarity with authentication protocols.

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

Pros of oauthlib

  • More comprehensive OAuth support, covering OAuth 1.0a and OAuth 2.0
  • Actively maintained with regular updates and bug fixes
  • Extensive documentation and examples for various use cases

Cons of oauthlib

  • Steeper learning curve due to its more complex architecture
  • Requires additional libraries for HTTP handling and request signing

Code Comparison

oauthlib:

from oauthlib.oauth2 import BackendApplicationClient
from requests_oauthlib import OAuth2Session

client = BackendApplicationClient(client_id=client_id)
oauth = OAuth2Session(client=client)
token = oauth.fetch_token(token_url=token_url, client_id=client_id, client_secret=client_secret)

rauth:

from rauth import OAuth2Service

service = OAuth2Service(client_id=client_id, client_secret=client_secret, name='example', authorize_url='https://example.com/oauth/authorize', access_token_url='https://example.com/oauth/token', base_url='https://example.com/api/')
session = service.get_auth_session(data={'grant_type': 'client_credentials'})

While both libraries provide OAuth functionality, oauthlib offers a more comprehensive solution with support for various OAuth versions and flows. However, rauth provides a simpler API that may be easier to use for basic OAuth 2.0 implementations. The choice between the two depends on the specific requirements of your project and the level of OAuth support needed.

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

Pros of python-oauth2

  • More mature and established project with a longer history
  • Supports both OAuth 1.0a and OAuth 2.0 protocols
  • Provides lower-level control over the OAuth flow

Cons of python-oauth2

  • Less actively maintained, with fewer recent updates
  • More complex API, requiring more code to implement basic OAuth flows
  • Limited documentation and examples compared to rauth

Code Comparison

python-oauth2:

consumer = oauth.Consumer(key, secret)
client = oauth.Client(consumer)
resp, content = client.request(request_token_url, "GET")
request_token = dict(urlparse.parse_qsl(content))

rauth:

service = OAuth1Service(
    name='example',
    consumer_key=key,
    consumer_secret=secret,
    request_token_url=request_token_url
)
request_token, request_token_secret = service.get_request_token()

The code comparison shows that rauth provides a more streamlined and higher-level API for OAuth operations, while python-oauth2 offers more granular control but requires more code for basic tasks. rauth's approach is generally easier for beginners and simpler use cases, while python-oauth2 might be preferred for more complex scenarios or when lower-level access is needed.

Flask user session management.

Pros of flask-login

  • Specifically designed for Flask, offering seamless integration
  • Provides session management and "remember me" functionality out of the box
  • Actively maintained with regular updates and a large community

Cons of flask-login

  • Limited to Flask applications, not suitable for other frameworks
  • Focuses solely on user session management, lacking OAuth support
  • Requires additional libraries for more advanced authentication features

Code Comparison

flask-login:

from flask_login import LoginManager, UserMixin, login_user

login_manager = LoginManager()
login_manager.init_app(app)

class User(UserMixin):
    pass

@login_manager.user_loader
def load_user(user_id):
    return User.get(user_id)

rauth:

from rauth import OAuth2Service

service = OAuth2Service(
    name='example',
    client_id='123',
    client_secret='456',
    authorize_url='https://example.com/oauth/authorize',
    access_token_url='https://example.com/oauth/token',
    base_url='https://example.com/api/'
)

session = service.get_auth_session(data={'code': 'foo'})

Summary

flask-login is tailored for Flask applications, providing robust session management. rauth, on the other hand, offers more flexibility with OAuth support across different frameworks. Choose flask-login for Flask-specific projects with straightforward authentication needs, and rauth for multi-framework applications requiring OAuth integration.

Social auth made simple

Pros of python-social-auth

  • More comprehensive, supporting a wider range of social authentication providers
  • Actively maintained with regular updates and a larger community
  • Integrates well with popular Python web frameworks like Django and Flask

Cons of python-social-auth

  • More complex setup and configuration compared to rauth
  • Steeper learning curve for beginners due to its extensive features

Code Comparison

rauth example:

from rauth import OAuth2Service

service = OAuth2Service(
    client_id='your_client_id',
    client_secret='your_client_secret',
    name='example',
    authorize_url='https://example.com/oauth/authorize',
    access_token_url='https://example.com/oauth/token',
    base_url='https://example.com/api/'
)

python-social-auth example:

SOCIAL_AUTH_AUTHENTICATION_BACKENDS = (
    'social_core.backends.google.GoogleOAuth2',
    'social_core.backends.facebook.FacebookOAuth2',
)

SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = 'your_client_id'
SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = 'your_client_secret'

python-social-auth offers a more declarative approach with built-in support for multiple providers, while rauth requires more manual configuration but provides a simpler, lower-level interface for OAuth interactions.

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

Rauth

A simple Python OAuth 1.0/a, OAuth 2.0, and Ofly consumer library built on top of Requests.

build status

Features

  • Supports OAuth 1.0/a, 2.0 and Ofly
  • Service wrappers for convenient connection initialization
  • Authenticated session objects providing nifty things like keep-alive
  • Well tested (100% coverage)
  • Built on Requests (v1.x)

Installation

To install:

$ pip install rauth

Or if you must:

$ easy_install rauth

Example Usage

Let's get a user's Twitter timeline. Start by creating a service container object:

from rauth import OAuth1Service

# Get a real consumer key & secret from https://dev.twitter.com/apps/new
twitter = OAuth1Service(
    name='twitter',
    consumer_key='J8MoJG4bQ9gcmGh8H7XhMg',
    consumer_secret='7WAscbSy65GmiVOvMU5EBYn5z80fhQkcFWSLMJJu4',
    request_token_url='https://api.twitter.com/oauth/request_token',
    access_token_url='https://api.twitter.com/oauth/access_token',
    authorize_url='https://api.twitter.com/oauth/authorize',
    base_url='https://api.twitter.com/1.1/')

Then get an OAuth 1.0 request token:

request_token, request_token_secret = twitter.get_request_token()

Go through the authentication flow. Since our example is a simple console application, Twitter will give you a PIN to enter.

authorize_url = twitter.get_authorize_url(request_token)

print 'Visit this URL in your browser: ' + authorize_url
pin = raw_input('Enter PIN from browser: ')  # `input` if using Python 3!

Exchange the authorized request token for an authenticated OAuth1Session:

session = twitter.get_auth_session(request_token,
                                   request_token_secret,
                                   method='POST',
                                   data={'oauth_verifier': pin})

And now we can fetch our Twitter timeline!

params = {'include_rts': 1,  # Include retweets
          'count': 10}       # 10 tweets

r = session.get('statuses/home_timeline.json', params=params)

for i, tweet in enumerate(r.json(), 1):
    handle = tweet['user']['screen_name']
    text = tweet['text']
    print(u'{0}. @{1} - {2}'.format(i, handle, text))

Here's the full example: examples/twitter-timeline-cli.py.

Documentation

The Sphinx-compiled documentation is available here: http://readthedocs.org/docs/rauth/en/latest/

Contribution

Anyone who would like to contribute to the project is more than welcome. Basically there's just a few steps to getting started:

  1. Fork this repo
  2. Make your changes and write a test for them
  3. Add yourself to the AUTHORS file and submit a pull request!

Note: Before you make a pull request, please run make check. If your code passes then you should be good to go! Requirements for running tests are in requirements-dev@<python-version>.txt. You may also want to run tox to ensure that nothing broke in other supported environments, e.g. Python 3.

Copyright and License

Rauth is Copyright (c) 2013 litl, LLC and licensed under the MIT license. See the LICENSE file for full details.