Search in sources :

Example 1 with Response

use of org.javacord.api.util.auth.Response in project Javacord by BtoBastian.

the class ProxyAuthenticator method systemDefaultAuthentication.

/**
 * Generates a {@code Basic} auth header with credentials from the system default authenticator.
 *
 * @param route    The route to which a request is done that needs to be authenticated.
 * @param request  The originating request that led to the authentication attempt.
 * @param response The response that demands authentication.
 * @return The {@code Basic} auth header.
 */
private Map<String, List<String>> systemDefaultAuthentication(Route route, Request request, Response response) {
    InetSocketAddress proxyAddress = (InetSocketAddress) route.getProxy().address();
    String host = proxyAddress.getHostString();
    InetAddress addr = proxyAddress.getAddress();
    int port = proxyAddress.getPort();
    URL url = route.getUrl();
    String protocol = url.getProtocol();
    return response.getChallenges("basic").filter(challenge -> challenge.getRealm().isPresent()).filter(challenge -> {
        String charset = challenge.getAuthParams().get("charset");
        return charset == null || charset.equalsIgnoreCase("UTF-8");
    }).map(challenge -> {
        String realm = challenge.getRealm().orElseThrow(AssertionError::new);
        PasswordAuthentication passwordAuthentication = java.net.Authenticator.requestPasswordAuthentication(host, addr, port, protocol, realm, challenge.getScheme(), url, java.net.Authenticator.RequestorType.PROXY);
        if (passwordAuthentication != null) {
            Charset charset = challenge.getAuthParams().containsKey("charset") ? StandardCharsets.UTF_8 : StandardCharsets.ISO_8859_1;
            return Credentials.basic(passwordAuthentication.getUserName(), String.valueOf(passwordAuthentication.getPassword()), charset);
        }
        return null;
    }).filter(Objects::nonNull).filter(credentials -> request.getHeaders("Proxy-Authorization").stream().noneMatch(credentials::equals)).findAny().map(credentials -> Collections.singletonMap("Proxy-Authorization", Arrays.asList(null, credentials))).orElse(null);
}
Also used : Arrays(java.util.Arrays) OkHttpResponseImpl(org.javacord.core.util.auth.OkHttpResponseImpl) Request(org.javacord.api.util.auth.Request) URL(java.net.URL) IOException(java.io.IOException) Credentials(okhttp3.Credentials) InetSocketAddress(java.net.InetSocketAddress) StandardCharsets(java.nio.charset.StandardCharsets) Route(org.javacord.api.util.auth.Route) InetAddress(java.net.InetAddress) Objects(java.util.Objects) Authenticator(org.javacord.api.util.auth.Authenticator) List(java.util.List) PasswordAuthentication(java.net.PasswordAuthentication) Charset(java.nio.charset.Charset) Map(java.util.Map) Response(org.javacord.api.util.auth.Response) OkHttpRequestImpl(org.javacord.core.util.auth.OkHttpRequestImpl) Collections(java.util.Collections) OkHttpRouteImpl(org.javacord.core.util.auth.OkHttpRouteImpl) InetSocketAddress(java.net.InetSocketAddress) Objects(java.util.Objects) Charset(java.nio.charset.Charset) InetAddress(java.net.InetAddress) URL(java.net.URL) PasswordAuthentication(java.net.PasswordAuthentication)

Aggregations

IOException (java.io.IOException)1 InetAddress (java.net.InetAddress)1 InetSocketAddress (java.net.InetSocketAddress)1 PasswordAuthentication (java.net.PasswordAuthentication)1 URL (java.net.URL)1 Charset (java.nio.charset.Charset)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Credentials (okhttp3.Credentials)1 Authenticator (org.javacord.api.util.auth.Authenticator)1 Request (org.javacord.api.util.auth.Request)1 Response (org.javacord.api.util.auth.Response)1 Route (org.javacord.api.util.auth.Route)1 OkHttpRequestImpl (org.javacord.core.util.auth.OkHttpRequestImpl)1 OkHttpResponseImpl (org.javacord.core.util.auth.OkHttpResponseImpl)1 OkHttpRouteImpl (org.javacord.core.util.auth.OkHttpRouteImpl)1