2016-07-15 06:19:07 -06:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Copyright 2016 OpenMarket Ltd
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
2016-07-20 10:58:44 -06:00
|
|
|
|
|
|
|
from synapse.api import errors
|
2016-07-15 06:19:07 -06:00
|
|
|
from synapse.util import stringutils
|
|
|
|
from twisted.internet import defer
|
|
|
|
from ._base import BaseHandler
|
|
|
|
|
|
|
|
import logging
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class DeviceHandler(BaseHandler):
|
|
|
|
def __init__(self, hs):
|
|
|
|
super(DeviceHandler, self).__init__(hs)
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def check_device_registered(self, user_id, device_id,
|
2016-08-03 07:30:06 -06:00
|
|
|
initial_device_display_name=None):
|
2016-07-15 06:19:07 -06:00
|
|
|
"""
|
|
|
|
If the given device has not been registered, register it with the
|
|
|
|
supplied display name.
|
|
|
|
|
|
|
|
If no device_id is supplied, we make one up.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
user_id (str): @user:id
|
|
|
|
device_id (str | None): device id supplied by client
|
|
|
|
initial_device_display_name (str | None): device display name from
|
|
|
|
client
|
|
|
|
Returns:
|
|
|
|
str: device id (generated if none was supplied)
|
|
|
|
"""
|
|
|
|
if device_id is not None:
|
|
|
|
yield self.store.store_device(
|
|
|
|
user_id=user_id,
|
|
|
|
device_id=device_id,
|
|
|
|
initial_device_display_name=initial_device_display_name,
|
|
|
|
ignore_if_known=True,
|
|
|
|
)
|
|
|
|
defer.returnValue(device_id)
|
|
|
|
|
|
|
|
# if the device id is not specified, we'll autogen one, but loop a few
|
|
|
|
# times in case of a clash.
|
|
|
|
attempts = 0
|
|
|
|
while attempts < 5:
|
|
|
|
try:
|
2016-09-12 17:02:39 -06:00
|
|
|
device_id = stringutils.random_string(10).upper()
|
2016-07-15 06:19:07 -06:00
|
|
|
yield self.store.store_device(
|
|
|
|
user_id=user_id,
|
|
|
|
device_id=device_id,
|
|
|
|
initial_device_display_name=initial_device_display_name,
|
|
|
|
ignore_if_known=False,
|
|
|
|
)
|
|
|
|
defer.returnValue(device_id)
|
2016-07-20 10:58:44 -06:00
|
|
|
except errors.StoreError:
|
2016-07-15 06:19:07 -06:00
|
|
|
attempts += 1
|
|
|
|
|
2016-07-20 10:58:44 -06:00
|
|
|
raise errors.StoreError(500, "Couldn't generate a device ID.")
|
2016-07-20 09:34:00 -06:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def get_devices_by_user(self, user_id):
|
|
|
|
"""
|
|
|
|
Retrieve the given user's devices
|
|
|
|
|
|
|
|
Args:
|
|
|
|
user_id (str):
|
|
|
|
Returns:
|
2016-07-21 08:55:13 -06:00
|
|
|
defer.Deferred: list[dict[str, X]]: info on each device
|
2016-07-20 09:34:00 -06:00
|
|
|
"""
|
|
|
|
|
2016-07-21 08:55:13 -06:00
|
|
|
device_map = yield self.store.get_devices_by_user(user_id)
|
2016-07-20 09:34:00 -06:00
|
|
|
|
|
|
|
ips = yield self.store.get_last_client_ip_by_device(
|
2016-07-21 08:55:13 -06:00
|
|
|
devices=((user_id, device_id) for device_id in device_map.keys())
|
2016-07-20 09:34:00 -06:00
|
|
|
)
|
|
|
|
|
2016-07-21 08:55:13 -06:00
|
|
|
devices = device_map.values()
|
|
|
|
for device in devices:
|
2016-07-20 10:58:44 -06:00
|
|
|
_update_device_from_client_ips(device, ips)
|
2016-07-20 09:34:00 -06:00
|
|
|
|
|
|
|
defer.returnValue(devices)
|
2016-07-20 10:58:44 -06:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def get_device(self, user_id, device_id):
|
|
|
|
""" Retrieve the given device
|
|
|
|
|
|
|
|
Args:
|
|
|
|
user_id (str):
|
2016-07-22 07:52:53 -06:00
|
|
|
device_id (str):
|
2016-07-20 10:58:44 -06:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
defer.Deferred: dict[str, X]: info on the device
|
|
|
|
Raises:
|
|
|
|
errors.NotFoundError: if the device was not found
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
device = yield self.store.get_device(user_id, device_id)
|
2016-07-21 06:15:15 -06:00
|
|
|
except errors.StoreError:
|
2016-07-20 10:58:44 -06:00
|
|
|
raise errors.NotFoundError
|
|
|
|
ips = yield self.store.get_last_client_ip_by_device(
|
|
|
|
devices=((user_id, device_id),)
|
|
|
|
)
|
|
|
|
_update_device_from_client_ips(device, ips)
|
|
|
|
defer.returnValue(device)
|
|
|
|
|
2016-07-22 07:52:53 -06:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def delete_device(self, user_id, device_id):
|
|
|
|
""" Delete the given device
|
|
|
|
|
|
|
|
Args:
|
|
|
|
user_id (str):
|
|
|
|
device_id (str):
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
defer.Deferred:
|
|
|
|
"""
|
|
|
|
|
|
|
|
try:
|
|
|
|
yield self.store.delete_device(user_id, device_id)
|
|
|
|
except errors.StoreError, e:
|
|
|
|
if e.code == 404:
|
|
|
|
# no match
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
raise
|
|
|
|
|
2016-07-26 04:09:47 -06:00
|
|
|
yield self.store.user_delete_access_tokens(
|
|
|
|
user_id, device_id=device_id,
|
|
|
|
delete_refresh_tokens=True,
|
|
|
|
)
|
2016-07-22 07:52:53 -06:00
|
|
|
|
2016-07-27 05:18:03 -06:00
|
|
|
yield self.store.delete_e2e_keys_by_device(
|
|
|
|
user_id=user_id, device_id=device_id
|
|
|
|
)
|
|
|
|
|
2016-07-25 10:51:24 -06:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def update_device(self, user_id, device_id, content):
|
|
|
|
""" Update the given device
|
|
|
|
|
|
|
|
Args:
|
|
|
|
user_id (str):
|
|
|
|
device_id (str):
|
|
|
|
content (dict): body of update request
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
defer.Deferred:
|
|
|
|
"""
|
|
|
|
|
|
|
|
try:
|
|
|
|
yield self.store.update_device(
|
|
|
|
user_id,
|
|
|
|
device_id,
|
|
|
|
new_display_name=content.get("display_name")
|
|
|
|
)
|
|
|
|
except errors.StoreError, e:
|
|
|
|
if e.code == 404:
|
|
|
|
raise errors.NotFoundError()
|
|
|
|
else:
|
|
|
|
raise
|
2016-07-22 07:52:53 -06:00
|
|
|
|
2016-07-20 10:58:44 -06:00
|
|
|
|
|
|
|
def _update_device_from_client_ips(device, client_ips):
|
|
|
|
ip = client_ips.get((device["user_id"], device["device_id"]), {})
|
|
|
|
device.update({
|
|
|
|
"last_seen_ts": ip.get("last_seen"),
|
|
|
|
"last_seen_ip": ip.get("ip"),
|
|
|
|
})
|