
Security News
Meet Socket at Black Hat and DEF CON 2025 in Las Vegas
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
fastapi-walletauth provides a simple way to authenticate users in FastAPI applications using a wallet. It currently supports Ethereum and Solana wallets/signatures.
pip install fastapi-walletauth
Adding the authentication endpoints is as simple as importing the authorization_routes
from fastapi_walletauth
:
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 signPOST /authentication/solve
: Returns a Bearer token if the signature is validPOST /authentication/logout
: Invalidates the current tokenPOST /authentication/refresh
: Returns a new token if the current token is validYou can then use WalletAuthDep
to protect your endpoints:
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
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.
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.
Starting from version 3.0.0, fastapi-walletauth
supports transaction-based authentication as an alternative to message signing. This is especially useful for hardware wallets (like Ledger) that may not support message signing in browser wallets.
To enable transaction-based authentication, import the transaction authorization router:
from fastapi import FastAPI
from fastapi_walletauth import jwt_transaction_authorization_router
app = FastAPI()
app.include_router(jwt_transaction_authorization_router)
This adds the following endpoints to your application:
POST /transaction-auth/challenge
: Returns a transaction to sign instead of a messagePOST /transaction-auth/solve
: Returns a Bearer token if the transaction signature is validPOST /transaction-auth/refresh
: Returns a new token if the current token is validThe transaction challenges are simple memo transactions:
The flow for transaction-based authentication is:
/transaction-auth/challenge
/transaction-auth/solve
This approach is compatible with hardware wallets that support transaction signing but not message signing.
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.
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:
from fastapi_walletauth.common import settings
# Set a custom greeting
settings.GREETING = "Welcome! Please sign this message to continue."
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.
FAQs
FastAPI extension for user authentication through signature challenges
We found that fastapi-walletauth demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.