Search in sources :

Example 31 with PasswordAuthentication

use of java.net.PasswordAuthentication in project robovm by robovm.

the class OldPasswordAuthenticationTest method test_ConstructorLjava_lang_String$C.

public void test_ConstructorLjava_lang_String$C() {
    String name = "name";
    char[] password = "hunter2".toCharArray();
    try {
        new PasswordAuthentication(name, null);
        fail("NullPointerException was not thrown.");
    } catch (NullPointerException npe) {
    //expected
    }
    PasswordAuthentication pa = new PasswordAuthentication(null, password);
    assertNull(pa.getUserName());
    assertEquals(password.length, pa.getPassword().length);
}
Also used : PasswordAuthentication(java.net.PasswordAuthentication)

Example 32 with PasswordAuthentication

use of java.net.PasswordAuthentication in project sonarqube by SonarSource.

the class DefaultHttpDownloaderTest method configure_http_proxy_credentials.

@Test
public void configure_http_proxy_credentials() {
    DefaultHttpDownloader.AuthenticatorFacade system = mock(DefaultHttpDownloader.AuthenticatorFacade.class);
    Settings settings = new MapSettings();
    settings.setProperty("https.proxyHost", "1.2.3.4");
    settings.setProperty("http.proxyUser", "the_login");
    settings.setProperty("http.proxyPassword", "the_passwd");
    new DefaultHttpDownloader.BaseHttpDownloader(system, settings, null);
    verify(system).setDefaultAuthenticator(argThat(new TypeSafeMatcher<Authenticator>() {

        @Override
        protected boolean matchesSafely(Authenticator authenticator) {
            DefaultHttpDownloader.ProxyAuthenticator a = (DefaultHttpDownloader.ProxyAuthenticator) authenticator;
            PasswordAuthentication authentication = a.getPasswordAuthentication();
            return authentication.getUserName().equals("the_login") && new String(authentication.getPassword()).equals("the_passwd");
        }

        @Override
        public void describeTo(Description description) {
        }
    }));
}
Also used : TypeSafeMatcher(org.hamcrest.TypeSafeMatcher) Description(org.hamcrest.Description) MapSettings(org.sonar.api.config.MapSettings) Settings(org.sonar.api.config.Settings) MapSettings(org.sonar.api.config.MapSettings) Authenticator(java.net.Authenticator) PasswordAuthentication(java.net.PasswordAuthentication) Test(org.junit.Test)

Example 33 with PasswordAuthentication

use of java.net.PasswordAuthentication in project XobotOS by xamarin.

the class HttpURLConnectionImpl method getAuthorizationCredentials.

/**
     * Returns the authorization credentials on the base of provided challenge.
     */
private String getAuthorizationCredentials(String challenge) throws IOException {
    int idx = challenge.indexOf(" ");
    if (idx == -1) {
        return null;
    }
    String scheme = challenge.substring(0, idx);
    int realm = challenge.indexOf("realm=\"") + 7;
    String prompt = null;
    if (realm != -1) {
        int end = challenge.indexOf('"', realm);
        if (end != -1) {
            prompt = challenge.substring(realm, end);
        }
    }
    // use the global authenticator to get the password
    PasswordAuthentication pa = Authenticator.requestPasswordAuthentication(getConnectToInetAddress(), getConnectToPort(), url.getProtocol(), prompt, scheme);
    if (pa == null) {
        return null;
    }
    // base64 encode the username and password
    String usernameAndPassword = pa.getUserName() + ":" + new String(pa.getPassword());
    byte[] bytes = usernameAndPassword.getBytes(Charsets.ISO_8859_1);
    String encoded = Base64.encode(bytes);
    return scheme + " " + encoded;
}
Also used : PasswordAuthentication(java.net.PasswordAuthentication)

Example 34 with PasswordAuthentication

use of java.net.PasswordAuthentication in project intellij-community by JetBrains.

the class IdeaWideAuthenticator method getPasswordAuthentication.

@Override
public PasswordAuthentication getPasswordAuthentication() {
    final String host = CommonProxy.getHostNameReliably(getRequestingHost(), getRequestingSite(), getRequestingURL());
    final boolean isProxy = Authenticator.RequestorType.PROXY.equals(getRequestorType());
    final String prefix = isProxy ? "Proxy authentication: " : "Server authentication: ";
    Application application = ApplicationManager.getApplication();
    if (isProxy) {
        // according to idea-wide settings
        if (myHttpConfigurable.USE_HTTP_PROXY) {
            LOG.debug("CommonAuthenticator.getPasswordAuthentication will return common defined proxy");
            return myHttpConfigurable.getPromptedAuthentication(host + ":" + getRequestingPort(), getRequestingPrompt());
        } else if (myHttpConfigurable.USE_PROXY_PAC) {
            LOG.debug("CommonAuthenticator.getPasswordAuthentication will return autodetected proxy");
            if (myHttpConfigurable.isGenericPasswordCanceled(host, getRequestingPort()))
                return null;
            // same but without remembering the results..
            final PasswordAuthentication password = myHttpConfigurable.getGenericPassword(host, getRequestingPort());
            if (password != null) {
                return password;
            }
            // do not try to show any dialogs if application is exiting
            if (application == null || application.isDisposeInProgress() || application.isDisposed()) {
                return null;
            }
            return myHttpConfigurable.getGenericPromptedAuthentication(prefix, host, getRequestingPrompt(), getRequestingPort(), true);
        }
    }
    // do not try to show any dialogs if application is exiting
    if (application == null || application.isDisposeInProgress() || application.isDisposed()) {
        return null;
    }
    LOG.debug("CommonAuthenticator.getPasswordAuthentication generic authentication will be asked");
    //return myHttpConfigurable.getGenericPromptedAuthentication(prefix, host, getRequestingPrompt(), getRequestingPort(), false);
    return null;
}
Also used : Application(com.intellij.openapi.application.Application) PasswordAuthentication(java.net.PasswordAuthentication)

Example 35 with PasswordAuthentication

use of java.net.PasswordAuthentication in project jdk8u_jdk by JetBrains.

the class EmptyInputStream method getServerAuthentication.

/**
     * Gets the authentication for an HTTP server, and applies it to
     * the connection.
     * @param authHdr the AuthenticationHeader which tells what auth scheme is
     * preferred.
     */
@SuppressWarnings("fallthrough")
private AuthenticationInfo getServerAuthentication(AuthenticationHeader authhdr) {
    /* get authorization from authenticator */
    AuthenticationInfo ret = null;
    String raw = authhdr.raw();
    /* When we get an NTLM auth from cache, don't set any special headers */
    if (authhdr.isPresent()) {
        HeaderParser p = authhdr.headerParser();
        String realm = p.findValue("realm");
        String scheme = authhdr.scheme();
        AuthScheme authScheme = UNKNOWN;
        if ("basic".equalsIgnoreCase(scheme)) {
            authScheme = BASIC;
        } else if ("digest".equalsIgnoreCase(scheme)) {
            authScheme = DIGEST;
        } else if ("ntlm".equalsIgnoreCase(scheme)) {
            authScheme = NTLM;
            doingNTLM2ndStage = true;
        } else if ("Kerberos".equalsIgnoreCase(scheme)) {
            authScheme = KERBEROS;
            doingNTLM2ndStage = true;
        } else if ("Negotiate".equalsIgnoreCase(scheme)) {
            authScheme = NEGOTIATE;
            doingNTLM2ndStage = true;
        }
        domain = p.findValue("domain");
        if (realm == null)
            realm = "";
        serverAuthKey = AuthenticationInfo.getServerAuthKey(url, realm, authScheme);
        ret = AuthenticationInfo.getServerAuth(serverAuthKey);
        InetAddress addr = null;
        if (ret == null) {
            try {
                addr = InetAddress.getByName(url.getHost());
            } catch (java.net.UnknownHostException ignored) {
            // User will have addr = null
            }
        }
        // replacing -1 with default port for a protocol
        int port = url.getPort();
        if (port == -1) {
            port = url.getDefaultPort();
        }
        if (ret == null) {
            switch(authScheme) {
                case KERBEROS:
                    ret = new NegotiateAuthentication(new HttpCallerInfo(authhdr.getHttpCallerInfo(), "Kerberos"));
                    break;
                case NEGOTIATE:
                    ret = new NegotiateAuthentication(new HttpCallerInfo(authhdr.getHttpCallerInfo(), "Negotiate"));
                    break;
                case BASIC:
                    PasswordAuthentication a = privilegedRequestPasswordAuthentication(url.getHost(), addr, port, url.getProtocol(), realm, scheme, url, RequestorType.SERVER);
                    if (a != null) {
                        ret = new BasicAuthentication(false, url, realm, a);
                    }
                    break;
                case DIGEST:
                    a = privilegedRequestPasswordAuthentication(url.getHost(), addr, port, url.getProtocol(), realm, scheme, url, RequestorType.SERVER);
                    if (a != null) {
                        digestparams = new DigestAuthentication.Parameters();
                        ret = new DigestAuthentication(false, url, realm, scheme, a, digestparams);
                    }
                    break;
                case NTLM:
                    if (NTLMAuthenticationProxy.supported) {
                        URL url1;
                        try {
                            url1 = new URL(url, "/");
                        /* truncate the path */
                        } catch (Exception e) {
                            url1 = url;
                        }
                        /* tryTransparentNTLMServer will always be true the first
                         * time around, but verify that the platform supports it
                         * otherwise don't try. */
                        if (tryTransparentNTLMServer) {
                            tryTransparentNTLMServer = NTLMAuthenticationProxy.supportsTransparentAuth;
                            /* If the platform supports transparent authentication
                             * then check if we are in a secure environment
                             * whether, or not, we should try transparent authentication.*/
                            if (tryTransparentNTLMServer) {
                                tryTransparentNTLMServer = NTLMAuthenticationProxy.isTrustedSite(url);
                            }
                        }
                        a = null;
                        if (tryTransparentNTLMServer) {
                            logger.finest("Trying Transparent NTLM authentication");
                        } else {
                            a = privilegedRequestPasswordAuthentication(url.getHost(), addr, port, url.getProtocol(), "", scheme, url, RequestorType.SERVER);
                        }
                        /* If we are not trying transparent authentication then
                         * we need to have a PasswordAuthentication instance. For
                         * transparent authentication (Windows only) the username
                         * and password will be picked up from the current logged
                         * on users credentials.
                         */
                        if (tryTransparentNTLMServer || (!tryTransparentNTLMServer && a != null)) {
                            ret = NTLMAuthenticationProxy.proxy.create(false, url1, a);
                        }
                        /* set to false so that we do not try again */
                        tryTransparentNTLMServer = false;
                    }
                    break;
                case UNKNOWN:
                    if (logger.isLoggable(PlatformLogger.Level.FINEST)) {
                        logger.finest("Unknown/Unsupported authentication scheme: " + scheme);
                    }
                /*fall through*/
                default:
                    throw new AssertionError("should not reach here");
            }
        }
        if (ret == null && defaultAuth != null && defaultAuth.schemeSupported(scheme)) {
            String a = defaultAuth.authString(url, scheme, realm);
            if (a != null) {
                ret = new BasicAuthentication(false, url, realm, a);
            // not in cache by default - cache on success
            }
        }
        if (ret != null) {
            if (!ret.setHeaders(this, p, raw)) {
                ret = null;
            }
        }
    }
    if (logger.isLoggable(PlatformLogger.Level.FINER)) {
        logger.finer("Server Authentication for " + authhdr.toString() + " returned " + (ret != null ? ret.toString() : "null"));
    }
    return ret;
}
Also used : UnknownHostException(java.net.UnknownHostException) URL(java.net.URL) HttpRetryException(java.net.HttpRetryException) SocketTimeoutException(java.net.SocketTimeoutException) PrivilegedActionException(java.security.PrivilegedActionException) MalformedURLException(java.net.MalformedURLException) UnknownHostException(java.net.UnknownHostException) ProtocolException(java.net.ProtocolException) java.net(java.net) sun.net(sun.net) InetAddress(java.net.InetAddress) PasswordAuthentication(java.net.PasswordAuthentication)

Aggregations

PasswordAuthentication (java.net.PasswordAuthentication)48 Authenticator (java.net.Authenticator)18 URL (java.net.URL)11 InetSocketAddress (java.net.InetSocketAddress)9 Proxy (java.net.Proxy)8 InetAddress (java.net.InetAddress)6 Test (org.junit.Test)5 HttpURLConnection (java.net.HttpURLConnection)4 MalformedURLException (java.net.MalformedURLException)4 URI (java.net.URI)4 IOException (java.io.IOException)3 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)3 Dimension (java.awt.Dimension)2 Font (java.awt.Font)2 File (java.io.File)2 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 Field (java.lang.reflect.Field)2 Method (java.lang.reflect.Method)2 java.net (java.net)2