From 802fb3baad8aed1c0f9dcfb040470947dfceb225 Mon Sep 17 00:00:00 2001 From: gruebel Date: Mon, 21 Oct 2019 22:51:00 +0200 Subject: [PATCH] Connect user with virtual mfa device --- moto/iam/models.py | 21 +++++++++++++ moto/iam/responses.py | 10 +++---- tests/test_iam/test_iam.py | 61 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+), 5 deletions(-) diff --git a/moto/iam/models.py b/moto/iam/models.py index 8921244cd..d7e8f8657 100644 --- a/moto/iam/models.py +++ b/moto/iam/models.py @@ -1251,6 +1251,21 @@ class IAMBackend(BaseBackend): "Device {0} already exists".format(serial_number) ) + device = self.virtual_mfa_devices.get(serial_number, None) + if device: + device.enable_date = datetime.utcnow() + device.user = user + device.user_attribute = { + 'Path': user.path, + 'UserName': user.name, + 'UserId': user.id, + 'Arn': user.arn, + 'CreateDate': user.created_iso_8601, + 'PasswordLastUsed': None, # not supported + 'PermissionsBoundary': {}, # ToDo: add put_user_permissions_boundary() functionality + 'Tags': {} # ToDo: add tag_user() functionality + } + user.enable_mfa_device( serial_number, authentication_code_1, @@ -1265,6 +1280,12 @@ class IAMBackend(BaseBackend): "Device {0} not found".format(serial_number) ) + device = self.virtual_mfa_devices.get(serial_number, None) + if device: + device.enable_date = None + device.user = None + device.user_attribute = None + user.deactivate_mfa_device(serial_number) def list_mfa_devices(self, user_name): diff --git a/moto/iam/responses.py b/moto/iam/responses.py index 8814a25a1..01cbeb712 100644 --- a/moto/iam/responses.py +++ b/moto/iam/responses.py @@ -1739,11 +1739,11 @@ LIST_VIRTUAL_MFA_DEVICES_TEMPLATE = """