Metadata-Version: 2.1
Name: django-serverless-oauth-session
Version: 0.2.0
Summary: Provides a Django app for storing tokens in AWS's DynamoDB, and providing a convenient requests session which uses the token. This is a use-case-specific library, and is intended for backend integrations that must authenticate with an API which only supports OAuth2 for its authentication protocol.
Home-page: https://github.com/brno32/django-serverless-oauth-session
License: MIT
Keywords: django,serverless,oauth,aws,dynamodb,requests,sessions
Author: Alex Drozd
Author-email: drozdster@gmail.com
Requires-Python: >=3.8,<4.0
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Requires-Dist: Authlib (>=0.15.3,<0.16.0)
Requires-Dist: pynamodb (>=5.0.3,<6.0.0)
Project-URL: Repository, https://github.com/brno32/django-serverless-oauth-session
Description-Content-Type: text/markdown

# 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 row, the token of the most recent user to authenticate.

# 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`

```python
# 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.

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`

```python
# settings.py

LOGIN_REDIRECT_URL = "/"
```

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

```python
# 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"
```

## urls

Register the following urls in your root url conf

```python
# urls.py

path(
    "oauth/",
    include("django_serverless_oauth_session.urls"),
),
```

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.

```html
<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.

```python
from django_serverless_oauth_session.oauth import get_oauth_session

def repos(request):
    client = get_oauth_session()
    response = client.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](https://docs.python-requests.org/en/master/) for more info on how to use
the session.

