View Javadoc
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  
12  package org.eclipse.jgit.util;
13  
14  import java.net.Authenticator;
15  import java.net.PasswordAuthentication;
16  import java.util.Collection;
17  import java.util.concurrent.CopyOnWriteArrayList;
18  
19  /**
20   * Abstract authenticator which remembers prior authentications.
21   */
22  public abstract class CachedAuthenticator extends Authenticator {
23  	private static final Collection<CachedAuthentication> cached = new CopyOnWriteArrayList<>();
24  
25  	/**
26  	 * Add a cached authentication for future use.
27  	 *
28  	 * @param ca
29  	 *            the information we should remember.
30  	 */
31  	public static void add(CachedAuthentication ca) {
32  		cached.add(ca);
33  	}
34  
35  	/** {@inheritDoc} */
36  	@Override
37  	protected final PasswordAuthentication getPasswordAuthentication() {
38  		final String host = getRequestingHost();
39  		final int port = getRequestingPort();
40  		for (CachedAuthentication ca : cached) {
41  			if (ca.host.equals(host) && ca.port == port)
42  				return ca.toPasswordAuthentication();
43  		}
44  		PasswordAuthentication pa = promptPasswordAuthentication();
45  		if (pa != null) {
46  			CachedAuthentication ca = new CachedAuthentication(host, port, pa
47  					.getUserName(), new String(pa.getPassword()));
48  			add(ca);
49  			return ca.toPasswordAuthentication();
50  		}
51  		return null;
52  	}
53  
54  	/**
55  	 * Prompt for and request authentication from the end-user.
56  	 *
57  	 * @return the authentication data; null if the user canceled the request
58  	 *         and does not want to continue.
59  	 */
60  	protected abstract PasswordAuthentication promptPasswordAuthentication();
61  
62  	/** Authentication data to remember and reuse. */
63  	public static class CachedAuthentication {
64  		final String host;
65  
66  		final int port;
67  
68  		final String user;
69  
70  		final String pass;
71  
72  		/**
73  		 * Create a new cached authentication.
74  		 *
75  		 * @param aHost
76  		 *            system this is for.
77  		 * @param aPort
78  		 *            port number of the service.
79  		 * @param aUser
80  		 *            username at the service.
81  		 * @param aPass
82  		 *            password at the service.
83  		 */
84  		public CachedAuthentication(final String aHost, final int aPort,
85  				final String aUser, final String aPass) {
86  			host = aHost;
87  			port = aPort;
88  			user = aUser;
89  			pass = aPass;
90  		}
91  
92  		PasswordAuthentication toPasswordAuthentication() {
93  			return new PasswordAuthentication(user, pass.toCharArray());
94  		}
95  	}
96  }