11import base64
22import json
33import logging
4- from typing import Any , Optional
4+ from typing import Optional
55from uuid import UUID
66
77from fastapi import Depends , HTTPException , Request
88from fastapi .exceptions import HTTPException
99from fastapi .security .utils import get_authorization_scheme_param
1010from web3login .auth import to_checksum_address , verify
11- from web3login .exceptions import MoonstreamVerificationError
12- from web3login .middlewares .fastapi import OAuth2BearerOrSignature
11+ from web3login .exceptions import Web3VerificationError
12+ from web3login .middlewares .fastapi import OAuth2BearerOrWeb3
1313
1414from . import actions , data
1515from .db import yield_db_read_only_session
16- from .settings import BOT_INSTALLATION_TOKEN , BOT_INSTALLATION_TOKEN_HEADER
16+ from .settings import (
17+ APPLICATION_NAME ,
18+ BOT_INSTALLATION_TOKEN ,
19+ BOT_INSTALLATION_TOKEN_HEADER ,
20+ )
1721
1822logger = logging .getLogger (__name__ )
1923
20-
2124# Login implementation follows:
2225# https://fastapi.tiangolo.com/tutorial/security/simple-oauth2/
23- oauth2_scheme = OAuth2BearerOrSignature (tokenUrl = "token" )
24- oauth2_scheme_manual = OAuth2BearerOrSignature (tokenUrl = "token" , auto_error = False )
26+ oauth2_scheme = OAuth2BearerOrWeb3 (tokenUrl = "token" )
27+ oauth2_scheme_manual = OAuth2BearerOrWeb3 (tokenUrl = "token" , auto_error = False )
2528
2629
2730async def get_current_user (
@@ -39,13 +42,16 @@ async def get_current_user(
3942 raise HTTPException (status_code = 404 , detail = "Access token not found" )
4043
4144 try :
42- if scheme == "moonstream " :
45+ if scheme == "web3 " :
4346 payload_json = base64 .decodebytes (str (token ).encode ()).decode ("utf-8" )
4447 payload = json .loads (payload_json )
45- verified = verify (authorization_payload = payload , schema = "registration" )
48+ verified = verify (
49+ authorization_payload = payload ,
50+ application_to_check = APPLICATION_NAME ,
51+ )
4652 if not verified :
47- logger .info ("Moonstream verification error" )
48- raise MoonstreamVerificationError ()
53+ logger .info ("Web3 verification error" )
54+ raise Web3VerificationError ()
4955 web3_address = payload .get ("address" )
5056 if web3_address is None :
5157 logger .error ("Web3 address in payload could not be None" )
@@ -76,7 +82,7 @@ async def get_current_user(
7682 except actions .UserInvalidParameters as e :
7783 logger .info (e )
7884 raise HTTPException (status_code = 500 )
79- except MoonstreamVerificationError :
85+ except Web3VerificationError :
8086 raise HTTPException (status_code = 403 , detail = "Signature not verified" )
8187 except Exception :
8288 logger .error ("Unhandled exception at get_current_user" )
@@ -112,13 +118,16 @@ async def get_current_user_with_groups(
112118 raise HTTPException (status_code = 404 , detail = "Access token not found" )
113119
114120 try :
115- if scheme == "moonstream " :
121+ if scheme == "web3 " :
116122 payload_json = base64 .decodebytes (str (token ).encode ()).decode ("utf-8" )
117123 payload = json .loads (payload_json )
118- verified = verify (authorization_payload = payload , schema = "registration" )
124+ verified = verify (
125+ authorization_payload = payload ,
126+ application_to_check = APPLICATION_NAME ,
127+ )
119128 if not verified :
120- logger .info ("Moonstream authorization verification error" )
121- raise MoonstreamVerificationError ()
129+ logger .info ("Web3 authorization verification error" )
130+ raise Web3VerificationError ()
122131 web3_address = payload .get ("address" )
123132 if web3_address is None :
124133 logger .error ("Web3 address in payload could not be None" )
@@ -154,7 +163,7 @@ async def get_current_user_with_groups(
154163 except actions .UserInvalidParameters as e :
155164 logger .info (e )
156165 raise HTTPException (status_code = 500 )
157- except MoonstreamVerificationError :
166+ except Web3VerificationError :
158167 raise HTTPException (status_code = 403 , detail = "Signature not verified" )
159168 except Exception :
160169 logger .error ("Unhandled exception at get_current_user_with_groups" )
0 commit comments