# FastAPI Wallet Authentication
fastapi-walletauth provides a simple way to authenticate users in FastAPI applications using a wallet.
It currently supports Ethereum and Solana wallets/signatures.
## Installation
```shell
pip install fastapi-walletauth
```
## Usage
Adding the authentication endpoints is as simple as importing the `authorization_routes` from `fastapi_walletauth`:
```python
from fastapi import FastAPI
from fastapi_walletauth import jwt_authorization_router
app = FastAPI()
app.include_router(jwt_authorization_router)
```
This will add the following endpoints to your application:
- `POST /authentication/challenge`: Returns a challenge for the user to sign
- `POST /authentication/solve`: Returns a Bearer token if the signature is valid
- `POST /authentication/logout`: Invalidates the current token
- `POST /authentication/refresh`: Returns a new token if the current token is valid
You can then use `WalletAuthDep` to protect your endpoints:
```python
from fastapi import FastAPI
from fastapi_walletauth import JWTWalletAuthDep, jwt_authorization_router
app = FastAPI()
app.include_router(jwt_authorization_router)
@app.get("/protected")
def protected(wa: JWTWalletAuthDep):
return wa.address
```
## Signing the challenge
The challenge message is now formatted in a human-readable way and includes the following fields:
```
Hello, please sign this message!
Chain: ETH
Address: 0x...
App: myapp
Time: 2025-01-29 15:22:39
```
**PLEASE NOTE**: The `app` field needs to be set to the name of your application. This is used to prevent replay attacks.
```shell
export FASTAPI_WALLETAUTH_APP=myapp
```
The signature format depends on the wallet type and is specified in the `chain` field. This signature is then sent to the `/authentication/solve` endpoint to obtain a Bearer token.
## Custom Greeting Configuration
Starting from version 2.1.0, `fastapi-walletauth` allows you to configure a custom greeting message that will be included in the challenge message. This greeting can be set in the server configuration and will be used for all challenge messages.
### Setting the Greeting
The greeting message can be configured in the `Settings` class within your application. By default, the greeting is set to "Hello, please sign this message!". You can change this by setting the `GREETING` environment variable or by modifying the `Settings` class directly.
Example:
```python
from fastapi_walletauth.common import settings
# Set a custom greeting
settings.GREETING = "Welcome! Please sign this message to continue."
```
## Liability
This software is provided "as is" and "with all faults." I make no representations or warranties of any kind concerning
the safety, suitability, inaccuracies, typographical errors, or other harmful components of this
software. There are inherent dangers in the use of any software, especially cryptographic implementations. You are solely
responsible for determining whether this software is compatible with your machine and other software installed on your
computer. You are also solely responsible for the choice of a wallet and the security of your private keys. You
acknowledge and agree to waive any liability claim against me from any loss or damage of any kind arising out of or in
connection with your use of this software.
Raw data
{
"_id": null,
"home_page": null,
"name": "fastapi-walletauth",
"maintainer": null,
"docs_url": null,
"requires_python": "<4.0,>=3.9",
"maintainer_email": null,
"keywords": "FastAPI, authentication, signature, wallet, ethereum, solana, web3, jwt",
"author": "mhh",
"author_email": "mike.hukiewitz@robotter.ai",
"download_url": "https://files.pythonhosted.org/packages/05/58/28ead1fdf2ed392f84e8098250618c754517ac5922fbcc27465a27e0bd78/fastapi_walletauth-2.2.0.tar.gz",
"platform": null,
"description": "# FastAPI Wallet Authentication\n\nfastapi-walletauth provides a simple way to authenticate users in FastAPI applications using a wallet.\nIt currently supports Ethereum and Solana wallets/signatures.\n\n## Installation\n\n```shell\npip install fastapi-walletauth\n```\n\n## Usage\n\nAdding the authentication endpoints is as simple as importing the `authorization_routes` from `fastapi_walletauth`:\n\n```python\nfrom fastapi import FastAPI\nfrom fastapi_walletauth import jwt_authorization_router\n\napp = FastAPI()\n\napp.include_router(jwt_authorization_router)\n```\n\nThis will add the following endpoints to your application:\n\n- `POST /authentication/challenge`: Returns a challenge for the user to sign\n- `POST /authentication/solve`: Returns a Bearer token if the signature is valid\n- `POST /authentication/logout`: Invalidates the current token\n- `POST /authentication/refresh`: Returns a new token if the current token is valid\n\nYou can then use `WalletAuthDep` to protect your endpoints:\n\n```python\nfrom fastapi import FastAPI\nfrom fastapi_walletauth import JWTWalletAuthDep, jwt_authorization_router\n\napp = FastAPI()\napp.include_router(jwt_authorization_router)\n\n@app.get(\"/protected\")\ndef protected(wa: JWTWalletAuthDep):\n return wa.address\n```\n\n## Signing the challenge\n\nThe challenge message is now formatted in a human-readable way and includes the following fields:\n\n```\nHello, please sign this message!\nChain: ETH\nAddress: 0x...\nApp: myapp\nTime: 2025-01-29 15:22:39\n```\n\n**PLEASE NOTE**: The `app` field needs to be set to the name of your application. This is used to prevent replay attacks.\n```shell\nexport FASTAPI_WALLETAUTH_APP=myapp\n```\n\nThe signature format depends on the wallet type and is specified in the `chain` field. This signature is then sent to the `/authentication/solve` endpoint to obtain a Bearer token.\n\n\n## Custom Greeting Configuration\n\nStarting from version 2.1.0, `fastapi-walletauth` allows you to configure a custom greeting message that will be included in the challenge message. This greeting can be set in the server configuration and will be used for all challenge messages.\n\n### Setting the Greeting\n\nThe greeting message can be configured in the `Settings` class within your application. By default, the greeting is set to \"Hello, please sign this message!\". You can change this by setting the `GREETING` environment variable or by modifying the `Settings` class directly.\n\nExample:\n\n```python\nfrom fastapi_walletauth.common import settings\n\n# Set a custom greeting\nsettings.GREETING = \"Welcome! Please sign this message to continue.\"\n```\n\n## Liability\n\nThis software is provided \"as is\" and \"with all faults.\" I make no representations or warranties of any kind concerning\nthe safety, suitability, inaccuracies, typographical errors, or other harmful components of this\nsoftware. There are inherent dangers in the use of any software, especially cryptographic implementations. You are solely\nresponsible for determining whether this software is compatible with your machine and other software installed on your\ncomputer. You are also solely responsible for the choice of a wallet and the security of your private keys. You\nacknowledge and agree to waive any liability claim against me from any loss or damage of any kind arising out of or in\nconnection with your use of this software.",
"bugtrack_url": null,
"license": null,
"summary": "FastAPI extension for user authentication through signature challenges",
"version": "2.2.0",
"project_urls": null,
"split_keywords": [
"fastapi",
" authentication",
" signature",
" wallet",
" ethereum",
" solana",
" web3",
" jwt"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "f51b0df4a18467f026af7b3e5ac11a6debce1a976ab3a2422e5ba34843fe4a09",
"md5": "839c600bbf75a5ed421cf3ab3dceeddc",
"sha256": "7b1196986b2f788f977ac5e019657f574f08fcd970aa1bb46b66ac44c32d8262"
},
"downloads": -1,
"filename": "fastapi_walletauth-2.2.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "839c600bbf75a5ed421cf3ab3dceeddc",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.9",
"size": 10670,
"upload_time": "2025-01-29T15:52:24",
"upload_time_iso_8601": "2025-01-29T15:52:24.018755Z",
"url": "https://files.pythonhosted.org/packages/f5/1b/0df4a18467f026af7b3e5ac11a6debce1a976ab3a2422e5ba34843fe4a09/fastapi_walletauth-2.2.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "055828ead1fdf2ed392f84e8098250618c754517ac5922fbcc27465a27e0bd78",
"md5": "7d432fbf616dc942b9583883869ae2f5",
"sha256": "7452bc2fd1d2e49c5419a404441e75c9ced41b20ddf0d6df7d59a0666ea39732"
},
"downloads": -1,
"filename": "fastapi_walletauth-2.2.0.tar.gz",
"has_sig": false,
"md5_digest": "7d432fbf616dc942b9583883869ae2f5",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.9",
"size": 8900,
"upload_time": "2025-01-29T15:52:25",
"upload_time_iso_8601": "2025-01-29T15:52:25.884424Z",
"url": "https://files.pythonhosted.org/packages/05/58/28ead1fdf2ed392f84e8098250618c754517ac5922fbcc27465a27e0bd78/fastapi_walletauth-2.2.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-01-29 15:52:25",
"github": false,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"lcname": "fastapi-walletauth"
}