No project description provided
Project description
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
pip install fastapi-walletauth
Usage
Adding the authentication endpoints is as simple as importing the fastapi_walletauth.magic
module after the
FastAPI application has been created:
from fastapi import FastAPI
app = FastAPI()
import fastapi_walletauth.magic
or if you prefer, you can add the endpoints manually:
from fastapi import FastAPI
from fastapi_walletauth.router import authorization
app = FastAPI()
app.include_router(authorization)
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 valid
You can then use WalletAuthDep
to protect your endpoints:
from fastapi import FastAPI
from fastapi_walletauth import WalletAuth, WalletAuthDep
app = FastAPI()
import fastapi_walletauth.magic
@app.get("/protected")
def protected(wa: WalletAuth = WalletAuthDep()):
return wa.address
Signing the challenge
The challenge is a JSON object (similar to Aleph messages) containing the following fields:
message = {
"chain": "ETH",
"address": "0x...",
"type": "authorization_challenge",
"item_hash": "0x...",
}
chain
: The chain ID (e.g.ETH
for Ethereum mainnet)address
: The address of the usertype
: The type of message to be signed (authorization_challenge
)item_hash
: A random hash
Currently, it requires to be preprocessed into a Verification Buffer like this:
"{chain}\n{sender}\n{type}\n{item_hash}".format(**message).encode("utf-8")
before being signed with the user's private key. This signature is then sent to the /authentication/solve
endpoint.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for fastapi_walletauth-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2076e11d061822d222827e202372a52b372dc29d080e8b650ed63079ed3bec02 |
|
MD5 | 30a67ca75cf3c73342d07a6fc7a073a9 |
|
BLAKE2b-256 | 56cc1f2facd8fbef60df28accd17153af47228fc61af7c6c7743ff55a5e3a870 |