PageRenderTime 26ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/core/cas-server-core-authentication-api/src/main/java/org/apereo/cas/authentication/support/password/PasswordEncoderUtils.java

https://github.com/frett/cas
Java | 90 lines | 71 code | 7 blank | 12 comment | 6 complexity | baf9bed9ad5f52848f85b89c5c19675f MD5 | raw file
  1. package org.apereo.cas.authentication.support.password;
  2. import org.apereo.cas.configuration.model.core.authentication.PasswordEncoderProperties;
  3. import org.apereo.cas.util.RandomUtils;
  4. import org.apereo.cas.util.crypto.DefaultPasswordEncoder;
  5. import lombok.extern.slf4j.Slf4j;
  6. import lombok.val;
  7. import org.apache.commons.lang3.StringUtils;
  8. import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
  9. import org.springframework.security.crypto.password.NoOpPasswordEncoder;
  10. import org.springframework.security.crypto.password.PasswordEncoder;
  11. import org.springframework.security.crypto.password.Pbkdf2PasswordEncoder;
  12. import org.springframework.security.crypto.password.StandardPasswordEncoder;
  13. import org.springframework.security.crypto.scrypt.SCryptPasswordEncoder;
  14. /**
  15. * This is {@link PasswordEncoderUtils}.
  16. *
  17. * @author Misagh Moayyed
  18. * @since 5.2.0
  19. */
  20. @Slf4j
  21. public class PasswordEncoderUtils {
  22. private static final int HASH_WIDTH = 256;
  23. /**
  24. * New password encoder password encoder.
  25. *
  26. * @param properties the properties
  27. * @return the password encoder
  28. */
  29. public static PasswordEncoder newPasswordEncoder(final PasswordEncoderProperties properties) {
  30. val type = properties.getType();
  31. if (StringUtils.isBlank(type)) {
  32. LOGGER.trace("No password encoder type is defined, and so none shall be created");
  33. return NoOpPasswordEncoder.getInstance();
  34. }
  35. if (type.endsWith(".groovy")) {
  36. LOGGER.debug("Creating Groovy-based password encoder at [{}]", type);
  37. return new GroovyPasswordEncoder(properties.getType());
  38. }
  39. if (type.contains(".")) {
  40. try {
  41. LOGGER.debug("Configuration indicates use of a custom password encoder [{}]", type);
  42. val clazz = (Class<PasswordEncoder>) Class.forName(type);
  43. return clazz.getDeclaredConstructor().newInstance();
  44. } catch (final Exception e) {
  45. LOGGER.error("Falling back to a no-op password encoder as CAS has failed to create "
  46. + "an instance of the custom password encoder class " + type, e);
  47. return NoOpPasswordEncoder.getInstance();
  48. }
  49. }
  50. val encoderType = PasswordEncoderProperties.PasswordEncoderTypes.valueOf(type);
  51. switch (encoderType) {
  52. case DEFAULT:
  53. LOGGER.debug("Creating default password encoder with encoding alg [{}] and character encoding [{}]",
  54. properties.getEncodingAlgorithm(), properties.getCharacterEncoding());
  55. return new DefaultPasswordEncoder(properties.getEncodingAlgorithm(), properties.getCharacterEncoding());
  56. case STANDARD:
  57. LOGGER.debug("Creating standard password encoder with the secret defined in the configuration");
  58. return new StandardPasswordEncoder(properties.getSecret());
  59. case BCRYPT:
  60. LOGGER.debug("Creating BCRYPT password encoder given the strength [{}] and secret in the configuration",
  61. properties.getStrength());
  62. if (StringUtils.isBlank(properties.getSecret())) {
  63. LOGGER.debug("Creating BCRYPT encoder without secret");
  64. return new BCryptPasswordEncoder(properties.getStrength());
  65. }
  66. LOGGER.debug("Creating BCRYPT encoder with secret");
  67. return new BCryptPasswordEncoder(properties.getStrength(), RandomUtils.getNativeInstance());
  68. case SCRYPT:
  69. LOGGER.debug("Creating SCRYPT encoder");
  70. return new SCryptPasswordEncoder();
  71. case PBKDF2:
  72. if (StringUtils.isBlank(properties.getSecret())) {
  73. LOGGER.trace("Creating PBKDF2 encoder without secret");
  74. return new Pbkdf2PasswordEncoder();
  75. }
  76. return new Pbkdf2PasswordEncoder(properties.getSecret(), properties.getStrength(), HASH_WIDTH);
  77. case NONE:
  78. default:
  79. LOGGER.trace("No password encoder shall be created given the requested encoder type [{}]", type);
  80. return NoOpPasswordEncoder.getInstance();
  81. }
  82. }
  83. }