Metadata-Version: 2.1
Name: quart-authlib
Version: 0.0.2
Summary: Quart extension for the Authlib library
License: BSD 3-Clause License
        
        Copyright (c) 2025, Michael Czigler
        
        Redistribution and use in source and binary forms, with or without
        modification, are permitted provided that the following conditions are met:
        
        1. Redistributions of source code must retain the above copyright notice, this
           list of conditions and the following disclaimer.
        
        2. Redistributions in binary form must reproduce the above copyright notice,
           this list of conditions and the following disclaimer in the documentation
           and/or other materials provided with the distribution.
        
        3. Neither the name of the copyright holder nor the names of its
           contributors may be used to endorse or promote products derived from
           this software without specific prior written permission.
        
        THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
        AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
        IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
        DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
        FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
        DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
        SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
        CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
        OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
        OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
        
Project-URL: repository, https://github.com/mcpcpc/quart-authlib
Project-URL: changelog, https://github.com/mcpcpc/quart-authlib/blob/master/CHANGELOG.md
Classifier: Environment :: Web Environment
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: BSD License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Internet :: WWW/HTTP :: Dynamic Content
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Requires-Python: >=3.11
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: authlib
Requires-Dist: requests
Requires-Dist: quart
Provides-Extra: cache
Requires-Dist: cachelib; extra == "cache"
Provides-Extra: dev
Requires-Dist: black; extra == "dev"

# Quart-Authlib

A Quart extension for the Authlib library. This is an adoption from the existing Authlib Flask Client.  Since the primary Authlib developer rejected previous client integration attempts, this project will remain under independent development.

**Important**: This project is in *early* development. Most features are considered unstable or non-functioning; thus, I would **strongly** advise against using any version less than 1.0 for production environments.

## Install

### PyPI

Install and update using pip:

```shell
pip install -U quart-authlib
```

### Repository

When using git, clone the repository and change your present working directory.

```shell
git clone http://github.com/mcpcpc/quart-authlib
cd quart_authlib/
```

Create and activate a virtual environment.

```shell
python3 -m venv venv
source venv/bin/activate
```

Install to the virtual environment.

```shell
pip install -e .
```

## Usage

Quart OAuth client can handle OAuth 1 and OAuth 2 services. It shares a similar API with Quart-OAuthlib, you can transfer your code from Quart-Authlib to Authlib with ease.

Create a registry with OAuth object:

```python
from quart_authlib import OAuth

oauth = OAuth(app)
```

You can also initialize it later with `init_app()` method:

```python
oauth = OAuth()
oauth.init_app(app)
```

The common use case for OAuth is authentication, e.g. let your users log in with Twitter, GitHub, Google etc.

## Configuration

Quart-Authlib OAuth registry can load the configuration from Quart `app.config` automatically. Every key-value pair in `.register` can be omitted. They can be configured in your Quart App configuration. Config keys are formatted as `{name}_{key}` in uppercase. If you register your remote app as `oauth.register('example', ...)`, the config keys would look like:

| Name                        | Key                                                        |
| --------------------------- | ---------------------------------------------------------- |
| EXAMPLE_CLIENT_ID           | OAuth Consumer Key                                         |
| EXAMPLE_CLIENT_SECRET       | OAuth Consumer Secret                                      |
| EXAMPLE_ACCESS_TOKEN_URL    | URL to fetch OAuth access token                            |

Here is a full list of the configuration keys:

* {name}_CLIENT_ID: Client key of OAuth 1, or Client ID of OAuth 2
* {name}_CLIENT_SECRET: Client secret of OAuth 2, or Client Secret of OAuth 2
* {name}_REQUEST_TOKEN_URL: Request Token endpoint for OAuth 1
* {name}_REQUEST_TOKEN_PARAMS: Extra parameters for Request Token endpoint
* {name}_ACCESS_TOKEN_URL: Access Token endpoint for OAuth 1 and OAuth 2
* {name}_ACCESS_TOKEN_PARAMS: Extra parameters for Access Token endpoint
* {name}_AUTHORIZE_URL: Endpoint for user authorization of OAuth 1 or OAuth 2
* {name}_AUTHORIZE_PARAMS: Extra parameters for Authorization Endpoint.
* {name}_API_BASE_URL: A base URL endpoint to make requests simple
* {name}_CLIENT_KWARGS: Extra keyword arguments for OAuth1Session or OAuth2Session

We suggest that you keep ONLY `{name}_CLIENT_ID` and `{name}_CLIENT_SECRET` in your Quart application configuration.

### Routes for Authorization

Routes for authorization should look like:

```python
from flask import url_for, redirect

@app.route('/login')
def login():
    redirect_uri = url_for('authorize', _external=True)
    return oauth.twitter.authorize_redirect(redirect_uri)

@app.route('/authorize')
def authorize():
    token = oauth.twitter.authorize_access_token()
    resp = oauth.twitter.get('account/verify_credentials.json')
    resp.raise_for_status()
    profile = resp.json()
    # do something with the token and profile
    return redirect('/')
```

### Accessing OAuth Resources

Just like above example, we don’t need to pass the `request` parameter, everything is handled by Authlib automatically:

```python
from flask import render_template

@app.route('/github')
def show_github_profile():
    resp = oauth.github.get('user')
    resp.raise_for_status()
    profile = resp.json()
    return render_template('github.html', profile=profile)
```

In this case, our `fetch_token` could look like:

```python
from your_project import current_user

def fetch_token(name):
    if name in OAUTH1_SERVICES:
        model = OAuth1Token
    else:
        model = OAuth2Token
    token = model.find(
        name=name,
        user=current_user,
    )
    return token.to_token()

# initialize the OAuth registry with this fetch_token function
oauth = OAuth(fetch_token=fetch_token)
```

### OpenID Connect Client

An OpenID Connect client is no different than a normal OAuth 2.0 client. When registered with `openid` scope, the built-in OAuth client will handle everything automatically:

```python
oauth.register(
    'google',
    ...
    server_metadata_url='https://accounts.google.com/.well-known/openid-configuration',
    client_kwargs={'scope': 'openid profile email'}
)
```

When we get the returned token:

```python
token = oauth.google.authorize_access_token()
```

There should be a `id_token` in the response. Authlib has called *.parse_id_token* automatically, we can get `userinfo` in the `token`:

```python
userinfo = token['userinfo']
```

## Examples

See the [examples](docs/examples) directory.


## Contributing

Please create a new [Issue](https://github.com/mcpcpc/quart-authlib/issues/new) or [Pull Request](https://github.com/mcpcpc/quart-authlib/compare).

## Resources

* https://authlib.org
* https://quart.palletsprojects.com/en/latest/
* https://github.com/lepture/authlib/issues/429
* https://docs.authlib.org/en/latest/client/flask.html
* https://github.com/authlib/demo-oauth-client/tree/master
