Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Ensure Basic Authentication adds the Authorization header correctly #1057

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions dotnet/src/dotnetframework/GxClasses/Domain/GxHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -484,10 +484,20 @@ public string get_ProxyHost()
{
return _proxyHost;
}
#if NETCORE
private static Encoding ISO_8859_1 = Encoding.Latin1;
#else
private static Encoding ISO_8859_1 = Encoding.GetEncoding("ISO-8859-1");
#endif
private const string AuthenticationSchemeBasic = "Basic";
public void AddAuthentication(int scheme, string realm, string user, string password)
{
if (scheme >= _Basic && scheme <= _Kerberos)
_authCollection.Add(new GxAuthScheme(scheme, realm, user, password));
if (scheme == _Basic) {
string authHeader = Convert.ToBase64String(ISO_8859_1.GetBytes($"{user}:{password}"));
AddHeader(HttpHeader.AUTHORIZATION, new AuthenticationHeaderValue(AuthenticationSchemeBasic, authHeader).ToString());
}
}

public void AddProxyAuthentication(int scheme, string realm, string user, string password)
Expand Down Expand Up @@ -727,7 +737,7 @@ void setContentHeaders(HttpRequestMessage request, string contentType)
}
InferContentType(contentType, request);
}
void setHeaders(HttpRequestMessage request, CookieContainer cookies, out string contentType)
internal void SetHeaders(HttpRequestMessage request, CookieContainer cookies, out string contentType)
{
HttpRequestHeaders headers = request.Headers;
contentType = null;
Expand Down Expand Up @@ -864,7 +874,7 @@ HttpResponseMessage ExecuteRequest(string method, string requestUrl, bool contex
RequestUri = new Uri(requestUrl),
Method = new HttpMethod(method),
};
setHeaders(request, cookies, out string contentType);
SetHeaders(request, cookies, out string contentType);
SetHttpVersion(request);
bool disposableInstance = true;
try
Expand Down Expand Up @@ -926,7 +936,7 @@ async Task<HttpResponseMessage> ExecuteRequestAsync(string method, string reques
RequestUri = new Uri(requestUrl),
Method = new HttpMethod(method),
};
setHeaders(request, cookies, out string contentType);
SetHeaders(request, cookies, out string contentType);
SetHttpVersion(request);
bool disposableInstance = true;
try
Expand Down Expand Up @@ -1032,7 +1042,7 @@ async Task ReadResponseDataAsync()
}
}
}
#endif
#endif
bool UseOldHttpClient(string name)
{
if (Config.GetValueOf("useoldhttpclient", out string useOld) && useOld.StartsWith("y", StringComparison.OrdinalIgnoreCase))
Expand Down Expand Up @@ -1581,6 +1591,8 @@ static ICredentials getCredentialCache(Uri URI, ArrayList authenticationCollecti
}
else
{
//return new NetworkCredential(auth.User, auth.Password);

if (cc == null)
{
cc = new CredentialCache();
Expand Down
1 change: 1 addition & 0 deletions dotnet/src/dotnetframework/GxClasses/Helpers/HttpHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public class HttpHeader
internal static string TRANSFER_ENCODING = "Transfer-Encoding";
internal static string X_CSRF_TOKEN_HEADER = "X-XSRF-TOKEN";
internal static string X_CSRF_TOKEN_COOKIE = "XSRF-TOKEN";
internal static string AUTHORIZATION = "Authorization";
}
internal class HttpHeaderValue
{
Expand Down
36 changes: 34 additions & 2 deletions dotnet/test/DotNetUnitTest/Domain/GxHttpClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using GeneXus.Application;
using GeneXus.Http.Client;
using Xunit;



#if !NETCORE
using System.Web.SessionState;
using System.Web;
Expand All @@ -18,8 +22,10 @@ namespace xUnitTesting

public class GxHttpClientTest
{
const int MAX_CONNECTIONS= 5;
public GxHttpClientTest() {
const int MAX_CONNECTIONS = 5;

public GxHttpClientTest()
{
Environment.SetEnvironmentVariable("GX_HTTPCLIENT_MAX_PER_ROUTE", MAX_CONNECTIONS.ToString(), EnvironmentVariableTarget.Process);
}
[Fact]
Expand Down Expand Up @@ -203,5 +209,31 @@ public void NoStoreHeader()
Assert.Equal(0, result);
}
#endif

#if NETCORE
[Fact]
public void BasicAuthenticationIncludesHeader()
{
GxContext context = new GxContext();
using (GxHttpClient httpclient = new GxHttpClient(context))
{
string url= "https://www.google.com/";
string username = "user";
string password = "pass";
string credentialsBase64 = Convert.ToBase64String(Encoding.ASCII.GetBytes($"{username}:{password}"));
httpclient.AddAuthentication(0, string.Empty, username, password);

HttpRequestMessage request = new HttpRequestMessage()
{
RequestUri = new Uri(url),
Method = HttpMethod.Post,
};
httpclient.SetHeaders(request, null, out string contentType);
string headerValue = request.Headers.GetValues("Authorization").FirstOrDefault();
Assert.Equal(headerValue, $"Basic {credentialsBase64}");
}
}
#endif
}

}
Loading