-
-
Notifications
You must be signed in to change notification settings - Fork 10.2k
feature: add the delegating password encoder for apollo-portal simple auth #3804
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
nobodyiam
merged 13 commits into
apolloconfig:master
from
vdiskg:delegate-password-encoder
Jul 10, 2021
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
1a9e29f
DelegatingPasswordEncoder
vdiskg c494f9a
sql
vdiskg 60db3f6
extend Password to 512
vdiskg f131709
update CHANGES.md
vdiskg 0ef6f89
Merge branch 'master' into delegate-password-encoder
vdiskg b65c7dd
add an adapter for old password
vdiskg d4a12e2
Merge branch 'master' into delegate-password-encoder
vdiskg 586a630
fix the unit test NullPointerException
vdiskg b8c2110
only throws Exception on password has an id
vdiskg d65ad73
mark add prefix for `Users`.`Password` optional
vdiskg 9c49ab9
Merge branch 'master' into delegate-password-encoder
vdiskg c22c551
modify unit test
vdiskg 3682805
remove {bcrypt} prefix on sql
vdiskg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 <vdisk@foxmail.com> | ||
| */ | ||
| 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 hidden or 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 <vdisk@foxmail.com> | ||
| */ | ||
| 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 hidden or 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 <vdisk@foxmail.com> | ||
| */ | ||
| @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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.