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

Commit 9fc71dc

Browse files
Use the v2 Identity Service API for lookups (MSC2134 + MSC2140) (#5976)
This is a redo of #5897 but with `id_access_token` accepted. Implements [MSC2134](matrix-org/matrix-spec-proposals#2134) plus Identity Service v2 authentication ala [MSC2140](matrix-org/matrix-spec-proposals#2140). Identity lookup-related functions were also moved from `RoomMemberHandler` to `IdentityHandler`.
1 parent cbcbfe6 commit 9fc71dc

File tree

6 files changed

+238
-35
lines changed

6 files changed

+238
-35
lines changed

changelog.d/5897.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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)).

synapse/handlers/identity.py

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -74,25 +74,6 @@ def _extract_items_from_creds_dict(self, creds):
7474
id_access_token = creds.get("id_access_token")
7575
return client_secret, id_server, id_access_token
7676

77-
def create_id_access_token_header(self, id_access_token):
78-
"""Create an Authorization header for passing to SimpleHttpClient as the header value
79-
of an HTTP request.
80-
81-
Args:
82-
id_access_token (str): An identity server access token.
83-
84-
Returns:
85-
list[str]: The ascii-encoded bearer token encased in a list.
86-
"""
87-
# Prefix with Bearer
88-
bearer_token = "Bearer %s" % id_access_token
89-
90-
# Encode headers to standard ascii
91-
bearer_token.encode("ascii")
92-
93-
# Return as a list as that's how SimpleHttpClient takes header values
94-
return [bearer_token]
95-
9677
@defer.inlineCallbacks
9778
def threepid_from_creds(self, id_server, creds):
9879
"""
@@ -178,9 +159,7 @@ def bind_threepid(self, creds, mxid, use_v2=True):
178159
bind_data = {"sid": sid, "client_secret": client_secret, "mxid": mxid}
179160
if use_v2:
180161
bind_url = "https://%s/_matrix/identity/v2/3pid/bind" % (id_server,)
181-
headers["Authorization"] = self.create_id_access_token_header(
182-
id_access_token
183-
)
162+
headers["Authorization"] = create_id_access_token_header(id_access_token)
184163
else:
185164
bind_url = "https://%s/_matrix/identity/api/v1/3pid/bind" % (id_server,)
186165

@@ -478,3 +457,36 @@ def requestMsisdnToken(
478457
except HttpResponseException as e:
479458
logger.info("Proxied requestToken failed: %r", e)
480459
raise e.to_synapse_error()
460+
461+
462+
def create_id_access_token_header(id_access_token):
463+
"""Create an Authorization header for passing to SimpleHttpClient as the header value
464+
of an HTTP request.
465+
466+
Args:
467+
id_access_token (str): An identity server access token.
468+
469+
Returns:
470+
list[str]: The ascii-encoded bearer token encased in a list.
471+
"""
472+
# Prefix with Bearer
473+
bearer_token = "Bearer %s" % id_access_token
474+
475+
# Encode headers to standard ascii
476+
bearer_token.encode("ascii")
477+
478+
# Return as a list as that's how SimpleHttpClient takes header values
479+
return [bearer_token]
480+
481+
482+
class LookupAlgorithm:
483+
"""
484+
Supported hashing algorithms when performing a 3PID lookup.
485+
486+
SHA256 - Hashing an (address, medium, pepper) combo with sha256, then url-safe base64
487+
encoding
488+
NONE - Not performing any hashing. Simply sending an (address, medium) combo in plaintext
489+
"""
490+
491+
SHA256 = "sha256"
492+
NONE = "none"

synapse/handlers/room.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -579,8 +579,8 @@ def create_room(self, requester, config, ratelimit=True, creator_join_profile=No
579579

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

582+
directory_handler = self.hs.get_handlers().directory_handler
582583
if room_alias:
583-
directory_handler = self.hs.get_handlers().directory_handler
584584
yield directory_handler.create_association(
585585
requester=requester,
586586
room_id=room_id,
@@ -665,6 +665,7 @@ def create_room(self, requester, config, ratelimit=True, creator_join_profile=No
665665

666666
for invite_3pid in invite_3pid_list:
667667
id_server = invite_3pid["id_server"]
668+
id_access_token = invite_3pid.get("id_access_token") # optional
668669
address = invite_3pid["address"]
669670
medium = invite_3pid["medium"]
670671
yield self.hs.get_room_member_handler().do_3pid_invite(
@@ -675,6 +676,7 @@ def create_room(self, requester, config, ratelimit=True, creator_join_profile=No
675676
id_server,
676677
requester,
677678
txn_id=None,
679+
id_access_token=id_access_token,
678680
)
679681

680682
result = {"room_id": room_id}

synapse/handlers/room_member.py

Lines changed: 166 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@
2929
from synapse import types
3030
from synapse.api.constants import EventTypes, Membership
3131
from synapse.api.errors import AuthError, Codes, HttpResponseException, SynapseError
32+
from synapse.handlers.identity import LookupAlgorithm, create_id_access_token_header
3233
from synapse.types import RoomID, UserID
3334
from synapse.util.async_helpers import Linearizer
3435
from synapse.util.distributor import user_joined_room, user_left_room
36+
from synapse.util.hash import sha256_and_url_safe_base64
3537

3638
from ._base import BaseHandler
3739

@@ -626,7 +628,7 @@ def lookup_room_alias(self, room_alias):
626628
servers.remove(room_alias.domain)
627629
servers.insert(0, room_alias.domain)
628630

629-
return (RoomID.from_string(room_id), servers)
631+
return RoomID.from_string(room_id), servers
630632

631633
@defer.inlineCallbacks
632634
def _get_inviter(self, user_id, room_id):
@@ -638,7 +640,15 @@ def _get_inviter(self, user_id, room_id):
638640

639641
@defer.inlineCallbacks
640642
def do_3pid_invite(
641-
self, room_id, inviter, medium, address, id_server, requester, txn_id
643+
self,
644+
room_id,
645+
inviter,
646+
medium,
647+
address,
648+
id_server,
649+
requester,
650+
txn_id,
651+
id_access_token=None,
642652
):
643653
if self.config.block_non_admin_invites:
644654
is_requester_admin = yield self.auth.is_server_admin(requester.user)
@@ -661,7 +671,12 @@ def do_3pid_invite(
661671
Codes.FORBIDDEN,
662672
)
663673

664-
invitee = yield self._lookup_3pid(id_server, medium, address)
674+
if not self._enable_lookup:
675+
raise SynapseError(
676+
403, "Looking up third-party identifiers is denied from this server"
677+
)
678+
679+
invitee = yield self._lookup_3pid(id_server, medium, address, id_access_token)
665680

666681
if invitee:
667682
yield self.update_membership(
@@ -673,9 +688,47 @@ def do_3pid_invite(
673688
)
674689

675690
@defer.inlineCallbacks
676-
def _lookup_3pid(self, id_server, medium, address):
691+
def _lookup_3pid(self, id_server, medium, address, id_access_token=None):
677692
"""Looks up a 3pid in the passed identity server.
678693
694+
Args:
695+
id_server (str): The server name (including port, if required)
696+
of the identity server to use.
697+
medium (str): The type of the third party identifier (e.g. "email").
698+
address (str): The third party identifier (e.g. "[email protected]").
699+
id_access_token (str|None): The access token to authenticate to the identity
700+
server with
701+
702+
Returns:
703+
str|None: the matrix ID of the 3pid, or None if it is not recognized.
704+
"""
705+
if id_access_token is not None:
706+
try:
707+
results = yield self._lookup_3pid_v2(
708+
id_server, id_access_token, medium, address
709+
)
710+
return results
711+
712+
except Exception as e:
713+
# Catch HttpResponseExcept for a non-200 response code
714+
# Check if this identity server does not know about v2 lookups
715+
if isinstance(e, HttpResponseException) and e.code == 404:
716+
# This is an old identity server that does not yet support v2 lookups
717+
logger.warning(
718+
"Attempted v2 lookup on v1 identity server %s. Falling "
719+
"back to v1",
720+
id_server,
721+
)
722+
else:
723+
logger.warning("Error when looking up hashing details: %s", e)
724+
return None
725+
726+
return (yield self._lookup_3pid_v1(id_server, medium, address))
727+
728+
@defer.inlineCallbacks
729+
def _lookup_3pid_v1(self, id_server, medium, address):
730+
"""Looks up a 3pid in the passed identity server using v1 lookup.
731+
679732
Args:
680733
id_server (str): The server name (including port, if required)
681734
of the identity server to use.
@@ -685,10 +738,6 @@ def _lookup_3pid(self, id_server, medium, address):
685738
Returns:
686739
str: the matrix ID of the 3pid, or None if it is not recognized.
687740
"""
688-
if not self._enable_lookup:
689-
raise SynapseError(
690-
403, "Looking up third-party identifiers is denied from this server"
691-
)
692741
try:
693742
data = yield self.simple_http_client.get_json(
694743
"%s%s/_matrix/identity/api/v1/lookup" % (id_server_scheme, id_server),
@@ -702,9 +751,116 @@ def _lookup_3pid(self, id_server, medium, address):
702751
return data["mxid"]
703752

704753
except IOError as e:
705-
logger.warn("Error from identity server lookup: %s" % (e,))
754+
logger.warning("Error from v1 identity server lookup: %s" % (e,))
755+
756+
return None
757+
758+
@defer.inlineCallbacks
759+
def _lookup_3pid_v2(self, id_server, id_access_token, medium, address):
760+
"""Looks up a 3pid in the passed identity server using v2 lookup.
761+
762+
Args:
763+
id_server (str): The server name (including port, if required)
764+
of the identity server to use.
765+
id_access_token (str): The access token to authenticate to the identity server with
766+
medium (str): The type of the third party identifier (e.g. "email").
767+
address (str): The third party identifier (e.g. "[email protected]").
768+
769+
Returns:
770+
Deferred[str|None]: the matrix ID of the 3pid, or None if it is not recognised.
771+
"""
772+
# Check what hashing details are supported by this identity server
773+
hash_details = yield self.simple_http_client.get_json(
774+
"%s%s/_matrix/identity/v2/hash_details" % (id_server_scheme, id_server),
775+
{"access_token": id_access_token},
776+
)
777+
778+
if not isinstance(hash_details, dict):
779+
logger.warning(
780+
"Got non-dict object when checking hash details of %s%s: %s",
781+
id_server_scheme,
782+
id_server,
783+
hash_details,
784+
)
785+
raise SynapseError(
786+
400,
787+
"Non-dict object from %s%s during v2 hash_details request: %s"
788+
% (id_server_scheme, id_server, hash_details),
789+
)
790+
791+
# Extract information from hash_details
792+
supported_lookup_algorithms = hash_details.get("algorithms")
793+
lookup_pepper = hash_details.get("lookup_pepper")
794+
if (
795+
not supported_lookup_algorithms
796+
or not isinstance(supported_lookup_algorithms, list)
797+
or not lookup_pepper
798+
or not isinstance(lookup_pepper, str)
799+
):
800+
raise SynapseError(
801+
400,
802+
"Invalid hash details received from identity server %s%s: %s"
803+
% (id_server_scheme, id_server, hash_details),
804+
)
805+
806+
# Check if any of the supported lookup algorithms are present
807+
if LookupAlgorithm.SHA256 in supported_lookup_algorithms:
808+
# Perform a hashed lookup
809+
lookup_algorithm = LookupAlgorithm.SHA256
810+
811+
# Hash address, medium and the pepper with sha256
812+
to_hash = "%s %s %s" % (address, medium, lookup_pepper)
813+
lookup_value = sha256_and_url_safe_base64(to_hash)
814+
815+
elif LookupAlgorithm.NONE in supported_lookup_algorithms:
816+
# Perform a non-hashed lookup
817+
lookup_algorithm = LookupAlgorithm.NONE
818+
819+
# Combine together plaintext address and medium
820+
lookup_value = "%s %s" % (address, medium)
821+
822+
else:
823+
logger.warning(
824+
"None of the provided lookup algorithms of %s are supported: %s",
825+
id_server,
826+
supported_lookup_algorithms,
827+
)
828+
raise SynapseError(
829+
400,
830+
"Provided identity server does not support any v2 lookup "
831+
"algorithms that this homeserver supports.",
832+
)
833+
834+
# Authenticate with identity server given the access token from the client
835+
headers = {"Authorization": create_id_access_token_header(id_access_token)}
836+
837+
try:
838+
lookup_results = yield self.simple_http_client.post_json_get_json(
839+
"%s%s/_matrix/identity/v2/lookup" % (id_server_scheme, id_server),
840+
{
841+
"addresses": [lookup_value],
842+
"algorithm": lookup_algorithm,
843+
"pepper": lookup_pepper,
844+
},
845+
headers=headers,
846+
)
847+
except Exception as e:
848+
logger.warning("Error when performing a v2 3pid lookup: %s", e)
849+
raise SynapseError(
850+
500, "Unknown error occurred during identity server lookup"
851+
)
852+
853+
# Check for a mapping from what we looked up to an MXID
854+
if "mappings" not in lookup_results or not isinstance(
855+
lookup_results["mappings"], dict
856+
):
857+
logger.warning("No results from 3pid lookup")
706858
return None
707859

860+
# Return the MXID if it's available, or None otherwise
861+
mxid = lookup_results["mappings"].get(lookup_value)
862+
return mxid
863+
708864
@defer.inlineCallbacks
709865
def _verify_any_signature(self, data, server_hostname):
710866
if server_hostname not in data["signatures"]:
@@ -844,7 +1000,6 @@ def _ask_id_server_for_third_party_invite(
8441000
display_name (str): A user-friendly name to represent the invited
8451001
user.
8461002
"""
847-
8481003
is_url = "%s%s/_matrix/identity/api/v1/store-invite" % (
8491004
id_server_scheme,
8501005
id_server,
@@ -862,7 +1017,6 @@ def _ask_id_server_for_third_party_invite(
8621017
"sender_display_name": inviter_display_name,
8631018
"sender_avatar_url": inviter_avatar_url,
8641019
}
865-
8661020
try:
8671021
data = yield self.simple_http_client.post_json_get_json(
8681022
is_url, invite_config
@@ -1049,7 +1203,7 @@ def _remote_reject_invite(self, requester, remote_room_hosts, room_id, target):
10491203
# The 'except' clause is very broad, but we need to
10501204
# capture everything from DNS failures upwards
10511205
#
1052-
logger.warn("Failed to reject invite: %s", e)
1206+
logger.warning("Failed to reject invite: %s", e)
10531207

10541208
yield self.store.locally_reject_invite(target.to_string(), room_id)
10551209
return {}

synapse/rest/client/v1/room.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,7 @@ def on_POST(self, request, room_id, membership_action, txn_id=None):
701701
content["id_server"],
702702
requester,
703703
txn_id,
704+
content.get("id_access_token"),
704705
)
705706
return 200, {}
706707

synapse/util/hash.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# -*- coding: utf-8 -*-
2+
3+
# Copyright 2019 The Matrix.org Foundation C.I.C.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
import hashlib
18+
19+
import unpaddedbase64
20+
21+
22+
def sha256_and_url_safe_base64(input_text):
23+
"""SHA256 hash an input string, encode the digest as url-safe base64, and
24+
return
25+
26+
:param input_text: string to hash
27+
:type input_text: str
28+
29+
:returns a sha256 hashed and url-safe base64 encoded digest
30+
:rtype: str
31+
"""
32+
digest = hashlib.sha256(input_text.encode()).digest()
33+
return unpaddedbase64.encode_base64(digest, urlsafe=True)

0 commit comments

Comments
 (0)