Skip to content

Commit b1bd0b3

Browse files
authored
Merge pull request #52 from bugout-dev/update-user-web3-cli
Update cli user web3 address
2 parents 3e0a3be + 2fc1ff6 commit b1bd0b3

File tree

6 files changed

+82
-21
lines changed

6 files changed

+82
-21
lines changed

brood/actions.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ def user_as_json_dict(user: User) -> Dict[str, Any]:
218218
"username": user.username,
219219
"email": user.email,
220220
"normalized_email": user.normalized_email,
221+
"web3_address": user.web3_address,
221222
"verified": user.verified,
222223
"created_at": str(user.created_at),
223224
"updated_at": str(user.updated_at),

brood/api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ async def delete_token_handler(
275275
276276
- **target_token** (uuid, null): Token ID to revoke
277277
"""
278-
authorization: str = request.headers.get("Authorization")
278+
authorization: str = request.headers.get("Authorization") # type: ignore
279279
scheme_raw, _ = get_authorization_scheme_param(authorization)
280280
scheme = scheme_raw.lower()
281281
if scheme != "bearer":

brood/cli.py

Lines changed: 70 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,26 @@
22
Brood CLI
33
"""
44
import argparse
5-
from distutils.util import strtobool
5+
import base64
66
import json
7-
from typing import List
87
import uuid
8+
from distutils.util import strtobool
9+
from typing import List
10+
11+
from web3login.auth import to_checksum_address, verify
12+
from web3login.exceptions import Web3VerificationError
913

10-
from . import actions
11-
from . import data
12-
from . import exceptions
13-
from . import subscriptions
14+
from . import actions, data, exceptions, subscriptions
1415
from .db import SessionLocal
1516
from .models import (
16-
User,
17+
Application,
1718
Group,
19+
KVBrood,
1820
Role,
19-
TokenType,
2021
Subscription,
2122
SubscriptionPlan,
22-
KVBrood,
23-
Application,
23+
TokenType,
24+
User,
2425
)
2526

2627

@@ -79,6 +80,49 @@ def users_create_handler(args: argparse.Namespace) -> None:
7980
session.close()
8081

8182

83+
def users_update_handler(args: argparse.Namespace) -> None:
84+
"""
85+
Handler for "user update" subcommand.
86+
"""
87+
if args.web3_signature is None:
88+
raise Exception("No arguments specified to update")
89+
90+
session = SessionLocal()
91+
try:
92+
query = session.query(User).filter(User.id == args.id)
93+
user = query.one_or_none()
94+
if user is None:
95+
raise Exception("User not found")
96+
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)
116+
query.update({User.web3_address: web3_address})
117+
118+
session.commit()
119+
print_user(user)
120+
except Exception as e:
121+
print(e)
122+
finally:
123+
session.close()
124+
125+
82126
def users_get_handler(args: argparse.Namespace) -> None:
83127
"""
84128
Handler for "users get" subcommand.
@@ -723,6 +767,22 @@ def main() -> None:
723767
)
724768
parser_users_create.set_defaults(func=users_create_handler)
725769

770+
parser_users_update = subcommands_users.add_parser(
771+
"update", description="Update Brood user"
772+
)
773+
parser_users_update.add_argument(
774+
"-i",
775+
"--id",
776+
required=True,
777+
help="ID of the user to update",
778+
)
779+
parser_users_update.add_argument(
780+
"-w",
781+
"--web3_signature",
782+
help="Set new web3 address with provided signature",
783+
)
784+
parser_users_update.set_defaults(func=users_update_handler)
785+
726786
parser_users_get = subcommands_users.add_parser("get", description="Get Brood user")
727787
parser_users_get.add_argument(
728788
"-u",

brood/middleware.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ async def get_current_user(
3939
"""
4040
Middleware returns user if its token or web3 signature verified.
4141
"""
42-
authorization: str = request.headers.get("Authorization")
42+
authorization: str = request.headers.get("Authorization") # type: ignore
4343
scheme_raw, _ = get_authorization_scheme_param(authorization)
4444
scheme = scheme_raw.lower()
4545
if token is None or token == "":
4646
raise HTTPException(status_code=404, detail="Access token not found")
4747

48-
signature_application: str = request.headers.get(BUGOUT_APPLICATION_ID_HEADER)
48+
signature_application: str = request.headers.get(BUGOUT_APPLICATION_ID_HEADER) # type: ignore
4949
application_id = None
5050
if signature_application is not None:
5151
try:
@@ -135,13 +135,13 @@ async def get_current_user_with_groups(
135135
"""
136136
Middleware returns user with groups it belongs if its token or web3 signature verified.
137137
"""
138-
authorization: str = request.headers.get("Authorization")
138+
authorization: str = request.headers.get("Authorization") # type: ignore
139139
scheme_raw, _ = get_authorization_scheme_param(authorization)
140140
scheme = scheme_raw.lower()
141141
if token is None or token == "":
142142
raise HTTPException(status_code=404, detail="Access token not found")
143143

144-
signature_application: str = request.headers.get(BUGOUT_APPLICATION_ID_HEADER)
144+
signature_application: str = request.headers.get(BUGOUT_APPLICATION_ID_HEADER) # type: ignore
145145
application_id = None
146146
if signature_application is not None:
147147
try:
@@ -225,7 +225,7 @@ def autogenerated_user_token_check(request: Request) -> bool:
225225
is_autogenerated_user = False
226226
installation_token_header: Optional[str] = request.headers.get(
227227
BOT_INSTALLATION_TOKEN_HEADER, None
228-
)
228+
) # type: ignore
229229
if (
230230
installation_token_header is not None
231231
and BOT_INSTALLATION_TOKEN == installation_token_header
@@ -253,7 +253,7 @@ async def is_token_restricted_or_installation(
253253
Because of oauth2_scheme_manual we could accept None
254254
for follow up Bugout header check.
255255
"""
256-
authorization: str = request.headers.get("Authorization")
256+
authorization: str = request.headers.get("Authorization") # type: ignore
257257
scheme_raw, _ = get_authorization_scheme_param(authorization)
258258
scheme = scheme_raw.lower()
259259

@@ -276,7 +276,7 @@ async def is_token_restricted(
276276
"""
277277
Check if user's token is restricted or not.
278278
"""
279-
authorization: str = request.headers.get("Authorization")
279+
authorization: str = request.headers.get("Authorization") # type: ignore
280280
scheme_raw, _ = get_authorization_scheme_param(authorization)
281281
scheme = scheme_raw.lower()
282282

brood/resources/actions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def acl_auth(
6464
def acl_check(
6565
acl: Dict[data.HolderType, List[str]],
6666
required_scopes: Set[data.ResourcePermissions],
67-
check_type: data.HolderType = None,
67+
check_type: Optional[data.HolderType] = None,
6868
) -> None:
6969
"""
7070
Checks if provided permissions from handler intersect with existing permissions for user/group.

deploy/deploy.monolith.bash

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,5 @@ echo
5757
echo -e "${PREFIX_INFO} Replacing existing Brood service definition with ${BROOD_SERVICE_FILE}"
5858
chmod 644 "${SCRIPT_DIR}/${BROOD_SOURCE_SERVICE_FILE}"
5959
cp "${SCRIPT_DIR}/${BROOD_SOURCE_SERVICE_FILE}" "/home/ubuntu/.config/systemd/user/${BROOD_SERVICE_FILE}"
60-
XDG_RUNTIME_DIR="/run/user/$UID" systemctl --user daemon-reload
61-
XDG_RUNTIME_DIR="/run/user/$UID" systemctl --user restart "${BROOD_SERVICE_FILE}"
60+
XDG_RUNTIME_DIR="/run/user/1000" systemctl --user daemon-reload
61+
XDG_RUNTIME_DIR="/run/user/1000" systemctl --user restart "${BROOD_SERVICE_FILE}"

0 commit comments

Comments
 (0)