PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/jboss-as-7.1.1.Final/domain-management/src/main/java/org/jboss/as/domain/management/security/PropertiesCallbackHandler.java

#
Java | 186 lines | 117 code | 29 blank | 40 comment | 30 complexity | 075f911234361ef1286fec3b8019242c MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1. /*
  2. * JBoss, Home of Professional Open Source.
  3. * Copyright 2011, Red Hat, Inc., and individual contributors
  4. * as indicated by the @author tags. See the copyright.txt file in the
  5. * distribution for a full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.jboss.as.domain.management.security;
  23. import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.PATH;
  24. import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.PLAIN_TEXT;
  25. import static org.jboss.as.domain.management.DomainManagementLogger.ROOT_LOGGER;
  26. import static org.jboss.as.domain.management.DomainManagementMessages.MESSAGES;
  27. import java.io.IOException;
  28. import java.util.LinkedList;
  29. import java.util.List;
  30. import java.util.Properties;
  31. import javax.security.auth.callback.Callback;
  32. import javax.security.auth.callback.NameCallback;
  33. import javax.security.auth.callback.PasswordCallback;
  34. import javax.security.auth.callback.UnsupportedCallbackException;
  35. import javax.security.sasl.AuthorizeCallback;
  36. import javax.security.sasl.RealmCallback;
  37. import org.jboss.dmr.ModelNode;
  38. import org.jboss.msc.service.Service;
  39. import org.jboss.msc.service.StartContext;
  40. import org.jboss.msc.service.StartException;
  41. import org.jboss.msc.service.StopContext;
  42. import org.jboss.sasl.callback.DigestHashCallback;
  43. /**
  44. * A CallbackHandler obtaining the users and their passwords from a properties file.
  45. *
  46. * @author <a href="mailto:darran.lofthouse@jboss.com">Darran Lofthouse</a>
  47. */
  48. public class PropertiesCallbackHandler extends PropertiesFileLoader implements Service<DomainCallbackHandler>,
  49. DomainCallbackHandler {
  50. public static final String SERVICE_SUFFIX = "properties_authentication";
  51. // Technically this CallbackHandler could also support the VerifyCallback callback, however at the moment
  52. // this is only likely to be used with the Digest mechanism so no need to add that support.
  53. private static final Class[] PLAIN_CALLBACKS = { AuthorizeCallback.class, RealmCallback.class, NameCallback.class,
  54. PasswordCallback.class };
  55. private static final Class[] DIGEST_CALLBACKS = { AuthorizeCallback.class, RealmCallback.class, NameCallback.class,
  56. DigestHashCallback.class };
  57. private static final String DOLLAR_LOCAL = "$local";
  58. private final Class[] supportedCallbacks;
  59. private final String realm;
  60. private final boolean plainText;
  61. public PropertiesCallbackHandler(String realm, ModelNode properties) {
  62. super(properties.require(PATH).asString());
  63. this.realm = realm;
  64. if (properties.hasDefined(PLAIN_TEXT)) {
  65. plainText = properties.require(PLAIN_TEXT).asBoolean();
  66. } else {
  67. plainText = false;
  68. }
  69. supportedCallbacks = plainText ? PLAIN_CALLBACKS : DIGEST_CALLBACKS;
  70. }
  71. /*
  72. * Service Methods
  73. */
  74. public void start(StartContext context) throws StartException {
  75. super.start(context);
  76. }
  77. @Override
  78. protected void verifyProperties(Properties properties) throws IOException {
  79. final String admin = "admin";
  80. if (properties.contains(admin) && admin.equals(properties.get(admin))) {
  81. ROOT_LOGGER.userAndPasswordWarning();
  82. }
  83. }
  84. public void stop(StopContext context) {
  85. super.stop(context);
  86. }
  87. public DomainCallbackHandler getValue() throws IllegalStateException, IllegalArgumentException {
  88. return this;
  89. }
  90. /*
  91. * DomainCallbackHandler Methods
  92. */
  93. public Class[] getSupportedCallbacks() {
  94. return supportedCallbacks;
  95. }
  96. @Override
  97. public boolean isReady() {
  98. Properties users;
  99. try {
  100. users = getProperties();
  101. } catch (IOException e) {
  102. return false;
  103. }
  104. return (users.size() > 0);
  105. }
  106. public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
  107. List<Callback> toRespondTo = new LinkedList<Callback>();
  108. String userName = null;
  109. boolean userFound = false;
  110. Properties users = getProperties();
  111. // A single pass may be sufficient but by using a two pass approach the Callbackhandler will not
  112. // fail if an unexpected order is encountered.
  113. // First Pass - is to double check no unsupported callbacks and to retrieve
  114. // information from the callbacks passing in information.
  115. for (Callback current : callbacks) {
  116. if (current instanceof AuthorizeCallback) {
  117. toRespondTo.add(current);
  118. } else if (current instanceof NameCallback) {
  119. NameCallback nameCallback = (NameCallback) current;
  120. userName = nameCallback.getDefaultName();
  121. userFound = users.containsKey(userName);
  122. } else if (current instanceof PasswordCallback && plainText) {
  123. toRespondTo.add(current);
  124. } else if (current instanceof DigestHashCallback && plainText == false) {
  125. toRespondTo.add(current);
  126. } else if (current instanceof RealmCallback) {
  127. String realm = ((RealmCallback) current).getDefaultText();
  128. if (this.realm.equals(realm) == false) {
  129. throw MESSAGES.invalidRealm(realm, this.realm);
  130. }
  131. } else {
  132. throw new UnsupportedCallbackException(current);
  133. }
  134. }
  135. // Second Pass - Now iterate the Callback(s) requiring a response.
  136. for (Callback current : toRespondTo) {
  137. if (current instanceof AuthorizeCallback) {
  138. AuthorizeCallback authorizeCallback = (AuthorizeCallback) current;
  139. // Don't support impersonating another identity
  140. authorizeCallback.setAuthorized(authorizeCallback.getAuthenticationID().equals(
  141. authorizeCallback.getAuthorizationID()));
  142. } else if (current instanceof PasswordCallback) {
  143. if (userFound == false) {
  144. throw new UserNotFoundException(userName);
  145. }
  146. String password = users.get(userName).toString();
  147. ((PasswordCallback) current).setPassword(password.toCharArray());
  148. } else if (current instanceof DigestHashCallback) {
  149. if (userFound == false) {
  150. throw new UserNotFoundException(userName);
  151. }
  152. String hash = users.get(userName).toString();
  153. ((DigestHashCallback) current).setHexHash(hash);
  154. }
  155. }
  156. }
  157. }