forked from Intel-bigdata/SSM
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ADH-5033] Add password encoding support
- Loading branch information
1 parent
da60416
commit 4c3dbe7
Showing
12 changed files
with
292 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
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
95 changes: 95 additions & 0 deletions
95
...ration/src/test/java/org/smartdata/integration/auth/TestWebServerPredefinedUsersAuth.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,95 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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 org.smartdata.integration.auth; | ||
|
||
import org.junit.runners.Parameterized.Parameters; | ||
import org.smartdata.conf.SmartConf; | ||
|
||
import static org.smartdata.integration.auth.TestWebServerAuth.TestParams.ExpectedResult.FAIL; | ||
import static org.smartdata.server.config.ConfigKeys.PREDEFINED_BASIC_AUTH_ENABLED; | ||
import static org.smartdata.server.config.ConfigKeys.PREDEFINED_USERS; | ||
import static org.smartdata.server.config.ConfigKeys.PREDEFINED_USERS_PASSWORD_ENCODER; | ||
|
||
public class TestWebServerPredefinedUsersAuth extends TestWebServerAuth { | ||
|
||
@Parameters(name = "{0}") | ||
public static TestParams[] parameters() { | ||
return new TestParams[]{ | ||
new TestParams("user1", "pass1", plainTextCredentials()), | ||
new TestParams("user2", "pass2", plainTextCredentials()), | ||
new TestParams("unknown", "pass", plainTextCredentials(), FAIL), | ||
new TestParams("user1", "pass1", bcryptCredentials()), | ||
new TestParams("unknown2", "unknown", bcryptCredentials(), FAIL), | ||
new TestParams("user2", "pass2", pbkdf2Credentials()), | ||
new TestParams("unknown3", "unknown2", pbkdf2Credentials(), FAIL), | ||
new TestParams("user1", "pass1", hashCredentialsWithDefaultEncoder()), | ||
new TestParams("user2", "pass2", hashCredentialsWithDefaultEncoder(), FAIL) | ||
}; | ||
} | ||
|
||
private static SmartConf plainTextCredentials() { | ||
SmartConf conf = baseConf(); | ||
conf.set(PREDEFINED_USERS, "user1:pass1,user2:pass2"); | ||
|
||
conf.set(TEST_PARAM_NAME_OPTION, "plainTextCredentials"); | ||
return conf; | ||
} | ||
|
||
private static SmartConf bcryptCredentials() { | ||
SmartConf conf = baseConf(); | ||
conf.set(PREDEFINED_USERS, | ||
// user1:pass1 | ||
"user1:{bcrypt}$2a$10$.E/VDhdRt85NyN7/kzrE8uCVu6Ey3tbGR/efyOOIgw.e4BeYcb8.G," | ||
// user2:pass2 | ||
+ "user2:{bcrypt}$2a$10$kEZuKAa9sL3C2YlTwSo7meNfvjkZMHGUkf6T/E6.eSO6.OIt.Q9qa"); | ||
|
||
conf.set(TEST_PARAM_NAME_OPTION, "bcryptCredentials"); | ||
return conf; | ||
} | ||
|
||
private static SmartConf pbkdf2Credentials() { | ||
SmartConf conf = baseConf(); | ||
// user1:pass1 | ||
conf.set(PREDEFINED_USERS, "user1:{pbkdf2}" | ||
+ "8d831f4f23dc4f0f43bdabd573372c6ea091f8fb55cb7d0242534d35f7fe14bb932da695a20c3c23," | ||
// user2:pass2 | ||
+ "user2:{pbkdf2}" | ||
+ "b4f2a5e4a827e83fae4346e2ffc806ee61a79ff40aeea3afea7acac2d6651fe17869968c55902618"); | ||
|
||
conf.set(TEST_PARAM_NAME_OPTION, "pbkdf2Credentials"); | ||
return conf; | ||
} | ||
|
||
private static SmartConf hashCredentialsWithDefaultEncoder() { | ||
SmartConf conf = baseConf(); | ||
// user1:pass1 | ||
conf.set(PREDEFINED_USERS, "user1:$e0801$LBsB5RY9rXDw343mFrViv74uFhi6SRqE5R8KirZjIcDd966x" | ||
+ "TJbzrDVgtgnFAX8h+zU+6+raIsPnckqF4D7Smg==" | ||
+ "$l72uGTcby92qvT6vd+gekAjUL/WJSDvujf///s/l+sQ="); | ||
conf.set(PREDEFINED_USERS_PASSWORD_ENCODER, "scrypt"); | ||
|
||
conf.set(TEST_PARAM_NAME_OPTION, "hashCredentialsWithDefaultEncoder"); | ||
return conf; | ||
} | ||
|
||
private static SmartConf baseConf() { | ||
SmartConf conf = new SmartConf(); | ||
conf.setBoolean(PREDEFINED_BASIC_AUTH_ENABLED, true); | ||
return conf; | ||
} | ||
} |
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
85 changes: 85 additions & 0 deletions
85
smart-web-server/src/main/java/org/smartdata/server/config/PasswordEncoderFactory.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,85 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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 org.smartdata.server.config; | ||
|
||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; | ||
import org.springframework.security.crypto.factory.PasswordEncoderFactories; | ||
import org.springframework.security.crypto.password.DelegatingPasswordEncoder; | ||
import org.springframework.security.crypto.password.LdapShaPasswordEncoder; | ||
import org.springframework.security.crypto.password.Md4PasswordEncoder; | ||
import org.springframework.security.crypto.password.MessageDigestPasswordEncoder; | ||
import org.springframework.security.crypto.password.NoOpPasswordEncoder; | ||
import org.springframework.security.crypto.password.PasswordEncoder; | ||
import org.springframework.security.crypto.password.Pbkdf2PasswordEncoder; | ||
import org.springframework.security.crypto.scrypt.SCryptPasswordEncoder; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* Enhanced version of Spring's | ||
* {@link PasswordEncoderFactories#createDelegatingPasswordEncoder}. | ||
*/ | ||
public interface PasswordEncoderFactory { | ||
|
||
@RequiredArgsConstructor | ||
@Getter | ||
enum EncoderType { | ||
NOOP("noop"), | ||
BCRYPT("bcrypt"), | ||
LDAP("ldap"), | ||
MD4("md4"), | ||
MD5("md5"), | ||
PBKDF2("pbkdf2"), | ||
SCRYPT("scrypt"), | ||
SHA_1("sha-1"), | ||
SHA_256("sha-256"); | ||
|
||
private final String id; | ||
|
||
public static EncoderType fromId(String id) { | ||
for (EncoderType encoderType : EncoderType.values()) { | ||
if (encoderType.id.equals(id)) { | ||
return encoderType; | ||
} | ||
} | ||
throw new IllegalArgumentException("Wrong encoder id: " + id); | ||
} | ||
} | ||
|
||
@SuppressWarnings("deprecation") | ||
static PasswordEncoder build(EncoderType defaultEncoderType) { | ||
Map<String, PasswordEncoder> encoders = new HashMap<>(); | ||
encoders.put(EncoderType.BCRYPT.id, new BCryptPasswordEncoder()); | ||
encoders.put(EncoderType.LDAP.id, new LdapShaPasswordEncoder()); | ||
encoders.put(EncoderType.MD4.id, new Md4PasswordEncoder()); | ||
encoders.put(EncoderType.MD5.id, new MessageDigestPasswordEncoder("MD5")); | ||
encoders.put(EncoderType.NOOP.id, NoOpPasswordEncoder.getInstance()); | ||
encoders.put(EncoderType.PBKDF2.id, new Pbkdf2PasswordEncoder()); | ||
encoders.put(EncoderType.SCRYPT.id, new SCryptPasswordEncoder()); | ||
encoders.put(EncoderType.SHA_1.id, new MessageDigestPasswordEncoder("SHA-1")); | ||
encoders.put(EncoderType.SHA_256.id, new MessageDigestPasswordEncoder("SHA-256")); | ||
|
||
DelegatingPasswordEncoder passwordEncoder = | ||
new DelegatingPasswordEncoder(defaultEncoderType.id, encoders); | ||
passwordEncoder.setDefaultPasswordEncoderForMatches(encoders.get(defaultEncoderType.id)); | ||
return passwordEncoder; | ||
} | ||
} |
Oops, something went wrong.