Skip to main content

Don't Use

This is in super-duper early development. Stay away!

Introduction

This is a use-case specific library, enabling you to quickly get up and running with a backend integration where OAuth2 is necessary.

This package assumes you're not using Django's ORM (a SQL database) and that you are using AWS. If so, the point is to spin up a DynamoDB table with which your application can store an OAuth token from an authenticating user. This table will only have one active token at a time, which is the token of the most recent user to authenticate. Past tokens are kept around for up to one month.

This package is certainly not intended for a user-facing web-application.

Usage

Taking a looking at the example project will probably tell you everything you need to know, but here are the explicit details.

settings.py

In your settings.py, add django_serverless_oauth_session to your INSTALLED_APPS

# settings.py

INSTALLED_APPS = [
    # ...
    "django_serverless_oauth_session",
]

NOTE

By registering this app, the DynamoDB table will be created in AWS on the start-up of the app if it doesn't already exist. To turn this off, set OAUTH_TOKEN_TABLE_CREATE = False in settings.py. This might be useful if you need to add KMS keys to your table, or you'd rather provision in some other way.

Also, your environment must have your AWS credentials ready to go, just like you would have them set-up for boto3.


Set a LOGIN_REDIRECT_URL

# settings.py

LOGIN_REDIRECT_URL = "/"

And finally, fill in your OAuth provider's details, as well as some info for AWS

# settings.py

# AWS stuff
OAUTH_TOKEN_TABLE_NAME = "some-table-name"
AWS_REGION = "us-west-2"

# OAuth app stuff
OAUTH_CLIENT_ID = os.getenv('GITHUB_CLIENT_ID')
OAUTH_CLIENT_SECRET = os.getenv('GITHUB_CLIENT_SECRET')
OAUTH_ACCESS_TOKEN_URL = "https://github.com/login/oauth/access_token"
OAUTH_AUTHORIZE_URL = "https://github.com/login/oauth/authorize"
OAUTH_USER_INFO_URL = "https://api.github.com/user"
OAUTH_SCOPE = "user:email"

# optional OAuth stuff
OAUTH_INCLUDE_SCOPE_IN_REFRESH = True  # defaults to False, shouldn't be common
OAUTH_ACCESS_TOKEN_KWARGS = {
    "client_id": OAUTH_CLIENT_ID,
    "client_secret": OAUTH_CLIENT_SECRET
}  # passed when trying to obtain the token

# if the state should be passed again to the provider in the POST to obtain the token
# (this is not typical, and should be False in 99% of cases)
OAUTH_STATE_PROVIDER_CHECK = False

urls

Register the following urls in your root url conf

# urls.py
from django.urls import include, path

urlpatterns = [
    # ...
    path("oauth/", include("django_serverless_oauth_session.urls")),
]

The callback url will be the above, appended with callback, e.g., http://localhost:8000/oauth/callback

Support for custom URL callbacks will be worked on in a future version.

Getting the token

Somewhere in your site, you'll need a view with a button with which users can click to get started. Put this in your template to kick off the OAuth process.

<a href="{% url 'sls-login' %}" class="btn btn-primary">Click to OAuth</a>

Using it!

After all that set-up, you probably want to use it. The above enables to you grab an authenticated requests session that handles authenticated and token refreshing for you.

from django_serverless_oauth_session import get_oauth_session

def repos(request):
    session = get_oauth_session()
    response = session.get("https://api.github.com/user/repos")
    repos = response.json()
    return render(request, "repos.html", {"repos": repos})

This allows you to simply import this function and start making calls to your API in backend scripts and the like.

Please refer to the documentation for requests for more info on how to use the session.

Metadata

Release files for django-serverless-oauth-session 0.5.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-serverless-oauth-session 0.5.0
File Size Uploaded Provenance Actions
django-serverless-oauth-session-0.5.0.tar.gz 7.0 kB Without provenance Details for django-serverless-oauth-session-0.5.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for django-serverless-oauth-session 0.5.0
File Interpreter ABI Platform Actions
django_serverless_oauth_session-0.5.0-py3-none-any.whl Python 3 none any Details for django_serverless_oauth_session-0.5.0-py3-none-any.whl

Total release size: 15.3 kB

Release files / django-serverless-oauth-session-0.5.0.tar.gz

Download URL django-serverless-oauth-session-0.5.0.tar.gz
Size 7.0 kB
Tags Source
SHA-256 checksum
How to use checksums
e80056eab27ac381d8aea95b7ff47d025027a6f4c343318086c6a3461ad131c6
BLAKE2b-256 checksum
How to use checksums
2d2b53d9c74a3fd0f207543dbb9d6e49da406528a53ef89ba0edbef9711cbfc3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.4 CPython/3.8.7 Windows/10

Release files / django_serverless_oauth_session-0.5.0-py3-none-any.whl

Download URL django_serverless_oauth_session-0.5.0-py3-none-any.whl
Size 8.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
65adde4b9c65a4ceb1882098f6c5e231a06360b918f1032b4659e32039f8c05b
BLAKE2b-256 checksum
How to use checksums
b7fed57bfb9d69dd65aae9b772b0b9972fc6bb8225de72f6aaaec52592d28c85
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.4 CPython/3.8.7 Windows/10

Security for django-serverless-oauth-session 0.5.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page