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);
}
Aggregations