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.

CredentialsProviderUserInfo.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Copyright (C) 2010, Google Inc.
  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.transport;
  44. import java.util.ArrayList;
  45. import java.util.Arrays;
  46. import java.util.List;
  47. import com.jcraft.jsch.Session;
  48. import com.jcraft.jsch.UIKeyboardInteractive;
  49. import com.jcraft.jsch.UserInfo;
  50. /**
  51. * A JSch {@link com.jcraft.jsch.UserInfo} adapter for a
  52. * {@link org.eclipse.jgit.transport.CredentialsProvider}.
  53. */
  54. public class CredentialsProviderUserInfo implements UserInfo,
  55. UIKeyboardInteractive {
  56. private final URIish uri;
  57. private final CredentialsProvider provider;
  58. private String password;
  59. private String passphrase;
  60. /**
  61. * Wrap a CredentialsProvider to make it suitable for use with JSch.
  62. *
  63. * @param session
  64. * the JSch session this UserInfo will support authentication on.
  65. * @param credentialsProvider
  66. * the provider that will perform the authentication.
  67. */
  68. public CredentialsProviderUserInfo(Session session,
  69. CredentialsProvider credentialsProvider) {
  70. this.uri = createURI(session);
  71. this.provider = credentialsProvider;
  72. }
  73. private static URIish createURI(Session session) {
  74. URIish uri = new URIish();
  75. uri = uri.setScheme("ssh"); //$NON-NLS-1$
  76. uri = uri.setUser(session.getUserName());
  77. uri = uri.setHost(session.getHost());
  78. uri = uri.setPort(session.getPort());
  79. return uri;
  80. }
  81. /** {@inheritDoc} */
  82. @Override
  83. public String getPassword() {
  84. return password;
  85. }
  86. /** {@inheritDoc} */
  87. @Override
  88. public String getPassphrase() {
  89. return passphrase;
  90. }
  91. /** {@inheritDoc} */
  92. @Override
  93. public boolean promptPassphrase(String msg) {
  94. CredentialItem.StringType v = newPrompt(msg);
  95. if (provider.get(uri, v)) {
  96. passphrase = v.getValue();
  97. return true;
  98. } else {
  99. passphrase = null;
  100. return false;
  101. }
  102. }
  103. /** {@inheritDoc} */
  104. @Override
  105. public boolean promptPassword(String msg) {
  106. CredentialItem.Password p = new CredentialItem.Password(msg);
  107. if (provider.get(uri, p)) {
  108. password = new String(p.getValue());
  109. return true;
  110. } else {
  111. password = null;
  112. return false;
  113. }
  114. }
  115. private CredentialItem.StringType newPrompt(String msg) {
  116. return new CredentialItem.StringType(msg, true);
  117. }
  118. /** {@inheritDoc} */
  119. @Override
  120. public boolean promptYesNo(String msg) {
  121. CredentialItem.YesNoType v = new CredentialItem.YesNoType(msg);
  122. return provider.get(uri, v) && v.getValue();
  123. }
  124. /** {@inheritDoc} */
  125. @Override
  126. public void showMessage(String msg) {
  127. provider.get(uri, new CredentialItem.InformationalMessage(msg));
  128. }
  129. /** {@inheritDoc} */
  130. @Override
  131. public String[] promptKeyboardInteractive(String destination, String name,
  132. String instruction, String[] prompt, boolean[] echo) {
  133. CredentialItem.StringType[] v = new CredentialItem.StringType[prompt.length];
  134. for (int i = 0; i < prompt.length; i++)
  135. v[i] = new CredentialItem.StringType(prompt[i], !echo[i]);
  136. List<CredentialItem> items = new ArrayList<>();
  137. if (instruction != null && instruction.length() > 0)
  138. items.add(new CredentialItem.InformationalMessage(instruction));
  139. items.addAll(Arrays.asList(v));
  140. if (!provider.get(uri, items))
  141. return null; // cancel
  142. String[] result = new String[v.length];
  143. for (int i = 0; i < v.length; i++)
  144. result[i] = v[i].getValue();
  145. return result;
  146. }
  147. }