-
-
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: add the delegating password encoder for apollo-portal simple…
… auth (#3804) * DelegatingPasswordEncoder * sql * extend Password to 512 * update CHANGES.md * add an adapter for old password * fix the unit test NullPointerException * only throws Exception on password has an id * mark add prefix for `Users`.`Password` optional * modify unit test * remove {bcrypt} prefix on sql
- Loading branch information
Showing
13 changed files
with
283 additions
and
52 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
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
47 changes: 47 additions & 0 deletions
47
.../src/main/java/com/ctrip/framework/apollo/portal/spi/oidc/PlaceholderPasswordEncoder.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,47 @@ | ||
/* | ||
* 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.spi.oidc; | ||
|
||
import java.util.Base64; | ||
import java.util.concurrent.ThreadLocalRandom; | ||
import org.springframework.security.crypto.password.PasswordEncoder; | ||
|
||
/** | ||
* @author vdisk <[email protected]> | ||
*/ | ||
public class PlaceholderPasswordEncoder implements PasswordEncoder { | ||
|
||
public static final String ENCODING_ID = "placeholder"; | ||
|
||
/** | ||
* generate a random string as a password placeholder. | ||
*/ | ||
@Override | ||
public String encode(CharSequence rawPassword) { | ||
byte[] bytes = new byte[32]; | ||
ThreadLocalRandom.current().nextBytes(bytes); | ||
return Base64.getEncoder().encodeToString(bytes); | ||
} | ||
|
||
/** | ||
* placeholder will never matches a password | ||
*/ | ||
@Override | ||
public boolean matches(CharSequence rawPassword, String encodedPassword) { | ||
return false; | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
...va/com/ctrip/framework/apollo/portal/spi/springsecurity/ApolloPasswordEncoderFactory.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,76 @@ | ||
/* | ||
* 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.spi.springsecurity; | ||
|
||
import com.ctrip.framework.apollo.portal.spi.oidc.PlaceholderPasswordEncoder; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import org.springframework.security.crypto.argon2.Argon2PasswordEncoder; | ||
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.PasswordEncoder; | ||
import org.springframework.security.crypto.password.Pbkdf2PasswordEncoder; | ||
import org.springframework.security.crypto.scrypt.SCryptPasswordEncoder; | ||
|
||
/** | ||
* @author vdisk <[email protected]> | ||
*/ | ||
public final class ApolloPasswordEncoderFactory { | ||
|
||
private ApolloPasswordEncoderFactory() { | ||
} | ||
|
||
/** | ||
* Creates a {@link DelegatingPasswordEncoder} with default mappings {@link | ||
* PasswordEncoderFactories#createDelegatingPasswordEncoder()}, and add a placeholder encoder for | ||
* oidc {@link PlaceholderPasswordEncoder} | ||
* | ||
* @return the {@link PasswordEncoder} to use | ||
*/ | ||
@SuppressWarnings("deprecation") | ||
public static PasswordEncoder createDelegatingPasswordEncoder() { | ||
// copy from PasswordEncoderFactories, and it's should follow the upgrade of the PasswordEncoderFactories | ||
String encodingId = "bcrypt"; | ||
Map<String, PasswordEncoder> encoders = new HashMap<>(); | ||
encoders.put(encodingId, new BCryptPasswordEncoder()); | ||
encoders.put("ldap", new org.springframework.security.crypto.password.LdapShaPasswordEncoder()); | ||
encoders.put("MD4", new org.springframework.security.crypto.password.Md4PasswordEncoder()); | ||
encoders.put("MD5", | ||
new org.springframework.security.crypto.password.MessageDigestPasswordEncoder("MD5")); | ||
encoders.put("noop", | ||
org.springframework.security.crypto.password.NoOpPasswordEncoder.getInstance()); | ||
encoders.put("pbkdf2", new Pbkdf2PasswordEncoder()); | ||
encoders.put("scrypt", new SCryptPasswordEncoder()); | ||
encoders.put("SHA-1", | ||
new org.springframework.security.crypto.password.MessageDigestPasswordEncoder("SHA-1")); | ||
encoders.put("SHA-256", | ||
new org.springframework.security.crypto.password.MessageDigestPasswordEncoder("SHA-256")); | ||
encoders | ||
.put("sha256", new org.springframework.security.crypto.password.StandardPasswordEncoder()); | ||
encoders.put("argon2", new Argon2PasswordEncoder()); | ||
|
||
// placeholder encoder for oidc | ||
encoders.put(PlaceholderPasswordEncoder.ENCODING_ID, new PlaceholderPasswordEncoder()); | ||
DelegatingPasswordEncoder delegatingPasswordEncoder = new DelegatingPasswordEncoder(encodingId, | ||
encoders); | ||
|
||
// todo: adapt the old password, and it should be removed in the next feature version of the 1.9.x | ||
delegatingPasswordEncoder.setDefaultPasswordEncoderForMatches(new PasswordEncoderAdapter(encoders.get(encodingId))); | ||
return delegatingPasswordEncoder; | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
...ain/java/com/ctrip/framework/apollo/portal/spi/springsecurity/PasswordEncoderAdapter.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.spi.springsecurity; | ||
|
||
import org.springframework.security.crypto.password.PasswordEncoder; | ||
import org.springframework.util.StringUtils; | ||
|
||
/** | ||
* @author vdisk <[email protected]> | ||
*/ | ||
@Deprecated | ||
public class PasswordEncoderAdapter implements PasswordEncoder { | ||
|
||
private static final String PREFIX = "{"; | ||
|
||
private static final String SUFFIX = "}"; | ||
|
||
private final PasswordEncoder encoder; | ||
|
||
public PasswordEncoderAdapter( | ||
PasswordEncoder encoder) { | ||
this.encoder = encoder; | ||
} | ||
|
||
@Override | ||
public String encode(CharSequence rawPassword) { | ||
throw new UnsupportedOperationException("encode is not supported"); | ||
} | ||
|
||
@Override | ||
public boolean matches(CharSequence rawPassword, String encodedPassword) { | ||
boolean matches = this.encoder.matches(rawPassword, encodedPassword); | ||
if (matches) { | ||
return true; | ||
} | ||
String id = this.extractId(encodedPassword); | ||
if (StringUtils.hasText(id)) { | ||
throw new IllegalArgumentException( | ||
"There is no PasswordEncoder mapped for the id \"" + id + "\""); | ||
} | ||
return false; | ||
} | ||
|
||
private String extractId(String prefixEncodedPassword) { | ||
if (prefixEncodedPassword == null) { | ||
return null; | ||
} | ||
int start = prefixEncodedPassword.indexOf(PREFIX); | ||
if (start != 0) { | ||
return null; | ||
} | ||
int end = prefixEncodedPassword.indexOf(SUFFIX, start); | ||
if (end < 0) { | ||
return null; | ||
} | ||
return prefixEncodedPassword.substring(start + 1, end); | ||
} | ||
|
||
} |
Oops, something went wrong.