PlainClient.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /* PlainClient.java --
  2. Copyright (C) 2003, 2006 Free Software Foundation, Inc.
  3. This file is a part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or (at
  7. your option) any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
  15. USA
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package gnu.javax.crypto.sasl.plain;
  32. import gnu.java.lang.CPStringBuilder;
  33. import gnu.java.security.Registry;
  34. import gnu.javax.crypto.sasl.ClientMechanism;
  35. import javax.security.sasl.SaslClient;
  36. import javax.security.sasl.SaslException;
  37. import javax.security.auth.callback.Callback;
  38. import javax.security.auth.callback.NameCallback;
  39. import javax.security.auth.callback.PasswordCallback;
  40. /**
  41. * The PLAIN SASL client-side mechanism.
  42. */
  43. public class PlainClient
  44. extends ClientMechanism
  45. implements SaslClient
  46. {
  47. public PlainClient()
  48. {
  49. super(Registry.SASL_PLAIN_MECHANISM);
  50. }
  51. protected void initMechanism() throws SaslException
  52. {
  53. }
  54. protected void resetMechanism() throws SaslException
  55. {
  56. }
  57. public boolean hasInitialResponse()
  58. {
  59. return true;
  60. }
  61. public byte[] evaluateChallenge(final byte[] challenge) throws SaslException
  62. {
  63. try
  64. {
  65. final String username;
  66. final char[] password;
  67. Callback[] callbacks;
  68. if ((! properties.containsKey(Registry.SASL_USERNAME))
  69. && (! properties.containsKey(Registry.SASL_PASSWORD)))
  70. {
  71. callbacks = new Callback[2];
  72. final NameCallback nameCB;
  73. final String defaultName = System.getProperty("user.name");
  74. if (defaultName == null)
  75. nameCB = new NameCallback("username: ");
  76. else
  77. nameCB = new NameCallback("username: ", defaultName);
  78. final PasswordCallback pwdCB = new PasswordCallback("password: ",
  79. false);
  80. callbacks[0] = nameCB;
  81. callbacks[1] = pwdCB;
  82. this.handler.handle(callbacks);
  83. username = nameCB.getName();
  84. password = pwdCB.getPassword();
  85. }
  86. else
  87. {
  88. if (properties.containsKey(Registry.SASL_USERNAME))
  89. username = (String) properties.get(Registry.SASL_USERNAME);
  90. else
  91. {
  92. callbacks = new Callback[1];
  93. final NameCallback nameCB;
  94. final String defaultName = System.getProperty("user.name");
  95. if (defaultName == null)
  96. nameCB = new NameCallback("username: ");
  97. else
  98. nameCB = new NameCallback("username: ", defaultName);
  99. callbacks[0] = nameCB;
  100. this.handler.handle(callbacks);
  101. username = nameCB.getName();
  102. }
  103. if (properties.containsKey(Registry.SASL_PASSWORD))
  104. password = ((String) properties.get(Registry.SASL_PASSWORD)).toCharArray();
  105. else
  106. {
  107. callbacks = new Callback[1];
  108. final PasswordCallback pwdCB = new PasswordCallback("password: ",
  109. false);
  110. callbacks[0] = pwdCB;
  111. this.handler.handle(callbacks);
  112. password = pwdCB.getPassword();
  113. }
  114. }
  115. if (password == null)
  116. throw new SaslException("null password supplied");
  117. final CPStringBuilder sb = new CPStringBuilder();
  118. if (authorizationID != null)
  119. sb.append(authorizationID);
  120. sb.append('\0');
  121. sb.append(username);
  122. sb.append('\0');
  123. sb.append(password);
  124. this.complete = true;
  125. final byte[] response = sb.toString().getBytes("UTF-8");
  126. return response;
  127. }
  128. catch (Exception x)
  129. {
  130. if (x instanceof SaslException)
  131. throw (SaslException) x;
  132. throw new SaslException("evaluateChallenge()", x);
  133. }
  134. }
  135. protected String getNegotiatedQOP()
  136. {
  137. return Registry.QOP_AUTH;
  138. }
  139. }