Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Use the v2 Identity Service API for lookups (MSC2134 + MSC2140) (#5976)
Browse files Browse the repository at this point in the history
  • Loading branch information
anoadragon453 committed Feb 25, 2020
2 parents 1dfbad8 + 9fc71dc commit dd61511
Show file tree
Hide file tree
Showing 6 changed files with 274 additions and 32 deletions.
1 change: 1 addition & 0 deletions changelog.d/5897.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Switch to using the v2 Identity Service `/lookup` API where available, with fallback to v1. (Implements [MSC2134](https://github.com/matrix-org/matrix-doc/pull/2134) plus id_access_token authentication for v2 Identity Service APIs from [MSC2140](https://github.com/matrix-org/matrix-doc/pull/2140)).
56 changes: 37 additions & 19 deletions synapse/handlers/identity.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,25 +86,6 @@ def _extract_items_from_creds_dict(self, creds):
id_access_token = creds.get("id_access_token")
return client_secret, id_server, id_access_token

def create_id_access_token_header(self, id_access_token):
"""Create an Authorization header for passing to SimpleHttpClient as the header value
of an HTTP request.
Args:
id_access_token (str): An identity server access token.
Returns:
list[str]: The ascii-encoded bearer token encased in a list.
"""
# Prefix with Bearer
bearer_token = "Bearer %s" % id_access_token

# Encode headers to standard ascii
bearer_token.encode("ascii")

# Return as a list as that's how SimpleHttpClient takes header values
return [bearer_token]

@defer.inlineCallbacks
def threepid_from_creds(self, id_server, creds):
"""
Expand Down Expand Up @@ -524,6 +505,10 @@ def requestMsisdnToken(
logger.info("Proxied requestToken failed: %r", e)
raise e.to_synapse_error()

# TODO: The following methods are used for proxying IS requests using
# the CS API. They should be consolidated with those in RoomMemberHandler
# https://github.com/matrix-org/synapse-dinsic/issues/25

@defer.inlineCallbacks
def lookup_3pid(self, id_server, medium, address):
"""Looks up a 3pid in the passed identity server.
Expand Down Expand Up @@ -630,3 +615,36 @@ def _verify_any_signature(self, data, server_hostname):
return

raise AuthError(401, "No signature from server %s" % (server_hostname,))


def create_id_access_token_header(id_access_token):
"""Create an Authorization header for passing to SimpleHttpClient as the header value
of an HTTP request.
Args:
id_access_token (str): An identity server access token.
Returns:
list[str]: The ascii-encoded bearer token encased in a list.
"""
# Prefix with Bearer
bearer_token = "Bearer %s" % id_access_token

# Encode headers to standard ascii
bearer_token.encode("ascii")

# Return as a list as that's how SimpleHttpClient takes header values
return [bearer_token]


class LookupAlgorithm:
"""
Supported hashing algorithms when performing a 3PID lookup.
SHA256 - Hashing an (address, medium, pepper) combo with sha256, then url-safe base64
encoding
NONE - Not performing any hashing. Simply sending an (address, medium) combo in plaintext
"""

SHA256 = "sha256"
NONE = "none"
4 changes: 3 additions & 1 deletion synapse/handlers/room.py
Original file line number Diff line number Diff line change
Expand Up @@ -596,8 +596,8 @@ def create_room(self, requester, config, ratelimit=True, creator_join_profile=No

room_id = yield self._generate_room_id(creator_id=user_id, is_public=is_public)

directory_handler = self.hs.get_handlers().directory_handler
if room_alias:
directory_handler = self.hs.get_handlers().directory_handler
yield directory_handler.create_association(
requester=requester,
room_id=room_id,
Expand Down Expand Up @@ -683,6 +683,7 @@ def create_room(self, requester, config, ratelimit=True, creator_join_profile=No

for invite_3pid in invite_3pid_list:
id_server = invite_3pid["id_server"]
id_access_token = invite_3pid.get("id_access_token") # optional
address = invite_3pid["address"]
medium = invite_3pid["medium"]
yield self.hs.get_room_member_handler().do_3pid_invite(
Expand All @@ -694,6 +695,7 @@ def create_room(self, requester, config, ratelimit=True, creator_join_profile=No
requester,
txn_id=None,
new_room=True,
id_access_token=id_access_token,
)

result = {"room_id": room_id}
Expand Down
211 changes: 199 additions & 12 deletions synapse/handlers/room_member.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@

from six.moves import http_client

from signedjson.key import decode_verify_key_bytes
from signedjson.sign import verify_signed_json
from unpaddedbase64 import decode_base64

from twisted.internet import defer

from synapse import types
Expand All @@ -28,15 +32,14 @@
from synapse.api.errors import (
AuthError,
Codes,
ProxiedRequestError,
HttpResponseException,
SynapseError,
)
from synapse.handlers.identity import LookupAlgorithm, create_id_access_token_header
from synapse.types import RoomID, UserID
from synapse.util.async_helpers import Linearizer
from synapse.util.distributor import user_joined_room, user_left_room

from ._base import BaseHandler
from synapse.util.hash import sha256_and_url_safe_base64

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -677,7 +680,7 @@ def lookup_room_alias(self, room_alias):
servers.remove(room_alias.domain)
servers.insert(0, room_alias.domain)

return (RoomID.from_string(room_id), servers)
return RoomID.from_string(room_id), servers

@defer.inlineCallbacks
def _get_inviter(self, user_id, room_id):
Expand All @@ -698,6 +701,7 @@ def do_3pid_invite(
requester,
txn_id,
new_room=False,
id_access_token=None,
):
if self.config.block_non_admin_invites:
is_requester_admin = yield self.auth.is_server_admin(requester.user)
Expand Down Expand Up @@ -736,7 +740,12 @@ def do_3pid_invite(
Codes.FORBIDDEN,
)

invitee = yield self._lookup_3pid(id_server, medium, address)
if not self._enable_lookup:
raise SynapseError(
403, "Looking up third-party identifiers is denied from this server"
)

invitee = yield self._lookup_3pid(id_server, medium, address, id_access_token)

is_published = yield self.store.is_room_published(room_id)

Expand Down Expand Up @@ -775,9 +784,50 @@ def _get_id_server_target(self, id_server):
return id_server

@defer.inlineCallbacks
def _lookup_3pid(self, id_server, medium, address):
def _lookup_3pid(self, id_server, medium, address, id_access_token=None):
"""Looks up a 3pid in the passed identity server.
Args:
id_server (str): The server name (including port, if required)
of the identity server to use.
medium (str): The type of the third party identifier (e.g. "email").
address (str): The third party identifier (e.g. "foo@example.com").
id_access_token (str|None): The access token to authenticate to the identity
server with
Returns:
str|None: the matrix ID of the 3pid, or None if it is not recognized.
"""
# Rewrite id_server URL if necessary
id_server = self._get_id_server_target(id_server)

if id_access_token is not None:
try:
results = yield self._lookup_3pid_v2(
id_server, id_access_token, medium, address
)
return results

except Exception as e:
# Catch HttpResponseExcept for a non-200 response code
# Check if this identity server does not know about v2 lookups
if isinstance(e, HttpResponseException) and e.code == 404:
# This is an old identity server that does not yet support v2 lookups
logger.warning(
"Attempted v2 lookup on v1 identity server %s. Falling "
"back to v1",
id_server,
)
else:
logger.warning("Error when looking up hashing details: %s", e)
return None

return (yield self._lookup_3pid_v1(id_server, medium, address))

@defer.inlineCallbacks
def _lookup_3pid_v1(self, id_server, medium, address):
"""Looks up a 3pid in the passed identity server using v1 lookup.
Args:
id_server (str): The server name (including port, if required)
of the identity server to use.
Expand All @@ -788,12 +838,150 @@ def _lookup_3pid(self, id_server, medium, address):
str: the matrix ID of the 3pid, or None if it is not recognized.
"""
try:
data = yield self.identity_handler.lookup_3pid(id_server, medium, address)
return data.get("mxid")
except ProxiedRequestError as e:
logger.warn("Error from identity server lookup: %s" % (e,))
data = yield self.simple_http_client.get_json(
"%s%s/_matrix/identity/api/v1/lookup" % (id_server_scheme, id_server),
{"medium": medium, "address": address},
)

if "mxid" in data:
if "signatures" not in data:
raise AuthError(401, "No signatures on 3pid binding")
yield self._verify_any_signature(data, id_server)
return data["mxid"]

except IOError as e:
logger.warning("Error from v1 identity server lookup: %s" % (e,))

return None

@defer.inlineCallbacks
def _lookup_3pid_v2(self, id_server, id_access_token, medium, address):
"""Looks up a 3pid in the passed identity server using v2 lookup.
Args:
id_server (str): The server name (including port, if required)
of the identity server to use.
id_access_token (str): The access token to authenticate to the identity server with
medium (str): The type of the third party identifier (e.g. "email").
address (str): The third party identifier (e.g. "foo@example.com").
Returns:
Deferred[str|None]: the matrix ID of the 3pid, or None if it is not recognised.
"""
# Check what hashing details are supported by this identity server
hash_details = yield self.simple_http_client.get_json(
"%s%s/_matrix/identity/v2/hash_details" % (id_server_scheme, id_server),
{"access_token": id_access_token},
)

if not isinstance(hash_details, dict):
logger.warning(
"Got non-dict object when checking hash details of %s%s: %s",
id_server_scheme,
id_server,
hash_details,
)
raise SynapseError(
400,
"Non-dict object from %s%s during v2 hash_details request: %s"
% (id_server_scheme, id_server, hash_details),
)

# Extract information from hash_details
supported_lookup_algorithms = hash_details.get("algorithms")
lookup_pepper = hash_details.get("lookup_pepper")
if (
not supported_lookup_algorithms
or not isinstance(supported_lookup_algorithms, list)
or not lookup_pepper
or not isinstance(lookup_pepper, str)
):
raise SynapseError(
400,
"Invalid hash details received from identity server %s%s: %s"
% (id_server_scheme, id_server, hash_details),
)

# Check if any of the supported lookup algorithms are present
if LookupAlgorithm.SHA256 in supported_lookup_algorithms:
# Perform a hashed lookup
lookup_algorithm = LookupAlgorithm.SHA256

# Hash address, medium and the pepper with sha256
to_hash = "%s %s %s" % (address, medium, lookup_pepper)
lookup_value = sha256_and_url_safe_base64(to_hash)

elif LookupAlgorithm.NONE in supported_lookup_algorithms:
# Perform a non-hashed lookup
lookup_algorithm = LookupAlgorithm.NONE

# Combine together plaintext address and medium
lookup_value = "%s %s" % (address, medium)

else:
logger.warning(
"None of the provided lookup algorithms of %s are supported: %s",
id_server,
supported_lookup_algorithms,
)
raise SynapseError(
400,
"Provided identity server does not support any v2 lookup "
"algorithms that this homeserver supports.",
)

# Authenticate with identity server given the access token from the client
headers = {"Authorization": create_id_access_token_header(id_access_token)}

try:
lookup_results = yield self.simple_http_client.post_json_get_json(
"%s%s/_matrix/identity/v2/lookup" % (id_server_scheme, id_server),
{
"addresses": [lookup_value],
"algorithm": lookup_algorithm,
"pepper": lookup_pepper,
},
headers=headers,
)
except Exception as e:
logger.warning("Error when performing a v2 3pid lookup: %s", e)
raise SynapseError(
500, "Unknown error occurred during identity server lookup"
)

# Check for a mapping from what we looked up to an MXID
if "mappings" not in lookup_results or not isinstance(
lookup_results["mappings"], dict
):
logger.warning("No results from 3pid lookup")
return None

# Return the MXID if it's available, or None otherwise
mxid = lookup_results["mappings"].get(lookup_value)
return mxid

@defer.inlineCallbacks
def _verify_any_signature(self, data, server_hostname):
if server_hostname not in data["signatures"]:
raise AuthError(401, "No signature from server %s" % (server_hostname,))
for key_name, signature in data["signatures"][server_hostname].items():
key_data = yield self.simple_http_client.get_json(
"%s%s/_matrix/identity/api/v1/pubkey/%s"
% (id_server_scheme, server_hostname, key_name)
)
if "public_key" not in key_data:
raise AuthError(
401, "No public key named %s from %s" % (key_name, server_hostname)
)
verify_signed_json(
data,
server_hostname,
decode_verify_key_bytes(
key_name, decode_base64(key_data["public_key"])
),
)
return

@defer.inlineCallbacks
def _make_and_store_3pid_invite(
self, requester, id_server, medium, address, room_id, user, txn_id
Expand Down Expand Up @@ -930,7 +1118,6 @@ def _ask_id_server_for_third_party_invite(
"sender_display_name": inviter_display_name,
"sender_avatar_url": inviter_avatar_url,
}

try:
data = yield self.simple_http_client.post_json_get_json(
is_url, invite_config
Expand Down Expand Up @@ -1117,7 +1304,7 @@ def _remote_reject_invite(self, requester, remote_room_hosts, room_id, target):
# The 'except' clause is very broad, but we need to
# capture everything from DNS failures upwards
#
logger.warn("Failed to reject invite: %s", e)
logger.warning("Failed to reject invite: %s", e)

yield self.store.locally_reject_invite(target.to_string(), room_id)
return {}
Expand Down
1 change: 1 addition & 0 deletions synapse/rest/client/v1/room.py
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,7 @@ def on_POST(self, request, room_id, membership_action, txn_id=None):
requester,
txn_id,
new_room=False,
id_access_token=content.get("id_access_token"),
)
return 200, {}

Expand Down
Loading

0 comments on commit dd61511

Please sign in to comment.