Metadata-Version: 2.4
Name: psp-auth
Version: 1.0.0
Summary: A Python library for authentication and authorisation in the PSP platform
License: MIT License
        
        Copyright (c) 2025 Portfolio Solver Platform
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        
Project-URL: Homepage, https://github.com/Portfolio-Solver-Platform/python-auth-lib
Keywords: psp,auth,authentication,authorisation,authorization
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3
Requires-Python: >=3.12
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: joserfc<2.0,>=1.3.4
Requires-Dist: fastapi[standard]<0.121,>=0.120.1
Requires-Dist: httpx<0.29.0,>=0.28.1
Requires-Dist: itsdangerous<3.0,>=2.2.0
Provides-Extra: dev
Requires-Dist: pytest<9,>=8; extra == "dev"
Requires-Dist: pytest-cov<8,>=7; extra == "dev"
Requires-Dist: ruff<0.14.0,>=0.13.2; extra == "dev"
Requires-Dist: bandit<2.0,>=1.7; extra == "dev"
Requires-Dist: pip-audit<3.0,>=2.7; extra == "dev"
Dynamic: license-file

# Auth Library for Python

A library that implements the authentication and authorisation for services. It assumes that you use FastAPI.

## Installation

WIP

## Usage

First, create the auth:
```python
auth = Auth(AuthConfig("my-service"))
```
, where `"my-service"` is the service name, corresponding with the resource in the auth provider.

> [!IMPORTANT]
> See the [user service](https://github.com/Portfolio-Solver-Platform/user) for how to set up the resource.

Then, for an endpoint, you can require that the user has a role:
```python
@app.get("/protected-route")
@auth.require_role("my-role"):
def protected_route(request: Request):
    # (...)
```

> [!IMPORTANT]
> You NEED to have the `request: Request` parameter to the function.
> Additionally, `@auth.require_role(...)` has to be after `@app.get(...)`.

You can also require that the user has one out of a list of roles: `auth.require_any_role(["first-role", "second-role", ...])`
You can also require that the user has multiple roles: `auth.require_all_roles(["first-role", "second-role", ...])`

> [!NOTE]
> There are also similar decorators for checking roles on other resources than your own, for example: `auth.require_resource_role("other-service", "their-role")`.

If you need to access the user's information in an endpoint, you can do:
```python
def protected_route(request: Request):
    token = auth.get_token(request)
    user = token.user()
    # user has information, like `user.id()` and `user.full_name()`.
```
