/components/server/src/ome/security/auth/ConfigurablePasswordProvider.java

https://github.com/aherbert/openmicroscopy · Java · 133 lines · 66 code · 20 blank · 47 comment · 7 complexity · 4ef54ca3ab4ef5b29f051e5148e5d182 MD5 · raw file

  1. /*
  2. * $Id$
  3. *
  4. * Copyright 2009 Glencoe Software, Inc. All rights reserved.
  5. * Use is subject to license terms supplied in LICENSE.txt
  6. */
  7. package ome.security.auth;
  8. import java.security.Permissions;
  9. import ome.security.SecuritySystem;
  10. import ome.services.messages.LoginAttemptMessage;
  11. import ome.system.OmeroContext;
  12. import org.apache.commons.logging.Log;
  13. import org.apache.commons.logging.LogFactory;
  14. import org.springframework.beans.BeansException;
  15. import org.springframework.context.ApplicationContext;
  16. import org.springframework.context.ApplicationContextAware;
  17. /**
  18. * Base class for most {@link PasswordProvider} implementations, providing
  19. * configuration for default behaviors. There is no need for a subclass to
  20. * use this implementation.
  21. *
  22. * @author Josh Moore, josh at glencoesoftware.com
  23. * @see SecuritySystem
  24. * @see Permissions
  25. * @since 4.0
  26. */
  27. public abstract class ConfigurablePasswordProvider implements PasswordProvider,
  28. PasswordUtility, ApplicationContextAware {
  29. final protected Log log = LogFactory.getLog(this.getClass());
  30. /**
  31. * Hash implementation to use for encoding passwords to check and changed
  32. * passwords. Default value: MD5 (For the moment, the only supported value!)
  33. */
  34. protected final String hash;
  35. /**
  36. * If true, this implementation should return a null on
  37. * {@link #checkPassword(String, String)} if the user is unknown, otherwise
  38. * a {@link Boolean#FALSE}. Default value: false
  39. */
  40. protected final boolean ignoreUnknown;
  41. protected final PasswordUtil util;
  42. protected OmeroContext ctx;
  43. public ConfigurablePasswordProvider(PasswordUtil util) {
  44. this(util, false);
  45. }
  46. public ConfigurablePasswordProvider(PasswordUtil util, boolean ignoreUnknown) {
  47. this.util = util;
  48. this.hash = "MD5";
  49. this.ignoreUnknown = ignoreUnknown;
  50. }
  51. public void setApplicationContext(ApplicationContext ctx)
  52. throws BeansException {
  53. this.ctx = (OmeroContext) ctx;
  54. }
  55. protected Boolean loginAttempt(String user, Boolean success) {
  56. try {
  57. this.ctx.publishMessage(new LoginAttemptMessage(this, user, success));
  58. } catch (Throwable e) {
  59. log.error("LoginAttemptMessage error", e);
  60. }
  61. return success;
  62. }
  63. /**
  64. * Always returns false, override with specific logic.
  65. */
  66. public boolean hasPassword(String user) {
  67. return false;
  68. }
  69. /**
  70. * If {@link #ignoreUnknown} is true, returns null, since the base class
  71. * knows no users. Otherwise, return {@link Boolean#FALSE} specifying that
  72. * authentication should fail.
  73. */
  74. public Boolean checkPassword(String user, String password, boolean readOnly) {
  75. if (ignoreUnknown) {
  76. return null;
  77. } else {
  78. return Boolean.FALSE;
  79. }
  80. }
  81. /**
  82. * Throws by default.
  83. */
  84. public void changePassword(String user, String password)
  85. throws PasswordChangeException {
  86. throw new PasswordChangeException(
  87. "Cannot change password with this implementation: "
  88. + getClass().getName());
  89. }
  90. /**
  91. * Encodes the password as it would be encoded for a check by
  92. * {@link #comparePasswords(String, String)}
  93. */
  94. public String encodePassword(String newPassword) {
  95. return util.preparePassword(newPassword);
  96. }
  97. /**
  98. * Compares the password provided by the user (unhashed) against the given
  99. * trusted password. In general, if the trusted password is null, return
  100. * {@link Boolean.FALSE}. If the trusted password is empty (only
  101. * whitespace), return {@link Boolean.TRUE}. Otherwise return the results of
  102. * {@link String#equals(Object)}.
  103. */
  104. public Boolean comparePasswords(String trusted, String provided) {
  105. if (trusted == null) {
  106. return Boolean.FALSE;
  107. } else if ("".equals(trusted.trim())) {
  108. return Boolean.TRUE;
  109. } else {
  110. return trusted.equals(encodePassword(provided));
  111. }
  112. }
  113. }