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.

AwtCredentialsProvider.java 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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.CredentialItem;
  56. import org.eclipse.jgit.transport.CredentialsProvider;
  57. import org.eclipse.jgit.transport.URIish;
  58. /** Interacts with the user during authentication by using AWT/Swing dialogs. */
  59. public class AwtCredentialsProvider extends CredentialsProvider {
  60. /** Install this implementation as the default. */
  61. public static void install() {
  62. CredentialsProvider.setDefault(new AwtCredentialsProvider());
  63. }
  64. @Override
  65. public boolean isInteractive() {
  66. return true;
  67. }
  68. @Override
  69. public boolean supports(CredentialItem... items) {
  70. for (CredentialItem i : items) {
  71. if (i instanceof CredentialItem.StringType)
  72. continue;
  73. else if (i instanceof CredentialItem.CharArrayType)
  74. continue;
  75. else if (i instanceof CredentialItem.YesNoType)
  76. continue;
  77. else if (i instanceof CredentialItem.InformationalMessage)
  78. continue;
  79. else
  80. return false;
  81. }
  82. return true;
  83. }
  84. @Override
  85. public boolean get(URIish uri, CredentialItem... items)
  86. throws UnsupportedCredentialItem {
  87. if (items.length == 0) {
  88. return true;
  89. } else if (items.length == 1) {
  90. final CredentialItem item = items[0];
  91. if (item instanceof CredentialItem.InformationalMessage) {
  92. JOptionPane.showMessageDialog(null, item.getPromptText(),
  93. UIText.get().warning, JOptionPane.INFORMATION_MESSAGE);
  94. return true;
  95. } else if (item instanceof CredentialItem.YesNoType) {
  96. CredentialItem.YesNoType v = (CredentialItem.YesNoType) item;
  97. int r = JOptionPane.showConfirmDialog(null, v.getPromptText(),
  98. UIText.get().warning, JOptionPane.YES_NO_OPTION);
  99. switch (r) {
  100. case JOptionPane.YES_OPTION:
  101. v.setValue(true);
  102. return true;
  103. case JOptionPane.NO_OPTION:
  104. v.setValue(false);
  105. return true;
  106. case JOptionPane.CANCEL_OPTION:
  107. case JOptionPane.CLOSED_OPTION:
  108. default:
  109. return false;
  110. }
  111. } else {
  112. return interactive(uri, items);
  113. }
  114. } else {
  115. return interactive(uri, items);
  116. }
  117. }
  118. private static boolean interactive(URIish uri, CredentialItem[] items) {
  119. final GridBagConstraints gbc = new GridBagConstraints(0, 0, 1, 1, 1, 1,
  120. GridBagConstraints.NORTHWEST, GridBagConstraints.NONE,
  121. new Insets(0, 0, 0, 0), 0, 0);
  122. final JPanel panel = new JPanel();
  123. panel.setLayout(new GridBagLayout());
  124. final JTextField[] texts = new JTextField[items.length];
  125. for (int i = 0; i < items.length; i++) {
  126. CredentialItem item = items[i];
  127. if (item instanceof CredentialItem.StringType
  128. || item instanceof CredentialItem.CharArrayType) {
  129. gbc.fill = GridBagConstraints.NONE;
  130. gbc.gridwidth = GridBagConstraints.RELATIVE;
  131. gbc.gridx = 0;
  132. panel.add(new JLabel(item.getPromptText()), gbc);
  133. gbc.fill = GridBagConstraints.HORIZONTAL;
  134. gbc.gridwidth = GridBagConstraints.RELATIVE;
  135. gbc.gridx = 1;
  136. if (item.isValueSecure())
  137. texts[i] = new JPasswordField(20);
  138. else
  139. texts[i] = new JTextField(20);
  140. panel.add(texts[i], gbc);
  141. gbc.gridy++;
  142. } else if (item instanceof CredentialItem.InformationalMessage) {
  143. gbc.fill = GridBagConstraints.NONE;
  144. gbc.gridwidth = GridBagConstraints.REMAINDER;
  145. gbc.gridx = 0;
  146. panel.add(new JLabel(item.getPromptText()), gbc);
  147. gbc.gridy++;
  148. } else {
  149. throw new UnsupportedCredentialItem(uri, item.getPromptText());
  150. }
  151. }
  152. if (JOptionPane.showConfirmDialog(null, panel,
  153. UIText.get().authenticationRequired,
  154. JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE) != JOptionPane.OK_OPTION)
  155. return false; // cancel
  156. for (int i = 0; i < items.length; i++) {
  157. CredentialItem item = items[i];
  158. JTextField f = texts[i];
  159. if (item instanceof CredentialItem.StringType) {
  160. CredentialItem.StringType v = (CredentialItem.StringType) item;
  161. if (f instanceof JPasswordField)
  162. v.setValue(new String(((JPasswordField) f).getPassword()));
  163. else
  164. v.setValue(f.getText());
  165. } else if (item instanceof CredentialItem.CharArrayType) {
  166. CredentialItem.CharArrayType v = (CredentialItem.CharArrayType) item;
  167. if (f instanceof JPasswordField)
  168. v.setValueNoCopy(((JPasswordField) f).getPassword());
  169. else
  170. v.setValueNoCopy(f.getText().toCharArray());
  171. }
  172. }
  173. return true;
  174. }
  175. }