From dc5cc99afd9f7acdb2491af8a36dac624f7f9052 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adolfo=20G=C3=B3mez=20Garc=C3=ADa?= Date: Mon, 1 Jan 2024 05:16:33 +0100 Subject: [PATCH] Fixed managed and unmanaged actor_v3 --- client | 2 +- server/src/uds/REST/methods/actor_v3.py | 7 +++-- server/tests/REST/actor/test_initialize.py | 31 +++++++++++----------- server/tests/utils/test.py | 5 +++- 4 files changed, 26 insertions(+), 19 deletions(-) diff --git a/client b/client index d340a8075..c69972535 160000 --- a/client +++ b/client @@ -1 +1 @@ -Subproject commit d340a8075e7692354a1b486fde98d78ba165890b +Subproject commit c699725350b09f1f9f3440708b3ed7659e0daa3e diff --git a/server/src/uds/REST/methods/actor_v3.py b/server/src/uds/REST/methods/actor_v3.py index cdf3a39f0..ea1fabebb 100644 --- a/server/src/uds/REST/methods/actor_v3.py +++ b/server/src/uds/REST/methods/actor_v3.py @@ -389,8 +389,8 @@ class Initialize(ActorV3Action): ) -> dict[str, typing.Any]: return ActorV3Action.actorResult( { - 'own_token': alias_token or own_token, # Compat with old actor versions, TBR on 5.0 - 'token': alias_token or own_token, # New token, will be used from now onwards + 'own_token': own_token or alias_token, # Compat with old actor versions, TBR on 5.0 + 'token': own_token or alias_token, # New token, will be used from now onwards 'unique_id': unique_id, 'os': os, } @@ -410,6 +410,9 @@ class Initialize(ActorV3Action): # Not on alias token, try to locate on Service table if not service: service = typing.cast('Service', Service.objects.get(token=token)) + # If exists, create and alias for it + alias_token = CryptoManager().randomString(40) # fix alias with new token + service.aliases.create(alias=alias_token) # Locate an userService that belongs to this service and which # Build the possible ids and make initial filter to match service diff --git a/server/tests/REST/actor/test_initialize.py b/server/tests/REST/actor/test_initialize.py index dd2f67eb1..4a43c602f 100644 --- a/server/tests/REST/actor/test_initialize.py +++ b/server/tests/REST/actor/test_initialize.py @@ -110,10 +110,8 @@ class ActorInitializeTest(rest.test.RESTActorTestCase): result = success(unique_id) # Ensure own token is assigned - self.assertEqual(result['own_token'], user_service.uuid) - - # Ensure no alias token is provided - self.assertIsNone(result['alias_token']) + self.assertEqual(result['token'], user_service.uuid) + self.assertEqual(result['own_token'], result['token']) # Compat value with 3.x actors # Ensure unique_id detected is ours self.assertEqual(result['unique_id'], unique_id) @@ -133,8 +131,8 @@ class ActorInitializeTest(rest.test.RESTActorTestCase): # Now invoke failure with valid token but invalid mac result = failure(actor_token, 'invalid mac', False) - self.assertIsNone(result['own_token'], None) - self.assertIsNone(result['alias_token']) + self.assertIsNone(result['own_token']) + self.assertIsNone(result['token']) self.assertIsNone(result['os']) self.assertIsNone(result['unique_id']) @@ -157,10 +155,13 @@ class ActorInitializeTest(rest.test.RESTActorTestCase): ) # Unmanaged host is the response for initialization of unmanaged actor ALWAYS - self.assertIsNone(result['alias_token']) - self.assertIsNone(result['own_token']) + self.assertIsInstance(result['token'], str) + self.assertEqual(result['token'], result['own_token']) self.assertIsNone(result['unique_id']) self.assertIsNone(result['os']) + + # Store alias token for later tests + alias_token = result['token'] # Now, invoke a "nice" initialize result = success( @@ -168,23 +169,23 @@ class ActorInitializeTest(rest.test.RESTActorTestCase): mac=unique_id, ) - alias_token = result['alias_token'] + token = result['token'] - self.assertIsInstance(alias_token, str) - self.assertEqual(result['own_token'], user_service.uuid) - self.assertEqual(result['alias_token'], alias_token) + self.assertIsInstance(token, str) + self.assertEqual(token, user_service.uuid) + self.assertEqual(token, result['own_token']) self.assertEqual(result['unique_id'], unique_id) # Ensure that the alias returned is on alias db, and it points to the same service as the one we belong to - alias = models.ServiceTokenAlias.objects.get(alias=result['alias_token']) + alias = models.ServiceTokenAlias.objects.get(alias=alias_token) self.assertEqual(alias.service, user_service.deployed_service.service) # Now, we should be able to "initialize" with valid mac and with original and alias tokens # If we call initialize and we get "own-token" means that we have already logged in with this data result = success(alias_token, mac=unique_id) - self.assertEqual(result['own_token'], user_service.uuid) - self.assertEqual(result['alias_token'], alias_token) + self.assertEqual(result['token'], user_service.uuid) + self.assertEqual(result['token'], result['own_token']) self.assertEqual(result['unique_id'], unique_id) # diff --git a/server/tests/utils/test.py b/server/tests/utils/test.py index 688fc8497..476dea1b1 100644 --- a/server/tests/utils/test.py +++ b/server/tests/utils/test.py @@ -52,7 +52,10 @@ class UDSHttpResponse(HttpResponse): def __init__(self, content, *args, **kwargs): super().__init__(content, *args, **kwargs) - self.content = content + self.content = content # type: ignore # mypy does not know about this setter + + def json(self) -> typing.Any: + return super().json() # type: ignore # mypy does not know about this method class UDSClientMixin: