Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * Copyright (C) 2010, Google Inc.
  3. * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
  4. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  5. * and other copyright owners as documented in the project's IP log.
  6. *
  7. * This program and the accompanying materials are made available
  8. * under the terms of the Eclipse Distribution License v1.0 which
  9. * accompanies this distribution, is reproduced below, and is
  10. * available at http://www.eclipse.org/org/documents/edl-v10.php
  11. *
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or
  15. * without modification, are permitted provided that the following
  16. * conditions are met:
  17. *
  18. * - Redistributions of source code must retain the above copyright
  19. * notice, this list of conditions and the following disclaimer.
  20. *
  21. * - Redistributions in binary form must reproduce the above
  22. * copyright notice, this list of conditions and the following
  23. * disclaimer in the documentation and/or other materials provided
  24. * with the distribution.
  25. *
  26. * - Neither the name of the Eclipse Foundation, Inc. nor the
  27. * names of its contributors may be used to endorse or promote
  28. * products derived from this software without specific prior
  29. * written permission.
  30. *
  31. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  32. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  33. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  34. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  35. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  36. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  37. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  38. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  39. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  40. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  41. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  42. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  43. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. */
  45. package org.eclipse.jgit.awtui;
  46. import java.awt.GridBagConstraints;
  47. import java.awt.GridBagLayout;
  48. import java.awt.Insets;
  49. import javax.swing.JLabel;
  50. import javax.swing.JOptionPane;
  51. import javax.swing.JPanel;
  52. import javax.swing.JPasswordField;
  53. import javax.swing.JTextField;
  54. import org.eclipse.jgit.errors.UnsupportedCredentialItem;
  55. import org.eclipse.jgit.transport.ChainingCredentialsProvider;
  56. import org.eclipse.jgit.transport.CredentialItem;
  57. import org.eclipse.jgit.transport.CredentialsProvider;
  58. import org.eclipse.jgit.transport.NetRCCredentialsProvider;
  59. import org.eclipse.jgit.transport.URIish;
  60. /**
  61. * Interacts with the user during authentication by using AWT/Swing dialogs.
  62. */
  63. public class AwtCredentialsProvider extends CredentialsProvider {
  64. /**
  65. * Install this implementation as the default.
  66. */
  67. public static void install() {
  68. final AwtCredentialsProvider c = new AwtCredentialsProvider();
  69. CredentialsProvider cp = new ChainingCredentialsProvider(
  70. new NetRCCredentialsProvider(), c);
  71. CredentialsProvider.setDefault(cp);
  72. }
  73. /** {@inheritDoc} */
  74. @Override
  75. public boolean isInteractive() {
  76. return true;
  77. }
  78. /** {@inheritDoc} */
  79. @Override
  80. public boolean supports(CredentialItem... items) {
  81. for (CredentialItem i : items) {
  82. if (i instanceof CredentialItem.StringType)
  83. continue;
  84. else if (i instanceof CredentialItem.CharArrayType)
  85. continue;
  86. else if (i instanceof CredentialItem.YesNoType)
  87. continue;
  88. else if (i instanceof CredentialItem.InformationalMessage)
  89. continue;
  90. else
  91. return false;
  92. }
  93. return true;
  94. }
  95. /** {@inheritDoc} */
  96. @Override
  97. public boolean get(URIish uri, CredentialItem... items)
  98. throws UnsupportedCredentialItem {
  99. if (items.length == 0) {
  100. return true;
  101. } else if (items.length == 1) {
  102. final CredentialItem item = items[0];
  103. if (item instanceof CredentialItem.InformationalMessage) {
  104. JOptionPane.showMessageDialog(null, item.getPromptText(),
  105. UIText.get().warning, JOptionPane.INFORMATION_MESSAGE);
  106. return true;
  107. } else if (item instanceof CredentialItem.YesNoType) {
  108. CredentialItem.YesNoType v = (CredentialItem.YesNoType) item;
  109. int r = JOptionPane.showConfirmDialog(null, v.getPromptText(),
  110. UIText.get().warning, JOptionPane.YES_NO_OPTION);
  111. switch (r) {
  112. case JOptionPane.YES_OPTION:
  113. v.setValue(true);
  114. return true;
  115. case JOptionPane.NO_OPTION:
  116. v.setValue(false);
  117. return true;
  118. case JOptionPane.CANCEL_OPTION:
  119. case JOptionPane.CLOSED_OPTION:
  120. default:
  121. return false;
  122. }
  123. } else {
  124. return interactive(uri, items);
  125. }
  126. } else {
  127. return interactive(uri, items);
  128. }
  129. }
  130. private static boolean interactive(URIish uri, CredentialItem[] items) {
  131. final GridBagConstraints gbc = new GridBagConstraints(0, 0, 1, 1, 1, 1,
  132. GridBagConstraints.NORTHWEST, GridBagConstraints.NONE,
  133. new Insets(0, 0, 0, 0), 0, 0);
  134. final JPanel panel = new JPanel();
  135. panel.setLayout(new GridBagLayout());
  136. final JTextField[] texts = new JTextField[items.length];
  137. for (int i = 0; i < items.length; i++) {
  138. CredentialItem item = items[i];
  139. if (item instanceof CredentialItem.StringType
  140. || item instanceof CredentialItem.CharArrayType) {
  141. gbc.fill = GridBagConstraints.NONE;
  142. gbc.gridwidth = GridBagConstraints.RELATIVE;
  143. gbc.gridx = 0;
  144. panel.add(new JLabel(item.getPromptText()), gbc);
  145. gbc.fill = GridBagConstraints.HORIZONTAL;
  146. gbc.gridwidth = GridBagConstraints.RELATIVE;
  147. gbc.gridx = 1;
  148. if (item.isValueSecure())
  149. texts[i] = new JPasswordField(20);
  150. else
  151. texts[i] = new JTextField(20);
  152. panel.add(texts[i], gbc);
  153. gbc.gridy++;
  154. } else if (item instanceof CredentialItem.InformationalMessage) {
  155. gbc.fill = GridBagConstraints.NONE;
  156. gbc.gridwidth = GridBagConstraints.REMAINDER;
  157. gbc.gridx = 0;
  158. panel.add(new JLabel(item.getPromptText()), gbc);
  159. gbc.gridy++;
  160. } else {
  161. throw new UnsupportedCredentialItem(uri, item.getPromptText());
  162. }
  163. }
  164. if (JOptionPane.showConfirmDialog(null, panel,
  165. UIText.get().authenticationRequired,
  166. JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE) != JOptionPane.OK_OPTION)
  167. return false; // cancel
  168. for (int i = 0; i < items.length; i++) {
  169. CredentialItem item = items[i];
  170. JTextField f = texts[i];
  171. if (item instanceof CredentialItem.StringType) {
  172. CredentialItem.StringType v = (CredentialItem.StringType) item;
  173. if (f instanceof JPasswordField)
  174. v.setValue(new String(((JPasswordField) f).getPassword()));
  175. else
  176. v.setValue(f.getText());
  177. } else if (item instanceof CredentialItem.CharArrayType) {
  178. CredentialItem.CharArrayType v = (CredentialItem.CharArrayType) item;
  179. if (f instanceof JPasswordField)
  180. v.setValueNoCopy(((JPasswordField) f).getPassword());
  181. else
  182. v.setValueNoCopy(f.getText().toCharArray());
  183. }
  184. }
  185. return true;
  186. }
  187. }