it.auties.whatsapp.util.ProxyAuthenticator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of whatsappweb4j Show documentation
Show all versions of whatsappweb4j Show documentation
Standalone fully-featured Whatsapp Web API for Java and Kotlin
package it.auties.whatsapp.util;
import lombok.NonNull;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.net.URI;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class ProxyAuthenticator extends Authenticator {
private final static Map credentials;
static {
System.setProperty("jdk.http.auth.proxying.disabledSchemes", "");
System.setProperty("jdk.http.auth.tunneling.disabledSchemes", "");
credentials = new ConcurrentHashMap<>();
}
public static void register(@NonNull URI uri){
credentials.put("%s:%s".formatted(uri.getHost(), uri.getPort()), uri);
}
public static void unregister(@NonNull URI uri){
credentials.remove("%s:%s".formatted(uri.getHost(), uri.getPort()));
}
@Override
protected PasswordAuthentication getPasswordAuthentication() {
var host = "%s:%s".formatted(getRequestingHost(), getRequestingPort());
var info = credentials.get(host);
if(info == null) {
return super.getPasswordAuthentication();
}
var userInfo = info.getUserInfo().split(":", 2);
return new PasswordAuthentication(userInfo[0], userInfo[1].toCharArray());
}
}