Merge pull request #8 from PeerD/patch-1

Add option to replace 3PIDs in synapse if removed/changed in the backend
This commit is contained in:
Max Dor 2019-03-13 21:15:27 +01:00 committed by GitHub
commit 776f3da0de
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 2 deletions

View File

@ -87,14 +87,16 @@ If you would like to change the behaviour, you can use the following configurati
``` ```
3PIDs received from the backend are merged with the ones already linked to the account. 3PIDs received from the backend are merged with the ones already linked to the account.
If you would like to change this behaviour, you can use the following configuration item: If you would like to change this behaviour, you can use the following configuration items:
```yaml ```yaml
config: config:
policy: policy:
all: all:
threepid: threepid:
update: false update: false
replace: false
``` ```
If update is set to `false`, the 3PIDs will not be changed at all. If replace is set to `true`, all 3PIDs not available in the backend anymore will be deleted from synapse.
## Integrate ## Integrate
To use this module with your back-end, you will need to implement a single REST endpoint: To use this module with your back-end, you will need to implement a single REST endpoint:

View File

@ -90,9 +90,12 @@ class RestAuthProvider(object):
if (self.config.updateThreepid): if (self.config.updateThreepid):
if "three_pids" in profile: if "three_pids" in profile:
logger.info("Handling 3PIDs") logger.info("Handling 3PIDs")
external_3pids = []
for threepid in profile["three_pids"]: for threepid in profile["three_pids"]:
medium = threepid["medium"].lower() medium = threepid["medium"].lower()
address = threepid["address"].lower() address = threepid["address"].lower()
external_3pids.append({"medium": medium, "address": address})
logger.info("Looking for 3PID %s:%s in user profile", medium, address) logger.info("Looking for 3PID %s:%s in user profile", medium, address)
validated_at = self.account_handler.hs.get_clock().time_msec() validated_at = self.account_handler.hs.get_clock().time_msec()
@ -107,6 +110,20 @@ class RestAuthProvider(object):
) )
else: else:
logger.info("3PID is present, skipping") logger.info("3PID is present, skipping")
if (self.config.replaceThreepid):
for threepid in (yield store.user_get_threepids(user_id)):
medium = threepid["medium"].lower()
address = threepid["address"].lower()
if {"medium": medium, "address": address} not in external_3pids:
logger.info("3PID is not present in external datastore, deleting")
yield store.user_delete_threepid(
user_id,
medium,
address
)
else: else:
logger.info("3PIDs were not updated due to policy") logger.info("3PIDs were not updated due to policy")
else: else:
@ -125,6 +142,7 @@ class RestAuthProvider(object):
setNameOnRegister = True setNameOnRegister = True
setNameOnLogin = False setNameOnLogin = False
updateThreepid = True updateThreepid = True
replaceThreepid = False
rest_config = _RestConfig() rest_config = _RestConfig()
rest_config.endpoint = config["endpoint"] rest_config.endpoint = config["endpoint"]
@ -165,6 +183,15 @@ class RestAuthProvider(object):
# we don't care # we don't care
pass pass
try:
rest_config.replaceThreepid = config['policy']['all']['threepid']['replace']
except TypeError:
# we don't care
pass
except KeyError:
# we don't care
pass
return rest_config return rest_config
def _require_keys(config, required): def _require_keys(config, required):
@ -175,4 +202,3 @@ def _require_keys(config, required):
", ".join(missing) ", ".join(missing)
) )
) )