CognitoIDP: Validate Password function is added (#5757)

This commit is contained in:
aarushisoni 2022-12-14 03:08:27 +05:30 committed by GitHub
parent 3c7bdcc5ea
commit 860d8bf4b7

View File

@ -4,6 +4,9 @@ import os
import time import time
import typing import typing
import enum import enum
import re
import boto3
from botocore.exceptions import ClientError
from jose import jws from jose import jws
from collections import OrderedDict from collections import OrderedDict
from typing import Any, Dict, List, Tuple, Optional, Set from typing import Any, Dict, List, Tuple, Optional, Set
@ -18,6 +21,7 @@ from .exceptions import (
UserNotConfirmedException, UserNotConfirmedException,
InvalidParameterException, InvalidParameterException,
ExpiredCodeException, ExpiredCodeException,
InvalidPasswordException,
) )
from .utils import ( from .utils import (
create_id, create_id,
@ -39,6 +43,10 @@ class UserStatus(str, enum.Enum):
UNCONFIRMED = "UNCONFIRMED" UNCONFIRMED = "UNCONFIRMED"
RESET_REQUIRED = "RESET_REQUIRED" RESET_REQUIRED = "RESET_REQUIRED"
class InvalidPasswordException(Exception):
"Raised when the input value is less than 18"
pass
class AuthFlow(str, enum.Enum): class AuthFlow(str, enum.Enum):
# Order follows AWS' order # Order follows AWS' order
@ -1963,10 +1971,41 @@ class CognitoIdpBackend(BaseBackend):
user.preferred_mfa_setting = "SMS_MFA" user.preferred_mfa_setting = "SMS_MFA"
return None return None
def validate_password(password):
tmp = password
lgt = len(tmp)
try:
if(lgt > 5 and lgt <99):
flagl = True
else:
flagl = False
flagn = bool(re.match("\d", tmp))
sc = "^ $ * . [ ] { } ( ) ? ! @ # % & / \ , > < ' : ; | _ ~ ` = + -"
for i in tmp:
if i in sc:
flagsc = True
break
else:
flagsc = False
flagu = bool(re.match('[A-Z]+', tmp))
flaglo = bool(re.match('[a-z]+', tmp))
if(flagl and flagn and flagsc and flagu and flaglo):
return True
else:
raise InvalidPasswordException("The Password is invalid")
except ClientError as e:
print(e)
def admin_set_user_password( def admin_set_user_password(
self, user_pool_id: str, username: str, password: str, permanent: bool self, user_pool_id: str, username: str, password: str, permanent: bool
) -> None: ) -> None:
user = self.admin_get_user(user_pool_id, username) user = self.admin_get_user(user_pool_id, username)
#user.password = password
flag = False
flag = validate_password(password)
if(flag == True):
user.password = password user.password = password
if permanent: if permanent:
user.status = UserStatus.CONFIRMED user.status = UserStatus.CONFIRMED