You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

JGitUserInteraction.java 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Copyright (C) 2018, Thomas Wolf <thomas.wolf@paranor.ch>
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.internal.transport.sshd;
  44. import java.net.InetSocketAddress;
  45. import java.util.ArrayList;
  46. import java.util.List;
  47. import org.apache.sshd.client.auth.keyboard.UserInteraction;
  48. import org.apache.sshd.client.session.ClientSession;
  49. import org.eclipse.jgit.transport.CredentialItem;
  50. import org.eclipse.jgit.transport.CredentialsProvider;
  51. import org.eclipse.jgit.transport.SshConstants;
  52. import org.eclipse.jgit.transport.URIish;
  53. /**
  54. * A {@link UserInteraction} callback implementation based on a
  55. * {@link CredentialsProvider}.
  56. */
  57. public class JGitUserInteraction implements UserInteraction {
  58. private final CredentialsProvider provider;
  59. /**
  60. * Creates a new {@link JGitUserInteraction} for interactive password input
  61. * based on the given {@link CredentialsProvider}.
  62. *
  63. * @param provider
  64. * to use
  65. */
  66. public JGitUserInteraction(CredentialsProvider provider) {
  67. this.provider = provider;
  68. }
  69. @Override
  70. public boolean isInteractionAllowed(ClientSession session) {
  71. return provider.isInteractive();
  72. }
  73. @Override
  74. public String[] interactive(ClientSession session, String name,
  75. String instruction, String lang, String[] prompt, boolean[] echo) {
  76. // This is keyboard-interactive authentication
  77. List<CredentialItem> items = new ArrayList<>();
  78. int numberOfHiddenInputs = 0;
  79. for (int i = 0; i < prompt.length; i++) {
  80. boolean hidden = i < echo.length && !echo[i];
  81. if (hidden) {
  82. numberOfHiddenInputs++;
  83. }
  84. }
  85. // RFC 4256 (SSH_MSG_USERAUTH_INFO_REQUEST) says: "The language tag is
  86. // deprecated and SHOULD be the empty string." and "[If there are no
  87. // prompts] the client SHOULD still display the name and instruction
  88. // fields" and "[The] client SHOULD print the name and instruction (if
  89. // non-empty)"
  90. if (name != null && !name.isEmpty()) {
  91. items.add(new CredentialItem.InformationalMessage(name));
  92. }
  93. if (instruction != null && !instruction.isEmpty()) {
  94. items.add(new CredentialItem.InformationalMessage(instruction));
  95. }
  96. for (int i = 0; i < prompt.length; i++) {
  97. boolean hidden = i < echo.length && !echo[i];
  98. if (hidden && numberOfHiddenInputs == 1) {
  99. // We need to somehow trigger storing the password in the
  100. // Eclipse secure storage in EGit. Currently, this is done only
  101. // for password fields.
  102. items.add(new CredentialItem.Password());
  103. // TODO Possibly change EGit to store all hidden strings
  104. // (keyed by the URI and the prompt?) so that we don't have to
  105. // use this kludge here.
  106. } else {
  107. items.add(new CredentialItem.StringType(prompt[i], hidden));
  108. }
  109. }
  110. if (items.isEmpty()) {
  111. // Huh? No info, no prompts?
  112. return prompt; // Is known to have length zero here
  113. }
  114. URIish uri = toURI(session.getUsername(),
  115. (InetSocketAddress) session.getIoSession().getRemoteAddress());
  116. if (provider.get(uri, items)) {
  117. return items.stream().map(i -> {
  118. if (i instanceof CredentialItem.Password) {
  119. return new String(((CredentialItem.Password) i).getValue());
  120. } else if (i instanceof CredentialItem.StringType) {
  121. return ((CredentialItem.StringType) i).getValue();
  122. }
  123. return null;
  124. }).filter(s -> s != null).toArray(String[]::new);
  125. }
  126. // TODO What to throw to abort the connection/authentication process?
  127. // In UserAuthKeyboardInteractive.getUserResponses() it's clear that
  128. // returning null is valid and signifies "an error"; we'll try the
  129. // next authentication method. But if the user explicitly canceled,
  130. // then we don't want to try the next methods...
  131. //
  132. // Probably not a serious issue with the typical order of public-key,
  133. // keyboard-interactive, password.
  134. return null;
  135. }
  136. @Override
  137. public String getUpdatedPassword(ClientSession session, String prompt,
  138. String lang) {
  139. // TODO Implement password update in password authentication?
  140. return null;
  141. }
  142. /**
  143. * Creates a {@link URIish} from the given remote address and user name.
  144. *
  145. * @param userName
  146. * for the uri
  147. * @param remote
  148. * address of the remote host
  149. * @return the uri, with {@link SshConstants#SSH_SCHEME} as scheme
  150. */
  151. public static URIish toURI(String userName, InetSocketAddress remote) {
  152. String host = remote.getHostString();
  153. int port = remote.getPort();
  154. return new URIish() //
  155. .setScheme(SshConstants.SSH_SCHEME) //
  156. .setHost(host) //
  157. .setPort(port) //
  158. .setUser(userName);
  159. }
  160. }