py-auth-fastapi 0.0.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) [2026] [Olatunji Jamaldeen]
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,85 @@
1
+ Metadata-Version: 2.4
2
+ Name: py-auth-fastapi
3
+ Version: 0.0.1
4
+ Summary: FastAPI integration for py-auth-core.
5
+ Author-email: Olatunji Jamaldeen Omotoyosi <jamaldeen.o@yahoo.com>
6
+ License-Expression: MIT
7
+ Project-URL: Homepage, https://github.com/jamaldeen09/py-auth
8
+ Project-URL: Repository, https://github.com/jamaldeen09/py-auth
9
+ Classifier: Programming Language :: Python :: 3
10
+ Classifier: Programming Language :: Python :: 3.9
11
+ Classifier: Programming Language :: Python :: 3.10
12
+ Classifier: Programming Language :: Python :: 3.11
13
+ Classifier: Programming Language :: Python :: 3.12
14
+ Classifier: Programming Language :: Python :: 3.13
15
+ Classifier: Operating System :: OS Independent
16
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
17
+ Requires-Python: >=3.9
18
+ Description-Content-Type: text/markdown
19
+ License-File: LICENSE
20
+ Requires-Dist: fastapi[standard]>=0.100.0
21
+ Requires-Dist: py-auth-core>=0.0.1
22
+ Dynamic: license-file
23
+
24
+ # py-auth-fastapi
25
+
26
+ **FastAPI integration for [py-auth-core](https://pypi.org/project/py-auth-core/).**
27
+
28
+ Removes the boilerplate of wiring `py-auth-core` authentication into a FastAPI application down to a single line.
29
+
30
+ [![PyPI version](https://img.shields.io/pypi/v/py-auth-fastapi.svg)](https://pypi.org/project/py-auth-fastapi/)
31
+ [![Python versions](https://img.shields.io/pypi/pyversions/py-auth-fastapi.svg)](https://pypi.org/project/py-auth-fastapi/)
32
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
33
+
34
+ ---
35
+
36
+ ## Features
37
+
38
+ - ✅ **One-liner setup** — Mounts as a standard FastAPI `APIRouter` with `app.include_router()`
39
+ - ✅ **Automatic cookie handling** — Sets and clears session and CSRF cookies automatically
40
+ - ✅ **Dependency injection** — Includes `get_current_session()` dependency factory for protecting endpoints
41
+ - ✅ **Built-in error handling** — Converts `py-auth-core` errors into standard FastAPI `HTTPException` responses
42
+
43
+ ---
44
+
45
+ ## Installation
46
+
47
+ ```bash
48
+ pip install py-auth-fastapi
49
+ ```
50
+
51
+ ---
52
+
53
+ ## Quick Start
54
+
55
+ ```python
56
+ from fastapi import FastAPI, Depends
57
+ from py_auth import PyAuth
58
+ from py_auth_fastapi import PyAuthFastAPI
59
+
60
+ # Initialize your PyAuth instance
61
+ auth = PyAuth(...)
62
+
63
+ app = FastAPI()
64
+
65
+ # Mount auth router (prefix="/auth" and tags=["Authentication"] by default)
66
+ auth_router = PyAuthFastAPI(auth)
67
+ app.include_router(auth_router)
68
+
69
+ # Protect routes using the session dependency
70
+ @app.get("/protected")
71
+ async def protected_route(session=Depends(auth_router.get_current_session())):
72
+ return {"message": f"Hello user {session['user_id']}"}
73
+ ```
74
+
75
+ ### Endpoints Registered
76
+
77
+ - `POST /auth/signin` — Authenticates credentials and sets session & CSRF cookies
78
+ - `POST /auth/signout` — Revokes session and deletes cookies
79
+ - `GET /auth/session` — Fetches current session and rotates CSRF token
80
+
81
+ ---
82
+
83
+ ## License
84
+
85
+ [MIT](LICENSE)
@@ -0,0 +1,62 @@
1
+ # py-auth-fastapi
2
+
3
+ **FastAPI integration for [py-auth-core](https://pypi.org/project/py-auth-core/).**
4
+
5
+ Removes the boilerplate of wiring `py-auth-core` authentication into a FastAPI application down to a single line.
6
+
7
+ [![PyPI version](https://img.shields.io/pypi/v/py-auth-fastapi.svg)](https://pypi.org/project/py-auth-fastapi/)
8
+ [![Python versions](https://img.shields.io/pypi/pyversions/py-auth-fastapi.svg)](https://pypi.org/project/py-auth-fastapi/)
9
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
10
+
11
+ ---
12
+
13
+ ## Features
14
+
15
+ - ✅ **One-liner setup** — Mounts as a standard FastAPI `APIRouter` with `app.include_router()`
16
+ - ✅ **Automatic cookie handling** — Sets and clears session and CSRF cookies automatically
17
+ - ✅ **Dependency injection** — Includes `get_current_session()` dependency factory for protecting endpoints
18
+ - ✅ **Built-in error handling** — Converts `py-auth-core` errors into standard FastAPI `HTTPException` responses
19
+
20
+ ---
21
+
22
+ ## Installation
23
+
24
+ ```bash
25
+ pip install py-auth-fastapi
26
+ ```
27
+
28
+ ---
29
+
30
+ ## Quick Start
31
+
32
+ ```python
33
+ from fastapi import FastAPI, Depends
34
+ from py_auth import PyAuth
35
+ from py_auth_fastapi import PyAuthFastAPI
36
+
37
+ # Initialize your PyAuth instance
38
+ auth = PyAuth(...)
39
+
40
+ app = FastAPI()
41
+
42
+ # Mount auth router (prefix="/auth" and tags=["Authentication"] by default)
43
+ auth_router = PyAuthFastAPI(auth)
44
+ app.include_router(auth_router)
45
+
46
+ # Protect routes using the session dependency
47
+ @app.get("/protected")
48
+ async def protected_route(session=Depends(auth_router.get_current_session())):
49
+ return {"message": f"Hello user {session['user_id']}"}
50
+ ```
51
+
52
+ ### Endpoints Registered
53
+
54
+ - `POST /auth/signin` — Authenticates credentials and sets session & CSRF cookies
55
+ - `POST /auth/signout` — Revokes session and deletes cookies
56
+ - `GET /auth/session` — Fetches current session and rotates CSRF token
57
+
58
+ ---
59
+
60
+ ## License
61
+
62
+ [MIT](LICENSE)
@@ -0,0 +1,37 @@
1
+ [build-system]
2
+ requires = ["setuptools>=61.0.0", "wheel"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "py-auth-fastapi"
7
+ version = "0.0.1"
8
+ authors = [
9
+ { name = "Olatunji Jamaldeen Omotoyosi", email = "jamaldeen.o@yahoo.com" },
10
+ ]
11
+ description = "FastAPI integration for py-auth-core."
12
+ readme = "README.md"
13
+ requires-python = ">=3.9"
14
+ classifiers = [
15
+ "Programming Language :: Python :: 3",
16
+ "Programming Language :: Python :: 3.9",
17
+ "Programming Language :: Python :: 3.10",
18
+ "Programming Language :: Python :: 3.11",
19
+ "Programming Language :: Python :: 3.12",
20
+ "Programming Language :: Python :: 3.13",
21
+ "Operating System :: OS Independent",
22
+ "Topic :: Software Development :: Libraries :: Python Modules",
23
+ ]
24
+ license = "MIT"
25
+ license-files = ["LICENSE"]
26
+
27
+ dependencies = [
28
+ "fastapi[standard]>=0.100.0",
29
+ "py-auth-core>=0.0.1",
30
+ ]
31
+
32
+ [project.urls]
33
+ "Homepage" = "https://github.com/jamaldeen09/py-auth"
34
+ "Repository" = "https://github.com/jamaldeen09/py-auth"
35
+
36
+ [tool.setuptools.packages.find]
37
+ where = ["src"]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,8 @@
1
+ """
2
+ py-auth-fastapi: FastAPI integration for py-auth.
3
+ """
4
+
5
+ from .core import PyAuthFastAPI
6
+
7
+ __all__ = ["PyAuthFastAPI"]
8
+ __version__ = "0.0.1"
@@ -0,0 +1,121 @@
1
+ from enum import Enum
2
+ from typing import Any, List, Optional, Union
3
+ from fastapi import APIRouter, Request, Response, Depends
4
+ from py_auth import PyAuth
5
+ from py_auth.schemas import CookieConfig
6
+ from .utils import raise_auth_exception
7
+
8
+ class PyAuthFastAPI(APIRouter):
9
+ def __init__(
10
+ self,
11
+ auth: PyAuth,
12
+ *,
13
+ prefix: str = "/auth",
14
+ tags: Optional[List[Union[str, Enum]]] = None,
15
+ **kwargs: Any,
16
+ ):
17
+ if tags is None:
18
+ tags = ["Authentication"]
19
+
20
+ super().__init__(prefix=prefix, tags=tags, **kwargs)
21
+ self.auth = auth
22
+ self._register_routes()
23
+
24
+ def _set_auth_cookie(
25
+ self, response: Response, cookie_config: CookieConfig, value: str
26
+ ):
27
+ """Reusable helper to apply cookie configurations dynamically."""
28
+ options = cookie_config["options"]
29
+ response.set_cookie(
30
+ key=cookie_config["name"],
31
+ value=value,
32
+ httponly=options.get("http_only"),
33
+ secure=options.get("secure"),
34
+ samesite=options.get("same_site"),
35
+ path=options.get("path"),
36
+ domain=options.get("domain"),
37
+ max_age=options.get("max_age"),
38
+ expires=options.get("expires"),
39
+ )
40
+
41
+ def _clear_auth_cookie(self, response: Response, cookie_config: CookieConfig):
42
+ """Reusable helper to clear auth cookies with matching attributes."""
43
+ options = cookie_config["options"]
44
+ response.delete_cookie(
45
+ key=cookie_config["name"],
46
+ path=options.get("path", "/"),
47
+ domain=options.get("domain"),
48
+ secure=options.get("secure", True),
49
+ httponly=options.get("http_only", False),
50
+ samesite=options.get("same_site", "lax"),
51
+ )
52
+
53
+ def get_current_session(self):
54
+ """Factory method returning a valid FastAPI dependency closure."""
55
+
56
+ async def dependency(request: Request):
57
+ session_token = request.cookies.get(
58
+ self.auth.cookies["session_token"]["name"]
59
+ )
60
+ csrf_token = request.cookies.get(self.auth.cookies["csrf_token"]["name"])
61
+ result = await self.auth.verify_session(session_token, csrf_token)
62
+
63
+ if result.get("error"):
64
+ raise_auth_exception(result["error"])
65
+
66
+ return result["data"]["session"]
67
+
68
+ return dependency
69
+
70
+ def _register_routes(self):
71
+ @self.post("/signin")
72
+ async def signin(request: Request, response: Response):
73
+ request_body = await request.json()
74
+ result = await self.auth.signin_with_credentials(request_body)
75
+
76
+ if result.get("error"):
77
+ raise_auth_exception(result["error"])
78
+
79
+ data = result["data"]
80
+ self._set_auth_cookie(
81
+ response, self.auth.cookies["session_token"], data["session_token"]
82
+ )
83
+ self._set_auth_cookie(
84
+ response, self.auth.cookies["csrf_token"], data["csrf_token"]
85
+ )
86
+
87
+ return {"success": True, "message": "You have successfully signed in."}
88
+
89
+ @self.post("/signout")
90
+ async def signout(
91
+ response: Response, session=Depends(self.get_current_session())
92
+ ):
93
+ session_id = session["id"]
94
+ await self.auth.signout(session_id)
95
+ self._clear_auth_cookie(response, self.auth.cookies["session_token"])
96
+ self._clear_auth_cookie(response, self.auth.cookies["csrf_token"])
97
+ return {"success": True, "message": "You have successfully signed out."}
98
+
99
+ @self.get("/session")
100
+ async def get_session(
101
+ response: Response, session=Depends(self.get_current_session())
102
+ ):
103
+ """Refreshes/fetches session data and rotates the CSRF token if needed."""
104
+ result = await self.auth.update_session_csrf_token(session["id"])
105
+
106
+ if result.get("error"):
107
+ raise_auth_exception(result["error"])
108
+
109
+ csrf_cookie = self.auth.cookies["csrf_token"]
110
+ self._set_auth_cookie(response, csrf_cookie, result["data"]["csrf_token"])
111
+
112
+ session_dict = {
113
+ "id": session.get("id", None),
114
+ "user_id": session.get("user_id", None),
115
+ "expires": session.get("expires", None),
116
+ }
117
+ return {
118
+ "success": True,
119
+ "message": "Session is active.",
120
+ "session": session_dict,
121
+ }
@@ -0,0 +1 @@
1
+ # Marker file for PEP 561.
@@ -0,0 +1,18 @@
1
+ from py_auth.schemas import AuthError
2
+ from fastapi.exceptions import HTTPException
3
+
4
+ def raise_auth_exception(error: AuthError):
5
+ """Helper to cleanly translate py-auth errors into FastAPI HTTPExceptions."""
6
+ status_code = error.get("status_code", 500)
7
+ message = error.get("message", "Something went wrong.")
8
+ code = error.get("code", "AUTH_ERROR")
9
+ details = error.get("details", {})
10
+
11
+ raise HTTPException(
12
+ status_code=status_code,
13
+ detail={
14
+ "success": False,
15
+ "message": message,
16
+ "error": {"status_code": status_code, "code": code, "details": details},
17
+ },
18
+ )
@@ -0,0 +1,85 @@
1
+ Metadata-Version: 2.4
2
+ Name: py-auth-fastapi
3
+ Version: 0.0.1
4
+ Summary: FastAPI integration for py-auth-core.
5
+ Author-email: Olatunji Jamaldeen Omotoyosi <jamaldeen.o@yahoo.com>
6
+ License-Expression: MIT
7
+ Project-URL: Homepage, https://github.com/jamaldeen09/py-auth
8
+ Project-URL: Repository, https://github.com/jamaldeen09/py-auth
9
+ Classifier: Programming Language :: Python :: 3
10
+ Classifier: Programming Language :: Python :: 3.9
11
+ Classifier: Programming Language :: Python :: 3.10
12
+ Classifier: Programming Language :: Python :: 3.11
13
+ Classifier: Programming Language :: Python :: 3.12
14
+ Classifier: Programming Language :: Python :: 3.13
15
+ Classifier: Operating System :: OS Independent
16
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
17
+ Requires-Python: >=3.9
18
+ Description-Content-Type: text/markdown
19
+ License-File: LICENSE
20
+ Requires-Dist: fastapi[standard]>=0.100.0
21
+ Requires-Dist: py-auth-core>=0.0.1
22
+ Dynamic: license-file
23
+
24
+ # py-auth-fastapi
25
+
26
+ **FastAPI integration for [py-auth-core](https://pypi.org/project/py-auth-core/).**
27
+
28
+ Removes the boilerplate of wiring `py-auth-core` authentication into a FastAPI application down to a single line.
29
+
30
+ [![PyPI version](https://img.shields.io/pypi/v/py-auth-fastapi.svg)](https://pypi.org/project/py-auth-fastapi/)
31
+ [![Python versions](https://img.shields.io/pypi/pyversions/py-auth-fastapi.svg)](https://pypi.org/project/py-auth-fastapi/)
32
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
33
+
34
+ ---
35
+
36
+ ## Features
37
+
38
+ - ✅ **One-liner setup** — Mounts as a standard FastAPI `APIRouter` with `app.include_router()`
39
+ - ✅ **Automatic cookie handling** — Sets and clears session and CSRF cookies automatically
40
+ - ✅ **Dependency injection** — Includes `get_current_session()` dependency factory for protecting endpoints
41
+ - ✅ **Built-in error handling** — Converts `py-auth-core` errors into standard FastAPI `HTTPException` responses
42
+
43
+ ---
44
+
45
+ ## Installation
46
+
47
+ ```bash
48
+ pip install py-auth-fastapi
49
+ ```
50
+
51
+ ---
52
+
53
+ ## Quick Start
54
+
55
+ ```python
56
+ from fastapi import FastAPI, Depends
57
+ from py_auth import PyAuth
58
+ from py_auth_fastapi import PyAuthFastAPI
59
+
60
+ # Initialize your PyAuth instance
61
+ auth = PyAuth(...)
62
+
63
+ app = FastAPI()
64
+
65
+ # Mount auth router (prefix="/auth" and tags=["Authentication"] by default)
66
+ auth_router = PyAuthFastAPI(auth)
67
+ app.include_router(auth_router)
68
+
69
+ # Protect routes using the session dependency
70
+ @app.get("/protected")
71
+ async def protected_route(session=Depends(auth_router.get_current_session())):
72
+ return {"message": f"Hello user {session['user_id']}"}
73
+ ```
74
+
75
+ ### Endpoints Registered
76
+
77
+ - `POST /auth/signin` — Authenticates credentials and sets session & CSRF cookies
78
+ - `POST /auth/signout` — Revokes session and deletes cookies
79
+ - `GET /auth/session` — Fetches current session and rotates CSRF token
80
+
81
+ ---
82
+
83
+ ## License
84
+
85
+ [MIT](LICENSE)
@@ -0,0 +1,12 @@
1
+ LICENSE
2
+ README.md
3
+ pyproject.toml
4
+ src/py_auth_fastapi/__init__.py
5
+ src/py_auth_fastapi/core.py
6
+ src/py_auth_fastapi/py.typed
7
+ src/py_auth_fastapi/utils.py
8
+ src/py_auth_fastapi.egg-info/PKG-INFO
9
+ src/py_auth_fastapi.egg-info/SOURCES.txt
10
+ src/py_auth_fastapi.egg-info/dependency_links.txt
11
+ src/py_auth_fastapi.egg-info/requires.txt
12
+ src/py_auth_fastapi.egg-info/top_level.txt
@@ -0,0 +1,2 @@
1
+ fastapi[standard]>=0.100.0
2
+ py-auth-core>=0.0.1
@@ -0,0 +1 @@
1
+ py_auth_fastapi