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.

UsernamePasswordCredentialsProvider.java 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright (C) 2010, 2021 Google Inc. and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.transport;
  11. import java.util.Arrays;
  12. import org.eclipse.jgit.errors.UnsupportedCredentialItem;
  13. /**
  14. * Simple {@link org.eclipse.jgit.transport.CredentialsProvider} that always
  15. * uses the same information.
  16. */
  17. public class UsernamePasswordCredentialsProvider extends CredentialsProvider {
  18. private String username;
  19. private char[] password;
  20. /**
  21. * Initialize the provider with a single username and password.
  22. *
  23. * @param username
  24. * user name
  25. * @param password
  26. * password
  27. */
  28. public UsernamePasswordCredentialsProvider(String username, String password) {
  29. this(username, password.toCharArray());
  30. }
  31. /**
  32. * Initialize the provider with a single username and password.
  33. *
  34. * @param username
  35. * user name
  36. * @param password
  37. * password
  38. */
  39. public UsernamePasswordCredentialsProvider(String username, char[] password) {
  40. this.username = username;
  41. this.password = password;
  42. }
  43. /** {@inheritDoc} */
  44. @Override
  45. public boolean isInteractive() {
  46. return false;
  47. }
  48. /** {@inheritDoc} */
  49. @Override
  50. public boolean supports(CredentialItem... items) {
  51. for (CredentialItem i : items) {
  52. if (i instanceof CredentialItem.InformationalMessage) {
  53. continue;
  54. }
  55. if (i instanceof CredentialItem.Username) {
  56. continue;
  57. }
  58. if (i instanceof CredentialItem.Password) {
  59. continue;
  60. }
  61. if (i instanceof CredentialItem.StringType) {
  62. if (i.getPromptText().equals("Password: ")) { //$NON-NLS-1$
  63. continue;
  64. }
  65. }
  66. return false;
  67. }
  68. return true;
  69. }
  70. /** {@inheritDoc} */
  71. @Override
  72. public boolean get(URIish uri, CredentialItem... items)
  73. throws UnsupportedCredentialItem {
  74. for (CredentialItem i : items) {
  75. if (i instanceof CredentialItem.InformationalMessage) {
  76. continue;
  77. }
  78. if (i instanceof CredentialItem.Username) {
  79. ((CredentialItem.Username) i).setValue(username);
  80. continue;
  81. }
  82. if (i instanceof CredentialItem.Password) {
  83. ((CredentialItem.Password) i).setValue(password);
  84. continue;
  85. }
  86. if (i instanceof CredentialItem.StringType) {
  87. if (i.getPromptText().equals("Password: ")) { //$NON-NLS-1$
  88. ((CredentialItem.StringType) i).setValue(new String(
  89. password));
  90. continue;
  91. }
  92. }
  93. throw new UnsupportedCredentialItem(uri, i.getClass().getName()
  94. + ":" + i.getPromptText()); //$NON-NLS-1$
  95. }
  96. return true;
  97. }
  98. /**
  99. * Destroy the saved username and password..
  100. */
  101. public void clear() {
  102. username = null;
  103. if (password != null) {
  104. Arrays.fill(password, (char) 0);
  105. password = null;
  106. }
  107. }
  108. }