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.

ConsoleCredentialsProvider.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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.console;
  13. import java.io.Console;
  14. import org.eclipse.jgit.errors.UnsupportedCredentialItem;
  15. import org.eclipse.jgit.pgm.internal.CLIText;
  16. import org.eclipse.jgit.transport.ChainingCredentialsProvider;
  17. import org.eclipse.jgit.transport.CredentialItem;
  18. import org.eclipse.jgit.transport.CredentialsProvider;
  19. import org.eclipse.jgit.transport.NetRCCredentialsProvider;
  20. import org.eclipse.jgit.transport.URIish;
  21. /**
  22. * Interacts with the user during authentication by using the text console.
  23. *
  24. * @since 4.0
  25. */
  26. public class ConsoleCredentialsProvider extends CredentialsProvider {
  27. /**
  28. * Install this implementation as the default.
  29. */
  30. public static void install() {
  31. final ConsoleCredentialsProvider c = new ConsoleCredentialsProvider();
  32. if (c.cons == null)
  33. throw new NoClassDefFoundError(
  34. CLIText.get().noSystemConsoleAvailable);
  35. CredentialsProvider cp = new ChainingCredentialsProvider(
  36. new NetRCCredentialsProvider(), c);
  37. CredentialsProvider.setDefault(cp);
  38. }
  39. private final Console cons = System.console();
  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. boolean ok = true;
  67. for (int i = 0; i < items.length && ok; i++) {
  68. CredentialItem item = items[i];
  69. if (item instanceof CredentialItem.StringType)
  70. ok = get((CredentialItem.StringType) item);
  71. else if (item instanceof CredentialItem.CharArrayType)
  72. ok = get((CredentialItem.CharArrayType) item);
  73. else if (item instanceof CredentialItem.YesNoType)
  74. ok = get((CredentialItem.YesNoType) item);
  75. else if (item instanceof CredentialItem.InformationalMessage)
  76. ok = get((CredentialItem.InformationalMessage) item);
  77. else
  78. throw new UnsupportedCredentialItem(uri, item.getPromptText());
  79. }
  80. return ok;
  81. }
  82. private boolean get(CredentialItem.StringType item) {
  83. if (item.isValueSecure()) {
  84. char[] v = cons.readPassword("%s: ", item.getPromptText()); //$NON-NLS-1$
  85. if (v != null) {
  86. item.setValue(new String(v));
  87. return true;
  88. }
  89. return false;
  90. }
  91. String v = cons.readLine("%s: ", item.getPromptText()); //$NON-NLS-1$
  92. if (v != null) {
  93. item.setValue(v);
  94. return true;
  95. }
  96. return false;
  97. }
  98. private boolean get(CredentialItem.CharArrayType item) {
  99. if (item.isValueSecure()) {
  100. char[] v = cons.readPassword("%s: ", item.getPromptText()); //$NON-NLS-1$
  101. if (v != null) {
  102. item.setValueNoCopy(v);
  103. return true;
  104. }
  105. return false;
  106. }
  107. String v = cons.readLine("%s: ", item.getPromptText()); //$NON-NLS-1$
  108. if (v != null) {
  109. item.setValueNoCopy(v.toCharArray());
  110. return true;
  111. }
  112. return false;
  113. }
  114. private boolean get(CredentialItem.InformationalMessage item) {
  115. cons.printf("%s\n", item.getPromptText()); //$NON-NLS-1$
  116. cons.flush();
  117. return true;
  118. }
  119. private boolean get(CredentialItem.YesNoType item) {
  120. String r = cons.readLine("%s [%s/%s]? ", item.getPromptText(), //$NON-NLS-1$
  121. CLIText.get().answerYes, CLIText.get().answerNo);
  122. if (r != null) {
  123. item.setValue(CLIText.get().answerYes.equalsIgnoreCase(r));
  124. return true;
  125. }
  126. return false;
  127. }
  128. }