-
-
Notifications
You must be signed in to change notification settings - Fork 10.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: implement password policies to avoid weak passwords
Signed-off-by: WillardHu <wei.hu@daocloud.io>
- Loading branch information
Showing
9 changed files
with
207 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/util/UserPasswordChecker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright 2021 Apollo Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
package com.ctrip.framework.apollo.portal.util; | ||
|
||
import com.google.common.base.Strings; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.regex.Pattern; | ||
|
||
public class UserPasswordChecker { | ||
|
||
private static final Pattern PWD_PATTERN = Pattern | ||
.compile("^(?=.*[0-9].*)(?=.*[a-z].*).{8,20}$"); | ||
|
||
private static final List<String> LIST_OF_CODE_FRAGMENT = Arrays.asList( | ||
"111", "222", "333", "444", "555", "666", "777", "888", "999", "000", | ||
"001122", "112233", "223344", "334455", "445566", "556677", "667788", "778899", "889900", | ||
"009988", "998877", "887766", "776655", "665544", "554433", "443322", "332211", "221100", | ||
"0123", "1234", "2345", "3456", "4567", "5678", "6789", "7890", | ||
"0987", "9876", "8765", "7654", "6543", "5432", "4321", "3210", | ||
"1q2w", "2w3e", "3e4r", "5t6y", "abcd", "qwer", "asdf", "zxcv" | ||
); | ||
|
||
public static boolean isWeakPassword(String password) { | ||
return !PWD_PATTERN.matcher(password).matches() || isCommonlyUsed(password); | ||
} | ||
|
||
/** | ||
* @return The password contains code fragment or is blank. | ||
*/ | ||
private static boolean isCommonlyUsed(String password) { | ||
if (Strings.isNullOrEmpty(password)) { | ||
return true; | ||
} | ||
for (String s : LIST_OF_CODE_FRAGMENT) { | ||
if (password.toLowerCase().contains(s)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
...al/src/test/java/com/ctrip/framework/apollo/portal/controller/UserInfoControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright 2021 Apollo Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
package com.ctrip.framework.apollo.portal.controller; | ||
|
||
import com.ctrip.framework.apollo.common.exception.BadRequestException; | ||
import com.ctrip.framework.apollo.portal.entity.po.UserPO; | ||
import com.ctrip.framework.apollo.portal.spi.springsecurity.SpringSecurityUserService; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.MockitoJUnitRunner; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class UserInfoControllerTest { | ||
|
||
@Mock | ||
private UserInfoController userInfoController; | ||
@InjectMocks | ||
private SpringSecurityUserService userService; | ||
|
||
@Test | ||
public void testCreateOrUpdateUser() { | ||
UserPO user = new UserPO(); | ||
user.setUsername("username"); | ||
user.setPassword("af12gvi3ox"); | ||
userInfoController.createOrUpdateUser(user); | ||
} | ||
|
||
@Test | ||
public void testCreateOrUpdateUserFailed() { | ||
List<String> pwdCases = Arrays.asList("1111111", "1111qwer"); | ||
for (String p : pwdCases) { | ||
UserPO user = new UserPO(); | ||
user.setUsername("username"); | ||
user.setPassword(p); | ||
try { | ||
userInfoController.createOrUpdateUser(user); | ||
} catch (BadRequestException e) { | ||
Assert.assertEquals( | ||
"Password needs a number and lowercase letter and between 8~20 characters. " | ||
+ "And cannot be a weak password.", | ||
e.getMessage()); | ||
} | ||
} | ||
} | ||
|
||
} |
40 changes: 40 additions & 0 deletions
40
...rtal/src/test/java/com/ctrip/framework/apollo/portal/util/CommonlyUsedPwdCheckerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright 2021 Apollo Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
package com.ctrip.framework.apollo.portal.util; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
public class CommonlyUsedPwdCheckerTest { | ||
|
||
@Test | ||
public void testIsWeakPassword() { | ||
List<String> weakPwdList = Arrays.asList( | ||
"12345678", "98765432", "11111111", "22222222", "33333333", "44444444", | ||
"55555555", "66666666", "77777777", "88888888", "99999999", "00000000", | ||
"1q2w3e4r", "qwertyuiop", "asdfghjkl", "asdfghjkl", "abcd1234" | ||
); | ||
for (String p : weakPwdList) { | ||
Assert.assertTrue(UserPasswordChecker.isWeakPassword(p)); | ||
} | ||
|
||
Assert.assertFalse(UserPasswordChecker.isWeakPassword("1s39gvisk")); | ||
} | ||
|
||
} |