{"id":28403483,"url":"https://github.com/losos3000/fastapi-lite-auth","last_synced_at":"2025-06-27T08:32:00.468Z","repository":{"id":293488074,"uuid":"977101141","full_name":"losos3000/fastapi-lite-auth","owner":"losos3000","description":"Simple auth module for FastAPI project","archived":false,"fork":false,"pushed_at":"2025-05-18T18:05:05.000Z","size":94,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-06-02T02:49:00.280Z","etag":null,"topics":["auth","authentication","fastapi"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/losos3000.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-05-03T12:31:09.000Z","updated_at":"2025-05-22T08:48:42.000Z","dependencies_parsed_at":"2025-06-01T18:01:12.720Z","dependency_job_id":"e5ed083c-88c0-409d-be9d-dae7416ada34","html_url":"https://github.com/losos3000/fastapi-lite-auth","commit_stats":null,"previous_names":["losos3000/fastapi-simple-auth","losos3000/fastapi-lite-auth"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/losos3000%2Ffastapi-lite-auth","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/losos3000%2Ffastapi-lite-auth/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/losos3000%2Ffastapi-lite-auth/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/losos3000%2Ffastapi-lite-auth/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/losos3000","download_url":"https://codeload.github.com/losos3000/fastapi-lite-auth/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/losos3000%2Ffastapi-lite-auth/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":258740712,"owners_count":22750029,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["auth","authentication","fastapi"],"created_at":"2025-06-01T18:00:56.788Z","updated_at":"2025-06-27T08:32:00.460Z","avatar_url":"https://github.com/losos3000.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# FastAPI Lite Auth\nThis is a login/password authentication module that can be quickly and easily integrated into your project.\nJWT token is used as the authentication method. It is recommended to use the module in small projects and pet projects.\n\nTo add the module to your project, you need to:\n\n1. Install the module\n2. Override the `get_user` method in `auth_manager`\n3. Override the user schema and model in `auth_manager` if they differ from yours\n4. Specify which fields in the user model represent the login and password \n\nMore detailed installation and configuration instructions can be found in the `Installation` section.\n\nBased on [AuthX](https://github.com/yezz123/authx)\n\n\n---\n\n## Installation\n### 1. Installing the module\nInstall module\n`pip install fastapi-lite-auth`\nThe module is currently being prepared for publishing to `PyPI`.\n\n### 2. Module integration\nIn the folder containing your API routers (usually `routers/`), create a file named `auth.py`.\nImport the necessary components and create the API router:\n`auth.py`:\n\n```python\nfrom fastapi import APIRouter\nfrom fastapi_lite_auth import auth_config, auth_router, auth_manager\n\nauth_config.authx_ready()\n\nrouter = APIRouter()\nrouter.include_router(\n    router=auth_router\n)\n```\n\nThe `auth_config.authx_ready()` function configures the AuthX object. You should call it after modifying any `auth_config` settings. \n\n### 3. Basic module configuration\nTo make the module work, you need to do a few things:\n\n#### 3.1. Override the user model and schema.\nThe model is an ORM-oriented class.\nThe schema is a class that describes the data the API will return.\n\nBy default, they look like this:\n```python\nfrom pydantic import BaseModel\n\nclass BasicGetUserSchema(BaseModel):\n    id: int\n    name: str\n    username: str\n    email: str\n    \nclass BasicUserModel:\n    id: int\n    name: str\n    email: str\n    username: str\n    password: str\n```\n\nExample of overriding the user model and schema:\n\n```python\nfrom pydantic import BaseModel\nfrom fastapi_lite_auth import auth_config\n\n\nclass CustomGetUserSchema(BaseModel):\n    id: int\n    full_name: str\n    phone: str\n    username: str\n    email: str\n    passport_number: str\n    insurance_number: str\n\n\nclass CustomUserModel:\n    id: int\n    full_name: str\n    phone: str\n    username: str\n    email: str\n    passport_number: str\n    insurance_number: str\n    password: str\n\n\nauth_config.models_config.UserModel = CustomUserModel\nauth_config.schemas_config.GetUserSchema = CustomGetUserSchema\n```\n\n#### 3.2. Define the user retrieval method.\nThis is a function that should find a user record in your database using the field used as login.\nRequirements:\n- The function must accept a `login` argument of type `str`\n- It must return an instance of the user model described above or `None` if not found\n\nExample override:\n\n```python\nimport sqlite3\nfrom fastapi_lite_auth import auth_config, auth_manager\n\n\ndef get_user_by_login(login: str | None = None) -\u003e auth_config.models_config.UserModel | None:\n    conn = sqlite3.connect(\"./db.db\")\n    select = conn.execute(f\"SELECT * FROM user WHERE email = ?\", (login,))\n    res = select.fetchone()\n\n    if res is None:\n        return None\n\n    user_model = auth_config.models_config.UserModel()\n    user_model.id = res[0]\n    user_model.full_name = res[1]\n    user_model.username = res[2]\n    user_model.email = res[3]\n    user_model.password = res[4]\n\n    return user_model\n\n\nauth_manager.get_user = get_user_by_login\n```\n\n#### 3.3. Configure login and password fields\nBy default, `username` and `password` fields are used.\nExample configuration:\n\n```python\nfrom fastapi_lite_auth import auth_config\n\nauth_config.login_config.login_field_name = \"email\"\nauth_config.login_config.password_field_name = \"password\"\nauth_config.authx_ready()\n```\n\n#### 3.4. Retrieving the Current User\nThe authentication token is stored in a cookie. When making a request to the server, it must be sent in the `Credentials` HTTP header.\nTo retrieve the user from the JWT token, you need to specify a dependency in the route function:\n```python\nfrom fastapi import APIRouter, Depends\nfrom fastapi_lite_auth import current_user\n\nrouter = APIRouter()\n\n@router.get(path=\"/me\")\nasync def get_user(user = Depends(current_user)):\n    return {\"user\": user}\n```\nThe `current_user` dependency returns an instance of the `GetUserSchema` class-schema with the authenticated user's data, which is configured in section **3.1.**\nHow it works:\n1. The application retrieves the JWT token from the `Credentials` header\n2. The user's login is extracted from the token\n3. The user is fetched by this login using the `get_user` function, which is configured in section **3.2.**\n\n### 4. Additional configuration\n#### 4.1. Define a password hashing function (if passwords are stored hashed in your DB)\nThis function is used to hash the incoming password for comparison.\nRequirements:\n- Must accept a `data` argument of type `str`\n- Must return a `str` hash\n\nExample:\n\n```python\nfrom hashlib import sha256\nfrom fastapi_lite_auth import auth_manager\n\n\ndef hash(data: str) -\u003e str:\n    return sha256(data.encode()).hexdigest()\n\n\nauth_manager.hash = hash\n```\n\n#### 4.2. Configure the secret key\nThe secret key is used to sign the JWT token.\nIt should be stored in environment variables.\nBy default, it’s generated from the current datetime.\n\nExample override:\n\n```python\nimport os\nfrom fastapi_lite_auth import auth_config\n\nauth_config.token_config.secret_key = os.getenv(\"AUTH_SECRET\")\nauth_config.authx_ready()\n```\n\n#### 4.3. Configure cookie parameters\nHere’s how to configure cookies and their default values:\n\n```python\nfrom fastapi_lite_auth import auth_config\n\nauth_config.cookie_config.cookie_name = \"auth_token\"\nauth_config.cookie_config.cookie_httponly = False\nauth_config.cookie_config.cookie_secure = False\nauth_config.cookie_config.cookie_samesite = \"lax\"\nauth_config.cookie_config.cookie_max_age = 3600\nauth_config.cookie_config.cookie_path = \"/\"\nauth_config.cookie_config.cookie_domain = None\nauth_config.cookie_config.cookie_expires = None\nauth_config.authx_ready()\n```\n\n---\n\n## Example integration\nYou can check out the example app in the `example` directory.\nIts configuration is described in `example/api/routers/auth.py`.\n\n### Requirements\n```shell\npip install -r requirements.txt\n```\nor\n```shell\npip install \"fastapi[standard]\"\npip install authx \n```\n\n### Start Example\n```shell\npython -m example.api.main\n```\nor\n```shell\npython3 -m example.api.main\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flosos3000%2Ffastapi-lite-auth","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flosos3000%2Ffastapi-lite-auth","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flosos3000%2Ffastapi-lite-auth/lists"}