Skip to main content

FastAPI extension for user authentication through signature challenges

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 authorization_routes from fastapi_walletauth:

from fastapi import FastAPI
from fastapi_walletauth import authorization_routes

app = FastAPI()

app.include_router(authorization_routes)

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:

from fastapi import FastAPI
from fastapi_walletauth import WalletAuthDep, authorization_routes

app = FastAPI()
app.include_router(authorization_routes)

@app.get("/protected")
def protected(wa: WalletAuthDep):
    return wa.address

Signing the challenge

The challenge is a serialized JSON object containing the following fields:

message = {
    "chain": "ETH",
    "address": "0x...",
    "app": "myapp",
    "time": 1688819493.8691394
}

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.

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastapi_walletauth-1.0.0a4.tar.gz (8.0 kB view details)

Uploaded Source

Built Distribution

fastapi_walletauth-1.0.0a4-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_walletauth-1.0.0a4.tar.gz.

File metadata

  • Download URL: fastapi_walletauth-1.0.0a4.tar.gz
  • Upload date:
  • Size: 8.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.11.4 Linux/5.15.0-86-generic

File hashes

Hashes for fastapi_walletauth-1.0.0a4.tar.gz
Algorithm Hash digest
SHA256 a20c613032734afed6bb96c872c998eebbb0657eedfc1198e125cceb23a581cf
MD5 eabdbe0b7575c6879e202e5640c47a83
BLAKE2b-256 0c01af2705e662d3a7f218ab0b2338660c4300711ae38bba3fd749fec3499af9

See more details on using hashes here.

Provenance

File details

Details for the file fastapi_walletauth-1.0.0a4-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_walletauth-1.0.0a4-py3-none-any.whl
Algorithm Hash digest
SHA256 87cd12cfdd9382419990af36af86060b7f66144b1a6c7dffcef4b6654ada2e19
MD5 4bd0c5caab15d1f22f63950e889d8066
BLAKE2b-256 dccce03f3278f511a9164956d55cd8cc40c7b1592f3058e1bd9a33c4c096185f

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page