Exemplo n.º 1
0
def validate_userid_signature(user: User) -> Optional[Address]:
    """ Validate a userId format and signature on displayName, and return its address"""
    # display_name should be an address in the USERID_RE format
    match = USERID_RE.match(user.user_id)
    if not match:
        return None

    encoded_address = match.group(1)
    address: Address = to_canonical_address(encoded_address)

    try:
        displayname = user.get_display_name()
        recovered = recover(
            data=user.user_id.encode(),
            signature=decode_hex(displayname),
        )
        if not (address and recovered and recovered == address):
            return None
    except (
            DecodeError,
            TypeError,
            InvalidSignature,
            MatrixRequestError,
            json.decoder.JSONDecodeError,
    ):
        return None
    return address
Exemplo n.º 2
0
 def _user_from_id(self,
                   user_id: str,
                   display_name: Optional[str] = None) -> Optional[User]:
     try:
         return User(self._client.api, user_id, display_name)
     except ValueError:
         log.error("Matrix server returned an invalid user_id.")
     return None
Exemplo n.º 3
0
def test_assumption_matrix_userid(local_matrix_servers):
    client, _ = create_logged_in_client(local_matrix_servers[0])

    # userid validation expects a str
    none_user_id = None
    with pytest.raises(AttributeError):
        User(client.api, none_user_id)

    # userid validation requires `@`
    empty_user_id = ""
    with pytest.raises(ValueError):
        User(client.api, empty_user_id)

    # userid validation requires `@`
    invalid_user_id = client.user_id[1:]
    with pytest.raises(ValueError):
        User(client.api, invalid_user_id)

    # The format of the userid is valid, however the user does not exist, the
    # server returns an error
    unexisting_user_id = replace_one_letter(client.user_id)
    user = User(client.api, unexisting_user_id)
    with pytest.raises(MatrixRequestError):
        user.get_display_name()

    # The userid is valid and the user exists, this should not raise
    newlogin_client, _ = create_logged_in_client(local_matrix_servers[0])
    user = User(client.api, newlogin_client.user_id)
    user.get_display_name()
Exemplo n.º 4
0
 def mock_get_user(klass, user: Union[User, str]) -> User:  # pylint: disable=unused-argument
     return User(None, USERID1)
Exemplo n.º 5
0
 def mock_get_user(klass, user: Union[User, str]) -> User:
     return User(None, USERID1)