Search in sources :

Example 61 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 62 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)

Example 63 with PasswordAuthentication

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

the class AuthenticationInfo method readObject.

/* used for serialization of pw */
private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException {
    s.defaultReadObject();
    pw = new PasswordAuthentication(s1, s2.toCharArray());
    s1 = null;
    s2 = null;
}
Also used : PasswordAuthentication(java.net.PasswordAuthentication)

Example 64 with PasswordAuthentication

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

the class HttpsProxyStackOverflow method startServer.

static BadAuthProxyServer startServer() throws IOException {
    Authenticator.setDefault(new Authenticator() {

        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication("xyz", "xyz".toCharArray());
        }
    });
    BadAuthProxyServer server = new BadAuthProxyServer(new ServerSocket(0));
    Thread serverThread = new Thread(server);
    serverThread.start();
    return server;
}
Also used : ServerSocket(java.net.ServerSocket) Authenticator(java.net.Authenticator) PasswordAuthentication(java.net.PasswordAuthentication)

Example 65 with PasswordAuthentication

use of java.net.PasswordAuthentication in project tdi-studio-se by Talend.

the class SforceBasicBulkConnection method setProxyToConnection.

private void setProxyToConnection(ConnectorConfig conn) {
    Proxy socketProxy = null;
    if (!useProxy) {
        proxyHost = System.getProperty("https.proxyHost");
        if (proxyHost != null && System.getProperty("https.proxyPort") != null) {
            proxyPort = Integer.parseInt(System.getProperty("https.proxyPort"));
            proxyUsername = System.getProperty("https.proxyUser");
            proxyPassword = System.getProperty("https.proxyPassword");
            useProxy = true;
        } else {
            proxyHost = System.getProperty("http.proxyHost");
            if (proxyHost != null && System.getProperty("http.proxyPort") != null) {
                proxyPort = Integer.parseInt(System.getProperty("http.proxyPort"));
                proxyUsername = System.getProperty("http.proxyUser");
                proxyPassword = System.getProperty("http.proxyPassword");
                useProxy = true;
            } else {
                proxyHost = System.getProperty("socksProxyHost");
                if (proxyHost != null && System.getProperty("socksProxyPort") != null) {
                    proxyPort = Integer.parseInt(System.getProperty("socksProxyPort"));
                    proxyUsername = System.getProperty("java.net.socks.username");
                    proxyPassword = System.getProperty("java.net.socks.password");
                    useProxy = true;
                    SocketAddress addr = new InetSocketAddress(proxyHost, proxyPort);
                    socketProxy = new Proxy(Proxy.Type.SOCKS, addr);
                }
            }
        }
    }
    if (useProxy) {
        if (socketProxy != null) {
            conn.setProxy(socketProxy);
        } else {
            conn.setProxy(proxyHost, proxyPort);
        }
        if (proxyUsername != null && !"".equals(proxyUsername)) {
            conn.setProxyUsername(proxyUsername);
            if (proxyPassword != null && !"".equals(proxyPassword)) {
                conn.setProxyPassword(proxyPassword);
                Authenticator.setDefault(new Authenticator() {

                    @Override
                    public PasswordAuthentication getPasswordAuthentication() {
                        if (getRequestorType() == Authenticator.RequestorType.PROXY) {
                            return new PasswordAuthentication(proxyUsername, proxyPassword.toCharArray());
                        } else {
                            return super.getPasswordAuthentication();
                        }
                    }
                });
            }
        }
    }
}
Also used : Proxy(java.net.Proxy) InetSocketAddress(java.net.InetSocketAddress) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) Authenticator(java.net.Authenticator) PasswordAuthentication(java.net.PasswordAuthentication)

Aggregations

PasswordAuthentication (java.net.PasswordAuthentication)108 Authenticator (java.net.Authenticator)52 URL (java.net.URL)27 InetSocketAddress (java.net.InetSocketAddress)22 Proxy (java.net.Proxy)19 Test (org.junit.Test)16 HttpURLConnection (java.net.HttpURLConnection)11 InetAddress (java.net.InetAddress)11 URI (java.net.URI)10 MalformedURLException (java.net.MalformedURLException)9 File (java.io.File)8 IOException (java.io.IOException)8 SocketAddress (java.net.SocketAddress)8 UnknownHostException (java.net.UnknownHostException)6 PrivilegedActionException (java.security.PrivilegedActionException)6 InputStream (java.io.InputStream)5 SocketTimeoutException (java.net.SocketTimeoutException)5 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)5 HttpRetryException (java.net.HttpRetryException)4 ProtocolException (java.net.ProtocolException)4