Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion python/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
version=VERSION,
packages=find_packages(),
install_requires=[
"eip712==0.1.0",
"eip712>=0.2.0",
"eth-typing>=2.3.0",
"web3>=5.30.0",
],
Expand Down
22 changes: 18 additions & 4 deletions python/web3login/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
from typing import Any, Dict, cast

import eth_keys # type: ignore
from eip712.messages import EIP712Message, _hash_eip191_message
from eip712.messages import EIP712Message
from eth_account._utils.signing import sign_message_hash
from eth_typing import ChecksumAddress
from eth_account.messages import SignableMessage
from eth_typing import ChecksumAddress, Hash32
from eth_utils.curried import ValidationError, keccak
from hexbytes import HexBytes
from web3 import Web3

Expand Down Expand Up @@ -52,14 +54,26 @@ class Web3Authorization(EIP712Message):


def sign_message(message_hash_bytes: HexBytes, private_key: HexBytes) -> HexBytes:

eth_private_key = eth_keys.keys.PrivateKey(private_key)
_, _, _, signed_message_bytes = sign_message_hash(
eth_private_key, message_hash_bytes
)
return signed_message_bytes


def hash_eip191_message(signable_message: SignableMessage) -> Hash32:
# https://github.com/ethereum/eth-account/blob/50ccddf/eth_account/messages.py#L65
version = signable_message.version
if len(version) != 1:
raise ValidationError(
f"The supplied message version is {version!r}. "
"The EIP-191 signable message standard only supports one-byte versions."
)

joined = b"\x19" + version + signable_message.header + signable_message.body
return Hash32(keccak(joined))


def authorize(
deadline: int, address: str, application: str, private_key: HexBytes
) -> Dict[str, Any]:
Expand All @@ -74,7 +88,7 @@ def authorize(
application=application,
) # type: ignore

msg_hash_bytes = HexBytes(_hash_eip191_message(message.signable_message))
msg_hash_bytes = HexBytes(hash_eip191_message(message.signable_message))

signed_message = sign_message(msg_hash_bytes, private_key)

Expand Down
16 changes: 14 additions & 2 deletions python/web3login/middlewares/fastapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from fastapi import Request, Response
from fastapi.exceptions import HTTPException
from fastapi.openapi.models import OAuthFlowPassword
from fastapi.openapi.models import OAuthFlows as OAuthFlowsModel
from fastapi.security import OAuth2
from fastapi.security.utils import get_authorization_scheme_param
Expand Down Expand Up @@ -35,7 +36,13 @@ def __init__(
):
if not scopes:
scopes = {}
flows = OAuthFlowsModel(password={"tokenUrl": tokenUrl, "scopes": scopes})

flows = OAuthFlowsModel(
password=OAuthFlowPassword(
tokenUrl=tokenUrl,
scopes=scopes,
)
)
super().__init__(
flows=flows,
scheme_name=scheme_name,
Expand Down Expand Up @@ -74,7 +81,12 @@ def __init__(
):
if not scopes:
scopes = {}
flows = OAuthFlowsModel(password={"tokenUrl": tokenUrl, "scopes": scopes})
flows = OAuthFlowsModel(
password=OAuthFlowPassword(
tokenUrl=tokenUrl,
scopes=scopes,
)
)
super().__init__(
flows=flows,
scheme_name=scheme_name,
Expand Down