Implement User Pool MFA Actions (#3903)
* implement user pool mfa actions * Add messages to errors Add messages to errors Fix error message * Change exception type * fix validation & add more tests Co-authored-by: George Lewis <glewis@evertz.com>
This commit is contained in:
parent
29ecd32752
commit
f76571199f
3 changed files with 156 additions and 1 deletions
|
|
@ -83,6 +83,10 @@ class CognitoIdpUserPool(BaseModel):
|
|||
self.creation_date = datetime.datetime.utcnow()
|
||||
self.last_modified_date = datetime.datetime.utcnow()
|
||||
|
||||
self.mfa_config = "OFF"
|
||||
self.sms_mfa_config = None
|
||||
self.token_mfa_config = None
|
||||
|
||||
self.clients = OrderedDict()
|
||||
self.identity_providers = OrderedDict()
|
||||
self.groups = OrderedDict()
|
||||
|
|
@ -105,6 +109,7 @@ class CognitoIdpUserPool(BaseModel):
|
|||
"Status": self.status,
|
||||
"CreationDate": time.mktime(self.creation_date.timetuple()),
|
||||
"LastModifiedDate": time.mktime(self.last_modified_date.timetuple()),
|
||||
"MfaConfiguration": self.mfa_config,
|
||||
}
|
||||
|
||||
def to_json(self, extended=False):
|
||||
|
|
@ -391,6 +396,25 @@ class CognitoIdpBackend(BaseBackend):
|
|||
self.user_pools[user_pool.id] = user_pool
|
||||
return user_pool
|
||||
|
||||
def set_user_pool_mfa_config(
|
||||
self, user_pool_id, sms_config, token_config, mfa_config
|
||||
):
|
||||
user_pool = self.describe_user_pool(user_pool_id)
|
||||
user_pool.mfa_config = mfa_config
|
||||
user_pool.sms_mfa_config = sms_config
|
||||
user_pool.token_mfa_config = token_config
|
||||
|
||||
return self.get_user_pool_mfa_config(user_pool_id)
|
||||
|
||||
def get_user_pool_mfa_config(self, user_pool_id):
|
||||
user_pool = self.describe_user_pool(user_pool_id)
|
||||
|
||||
return {
|
||||
"SmsMfaConfiguration": user_pool.sms_mfa_config,
|
||||
"SoftwareTokenMfaConfiguration": user_pool.token_mfa_config,
|
||||
"MfaConfiguration": user_pool.mfa_config,
|
||||
}
|
||||
|
||||
@paginate(60)
|
||||
def list_user_pools(self, max_results=None, next_token=None):
|
||||
return self.user_pools.values()
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import os
|
|||
|
||||
from moto.core.responses import BaseResponse
|
||||
from .models import cognitoidp_backends, find_region_by_value, UserStatus
|
||||
from .exceptions import InvalidParameterException
|
||||
|
||||
|
||||
class CognitoIdpResponse(BaseResponse):
|
||||
|
|
@ -20,6 +21,40 @@ class CognitoIdpResponse(BaseResponse):
|
|||
)
|
||||
return json.dumps({"UserPool": user_pool.to_json(extended=True)})
|
||||
|
||||
def set_user_pool_mfa_config(self):
|
||||
user_pool_id = self._get_param("UserPoolId")
|
||||
sms_config = self._get_param("SmsMfaConfiguration", None)
|
||||
token_config = self._get_param("SoftwareTokenMfaConfiguration", None)
|
||||
mfa_config = self._get_param("MfaConfiguration")
|
||||
|
||||
if mfa_config not in ["ON", "OFF", "OPTIONAL"]:
|
||||
raise InvalidParameterException(
|
||||
"[MfaConfiguration] must be one of 'ON', 'OFF', or 'OPTIONAL'."
|
||||
)
|
||||
|
||||
if mfa_config in ["ON", "OPTIONAL"]:
|
||||
if sms_config is None and token_config is None:
|
||||
raise InvalidParameterException(
|
||||
"At least one of [SmsMfaConfiguration] or [SoftwareTokenMfaConfiguration] must be provided."
|
||||
)
|
||||
if sms_config is not None:
|
||||
if "SmsConfiguration" not in sms_config:
|
||||
raise InvalidParameterException(
|
||||
"[SmsConfiguration] is a required member of [SoftwareTokenMfaConfiguration]."
|
||||
)
|
||||
|
||||
response = cognitoidp_backends[self.region].set_user_pool_mfa_config(
|
||||
user_pool_id, sms_config, token_config, mfa_config
|
||||
)
|
||||
return json.dumps(response)
|
||||
|
||||
def get_user_pool_mfa_config(self):
|
||||
user_pool_id = self._get_param("UserPoolId")
|
||||
response = cognitoidp_backends[self.region].get_user_pool_mfa_config(
|
||||
user_pool_id
|
||||
)
|
||||
return json.dumps(response)
|
||||
|
||||
def list_user_pools(self):
|
||||
max_results = self._get_param("MaxResults")
|
||||
next_token = self._get_param("NextToken", "0")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue