View Javadoc
1   /*
2    * Copyright (C) 2018, Thomas Wolf <thomas.wolf@paranor.ch> 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.internal.transport.sshd.auth;
11  
12  import static java.nio.charset.StandardCharsets.UTF_8;
13  
14  import java.net.Authenticator;
15  import java.net.Authenticator.RequestorType;
16  import java.net.InetSocketAddress;
17  import java.net.PasswordAuthentication;
18  import java.nio.ByteBuffer;
19  import java.nio.CharBuffer;
20  import java.security.AccessController;
21  import java.security.PrivilegedAction;
22  import java.util.Arrays;
23  import java.util.concurrent.CancellationException;
24  
25  import org.eclipse.jgit.internal.transport.sshd.SshdText;
26  import org.eclipse.jgit.transport.SshConstants;
27  
28  /**
29   * An abstract implementation of a username-password authentication. It can be
30   * given an initial known username-password pair; if so, this will be tried
31   * first. Subsequent rounds will then try to obtain a user name and password via
32   * the global {@link Authenticator}.
33   *
34   * @param <ParameterType>
35   *            defining the parameter type for the authentication
36   * @param <TokenType>
37   *            defining the token type for the authentication
38   */
39  public abstract class BasicAuthentication<ParameterType, TokenType>
40  		extends AbstractAuthenticationHandler<ParameterType, TokenType> {
41  
42  	/** The current user name. */
43  	protected String user;
44  
45  	/** The current password. */
46  	protected byte[] password;
47  
48  	/**
49  	 * Creates a new {@link BasicAuthentication} to authenticate with the given
50  	 * {@code proxy}.
51  	 *
52  	 * @param proxy
53  	 *            {@link InetSocketAddress} of the proxy to connect to
54  	 * @param initialUser
55  	 *            initial user name to try; may be {@code null}
56  	 * @param initialPassword
57  	 *            initial password to try, may be {@code null}
58  	 */
59  	public BasicAuthentication(InetSocketAddress proxy, String initialUser,
60  			char[] initialPassword) {
61  		super(proxy);
62  		this.user = initialUser;
63  		this.password = convert(initialPassword);
64  	}
65  
66  	private byte[] convert(char[] pass) {
67  		if (pass == null) {
68  			return new byte[0];
69  		}
70  		ByteBuffer bytes = UTF_8.encode(CharBuffer.wrap(pass));
71  		byte[] pwd = new byte[bytes.remaining()];
72  		bytes.get(pwd);
73  		if (bytes.hasArray()) {
74  			Arrays.fill(bytes.array(), (byte) 0);
75  		}
76  		Arrays.fill(pass, '\000');
77  		return pwd;
78  	}
79  
80  	/**
81  	 * Clears the {@link #password}.
82  	 */
83  	protected void clearPassword() {
84  		if (password != null) {
85  			Arrays.fill(password, (byte) 0);
86  		}
87  		password = new byte[0];
88  	}
89  
90  	@Override
91  	public final void close() {
92  		clearPassword();
93  		done = true;
94  	}
95  
96  	@Override
97  	public final void start() throws Exception {
98  		if (user != null && !user.isEmpty()
99  				|| password != null && password.length > 0) {
100 			return;
101 		}
102 		askCredentials();
103 	}
104 
105 	@Override
106 	public void process() throws Exception {
107 		askCredentials();
108 	}
109 
110 	/**
111 	 * Asks for credentials via the global {@link Authenticator}.
112 	 */
113 	protected void askCredentials() {
114 		clearPassword();
115 		PasswordAuthentication auth = AccessController.doPrivileged(
116 				(PrivilegedAction<PasswordAuthentication>) () -> Authenticator
117 						.requestPasswordAuthentication(proxy.getHostString(),
118 								proxy.getAddress(), proxy.getPort(),
119 								SshConstants.SSH_SCHEME,
120 								SshdText.get().proxyPasswordPrompt, "Basic", //$NON-NLS-1$
121 								null, RequestorType.PROXY));
122 		if (auth == null) {
123 			user = ""; //$NON-NLS-1$
124 			throw new CancellationException(
125 					SshdText.get().authenticationCanceled);
126 		}
127 		user = auth.getUserName();
128 		password = convert(auth.getPassword());
129 	}
130 }