22Brood CLI
33"""
44import argparse
5- from distutils . util import strtobool
5+ import base64
66import json
7- from typing import List
87import uuid
8+ from distutils .util import strtobool
9+ from typing import List
910
10- from web3login .auth import to_checksum_address
11+ from web3login .auth import to_checksum_address , verify
12+ from web3login .exceptions import Web3VerificationError
1113
12- from . import actions
13- from . import data
14- from . import exceptions
15- from . import subscriptions
14+ from . import actions , data , exceptions , subscriptions
1615from .db import SessionLocal
1716from .models import (
18- User ,
17+ Application ,
1918 Group ,
19+ KVBrood ,
2020 Role ,
21- TokenType ,
2221 Subscription ,
2322 SubscriptionPlan ,
24- KVBrood ,
25- Application ,
23+ TokenType ,
24+ User ,
2625)
2726
2827
@@ -85,7 +84,7 @@ def users_update_handler(args: argparse.Namespace) -> None:
8584 """
8685 Handler for "user update" subcommand.
8786 """
88- if args .web3_address is None :
87+ if args .web3_signature is None :
8988 raise Exception ("No arguments specified to update" )
9089
9190 session = SessionLocal ()
@@ -95,8 +94,25 @@ def users_update_handler(args: argparse.Namespace) -> None:
9594 if user is None :
9695 raise Exception ("User not found" )
9796
98- if args .web3_address is not None :
99- web3_address = to_checksum_address (args .web3_address )
97+ if args .web3_signature is not None :
98+ payload_json = base64 .decodebytes (args .web3_signature .encode ()).decode (
99+ "utf-8"
100+ )
101+ payload = json .loads (payload_json )
102+ verified = verify (
103+ authorization_payload = payload ,
104+ application_to_check = str (user .application_id )
105+ if user .application_id is not None
106+ else "" ,
107+ )
108+ if not verified :
109+ raise Web3VerificationError ("Web3 registration verification error" )
110+ web3_address = payload .get ("address" )
111+ if web3_address is None :
112+ raise Exception (
113+ f"Web3 address in payload could not be None for user with username: { user .username } "
114+ )
115+ web3_address = to_checksum_address (web3_address )
100116 query .update ({User .web3_address : web3_address })
101117
102118 session .commit ()
@@ -762,8 +778,8 @@ def main() -> None:
762778 )
763779 parser_users_update .add_argument (
764780 "-w" ,
765- "--web3_address " ,
766- help = "Set new web3 address" ,
781+ "--web3_signature " ,
782+ help = "Set new web3 address with provided signature " ,
767783 )
768784 parser_users_update .set_defaults (func = users_update_handler )
769785
0 commit comments