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.

CachedAuthenticator.java 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Copyright (C) 2009, Google Inc.
  3. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org> and others
  4. *
  5. * This program and the accompanying materials are made available under the
  6. * terms of the Eclipse Distribution License v. 1.0 which is available at
  7. * https://www.eclipse.org/org/documents/edl-v10.php.
  8. *
  9. * SPDX-License-Identifier: BSD-3-Clause
  10. */
  11. package org.eclipse.jgit.util;
  12. import java.net.Authenticator;
  13. import java.net.PasswordAuthentication;
  14. import java.util.Collection;
  15. import java.util.concurrent.CopyOnWriteArrayList;
  16. /**
  17. * Abstract authenticator which remembers prior authentications.
  18. */
  19. public abstract class CachedAuthenticator extends Authenticator {
  20. private static final Collection<CachedAuthentication> cached = new CopyOnWriteArrayList<>();
  21. /**
  22. * Add a cached authentication for future use.
  23. *
  24. * @param ca
  25. * the information we should remember.
  26. */
  27. public static void add(CachedAuthentication ca) {
  28. cached.add(ca);
  29. }
  30. /** {@inheritDoc} */
  31. @Override
  32. protected final PasswordAuthentication getPasswordAuthentication() {
  33. final String host = getRequestingHost();
  34. final int port = getRequestingPort();
  35. for (CachedAuthentication ca : cached) {
  36. if (ca.host.equals(host) && ca.port == port)
  37. return ca.toPasswordAuthentication();
  38. }
  39. PasswordAuthentication pa = promptPasswordAuthentication();
  40. if (pa != null) {
  41. CachedAuthentication ca = new CachedAuthentication(host, port, pa
  42. .getUserName(), new String(pa.getPassword()));
  43. add(ca);
  44. return ca.toPasswordAuthentication();
  45. }
  46. return null;
  47. }
  48. /**
  49. * Prompt for and request authentication from the end-user.
  50. *
  51. * @return the authentication data; null if the user canceled the request
  52. * and does not want to continue.
  53. */
  54. protected abstract PasswordAuthentication promptPasswordAuthentication();
  55. /** Authentication data to remember and reuse. */
  56. public static class CachedAuthentication {
  57. final String host;
  58. final int port;
  59. final String user;
  60. final String pass;
  61. /**
  62. * Create a new cached authentication.
  63. *
  64. * @param aHost
  65. * system this is for.
  66. * @param aPort
  67. * port number of the service.
  68. * @param aUser
  69. * username at the service.
  70. * @param aPass
  71. * password at the service.
  72. */
  73. public CachedAuthentication(final String aHost, final int aPort,
  74. final String aUser, final String aPass) {
  75. host = aHost;
  76. port = aPort;
  77. user = aUser;
  78. pass = aPass;
  79. }
  80. PasswordAuthentication toPasswordAuthentication() {
  81. return new PasswordAuthentication(user, pass.toCharArray());
  82. }
  83. }
  84. }