Java Code Examples for java.net.Authenticator#requestPasswordAuthentication()

The following examples show how to use java.net.Authenticator#requestPasswordAuthentication() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HttpAuthenticator.java    From wildfly-samples with MIT License 6 votes vote down vote up
@Override public Credential authenticate(
    Proxy proxy, URL url, List<Challenge> challenges) throws IOException {
  for (Challenge challenge : challenges) {
    if (!"Basic".equalsIgnoreCase(challenge.getScheme())) {
      continue;
    }

    PasswordAuthentication auth = Authenticator.requestPasswordAuthentication(url.getHost(),
        getConnectToInetAddress(proxy, url), url.getPort(), url.getProtocol(),
        challenge.getRealm(), challenge.getScheme(), url, Authenticator.RequestorType.SERVER);
    if (auth != null) {
      return Credential.basic(auth.getUserName(), new String(auth.getPassword()));
    }
  }
  return null;
}
 
Example 2
Source File: NegotiateCallbackHandler.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
private void getAnswer() {
    if (!answered) {
        answered = true;
        PasswordAuthentication passAuth =
                Authenticator.requestPasswordAuthentication(
                hci.host, hci.addr, hci.port, hci.protocol,
                hci.prompt, hci.scheme, hci.url, hci.authType);
        /**
         * To be compatible with existing callback handler implementations,
         * when the underlying Authenticator is canceled, username and
         * password are assigned null. No exception is thrown.
         */
        if (passAuth != null) {
            username = passAuth.getUserName();
            password = passAuth.getPassword();
        }
    }
}
 
Example 3
Source File: HttpAuthenticator.java    From L.TileLayer.Cordova with MIT License 6 votes vote down vote up
@Override public Credential authenticateProxy(
    Proxy proxy, URL url, List<Challenge> challenges) throws IOException {
  for (Challenge challenge : challenges) {
    if (!"Basic".equalsIgnoreCase(challenge.getScheme())) {
      continue;
    }

    InetSocketAddress proxyAddress = (InetSocketAddress) proxy.address();
    PasswordAuthentication auth = Authenticator.requestPasswordAuthentication(
        proxyAddress.getHostName(), getConnectToInetAddress(proxy, url), proxyAddress.getPort(),
        url.getProtocol(), challenge.getRealm(), challenge.getScheme(), url,
        Authenticator.RequestorType.PROXY);
    if (auth != null) {
      return Credential.basic(auth.getUserName(), new String(auth.getPassword()));
    }
  }
  return null;
}
 
Example 4
Source File: IvyAuthenticatorTest.java    From ant-ivy with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that when {@link IvyAuthenticator} can't handle a authentication request and falls back
 * on an authenticator that was previously set, before IvyAuthenticator installed on top of it,
 * the other authenticator gets passed all the relevant requesting information, including the
 * {@link Authenticator#getRequestingURL() requesting URL} and
 * {@link Authenticator#getRequestorType() request type}
 *
 * @throws Exception if something goes wrong
 * @see <a href="https://issues.apache.org/jira/browse/IVY-1557">IVY-1557</a>
 */
@Test
public void testRequestURLAndType() throws Exception {
    testAuthenticator.expectedHost = "localhost";
    testAuthenticator.expectedPort = 12345;
    testAuthenticator.expectedPrompt = "Test prompt - testRequestURLAndType";
    testAuthenticator.expectedProtocol = "HTTP/1.1";
    testAuthenticator.expectedURL = new URL("http", "localhost", 12345, "/a/b/c");
    testAuthenticator.expectedType = Authenticator.RequestorType.PROXY;
    testAuthenticator.expectedScheme = "BASIC";
    testAuthenticator.expectedSite = InetAddress.getLoopbackAddress();

    // trigger the authentication
    final PasswordAuthentication auth = Authenticator.requestPasswordAuthentication(testAuthenticator.expectedHost, testAuthenticator.expectedSite,
            testAuthenticator.expectedPort, testAuthenticator.expectedProtocol, testAuthenticator.expectedPrompt,
            testAuthenticator.expectedScheme, testAuthenticator.expectedURL, testAuthenticator.expectedType);

    assertNotNull("Expected a password authentication, but got none", auth);
    assertEquals("Unexpected username", "dummy", auth.getUserName());
    assertTrue("Unexpected password", Arrays.equals("dummy".toCharArray(), auth.getPassword()));
}
 
Example 5
Source File: NegotiateCallbackHandler.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
private void getAnswer() {
    if (!answered) {
        answered = true;

        if (LoginConfigImpl.HTTP_USE_GLOBAL_CREDS) {
            PasswordAuthentication passAuth =
                    Authenticator.requestPasswordAuthentication(
                            hci.host, hci.addr, hci.port, hci.protocol,
                            hci.prompt, hci.scheme, hci.url, hci.authType);
            /**
             * To be compatible with existing callback handler implementations,
             * when the underlying Authenticator is canceled, username and
             * password are assigned null. No exception is thrown.
             */
            if (passAuth != null) {
                username = passAuth.getUserName();
                password = passAuth.getPassword();
            }
        }
    }
}
 
Example 6
Source File: NegotiateCallbackHandler.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
private void getAnswer() {
    if (!answered) {
        answered = true;
        PasswordAuthentication passAuth =
                Authenticator.requestPasswordAuthentication(
                hci.host, hci.addr, hci.port, hci.protocol,
                hci.prompt, hci.scheme, hci.url, hci.authType);
        /**
         * To be compatible with existing callback handler implementations,
         * when the underlying Authenticator is canceled, username and
         * password are assigned null. No exception is thrown.
         */
        if (passAuth != null) {
            username = passAuth.getUserName();
            password = passAuth.getPassword();
        }
    }
}
 
Example 7
Source File: NegotiateCallbackHandler.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
private void getAnswer() {
    if (!answered) {
        answered = true;
        PasswordAuthentication passAuth =
                Authenticator.requestPasswordAuthentication(
                hci.host, hci.addr, hci.port, hci.protocol,
                hci.prompt, hci.scheme, hci.url, hci.authType);
        /**
         * To be compatible with existing callback handler implementations,
         * when the underlying Authenticator is canceled, username and
         * password are assigned null. No exception is thrown.
         */
        if (passAuth != null) {
            username = passAuth.getUserName();
            password = passAuth.getPassword();
        }
    }
}
 
Example 8
Source File: HttpAuthenticator.java    From cordova-amazon-fireos with Apache License 2.0 6 votes vote down vote up
@Override public Credential authenticate(
    Proxy proxy, URL url, List<Challenge> challenges) throws IOException {
  for (Challenge challenge : challenges) {
    if (!"Basic".equalsIgnoreCase(challenge.getScheme())) {
      continue;
    }

    PasswordAuthentication auth = Authenticator.requestPasswordAuthentication(url.getHost(),
        getConnectToInetAddress(proxy, url), url.getPort(), url.getProtocol(),
        challenge.getRealm(), challenge.getScheme(), url, Authenticator.RequestorType.SERVER);
    if (auth != null) {
      return Credential.basic(auth.getUserName(), new String(auth.getPassword()));
    }
  }
  return null;
}
 
Example 9
Source File: HttpAuthenticator.java    From phonegapbootcampsite with MIT License 6 votes vote down vote up
@Override public Credential authenticate(
    Proxy proxy, URL url, List<Challenge> challenges) throws IOException {
  for (Challenge challenge : challenges) {
    if (!"Basic".equalsIgnoreCase(challenge.getScheme())) {
      continue;
    }

    PasswordAuthentication auth = Authenticator.requestPasswordAuthentication(url.getHost(),
        getConnectToInetAddress(proxy, url), url.getPort(), url.getProtocol(),
        challenge.getRealm(), challenge.getScheme(), url, Authenticator.RequestorType.SERVER);
    if (auth != null) {
      return Credential.basic(auth.getUserName(), new String(auth.getPassword()));
    }
  }
  return null;
}
 
Example 10
Source File: NegotiateCallbackHandler.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
private void getAnswer() {
    if (!answered) {
        answered = true;

        if (LoginConfigImpl.HTTP_USE_GLOBAL_CREDS) {
            PasswordAuthentication passAuth =
                    Authenticator.requestPasswordAuthentication(
                            hci.host, hci.addr, hci.port, hci.protocol,
                            hci.prompt, hci.scheme, hci.url, hci.authType);
            /**
             * To be compatible with existing callback handler implementations,
             * when the underlying Authenticator is canceled, username and
             * password are assigned null. No exception is thrown.
             */
            if (passAuth != null) {
                username = passAuth.getUserName();
                password = passAuth.getPassword();
            }
        }
    }
}
 
Example 11
Source File: ProxyAuthenticatorTest.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void shouldInitHttpProxyAuthenticator() throws Exception {
  // when
  ProxyAuthenticator.initAuthenticator(HTTP_URL);

  // then
  PasswordAuthentication testAuthentication =
      Authenticator.requestPasswordAuthentication(null, 0, null, null, null);
  assertEquals(testAuthentication.getUserName(), "user1");
  assertEquals(String.valueOf(testAuthentication.getPassword()), "paswd1");

  // when
  ProxyAuthenticator.resetAuthenticator();

  // then
  testAuthentication = Authenticator.requestPasswordAuthentication(null, 0, null, null, null);
  assertEquals(testAuthentication, null);
}
 
Example 12
Source File: HttpAuthenticator.java    From phonegapbootcampsite with MIT License 6 votes vote down vote up
@Override public Credential authenticateProxy(
    Proxy proxy, URL url, List<Challenge> challenges) throws IOException {
  for (Challenge challenge : challenges) {
    if (!"Basic".equalsIgnoreCase(challenge.getScheme())) {
      continue;
    }

    InetSocketAddress proxyAddress = (InetSocketAddress) proxy.address();
    PasswordAuthentication auth = Authenticator.requestPasswordAuthentication(
        proxyAddress.getHostName(), getConnectToInetAddress(proxy, url), proxyAddress.getPort(),
        url.getProtocol(), challenge.getRealm(), challenge.getScheme(), url,
        Authenticator.RequestorType.PROXY);
    if (auth != null) {
      return Credential.basic(auth.getUserName(), new String(auth.getPassword()));
    }
  }
  return null;
}
 
Example 13
Source File: HttpAuthenticator.java    From CordovaYoutubeVideoPlayer with MIT License 6 votes vote down vote up
@Override public Credential authenticate(
    Proxy proxy, URL url, List<Challenge> challenges) throws IOException {
  for (Challenge challenge : challenges) {
    if (!"Basic".equalsIgnoreCase(challenge.getScheme())) {
      continue;
    }

    PasswordAuthentication auth = Authenticator.requestPasswordAuthentication(url.getHost(),
        getConnectToInetAddress(proxy, url), url.getPort(), url.getProtocol(),
        challenge.getRealm(), challenge.getScheme(), url, Authenticator.RequestorType.SERVER);
    if (auth != null) {
      return Credential.basic(auth.getUserName(), new String(auth.getPassword()));
    }
  }
  return null;
}
 
Example 14
Source File: HttpAuthenticator.java    From bluemix-parking-meter with MIT License 6 votes vote down vote up
@Override public Credential authenticateProxy(
    Proxy proxy, URL url, List<Challenge> challenges) throws IOException {
  for (Challenge challenge : challenges) {
    if (!"Basic".equalsIgnoreCase(challenge.getScheme())) {
      continue;
    }

    InetSocketAddress proxyAddress = (InetSocketAddress) proxy.address();
    PasswordAuthentication auth = Authenticator.requestPasswordAuthentication(
        proxyAddress.getHostName(), getConnectToInetAddress(proxy, url), proxyAddress.getPort(),
        url.getProtocol(), challenge.getRealm(), challenge.getScheme(), url,
        Authenticator.RequestorType.PROXY);
    if (auth != null) {
      return Credential.basic(auth.getUserName(), new String(auth.getPassword()));
    }
  }
  return null;
}
 
Example 15
Source File: NegotiateCallbackHandler.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
private void getAnswer() {
    if (!answered) {
        answered = true;

        if (LoginConfigImpl.HTTP_USE_GLOBAL_CREDS) {
            PasswordAuthentication passAuth =
                    Authenticator.requestPasswordAuthentication(
                            hci.host, hci.addr, hci.port, hci.protocol,
                            hci.prompt, hci.scheme, hci.url, hci.authType);
            /**
             * To be compatible with existing callback handler implementations,
             * when the underlying Authenticator is canceled, username and
             * password are assigned null. No exception is thrown.
             */
            if (passAuth != null) {
                username = passAuth.getUserName();
                password = passAuth.getPassword();
            }
        }
    }
}
 
Example 16
Source File: HttpAuthenticator.java    From cordova-amazon-fireos with Apache License 2.0 6 votes vote down vote up
@Override public Credential authenticateProxy(
    Proxy proxy, URL url, List<Challenge> challenges) throws IOException {
  for (Challenge challenge : challenges) {
    if (!"Basic".equalsIgnoreCase(challenge.getScheme())) {
      continue;
    }

    InetSocketAddress proxyAddress = (InetSocketAddress) proxy.address();
    PasswordAuthentication auth = Authenticator.requestPasswordAuthentication(
        proxyAddress.getHostName(), getConnectToInetAddress(proxy, url), proxyAddress.getPort(),
        url.getProtocol(), challenge.getRealm(), challenge.getScheme(), url,
        Authenticator.RequestorType.PROXY);
    if (auth != null) {
      return Credential.basic(auth.getUserName(), new String(auth.getPassword()));
    }
  }
  return null;
}
 
Example 17
Source File: NbAuthenticatorTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testUserInfoInUrl () throws Exception {
    NbAuthenticator.install4test();
    PasswordAuthentication auth = Authenticator.requestPasswordAuthentication("wher.ev.er", Inet4Address.getByName("1.2.3.4"), 1234, "http", null, "http",
            new URL("http://user:[email protected]/resource"), Authenticator.RequestorType.SERVER);
    
    assertNotNull(auth);
    assertEquals("user", auth.getUserName());
    assertEquals("password", new String(auth.getPassword()));
}
 
Example 18
Source File: HttpClientFactory.java    From rug-cli with GNU General Public License v3.0 5 votes vote down vote up
private static void configureProxy(HttpClientBuilder builder, String url) {
    List<Proxy> proxies = ProxySelector.getDefault().select(URI.create(url));
    if (!proxies.isEmpty()) {
        Optional<Proxy> proxy = proxies.stream().filter(p -> p.type().equals(Proxy.Type.HTTP))
                .findFirst();
        if (proxy.isPresent()) {
            InetSocketAddress address = (InetSocketAddress) proxy.get().address();
            builder.setProxy(new HttpHost(address.getHostName(), address.getPort()));

            try {
                PasswordAuthentication auth = Authenticator.requestPasswordAuthentication(
                        address.getHostName(), null, address.getPort(),
                        (url.startsWith("https://") ? "https" : "http"),
                        "Credentials for proxy " + proxy, null, new URL(url),
                        Authenticator.RequestorType.PROXY);
                if (auth != null) {
                    CredentialsProvider credsProvider = new BasicCredentialsProvider();
                    credsProvider.setCredentials(
                            new AuthScope(address.getHostName(), address.getPort()),
                            new UsernamePasswordCredentials(auth.getUserName(),
                                    String.valueOf(auth.getPassword())));
                    builder.setDefaultCredentialsProvider(credsProvider);
                }
            }
            catch (MalformedURLException e) {
            }
        }
    }
}
 
Example 19
Source File: NetworkSettingsTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testIsAuthenticationDialogNotSuppressed() throws Exception {
    final boolean[] suppressed = new boolean[1];
    Authenticator.setDefault(new Authenticator() {

        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            suppressed[0] = NetworkSettings.isAuthenticationDialogSuppressed();
            return super.getPasswordAuthentication();
        }
    });

    Authenticator.requestPasswordAuthentication("wher.ev.er", Inet4Address.getByName("1.2.3.4"), 1234, "http", null, "http");
    assertFalse(suppressed[0]);
}
 
Example 20
Source File: HttpAuthenticator.java    From cordova-android-chromeview with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the authorization credentials that may satisfy the challenge.
 * Returns null if a challenge header was not provided or if credentials
 * were not available.
 */
private static String getCredentials(RawHeaders responseHeaders, String challengeHeader,
    Proxy proxy, URL url) throws IOException {
  List<Challenge> challenges = parseChallenges(responseHeaders, challengeHeader);
  if (challenges.isEmpty()) {
    return null;
  }

  for (Challenge challenge : challenges) {
    // Use the global authenticator to get the password.
    PasswordAuthentication auth;
    if (responseHeaders.getResponseCode() == HTTP_PROXY_AUTH) {
      InetSocketAddress proxyAddress = (InetSocketAddress) proxy.address();
      auth = Authenticator.requestPasswordAuthentication(proxyAddress.getHostName(),
          getConnectToInetAddress(proxy, url), proxyAddress.getPort(), url.getProtocol(),
          challenge.realm, challenge.scheme, url, Authenticator.RequestorType.PROXY);
    } else {
      auth = Authenticator.requestPasswordAuthentication(url.getHost(),
          getConnectToInetAddress(proxy, url), url.getPort(), url.getProtocol(), challenge.realm,
          challenge.scheme, url, Authenticator.RequestorType.SERVER);
    }
    if (auth == null) {
      continue;
    }

    // Use base64 to encode the username and password.
    String usernameAndPassword = auth.getUserName() + ":" + new String(auth.getPassword());
    byte[] bytes = usernameAndPassword.getBytes("ISO-8859-1");
    String encoded = Base64.encode(bytes);
    return challenge.scheme + " " + encoded;
  }

  return null;
}