Skip to content

Commit 5f46b16

Browse files
MartinVigliaroloMartin Vigliarolo
andauthored
Basic auth changed to raw auth header (#444)
* Basic auth changed to raw auth header * Basic Auth header method created * Basic and Digest auth applied together fixed Co-authored-by: Martin Vigliarolo <[email protected]>
1 parent 12d9b5a commit 5f46b16

File tree

1 file changed

+34
-8
lines changed

1 file changed

+34
-8
lines changed

java/src/main/java/com/genexus/internet/HttpClientJavaLib.java

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.io.*;
44
import java.net.InetAddress;
55
import java.net.UnknownHostException;
6+
import java.nio.charset.StandardCharsets;
67
import java.security.KeyManagementException;
78
import java.security.KeyStoreException;
89
import java.security.NoSuchAlgorithmException;
@@ -114,6 +115,8 @@ public void setTimeout(int timeout)
114115
private RequestConfig reqConfig = null; // Atributo usado en la ejecucion del metodo (por ejemplo, httpGet, httpPost)
115116
private CookieStore cookies;
116117
private ByteArrayEntity entity = null; // Para mantener el stream luego de cerrada la conexion en la lectura de la response
118+
private Boolean lastAuthIsBasic = null;
119+
private Boolean lastAuthProxyIsBasic = null;
117120
private static IniFile clientCfg = new com.genexus.ModelContext(com.genexus.ModelContext.getModelContextPackageClass()).getPreferences().getIniFile();
118121

119122

@@ -138,6 +141,24 @@ private void resetErrorsAndConnParams()
138141
}
139142
}
140143

144+
@Override
145+
public void addAuthentication(int type, String realm, String name, String value) { // Metodo overriden por tratarse de forma distinta el pasaje de auth Basic y el resto
146+
if (type == BASIC)
147+
lastAuthIsBasic = true;
148+
else
149+
lastAuthIsBasic = false;
150+
super.addAuthentication(type,realm,name,value);
151+
}
152+
153+
@Override
154+
public void addProxyAuthentication(int type, String realm, String name, String value) { // Metodo overriden por tratarse de forma distinta el pasaje de auth Basic y el resto
155+
if (type == BASIC)
156+
lastAuthProxyIsBasic = true;
157+
else
158+
lastAuthProxyIsBasic = false;
159+
super.addProxyAuthentication(type,realm,name,value);
160+
}
161+
141162
private void resetStateAdapted()
142163
{
143164
resetState();
@@ -248,6 +269,15 @@ private void SetCookieAtr(CookieStore cookiesToSend) {
248269
}
249270
}
250271

272+
private void addBasicAuthHeader(String user, String password, Boolean isProxy) {
273+
Boolean typeAuth = isProxy ? this.lastAuthProxyIsBasic : this.lastAuthIsBasic;
274+
if (typeAuth) {
275+
String auth = user + ":" + password;
276+
String authHeader = "Basic " + Base64.getEncoder().encodeToString(auth.getBytes(StandardCharsets.ISO_8859_1));
277+
addHeader(isProxy ? HttpHeaders.PROXY_AUTHORIZATION : HttpHeaders.AUTHORIZATION, authHeader);
278+
}
279+
}
280+
251281
public void execute(String method, String url) {
252282
resetExecParams();
253283

@@ -287,11 +317,9 @@ public void execute(String method, String url) {
287317
if (getHostChanged() || getAuthorizationChanged()) { // Si el host cambio o si se agrego alguna credencial
288318
this.credentialsProvider = new BasicCredentialsProvider();
289319

290-
for (Enumeration en = getBasicAuthorization().elements(); en.hasMoreElements(); ) {
320+
for (Enumeration en = getBasicAuthorization().elements(); en.hasMoreElements(); ) { // No se puede hacer la autorizacion del tipo Basic con el BasicCredentialsProvider porque esta funcionando bien en todos los casos
291321
HttpClientPrincipal p = (HttpClientPrincipal) en.nextElement();
292-
this.credentialsProvider.setCredentials(
293-
AuthScope.ANY,
294-
new UsernamePasswordCredentials(p.user, p.password));
322+
addBasicAuthHeader(p.user,p.password,false);
295323
}
296324

297325
for (Enumeration en = getDigestAuthorization().elements(); en.hasMoreElements(); ) {
@@ -327,11 +355,9 @@ public void execute(String method, String url) {
327355
this.credentialsProvider = new BasicCredentialsProvider();
328356
}
329357

330-
for (Enumeration en = getBasicProxyAuthorization().elements(); en.hasMoreElements(); ) {
358+
for (Enumeration en = getBasicProxyAuthorization().elements(); en.hasMoreElements(); ) { // No se puede hacer la autorizacion del tipo Basic con el BasicCredentialsProvider porque esta funcionando bien en todos los casos
331359
HttpClientPrincipal p = (HttpClientPrincipal) en.nextElement();
332-
this.credentialsProvider.setCredentials(
333-
AuthScope.ANY,
334-
new UsernamePasswordCredentials(p.user, p.password));
360+
addBasicAuthHeader(p.user,p.password,true);
335361
}
336362

337363
for (Enumeration en = getDigestProxyAuthorization().elements(); en.hasMoreElements(); ) {

0 commit comments

Comments
 (0)