PasswordAuthentication getPasswordAuthentication()

in src/main/java/com/microsoft/azure/proton/transport/proxy/impl/ProxyAuthenticator.java [59:109]


    PasswordAuthentication getPasswordAuthentication(String scheme, String host) {
        if (configuration.hasUserDefinedCredentials()) {
            return configuration.credentials();
        }

        // The user has specified the proxy address, so we'll use that address to try to fetch the system-wide
        // credentials for this.
        if (configuration.isProxyAddressConfigured()) {
            // We can cast this because Proxy ctor verifies that address is an instance of InetSocketAddress.
            InetSocketAddress address = (InetSocketAddress) configuration.proxyAddress().address();
            return Authenticator.requestPasswordAuthentication(
                    address.getHostName(),
                    address.getAddress(),
                    0,
                    null,
                    PROMPT,
                    scheme,
                    null,
                    Authenticator.RequestorType.PROXY);
        }

        // Otherwise, use the system-configured proxies and authenticator to fetch the credentials.
        ProxySelector proxySelector = ProxySelector.getDefault();

        URI uri;
        List<Proxy> proxies = null;
        if (!StringUtils.isNullOrEmpty(host)) {
            uri = URI.create(host);
            proxies = proxySelector.select(uri);
        }

        InetAddress proxyAddr = null;
        java.net.Proxy.Type proxyType = null;
        if (isProxyAddressLegal(proxies)) {
            // will be only one element in the proxy list
            proxyAddr = ((InetSocketAddress) proxies.get(0).address()).getAddress();
            proxyType = proxies.get(0).type();
        }

        // It appears to be fine to pass in a null value for proxyAddr and proxyType (which maps to "scheme" argument in
        // the call to requestPasswordAuthentication).
        return Authenticator.requestPasswordAuthentication(
                "",
                proxyAddr,
                0,
                proxyType == null ? "" : proxyType.name(),
                PROMPT,
                scheme,
                null,
                Authenticator.RequestorType.PROXY);
    }