Change the concept of min complex character number 15/118615/1
authorDongsun Lee <ds73.lee@samsung.com>
Mon, 13 Mar 2017 09:09:02 +0000 (18:09 +0900)
committerDongsun Lee <ds73.lee@samsung.com>
Mon, 13 Mar 2017 09:10:35 +0000 (18:10 +0900)
- complexity 1 : Character + Number
- complexity 2 : Character + Number(same with complexity 1)
- complexity 3 : Character + Number + Special character
- complexity 4 : Upper case + Lower case + Number + Special character

Change-Id: Ia20ca456a1adc35c2340512f07c27bfc2a40c02f
Signed-off-by: Dongsun Lee <ds73.lee@samsung.com>
src/client/client-password-admin.cpp
src/include/auth-passwd-policy-types.h
src/server/service/policy-file.cpp

index 8b94a194833b7e59356c5d5a7e0399bd8d0a3242..166ad5f4e8e520bb53f646056054aa3f8548a5f7 100644 (file)
@@ -159,6 +159,8 @@ int auth_passwd_set_min_complex_char_num(policy_h *p_policy, unsigned int val)
 {
        if (!p_policy)
                return AUTH_PASSWD_API_ERROR_INPUT_PARAM;
+       if (val > AUTH_PWD_COMPLEX_CHAR_LAST)
+               return AUTH_PASSWD_API_ERROR_INPUT_PARAM;
 
        auto policy = reinterpret_cast<AuthPasswd::Policy *>(p_policy);
        policy->setFlag(POLICY_MIN_COMPLEX_CHAR_NUMBER);
index e024671f0eac6adeffff3a4808464cf620a9020b..8d60bf16405876ece1a5df859725044e313513df 100644 (file)
@@ -58,6 +58,15 @@ typedef enum {
        AUTH_PWD_QUALITY_LAST = AUTH_PWD_QUALITY_ALPHANUMERIC
 } password_quality_type;
 
+typedef enum {
+       AUTH_PWD_COMPLEX_CHAR_UNSPECIFIED = 0,
+       AUTH_PWD_COMPLEX_CHAR_GROUP_1 = 1, // Character + Number
+       AUTH_PWD_COMPLEX_CHAR_GROUP_2 = 2, // Same with GROUP1(It is the requirement from EAS.)
+       AUTH_PWD_COMPLEX_CHAR_GROUP_3 = 3, // Character + Number + Special character
+       AUTH_PWD_COMPLEX_CHAR_GROUP_4 = 4, // Upper case + Lower case + Number + Special character
+       AUTH_PWD_COMPLEX_CHAR_LAST = AUTH_PWD_COMPLEX_CHAR_GROUP_4,
+} password_complex_char_group;
+
 #ifdef __cplusplus
 }
 #endif
index a25343cba47420581c398f914d5b9f8c66acd72a..5fe7c3e729fe30627666ac74d74f6948a41d5e4f 100644 (file)
@@ -50,6 +50,17 @@ const unsigned int CURRENT_FILE_VERSION = 1;
 } // namespace anonymous
 
 namespace AuthPasswd {
+
+// This is a same policy wiht Android.
+// complexity 1 : Character + Number
+// complexity 2 : Character + Number (same with complexity1. It is the requirement from EAS.)
+// complexity 3 : Character + Number + Special character
+// complexity 4 : Upper case + Lower case + Number + Special character
+const std::string REGEX_COMPLEX_GROUP1 = "(?=.*[A-Za-z]+.*)(?=.*[0-9]+.*)";
+const std::string REGEX_COMPLEX_GROUP2 = REGEX_COMPLEX_GROUP1;
+const std::string REGEX_COMPLEX_GROUP3 = "(?=.*[A-Za-z]+.*)(?=.*[0-9]+.*)(?=.*[^A-Za-z0-9]+.*)";
+const std::string REGEX_COMPLEX_GROUP4 = "(?=.*[A-Z]+.*)(?=.*[a-z]+.*)(?=.*[0-9]+.*)(?=.*[^A-Za-z0-9]+.*)";
+
 PolicyFile::PolicyFile(unsigned int user): m_user(user), m_enable(false)
 {
        // check if data directory exists
@@ -185,20 +196,45 @@ void PolicyFile::setMinLength(unsigned int minLength)
 // policy minComplexCharNumber
 bool PolicyFile::checkMinComplexCharNumber(const std::string &password) const
 {
-       unsigned int i = 0, cnt = 0;
-       char ch;
+       std::string pattern;
 
-       if (m_policy.minComplexCharNumber == 0)
+       switch (m_policy.minComplexCharNumber) {
+       case AUTH_PWD_COMPLEX_CHAR_UNSPECIFIED:
                return true;
 
-       for (i = 0; i < password.size(); i++) {
-               ch = password[i];
+       case AUTH_PWD_COMPLEX_CHAR_GROUP_1:
+               pattern = REGEX_COMPLEX_GROUP1;
+               break;
+
+       case AUTH_PWD_COMPLEX_CHAR_GROUP_2:
+               pattern = REGEX_COMPLEX_GROUP2;
+               break;
 
-               if (ch < '0' || ('9' < ch && ch < 'A') || ('Z' < ch && ch < 'a')  || 'z' < ch)
-                       cnt++;
+       case AUTH_PWD_COMPLEX_CHAR_GROUP_3:
+               pattern = REGEX_COMPLEX_GROUP3;
+               break;
+
+       case AUTH_PWD_COMPLEX_CHAR_GROUP_4:
+               pattern = REGEX_COMPLEX_GROUP4;
+               break;
+
+       default:
+               return false;
        }
 
-       return (cnt >= m_policy.minComplexCharNumber);
+       try {
+               std::regex rx(pattern);
+               std::smatch match;
+               return std::regex_search(password, match, rx);
+       } catch (const std::regex_error& rerr) {
+               LogError("Fail to check min complex char number due to invalid pattern: minComplexCharNumber="
+                       << m_policy.minComplexCharNumber << ", Pattern=" << pattern << ", error=" << rerr.code());
+               return false;
+       } catch (...) {
+               LogError("Fail to check min complex char number with unknown reason: minComplexCharNumber="
+                       << m_policy.minComplexCharNumber << ", Pattern=" << pattern);
+               return false;
+       }
 }
 
 void PolicyFile::setMinComplexCharNumber(unsigned int minComplexCharNumber)