fastapi-tj-tpauth-jwt


Namefastapi-tj-tpauth-jwt JSON
Version 1.0.0 PyPI version JSON
download
home_pagehttps://github.com/duynguyen02/fastapi-tj-tpauth-jwt
SummaryIntegrate JWT into FastAPI using TP Servers authentication.
upload_time2024-10-18 19:04:24
maintainerNone
docs_urlNone
authorDuy Nguyen
requires_python<4.0,>=3.9
licenseNone
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # fastapi-tj-tpauth-jwt

![PyPI - Version](https://img.shields.io/pypi/v/fastapi-tj-tpauth-jwt)

Integrate JWT into FastAPI using TP Servers authentication.

## Installation

```bash
pip install fastapi-tj-tpauth-jwt
```

## Usage

### Basic Example

```python
from datetime import timedelta
from typing import Annotated

from fastapi import FastAPI, Depends
from tj_tpauth import TJTPAuth, TPAuthData

from fastapi_tj_tpauth_jwt.tpauth_jwt import TPAuthJWT

app = FastAPI()

tpauth = TJTPAuth(
    host="localhost:8080"
)

tpauth_jwt = TPAuthJWT(
    tp_auth=tpauth,
    secret_key="<SECRET_KEY>",
    refresh_secret_key="<SECRET_KEY>",
    access_token_expires_in=timedelta(minutes=60),
    refresh_token_expires_in=timedelta(minutes=120),
    algorithm='HS256'
)


@app.get("/")
async def root():
    return {"message": "Hello World"}


@app.post("/login")
async def login(
        login_res: Annotated[
            tpauth_jwt.provide_login_handler(), Depends()
        ]
):
    return login_res


@app.post("/refresh")
async def refresh(
        refresh_res: Annotated[
            tpauth_jwt.provide_refresh_token_handler(), Depends()
        ]
):
    return refresh_res


@app.get("/secret_data")
async def secret_data(
        payload: Annotated[
            tpauth_jwt.provide_require_jwt(), Depends()
        ]
):
    if not isinstance(payload, TPAuthData):
        return payload

    payload: TPAuthData

    return {
        "payload": payload.id,
    }

```

### Custom Unauthorized Error Response

```python
def unauthorized_response_provider(tpauth_status: TPAuthStatus):
    return {
        "error": "Unauthorized",
        "status": tpauth_status.error.value
    }


tpauth_jwt.unauthorized_response_provider = unauthorized_response_provider
```

### Custom JWT Response

```python
def jwt_provider(access_token: str, refresh_token: str, token_type: str):
    return {
        "access_token": access_token,
        "refresh_token": refresh_token,
        "token_type": token_type
    }


tpauth_jwt.jwt_response_provider = jwt_provider
```

## License

This library is released under the MIT License.

## Contact

If you have any questions or issues, please open an issue
on [GitHub](https://github.com/duynguyen02/fastapi-tj-tpauth-jwt/issues) or
email us at [duynguyen02.dev@gmail.com](mailto:duynguyen02.dev@gmail.com).
            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/duynguyen02/fastapi-tj-tpauth-jwt",
    "name": "fastapi-tj-tpauth-jwt",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4.0,>=3.9",
    "maintainer_email": null,
    "keywords": null,
    "author": "Duy Nguyen",
    "author_email": "duynguyen02.dev@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/34/d7/adac3525faa29d16a284390f8d383ef51554fc0af91dcd0c3e403083ab14/fastapi_tj_tpauth_jwt-1.0.0.tar.gz",
    "platform": null,
    "description": "# fastapi-tj-tpauth-jwt\n\n![PyPI - Version](https://img.shields.io/pypi/v/fastapi-tj-tpauth-jwt)\n\nIntegrate JWT into FastAPI using TP Servers authentication.\n\n## Installation\n\n```bash\npip install fastapi-tj-tpauth-jwt\n```\n\n## Usage\n\n### Basic Example\n\n```python\nfrom datetime import timedelta\nfrom typing import Annotated\n\nfrom fastapi import FastAPI, Depends\nfrom tj_tpauth import TJTPAuth, TPAuthData\n\nfrom fastapi_tj_tpauth_jwt.tpauth_jwt import TPAuthJWT\n\napp = FastAPI()\n\ntpauth = TJTPAuth(\n    host=\"localhost:8080\"\n)\n\ntpauth_jwt = TPAuthJWT(\n    tp_auth=tpauth,\n    secret_key=\"<SECRET_KEY>\",\n    refresh_secret_key=\"<SECRET_KEY>\",\n    access_token_expires_in=timedelta(minutes=60),\n    refresh_token_expires_in=timedelta(minutes=120),\n    algorithm='HS256'\n)\n\n\n@app.get(\"/\")\nasync def root():\n    return {\"message\": \"Hello World\"}\n\n\n@app.post(\"/login\")\nasync def login(\n        login_res: Annotated[\n            tpauth_jwt.provide_login_handler(), Depends()\n        ]\n):\n    return login_res\n\n\n@app.post(\"/refresh\")\nasync def refresh(\n        refresh_res: Annotated[\n            tpauth_jwt.provide_refresh_token_handler(), Depends()\n        ]\n):\n    return refresh_res\n\n\n@app.get(\"/secret_data\")\nasync def secret_data(\n        payload: Annotated[\n            tpauth_jwt.provide_require_jwt(), Depends()\n        ]\n):\n    if not isinstance(payload, TPAuthData):\n        return payload\n\n    payload: TPAuthData\n\n    return {\n        \"payload\": payload.id,\n    }\n\n```\n\n### Custom Unauthorized Error Response\n\n```python\ndef unauthorized_response_provider(tpauth_status: TPAuthStatus):\n    return {\n        \"error\": \"Unauthorized\",\n        \"status\": tpauth_status.error.value\n    }\n\n\ntpauth_jwt.unauthorized_response_provider = unauthorized_response_provider\n```\n\n### Custom JWT Response\n\n```python\ndef jwt_provider(access_token: str, refresh_token: str, token_type: str):\n    return {\n        \"access_token\": access_token,\n        \"refresh_token\": refresh_token,\n        \"token_type\": token_type\n    }\n\n\ntpauth_jwt.jwt_response_provider = jwt_provider\n```\n\n## License\n\nThis library is released under the MIT License.\n\n## Contact\n\nIf you have any questions or issues, please open an issue\non [GitHub](https://github.com/duynguyen02/fastapi-tj-tpauth-jwt/issues) or\nemail us at [duynguyen02.dev@gmail.com](mailto:duynguyen02.dev@gmail.com).",
    "bugtrack_url": null,
    "license": null,
    "summary": "Integrate JWT into FastAPI using TP Servers authentication.",
    "version": "1.0.0",
    "project_urls": {
        "Homepage": "https://github.com/duynguyen02/fastapi-tj-tpauth-jwt",
        "Repository": "https://github.com/duynguyen02/fastapi-tj-tpauth-jwt"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "826b43629f2f82107d6b35339622cf40ee0b0f73eb83606306da076c449fb472",
                "md5": "30d0e2a1504e39e8f9f4ba6d1d92612f",
                "sha256": "418c28e71ae5fc9ca362801e1c3dde2522b85fc112ed78ec2ff22fe96c49a67a"
            },
            "downloads": -1,
            "filename": "fastapi_tj_tpauth_jwt-1.0.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "30d0e2a1504e39e8f9f4ba6d1d92612f",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0,>=3.9",
            "size": 4923,
            "upload_time": "2024-10-18T19:04:22",
            "upload_time_iso_8601": "2024-10-18T19:04:22.924348Z",
            "url": "https://files.pythonhosted.org/packages/82/6b/43629f2f82107d6b35339622cf40ee0b0f73eb83606306da076c449fb472/fastapi_tj_tpauth_jwt-1.0.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "34d7adac3525faa29d16a284390f8d383ef51554fc0af91dcd0c3e403083ab14",
                "md5": "630db9c81a506429e726f59cb09f74c2",
                "sha256": "01c9b2c678c0517b0f45affcbf884748e310fe662a9f253cede4515d683d397d"
            },
            "downloads": -1,
            "filename": "fastapi_tj_tpauth_jwt-1.0.0.tar.gz",
            "has_sig": false,
            "md5_digest": "630db9c81a506429e726f59cb09f74c2",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0,>=3.9",
            "size": 3883,
            "upload_time": "2024-10-18T19:04:24",
            "upload_time_iso_8601": "2024-10-18T19:04:24.272195Z",
            "url": "https://files.pythonhosted.org/packages/34/d7/adac3525faa29d16a284390f8d383ef51554fc0af91dcd0c3e403083ab14/fastapi_tj_tpauth_jwt-1.0.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-10-18 19:04:24",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "duynguyen02",
    "github_project": "fastapi-tj-tpauth-jwt",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "requirements": [],
    "lcname": "fastapi-tj-tpauth-jwt"
}
        
Elapsed time: 0.32184s