|
1 | | -# Authx |
| 1 | +# AuthX |
2 | 2 |
|
3 | 3 | <p align="center"> |
4 | 4 | <a href="https://authx.yezz.me" target="_blank"> |
|
11 | 11 |
|
12 | 12 | --- |
13 | 13 |
|
14 | | -| Project | Status | |
15 | | -|---------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| |
16 | | -| CI | [  ]( https://github.com/yezz123/authx/actions/workflows/ci.yml ) [  ]( https://results.pre-commit.ci/latest/github/yezz123/authx/main ) [  ]( https://codecov.io/gh/yezz123/authx ) | |
17 | | -| Meta | [  ]( https://pypi.org/project/authx ) [  ]( https://pepy.tech/project/authx ) [  ]( https://pydantic.dev ) [  ]( https://github.com/astral-sh/ruff ) [](https://sonarcloud.io/summary/new_code?id=yezz123_authx) | |
| 14 | +| Project | Status | |
| 15 | +|---------|--------| |
| 16 | +| CI | [](https://github.com/yezz123/authx/actions/workflows/ci.yml) [](https://results.pre-commit.ci/latest/github/yezz123/authx/main) [](https://codecov.io/gh/yezz123/authx) | |
| 17 | +| Meta | [](https://pypi.org/project/authx) [](https://pepy.tech/project/authx) [](https://pydantic.dev) [](https://github.com/astral-sh/ruff) [](https://sonarcloud.io/summary/new_code?id=yezz123_authx) | |
18 | 18 |
|
19 | 19 | --- |
20 | 20 |
|
|
24 | 24 |
|
25 | 25 | --- |
26 | 26 |
|
27 | | -Add a Fully registration and authentication or authorization system to your |
28 | | -[FastAPI](https://fastapi.tiangolo.com/) project. **AuthX** is designed to be as |
29 | | -customizable and adaptable as possible. |
| 27 | +Add a fully featured authentication and authorization system to your [FastAPI](https://fastapi.tiangolo.com/) project. **AuthX** is designed to be simple, customizable, and secure. |
30 | 28 |
|
31 | | -## Features |
32 | | - |
33 | | -- [x] Support Python 3.8+ & Pydantic 1.7+. |
34 | | -- [x] Multiple customizable authentication backend: |
35 | | - - [x] JWT authentication backend included |
36 | | - - [x] JWT encoding/decoding for application authentication |
37 | | - - [x] Automatic detection of JWTs in requests: |
38 | | - - [x] JWTs in headers |
39 | | - - [x] JWTs in cookies |
40 | | - - [x] JWTs in query parameters |
41 | | - - [x] JWTs in request bodies |
42 | | - - [x] Cookie authentication backend included |
43 | | -- [x] Middleware for authentication and authorization through JWT. |
44 | | -- [x] Extensible Error Handling System. |
45 | | - |
46 | | -### Extra Features |
47 | | - |
48 | | -AuthX is designed to be as customizable and adaptable as possible. |
49 | | - |
50 | | -So you need to install [`authx-extra`](https://github.com/yezz123/authx-extra) to get extra features. |
51 | | - |
52 | | -- [x] Using Redis as a session store & cache. |
53 | | -- [x] Support HTTPCache. |
54 | | -- [x] Support Sessions and Pre-built CRUD functions and Instance to launch Redis. |
55 | | -- [x] Support Middleware of [pyinstrument](https://pyinstrument.readthedocs.io/) to check your service performance. |
56 | | -- [x] Support Middleware for collecting and exposing [Prometheus](https://prometheus.io/) metrics. |
57 | | - |
58 | | -**Note:** Check [Release Notes](https://authx.yezz.me/release/). |
| 29 | +## Installation |
59 | 30 |
|
60 | | -## Project using |
| 31 | +```bash |
| 32 | +pip install authx |
| 33 | +``` |
61 | 34 |
|
62 | | -Here is a simple way to kickstart your project with AuthX: |
| 35 | +## Quick Start |
63 | 36 |
|
64 | 37 | ```python |
65 | 38 | from fastapi import FastAPI, Depends, HTTPException |
66 | | -from authx import AuthX, AuthXConfig, RequestToken |
| 39 | +from authx import AuthX, AuthXConfig |
67 | 40 |
|
68 | 41 | app = FastAPI() |
69 | 42 |
|
70 | 43 | config = AuthXConfig( |
71 | | - JWT_ALGORITHM = "HS256", |
72 | | - JWT_SECRET_KEY = "SECRET_KEY", |
73 | | - JWT_TOKEN_LOCATION = ["headers"], |
| 44 | + JWT_SECRET_KEY="your-secret-key", # Change this! |
| 45 | + JWT_TOKEN_LOCATION=["headers"], |
74 | 46 | ) |
75 | 47 |
|
76 | 48 | auth = AuthX(config=config) |
77 | 49 | auth.handle_errors(app) |
78 | 50 |
|
79 | | -@app.get('/login') |
| 51 | +@app.post("/login") |
80 | 52 | def login(username: str, password: str): |
81 | | - if username == "xyz" and password == "xyz": |
82 | | - token = auth.create_access_token(uid=username) |
83 | | - return {"access_token": token} |
84 | | - raise HTTPException(401, detail={"message": "Invalid credentials"}) |
85 | | - |
86 | | -@app.get("/protected", dependencies=[Depends(auth.get_token_from_request)]) |
87 | | -def get_protected(token: RequestToken = Depends()): |
88 | | - try: |
89 | | - auth.verify_token(token=token) |
90 | | - return {"message": "Hello world !"} |
91 | | - except Exception as e: |
92 | | - raise HTTPException(401, detail={"message": str(e)}) from e |
| 53 | + if username == "test" and password == "test": |
| 54 | + token = auth.create_access_token(uid=username) |
| 55 | + return {"access_token": token} |
| 56 | + raise HTTPException(401, detail="Invalid credentials") |
| 57 | + |
| 58 | +@app.get("/protected", dependencies=[Depends(auth.access_token_required)]) |
| 59 | +def protected(): |
| 60 | + return {"message": "Hello World"} |
| 61 | +``` |
| 62 | + |
| 63 | +**Test it:** |
| 64 | + |
| 65 | +```bash |
| 66 | +# Get a token |
| 67 | +curl -X POST "http://localhost:8000/login?username=test&password=test" |
| 68 | + |
| 69 | +# Access protected route |
| 70 | +curl -H "Authorization: Bearer <your-token>" http://localhost:8000/protected |
| 71 | +``` |
| 72 | + |
| 73 | +## Features |
| 74 | + |
| 75 | +- Support for Python 3.9+ and Pydantic 2 |
| 76 | +- JWT authentication with multiple token locations: |
| 77 | + - Headers (Bearer token) |
| 78 | + - Cookies (with CSRF protection) |
| 79 | + - Query parameters |
| 80 | + - JSON body |
| 81 | +- Access and refresh token support |
| 82 | +- Token freshness for sensitive operations |
| 83 | +- Token blocklist/revocation |
| 84 | +- Extensible error handling |
| 85 | + |
| 86 | +### Extra Features |
| 87 | + |
| 88 | +Install [`authx-extra`](https://github.com/yezz123/authx-extra) for additional features: |
| 89 | + |
| 90 | +```bash |
| 91 | +pip install authx-extra |
93 | 92 | ``` |
94 | 93 |
|
95 | | -## Contributors and sponsors |
| 94 | +- Redis session store and cache |
| 95 | +- HTTP caching |
| 96 | +- Performance profiling with pyinstrument |
| 97 | +- Prometheus metrics |
| 98 | + |
| 99 | +**Note:** Check [Release Notes](https://authx.yezz.me/release/). |
| 100 | + |
| 101 | +## Contributors and Sponsors |
96 | 102 |
|
97 | 103 | <!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section --> |
98 | 104 | [](#contributors-) |
@@ -136,15 +142,6 @@ Thanks goes to these wonderful people |
136 | 142 |
|
137 | 143 | <!-- ALL-CONTRIBUTORS-LIST:END --> |
138 | 144 |
|
139 | | -<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section --> |
140 | | -<!-- prettier-ignore-start --> |
141 | | -<!-- markdownlint-disable --> |
142 | | - |
143 | | -<!-- markdownlint-restore --> |
144 | | -<!-- prettier-ignore-end --> |
145 | | - |
146 | | -<!-- ALL-CONTRIBUTORS-LIST:END --> |
147 | | - |
148 | 145 | This project follows the |
149 | 146 | [all-contributors](https://github.com/all-contributors/all-contributors) |
150 | 147 | specification. Contributions of any kind welcome! |
|
0 commit comments