Metadata-Version: 2.1
Name: fastapi-authlib
Version: 0.0.4
Summary: A fastapi authlib authentication library
License: MIT
Author: qiang.xie
Author-email: qiang.xie@zncdata.com
Requires-Python: >=3.10,<4.0
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Requires-Dist: aiomysql (>=0.1.1,<0.2.0)
Requires-Dist: alembic (>=1.10.3,<2.0.0)
Requires-Dist: authlib (>=1.2.0,<2.0.0)
Requires-Dist: dynaconf (>=3.1.12,<4.0.0)
Requires-Dist: fastapi (>=0.95.0,<0.96.0)
Requires-Dist: fastapi-sa (>=0.1.0,<0.2.0)
Requires-Dist: httpx (>=0.24.0,<0.25.0)
Requires-Dist: inflection (>=0.5.1,<0.6.0)
Requires-Dist: itsdangerous (>=2.1.2,<3.0.0)
Requires-Dist: pyjwt (>=2.8.0,<3.0.0)
Requires-Dist: sqlalchemy (==1.4.46)
Requires-Dist: uvicorn (>=0.21.1,<0.22.0)
Description-Content-Type: text/markdown

# fastapi-authlib

fastapi-authlib provides easy integration between FastAPI and openid connection in your application.
Provides the initialization and dependencies of oidc, aiming to unify authentication management and
reduce the difficulty of use.

## Installing

install and update using pip:

```shell
pip install fastapi-authlib
```

## Examples

### Create settings for examples, `settings.py`

```python
config = {
    'database': 'sqlite+aiosqlite:////tmp/oidc_demo.db',
    'oauth_client_id': 'client_id',
    'oauth_client_secret': 'client_secret',
    'oauth_conf_url': 'conf_url',
    'secret_key': 'secret_key',
    'router_prefix': '',
}
```

settings.py is a simple configuration file of the use case, which mainly provides the database link,
the necessary parameters used by oidc, the session authentication key and the routing prefix.

Please use your authentication server configuration to populate the parameter value prefixed with oauth.
Other parameters can be modified according to the actual situation.

### Create api route, `api_router.py`

```python
from fastapi import APIRouter
from starlette.requests import Request

router = APIRouter()


@router.get('/index')
async def index(
        *,
        request: Request,
):
    """
    User
    """
    user_info = request.state.user
    return {'name': user_info.get('user_name')}
```

For authenticated api, you can use `request.state.user` to get the current user.

### Create oidc demo entry, `main.py`

```python
"""main"""
import uvicorn
from fastapi import Depends, FastAPI
from fastapi_sa.database import db
from fastapi_sa.middleware import DBSessionMiddleware

from fastapi_authlib.oidc import OIDCClient
from fastapi_authlib.utils.auth_dependency import check_auth_depends
from api_router import index
from .settings import config


class OIDCDemo:
    """OIDCDemo"""

    def __init__(self, settings: dict):
        self.settings = settings
        self.router_prefix = self.settings.get('router_prefix')

    def run(self):
        """Run"""
        # Early environment initialization
        app = FastAPI(title='FastAPIOIDCSupportDemo', version='0.1.0')
        db.init(self.settings.get('database'))

        # Oidc environment initialization
        client = OIDCClient(
            app=app,
            **config
        )
        # If you only init app, you should use init_app() instead
        client.init_oidc()

        # Customize the environment initialization
        # add dependencies to the interface that needs to be authenticated
        app.include_router(
            index.router,
            tags=['index'],
            prefix=config.get('router_prefix'),
            dependencies=[Depends(check_auth_depends)]
        )
        app.add_middleware(DBSessionMiddleware)
        return app


if __name__ == '__main__':
    client_app = OIDCDemo(config).run()
    uvicorn.run(client_app, host="0.0.0.0", port=8001)

```

### Use Step

- Create app and init db
- Init the environment of oidc, If you don't want to do data migration, you should use init_app method.
  Usually database migration and oidc initialization are performed together
- Register routing and other middleware, the DBSessionMiddleware is required
- Start a fastapi server with uvicorn or other

### Other

Provide the following apis
- /login
- /auth
- /logout
- /users

## Based on

- [FastAPI](https://github.com/tiangolo/fastapi)
- [SQLAlchemy](https://github.com/sqlalchemy/sqlalchemy)
- [Fastapi-sa](https://github.com/whg517/fastapi-sa)
- [Authlib](https://github.com/lepture/authlib)

## Develop

You may need to read the [develop document](./docs/development.md) to use SRC Layout in your IDE.

