BasicAuthentication.java

  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. import static java.nio.charset.StandardCharsets.UTF_8;

  12. import java.net.Authenticator;
  13. import java.net.Authenticator.RequestorType;
  14. import java.net.InetSocketAddress;
  15. import java.net.PasswordAuthentication;
  16. import java.nio.ByteBuffer;
  17. import java.nio.CharBuffer;
  18. import java.security.AccessController;
  19. import java.security.PrivilegedAction;
  20. import java.util.Arrays;
  21. import java.util.concurrent.CancellationException;

  22. import org.eclipse.jgit.internal.transport.sshd.SshdText;
  23. import org.eclipse.jgit.transport.SshConstants;

  24. /**
  25.  * An abstract implementation of a username-password authentication. It can be
  26.  * given an initial known username-password pair; if so, this will be tried
  27.  * first. Subsequent rounds will then try to obtain a user name and password via
  28.  * the global {@link Authenticator}.
  29.  *
  30.  * @param <ParameterType>
  31.  *            defining the parameter type for the authentication
  32.  * @param <TokenType>
  33.  *            defining the token type for the authentication
  34.  */
  35. public abstract class BasicAuthentication<ParameterType, TokenType>
  36.         extends AbstractAuthenticationHandler<ParameterType, TokenType> {

  37.     /** The current user name. */
  38.     protected String user;

  39.     /** The current password. */
  40.     protected byte[] password;

  41.     /**
  42.      * Creates a new {@link BasicAuthentication} to authenticate with the given
  43.      * {@code proxy}.
  44.      *
  45.      * @param proxy
  46.      *            {@link InetSocketAddress} of the proxy to connect to
  47.      * @param initialUser
  48.      *            initial user name to try; may be {@code null}
  49.      * @param initialPassword
  50.      *            initial password to try, may be {@code null}
  51.      */
  52.     public BasicAuthentication(InetSocketAddress proxy, String initialUser,
  53.             char[] initialPassword) {
  54.         super(proxy);
  55.         this.user = initialUser;
  56.         this.password = convert(initialPassword);
  57.     }

  58.     private byte[] convert(char[] pass) {
  59.         if (pass == null) {
  60.             return new byte[0];
  61.         }
  62.         ByteBuffer bytes = UTF_8.encode(CharBuffer.wrap(pass));
  63.         byte[] pwd = new byte[bytes.remaining()];
  64.         bytes.get(pwd);
  65.         if (bytes.hasArray()) {
  66.             Arrays.fill(bytes.array(), (byte) 0);
  67.         }
  68.         Arrays.fill(pass, '\000');
  69.         return pwd;
  70.     }

  71.     /**
  72.      * Clears the {@link #password}.
  73.      */
  74.     protected void clearPassword() {
  75.         if (password != null) {
  76.             Arrays.fill(password, (byte) 0);
  77.         }
  78.         password = new byte[0];
  79.     }

  80.     @Override
  81.     public final void close() {
  82.         clearPassword();
  83.         done = true;
  84.     }

  85.     @Override
  86.     public final void start() throws Exception {
  87.         if (user != null && !user.isEmpty()
  88.                 || password != null && password.length > 0) {
  89.             return;
  90.         }
  91.         askCredentials();
  92.     }

  93.     @Override
  94.     public void process() throws Exception {
  95.         askCredentials();
  96.     }

  97.     /**
  98.      * Asks for credentials via the global {@link Authenticator}.
  99.      */
  100.     protected void askCredentials() {
  101.         clearPassword();
  102.         PasswordAuthentication auth = AccessController.doPrivileged(
  103.                 (PrivilegedAction<PasswordAuthentication>) () -> Authenticator
  104.                         .requestPasswordAuthentication(proxy.getHostString(),
  105.                                 proxy.getAddress(), proxy.getPort(),
  106.                                 SshConstants.SSH_SCHEME,
  107.                                 SshdText.get().proxyPasswordPrompt, "Basic", //$NON-NLS-1$
  108.                                 null, RequestorType.PROXY));
  109.         if (auth == null) {
  110.             user = ""; //$NON-NLS-1$
  111.             throw new CancellationException(
  112.                     SshdText.get().authenticationCanceled);
  113.         }
  114.         user = auth.getUserName();
  115.         password = convert(auth.getPassword());
  116.     }
  117. }