Search in sources :

Example 16 with PasswordAuthentication

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

the class EmptyInputStream method getHttpProxyAuthentication.

/**
     * Gets the authentication for an HTTP proxy, and applies it to
     * the connection.
     */
@SuppressWarnings("fallthrough")
private AuthenticationInfo getHttpProxyAuthentication(AuthenticationHeader authhdr) {
    /* get authorization from authenticator */
    AuthenticationInfo ret = null;
    String raw = authhdr.raw();
    String host = http.getProxyHostUsed();
    int port = http.getProxyPortUsed();
    if (host != null && 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;
            doingNTLMp2ndStage = true;
        } else if ("Kerberos".equalsIgnoreCase(scheme)) {
            authScheme = KERBEROS;
            doingNTLMp2ndStage = true;
        } else if ("Negotiate".equalsIgnoreCase(scheme)) {
            authScheme = NEGOTIATE;
            doingNTLMp2ndStage = true;
        }
        if (realm == null)
            realm = "";
        proxyAuthKey = AuthenticationInfo.getProxyAuthKey(host, port, realm, authScheme);
        ret = AuthenticationInfo.getProxyAuth(proxyAuthKey);
        if (ret == null) {
            switch(authScheme) {
                case BASIC:
                    InetAddress addr = null;
                    try {
                        final String finalHost = host;
                        addr = java.security.AccessController.doPrivileged(new java.security.PrivilegedExceptionAction<InetAddress>() {

                            public InetAddress run() throws java.net.UnknownHostException {
                                return InetAddress.getByName(finalHost);
                            }
                        });
                    } catch (java.security.PrivilegedActionException ignored) {
                    // User will have an unknown host.
                    }
                    PasswordAuthentication a = privilegedRequestPasswordAuthentication(host, addr, port, "http", realm, scheme, url, RequestorType.PROXY);
                    if (a != null) {
                        ret = new BasicAuthentication(true, host, port, realm, a);
                    }
                    break;
                case DIGEST:
                    a = privilegedRequestPasswordAuthentication(host, null, port, url.getProtocol(), realm, scheme, url, RequestorType.PROXY);
                    if (a != null) {
                        DigestAuthentication.Parameters params = new DigestAuthentication.Parameters();
                        ret = new DigestAuthentication(true, host, port, realm, scheme, a, params);
                    }
                    break;
                case NTLM:
                    if (NTLMAuthenticationProxy.supported) {
                        /* tryTransparentNTLMProxy will always be true the first
                         * time around, but verify that the platform supports it
                         * otherwise don't try. */
                        if (tryTransparentNTLMProxy) {
                            tryTransparentNTLMProxy = NTLMAuthenticationProxy.supportsTransparentAuth;
                            /* If the platform supports transparent authentication
                             * then normally it's ok to do transparent auth to a proxy
                             * because we generally trust proxies (chosen by the user)
                             * But not in the case of 305 response where the server
                             * chose it. */
                            if (tryTransparentNTLMProxy && useProxyResponseCode) {
                                tryTransparentNTLMProxy = false;
                            }
                        }
                        a = null;
                        if (tryTransparentNTLMProxy) {
                            logger.finest("Trying Transparent NTLM authentication");
                        } else {
                            a = privilegedRequestPasswordAuthentication(host, null, port, url.getProtocol(), "", scheme, url, RequestorType.PROXY);
                        }
                        /* 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 (tryTransparentNTLMProxy || (!tryTransparentNTLMProxy && a != null)) {
                            ret = NTLMAuthenticationProxy.proxy.create(true, host, port, a);
                        }
                        /* set to false so that we do not try again */
                        tryTransparentNTLMProxy = false;
                    }
                    break;
                case NEGOTIATE:
                    ret = new NegotiateAuthentication(new HttpCallerInfo(authhdr.getHttpCallerInfo(), "Negotiate"));
                    break;
                case KERBEROS:
                    ret = new NegotiateAuthentication(new HttpCallerInfo(authhdr.getHttpCallerInfo(), "Kerberos"));
                    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)) {
            try {
                URL u = new URL("http", host, port, "/");
                String a = defaultAuth.authString(u, scheme, realm);
                if (a != null) {
                    ret = new BasicAuthentication(true, host, port, realm, a);
                // not in cache by default - cache on success
                }
            } catch (java.net.MalformedURLException ignored) {
            }
        }
        if (ret != null) {
            if (!ret.setHeaders(this, p, raw)) {
                ret = null;
            }
        }
    }
    if (logger.isLoggable(PlatformLogger.Level.FINER)) {
        logger.finer("Proxy Authentication for " + authhdr.toString() + " returned " + (ret != null ? ret.toString() : "null"));
    }
    return ret;
}
Also used : PrivilegedActionException(java.security.PrivilegedActionException) MalformedURLException(java.net.MalformedURLException) PrivilegedExceptionAction(java.security.PrivilegedExceptionAction) URL(java.net.URL) java.net(java.net) sun.net(sun.net) InetAddress(java.net.InetAddress) PasswordAuthentication(java.net.PasswordAuthentication)

Example 17 with PasswordAuthentication

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

the class NegotiateCallbackHandler method getAnswer.

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();
        }
    }
}
Also used : PasswordAuthentication(java.net.PasswordAuthentication)

Example 18 with PasswordAuthentication

use of java.net.PasswordAuthentication in project bnd by bndtools.

the class ConnectionSettings method createProxyHandler.

/**
	 * Create Proxy Handler from ProxyDTO
	 */
public static ProxyHandler createProxyHandler(final ProxyDTO proxyDTO) {
    return new ProxyHandler() {

        Glob[] globs;

        private ProxySetup proxySetup;

        @Override
        public ProxySetup forURL(URL url) throws Exception {
            switch(proxyDTO.protocol) {
                case DIRECT:
                    break;
                case HTTP:
                    String scheme = url.getProtocol();
                    if (scheme.equalsIgnoreCase("http") || scheme.equalsIgnoreCase("https")) {
                    // ok
                    } else
                        return null;
                    break;
                case SOCKS:
                    break;
                default:
                    break;
            }
            String host = url.getHost();
            if (host != null) {
                if (isNonProxyHost(host))
                    return null;
            }
            if (proxySetup == null) {
                proxySetup = new ProxySetup();
                if (proxyDTO.username != null && proxyDTO.password != null)
                    proxySetup.authentication = new PasswordAuthentication(proxyDTO.username, proxyDTO.password.toCharArray());
                SocketAddress socketAddress;
                if (proxyDTO.host != null)
                    socketAddress = new InetSocketAddress(proxyDTO.host, proxyDTO.port);
                else
                    socketAddress = new InetSocketAddress(proxyDTO.port);
                proxySetup.proxy = new Proxy(proxyDTO.protocol, socketAddress);
            }
            return proxySetup;
        }

        public boolean isNonProxyHost(String host) {
            Glob[] globs = getNonProxyHosts(proxyDTO);
            for (Glob glob : globs) {
                if (glob.matcher(host).matches())
                    return true;
            }
            return false;
        }

        public Glob[] getNonProxyHosts(final ProxyDTO proxyDTO) {
            // not synchronized because conflicts only do some double work
            if (globs == null) {
                if (proxyDTO.nonProxyHosts == null)
                    globs = new Glob[0];
                else {
                    String[] parts = proxyDTO.nonProxyHosts.split("\\s*\\|\\s*");
                    globs = new Glob[parts.length];
                    for (int i = 0; i < parts.length; i++) globs[i] = new Glob(parts[i]);
                }
            }
            return globs;
        }
    };
}
Also used : ProxyHandler(aQute.bnd.service.url.ProxyHandler) InetSocketAddress(java.net.InetSocketAddress) URL(java.net.URL) Proxy(java.net.Proxy) Glob(aQute.libg.glob.Glob) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) PasswordAuthentication(java.net.PasswordAuthentication)

Example 19 with PasswordAuthentication

use of java.net.PasswordAuthentication in project bnd by bndtools.

the class HttpClient method init.

synchronized void init() {
    if (inited)
        return;
    inited = true;
    Authenticator.setDefault(new Authenticator() {

        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return passwordAuthentication.get();
        }
    });
}
Also used : Authenticator(java.net.Authenticator) PasswordAuthentication(java.net.PasswordAuthentication)

Example 20 with PasswordAuthentication

use of java.net.PasswordAuthentication in project jgnash by ccavanaugh.

the class NetworkAuthenticator method getPasswordAuthentication.

@Override
protected PasswordAuthentication getPasswordAuthentication() {
    Preferences auth = Preferences.userRoot().node(NODEHTTP);
    char[] pass = null;
    String user;
    // get the password
    String _pass = auth.get(HTTPPASS, null);
    if (_pass != null) {
        if (!_pass.isEmpty()) {
            pass = _pass.toCharArray();
        }
    }
    // get the user
    user = auth.get(HTTPUSER, null);
    if (user != null) {
        if (user.length() <= 0) {
            user = null;
        }
    }
    // if either returns null, pop a dialog
    if (user == null || pass == null) {
        JTextField username = new JTextField();
        JPasswordField password = new JPasswordField();
        JPanel panel = new JPanel(new GridLayout(2, 2));
        panel.add(new JLabel(ResourceUtils.getString("Label.UserName")));
        panel.add(username);
        panel.add(new JLabel(ResourceUtils.getString("Label.Password")));
        panel.add(password);
        int option = JOptionPane.showConfirmDialog(null, new Object[] { "Site: " + getRequestingHost(), "Realm: " + getRequestingPrompt(), panel }, "Enter Network Password", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
        if (option == JOptionPane.OK_OPTION) {
            user = username.getText();
            pass = password.getPassword();
        } else {
            return null;
        }
    }
    return new PasswordAuthentication(user, pass);
}
Also used : JPanel(javax.swing.JPanel) GridLayout(java.awt.GridLayout) JPasswordField(javax.swing.JPasswordField) JLabel(javax.swing.JLabel) Preferences(java.util.prefs.Preferences) JTextField(javax.swing.JTextField) PasswordAuthentication(java.net.PasswordAuthentication)

Aggregations

PasswordAuthentication (java.net.PasswordAuthentication)40 Authenticator (java.net.Authenticator)14 URL (java.net.URL)11 InetSocketAddress (java.net.InetSocketAddress)9 Proxy (java.net.Proxy)8 HttpURLConnection (java.net.HttpURLConnection)4 InetAddress (java.net.InetAddress)4 MalformedURLException (java.net.MalformedURLException)4 URI (java.net.URI)4 IOException (java.io.IOException)3 ServerSocket (java.net.ServerSocket)3 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)3 Test (org.junit.Test)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 java.net (java.net)2 UnknownHostException (java.net.UnknownHostException)2