From 87cac36127a77c7dac9cb648ac6334835fe5ac6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ensar=20Saraj=C4=8Di=C4=87?= Date: Sat, 3 Nov 2018 18:10:46 +0100 Subject: [PATCH] Add update device secret route --- app/api/resources/device.py | 13 +++++++- .../swagger/update_device_secret_spec.yaml | 26 ++++++++++++++++ app/devices/api.py | 30 +++++++++++++++++++ 3 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 app/api/resources/swagger/update_device_secret_spec.yaml diff --git a/app/api/resources/device.py b/app/api/resources/device.py index a53b133..3cad303 100644 --- a/app/api/resources/device.py +++ b/app/api/resources/device.py @@ -47,7 +47,7 @@ class RecordingsQuerySchema(Schema): class DeviceSecretSchema(BaseResourceSchema): device_secret = fields.String(dump_only=True) - secret_algorithm = fields.String() + secret_algorithm = fields.String(required=True) class DeviceShareSchema(BaseResourceSchema): @@ -173,6 +173,17 @@ class DeviceSecretResource(ProtectedResource): validate_device_ownership(device_id) return DeviceSecretSchema().dump(devices.get_device(device_id)), 200 + @use_args(DeviceSecretSchema(), locations=('json',)) + @swag_from('swagger/update_device_secret_spec.yaml') + def put(self, args, device_id): + validate_device_ownership(device_id) + return DeviceSecretSchema().dump( + devices.update_algorithm( + device_id, + args['secret_algorithm'] + ) + ), 200 + class DeviceSecretResetResource(ProtectedResource): @swag_from('swagger/reset_device_secret_spec.yaml') diff --git a/app/api/resources/swagger/update_device_secret_spec.yaml b/app/api/resources/swagger/update_device_secret_spec.yaml new file mode 100644 index 0000000..24f7e88 --- /dev/null +++ b/app/api/resources/swagger/update_device_secret_spec.yaml @@ -0,0 +1,26 @@ +Updates device secret info (algorithm) +--- +tags: + - Device +parameters: + - in: path + name: device_id + required: true + type: integer + description: Id of the device + - in: body + name: body + required: true + schema: + type: object + $ref: '#/definitions/DeviceSecretInfo' +responses: + 200: + description: Success + schema: + type: object + required: + - content + properties: + content: + $ref: '#/definitions/DeviceSecretInfo' diff --git a/app/devices/api.py b/app/devices/api.py index 1ad844d..8b5fb7f 100644 --- a/app/devices/api.py +++ b/app/devices/api.py @@ -2,6 +2,7 @@ import sys import hmac import urllib.parse import datetime +import hashlib from secrets import token_urlsafe from .models import (Device, Recording, @@ -155,6 +156,11 @@ def reset_device_secret(device_id): """ Resets device secret for device with given parameters. Raises error on failure + + :param device_id: Id of device + :type device_id: int + :returns: Requested device + :rtype: Device """ device = Device.get(id=device_id) device.device_secret = token_urlsafe(32) @@ -162,6 +168,30 @@ def reset_device_secret(device_id): return device +def update_algorithm(device_id, algorithm): + """ + Updates device secret algorithm for device with given parameters. Raises + error on failure + + :param device_id: Id of device + :type device_id: int + :param algorithm: Name of new algorithm + :type algorithm: string + :returns: Requested device + :rtype: Device + """ + if algorithm not in hashlib.algorithms_available: + raise ValueError("Unsupported algorithm! Supported algorithms: " + + str(hashlib.algorithms_available) + ". Some of " + + "these may not work on all platforms. These are " + + "guaranteed to work on every platform: " + + str(hashlib.algorithms_guaranteed)) + device = Device.get(id=device_id) + device.secret_algorithm = algorithm + device.save() + return device + + def can_user_access_device(account_id, device_id): """ Checks if user with given account_id can access device with given device_id