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 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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> and others
  5. *
  6. * This program and the accompanying materials are made available under the
  7. * terms of the Eclipse Distribution License v. 1.0 which is available at
  8. * https://www.eclipse.org/org/documents/edl-v10.php.
  9. *
  10. * SPDX-License-Identifier: BSD-3-Clause
  11. */
  12. package org.eclipse.jgit.awtui;
  13. import java.awt.GridBagConstraints;
  14. import java.awt.GridBagLayout;
  15. import java.awt.Insets;
  16. import javax.swing.JLabel;
  17. import javax.swing.JOptionPane;
  18. import javax.swing.JPanel;
  19. import javax.swing.JPasswordField;
  20. import javax.swing.JTextField;
  21. import org.eclipse.jgit.errors.UnsupportedCredentialItem;
  22. import org.eclipse.jgit.transport.ChainingCredentialsProvider;
  23. import org.eclipse.jgit.transport.CredentialItem;
  24. import org.eclipse.jgit.transport.CredentialsProvider;
  25. import org.eclipse.jgit.transport.NetRCCredentialsProvider;
  26. import org.eclipse.jgit.transport.URIish;
  27. /**
  28. * Interacts with the user during authentication by using AWT/Swing dialogs.
  29. */
  30. public class AwtCredentialsProvider extends CredentialsProvider {
  31. /**
  32. * Install this implementation as the default.
  33. */
  34. public static void install() {
  35. final AwtCredentialsProvider c = new AwtCredentialsProvider();
  36. CredentialsProvider cp = new ChainingCredentialsProvider(
  37. new NetRCCredentialsProvider(), c);
  38. CredentialsProvider.setDefault(cp);
  39. }
  40. /** {@inheritDoc} */
  41. @Override
  42. public boolean isInteractive() {
  43. return true;
  44. }
  45. /** {@inheritDoc} */
  46. @Override
  47. public boolean supports(CredentialItem... items) {
  48. for (CredentialItem i : items) {
  49. if (i instanceof CredentialItem.StringType)
  50. continue;
  51. else if (i instanceof CredentialItem.CharArrayType)
  52. continue;
  53. else if (i instanceof CredentialItem.YesNoType)
  54. continue;
  55. else if (i instanceof CredentialItem.InformationalMessage)
  56. continue;
  57. else
  58. return false;
  59. }
  60. return true;
  61. }
  62. /** {@inheritDoc} */
  63. @Override
  64. public boolean get(URIish uri, CredentialItem... items)
  65. throws UnsupportedCredentialItem {
  66. switch (items.length) {
  67. case 0:
  68. return true;
  69. case 1:
  70. final CredentialItem item = items[0];
  71. if (item instanceof CredentialItem.InformationalMessage) {
  72. JOptionPane.showMessageDialog(null, item.getPromptText(),
  73. UIText.get().warning, JOptionPane.INFORMATION_MESSAGE);
  74. return true;
  75. } else if (item instanceof CredentialItem.YesNoType) {
  76. CredentialItem.YesNoType v = (CredentialItem.YesNoType) item;
  77. int r = JOptionPane.showConfirmDialog(null, v.getPromptText(),
  78. UIText.get().warning, JOptionPane.YES_NO_OPTION);
  79. switch (r) {
  80. case JOptionPane.YES_OPTION:
  81. v.setValue(true);
  82. return true;
  83. case JOptionPane.NO_OPTION:
  84. v.setValue(false);
  85. return true;
  86. case JOptionPane.CANCEL_OPTION:
  87. case JOptionPane.CLOSED_OPTION:
  88. default:
  89. return false;
  90. }
  91. } else {
  92. return interactive(uri, items);
  93. }
  94. default:
  95. return interactive(uri, items);
  96. }
  97. }
  98. private static boolean interactive(URIish uri, CredentialItem[] items) {
  99. final GridBagConstraints gbc = new GridBagConstraints(0, 0, 1, 1, 1, 1,
  100. GridBagConstraints.NORTHWEST, GridBagConstraints.NONE,
  101. new Insets(0, 0, 0, 0), 0, 0);
  102. final JPanel panel = new JPanel();
  103. panel.setLayout(new GridBagLayout());
  104. final JTextField[] texts = new JTextField[items.length];
  105. for (int i = 0; i < items.length; i++) {
  106. CredentialItem item = items[i];
  107. if (item instanceof CredentialItem.StringType
  108. || item instanceof CredentialItem.CharArrayType) {
  109. gbc.fill = GridBagConstraints.NONE;
  110. gbc.gridwidth = GridBagConstraints.RELATIVE;
  111. gbc.gridx = 0;
  112. panel.add(new JLabel(item.getPromptText()), gbc);
  113. gbc.fill = GridBagConstraints.HORIZONTAL;
  114. gbc.gridwidth = GridBagConstraints.RELATIVE;
  115. gbc.gridx = 1;
  116. if (item.isValueSecure())
  117. texts[i] = new JPasswordField(20);
  118. else
  119. texts[i] = new JTextField(20);
  120. panel.add(texts[i], gbc);
  121. gbc.gridy++;
  122. } else if (item instanceof CredentialItem.InformationalMessage) {
  123. gbc.fill = GridBagConstraints.NONE;
  124. gbc.gridwidth = GridBagConstraints.REMAINDER;
  125. gbc.gridx = 0;
  126. panel.add(new JLabel(item.getPromptText()), gbc);
  127. gbc.gridy++;
  128. } else {
  129. throw new UnsupportedCredentialItem(uri, item.getPromptText());
  130. }
  131. }
  132. if (JOptionPane.showConfirmDialog(null, panel,
  133. UIText.get().authenticationRequired,
  134. JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE) != JOptionPane.OK_OPTION)
  135. return false; // cancel
  136. for (int i = 0; i < items.length; i++) {
  137. CredentialItem item = items[i];
  138. JTextField f = texts[i];
  139. if (item instanceof CredentialItem.StringType) {
  140. CredentialItem.StringType v = (CredentialItem.StringType) item;
  141. if (f instanceof JPasswordField)
  142. v.setValue(new String(((JPasswordField) f).getPassword()));
  143. else
  144. v.setValue(f.getText());
  145. } else if (item instanceof CredentialItem.CharArrayType) {
  146. CredentialItem.CharArrayType v = (CredentialItem.CharArrayType) item;
  147. if (f instanceof JPasswordField)
  148. v.setValueNoCopy(((JPasswordField) f).getPassword());
  149. else
  150. v.setValueNoCopy(f.getText().toCharArray());
  151. }
  152. }
  153. return true;
  154. }
  155. }