src/jdk.incubator.httpclient/share/classes/jdk/incubator/http/AuthenticationFilter.java
branchhttp-client-branch
changeset 56041 b4b5e09ef3cc
parent 56010 782b2f2d1e76
child 56054 352e845ae744
equal deleted inserted replaced
56040:f8eabb9a5c0f 56041:b4b5e09ef3cc
     1 /*
     1 /*
     2  * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    24  */
    24  */
    25 
    25 
    26 package jdk.incubator.http;
    26 package jdk.incubator.http;
    27 
    27 
    28 import java.io.IOException;
    28 import java.io.IOException;
       
    29 import java.net.MalformedURLException;
    29 import java.net.PasswordAuthentication;
    30 import java.net.PasswordAuthentication;
    30 import java.net.URI;
    31 import java.net.URI;
    31 import java.net.InetSocketAddress;
    32 import java.net.InetSocketAddress;
    32 import java.net.URISyntaxException;
    33 import java.net.URISyntaxException;
       
    34 import java.net.URL;
    33 import java.util.Base64;
    35 import java.util.Base64;
    34 import java.util.LinkedList;
    36 import java.util.LinkedList;
    35 import java.util.Objects;
    37 import java.util.Objects;
    36 import java.util.WeakHashMap;
    38 import java.util.WeakHashMap;
    37 import jdk.incubator.http.internal.common.Utils;
    39 import jdk.incubator.http.internal.common.Utils;
    70         HeaderParser parser = new HeaderParser(header);
    72         HeaderParser parser = new HeaderParser(header);
    71         String authscheme = parser.findKey(0);
    73         String authscheme = parser.findKey(0);
    72 
    74 
    73         String realm = parser.findValue("realm");
    75         String realm = parser.findValue("realm");
    74         java.net.Authenticator.RequestorType rtype = proxy ? PROXY : SERVER;
    76         java.net.Authenticator.RequestorType rtype = proxy ? PROXY : SERVER;
       
    77         URL url = toURL(uri, req.method(), proxy);
    75 
    78 
    76         // needs to be instance method in Authenticator
    79         // needs to be instance method in Authenticator
    77         return auth.requestPasswordAuthenticationInstance(uri.getHost(),
    80         return auth.requestPasswordAuthenticationInstance(uri.getHost(),
    78                                                           null,
    81                                                           null,
    79                                                           uri.getPort(),
    82                                                           uri.getPort(),
    80                                                           uri.getScheme(),
    83                                                           uri.getScheme(),
    81                                                           realm,
    84                                                           realm,
    82                                                           authscheme,
    85                                                           authscheme,
    83                                                           uri.toURL(),
    86                                                           url,
    84                                                           rtype
    87                                                           rtype
    85         );
    88         );
       
    89     }
       
    90 
       
    91     private URL toURL(URI uri, String method, boolean proxy)
       
    92             throws MalformedURLException
       
    93     {
       
    94         if (proxy && "CONNECT".equalsIgnoreCase(method)
       
    95                 && "socket".equalsIgnoreCase(uri.getScheme())) {
       
    96             return null; // proxy tunneling
       
    97         }
       
    98         return uri.toURL();
    86     }
    99     }
    87 
   100 
    88     private URI getProxyURI(HttpRequestImpl r) {
   101     private URI getProxyURI(HttpRequestImpl r) {
    89         InetSocketAddress proxy = r.proxy();
   102         InetSocketAddress proxy = r.proxy();
    90         if (proxy == null) {
   103         if (proxy == null) {
   219             return null;   // error gets returned to app
   232             return null;   // error gets returned to app
   220         }
   233         }
   221 
   234 
   222         AuthInfo au = proxy ? exchange.proxyauth : exchange.serverauth;
   235         AuthInfo au = proxy ? exchange.proxyauth : exchange.serverauth;
   223         if (au == null) {
   236         if (au == null) {
       
   237             // if no authenticator, let the user deal with 407/401
       
   238             if (!exchange.client().authenticator().isPresent()) return null;
       
   239 
   224             PasswordAuthentication pw = getCredentials(authval, proxy, req);
   240             PasswordAuthentication pw = getCredentials(authval, proxy, req);
   225             if (pw == null) {
   241             if (pw == null) {
   226                 throw new IOException("No credentials provided");
   242                 throw new IOException("No credentials provided");
   227             }
   243             }
   228             // No authentication in request. Get credentials from user
   244             // No authentication in request. Get credentials from user
   240         } else {
   256         } else {
   241             // we sent credentials, but they were rejected
   257             // we sent credentials, but they were rejected
   242             if (au.fromcache) {
   258             if (au.fromcache) {
   243                 cache.remove(au.cacheEntry);
   259                 cache.remove(au.cacheEntry);
   244             }
   260             }
       
   261 
       
   262             // if no authenticator, let the user deal with 407/401
       
   263             if (!exchange.client().authenticator().isPresent()) return null;
       
   264 
   245             // try again
   265             // try again
   246             PasswordAuthentication pw = getCredentials(authval, proxy, req);
   266             PasswordAuthentication pw = getCredentials(authval, proxy, req);
   247             if (pw == null) {
   267             if (pw == null) {
   248                 throw new IOException("No credentials provided");
   268                 throw new IOException("No credentials provided");
   249             }
   269             }