-
-
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.
Implement password policies to avoid weak passwords (#4008)
- Loading branch information
Showing
7 changed files
with
301 additions
and
11 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
68 changes: 68 additions & 0 deletions
68
...src/main/java/com/ctrip/framework/apollo/portal/util/checker/AuthUserPasswordChecker.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,68 @@ | ||
/* | ||
* 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.checker; | ||
|
||
import com.google.common.base.Strings; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.regex.Pattern; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class AuthUserPasswordChecker implements UserPasswordChecker { | ||
|
||
private static final Pattern PWD_PATTERN = Pattern | ||
.compile("^(?=.*[0-9].*)(?=.*[a-zA-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" | ||
); | ||
|
||
@Override | ||
public CheckResult checkWeakPassword(String password) { | ||
if (!PWD_PATTERN.matcher(password).matches()) { | ||
return new CheckResult(Boolean.FALSE, | ||
"Password needs a number and letter and between 8~20 characters"); | ||
} | ||
if (isCommonlyUsed(password)) { | ||
return new CheckResult(Boolean.FALSE, | ||
"Passwords cannot be consecutive, regular letters or numbers. And cannot be commonly used. " | ||
+ "e.g: abcd1234, 1234qwer, 1q2w3e4r, 1234asdfghjk, ..."); | ||
} | ||
return new CheckResult(Boolean.TRUE, null); | ||
} | ||
|
||
/** | ||
* @return The password contains code fragment or is blank. | ||
*/ | ||
private 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; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/util/checker/CheckResult.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,36 @@ | ||
/* | ||
* 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.checker; | ||
|
||
public class CheckResult { | ||
|
||
private final boolean success; | ||
private final String message; | ||
|
||
public CheckResult(boolean success, String message) { | ||
this.success = success; | ||
this.message = message; | ||
} | ||
|
||
public boolean isSuccess() { | ||
return success; | ||
} | ||
|
||
public String getMessage() { | ||
return message; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...tal/src/main/java/com/ctrip/framework/apollo/portal/util/checker/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,22 @@ | ||
/* | ||
* 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.checker; | ||
|
||
public interface UserPasswordChecker { | ||
|
||
CheckResult checkWeakPassword(String password); | ||
} |
73 changes: 73 additions & 0 deletions
73
...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,73 @@ | ||
/* | ||
* 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 com.ctrip.framework.apollo.portal.util.checker.AuthUserPasswordChecker; | ||
import com.ctrip.framework.apollo.portal.util.checker.CheckResult; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
import org.mockito.junit.MockitoJUnitRunner; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class UserInfoControllerTest { | ||
|
||
@InjectMocks | ||
private UserInfoController userInfoController; | ||
@Mock | ||
private SpringSecurityUserService userService; | ||
@Mock | ||
private AuthUserPasswordChecker userPasswordChecker; | ||
|
||
@Test | ||
public void testCreateOrUpdateUser() { | ||
UserPO user = new UserPO(); | ||
user.setUsername("username"); | ||
user.setPassword("password"); | ||
|
||
Mockito.when(userPasswordChecker.checkWeakPassword(Mockito.anyString())) | ||
.thenReturn(new CheckResult(Boolean.TRUE, "")); | ||
|
||
userInfoController.createOrUpdateUser(user); | ||
} | ||
|
||
@Test(expected = BadRequestException.class) | ||
public void testCreateOrUpdateUserFailed() { | ||
UserPO user = new UserPO(); | ||
user.setUsername("username"); | ||
user.setPassword("password"); | ||
|
||
String msg = "fake error message"; | ||
|
||
Mockito.when(userPasswordChecker.checkWeakPassword(Mockito.anyString())) | ||
.thenReturn(new CheckResult(Boolean.FALSE, msg)); | ||
|
||
try { | ||
userInfoController.createOrUpdateUser(user); | ||
} catch (BadRequestException e) { | ||
Assert.assertEquals(msg, e.getMessage()); | ||
throw e; | ||
} | ||
} | ||
|
||
} |
84 changes: 84 additions & 0 deletions
84
...tal/src/test/java/com/ctrip/framework/apollo/portal/util/AuthUserPasswordCheckerTest.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,84 @@ | ||
/* | ||
* 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.ctrip.framework.apollo.portal.util.checker.AuthUserPasswordChecker; | ||
import com.ctrip.framework.apollo.portal.util.checker.CheckResult; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
public class AuthUserPasswordCheckerTest { | ||
|
||
private AuthUserPasswordChecker checker; | ||
|
||
@Before | ||
public void setup() { | ||
checker = new AuthUserPasswordChecker(); | ||
} | ||
|
||
@Test | ||
public void testRegexMatch() { | ||
List<String> unMatchList = Arrays.asList( | ||
"11111111", | ||
"oibjdiel", | ||
"oso87b6", | ||
"0vb9xibowkd8bz9dsxbef" | ||
); | ||
String exceptedErrMsg = "Password needs a number and letter and between 8~20 characters"; | ||
|
||
for (String p : unMatchList) { | ||
CheckResult res = checker.checkWeakPassword(p); | ||
Assert.assertFalse(res.isSuccess()); | ||
Assert.assertEquals(exceptedErrMsg, res.getMessage()); | ||
} | ||
|
||
List<String> matchList = Arrays.asList( | ||
"pziv0g87", | ||
"8f7zjpf8sci93", | ||
"Upz4jF8u2yjV3wn8zp6c" | ||
); | ||
|
||
for (String p : matchList) { | ||
CheckResult res = checker.checkWeakPassword(p); | ||
Assert.assertTrue(res.isSuccess()); | ||
} | ||
} | ||
|
||
@Test | ||
public void testIsWeakPassword() { | ||
List<String> weakPwdList = Arrays.asList( | ||
"a1234567", "b98765432", "c11111111", "d2222222", "e3333333", "f4444444", | ||
"g5555555", "h6666666", "i7777777", "j8888888", "k9999999", "l0000000", | ||
"1q2w3e4r", "qwertyuiop1", "asdfghjkl2", "asdfghjkl3", "abcd1234" | ||
); | ||
String exceptedErrMsg = | ||
"Passwords cannot be consecutive, regular letters or numbers. And cannot be commonly used."; | ||
|
||
for (String p : weakPwdList) { | ||
CheckResult res = checker.checkWeakPassword(p); | ||
Assert.assertFalse(res.isSuccess()); | ||
Assert.assertTrue(res.getMessage().startsWith(exceptedErrMsg)); | ||
} | ||
|
||
CheckResult res = checker.checkWeakPassword("1s39gvisk"); | ||
Assert.assertTrue(res.isSuccess()); | ||
} | ||
|
||
} |