Skip to content

Commit

Permalink
Add ability to specify NTLM authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
basdijkstra committed Oct 19, 2024
1 parent 9fc554b commit 2b4a105
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 3 deletions.
75 changes: 75 additions & 0 deletions RestAssured.Net.Tests/NtlmAuthenticationTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// <copyright file="NtlmAuthenticationTests.cs" company="On Test Automation">
// Copyright 2019 the original author or authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
namespace RestAssured.Tests
{
using NUnit.Framework;
using WireMock.Matchers;
using WireMock.RequestBuilders;
using WireMock.ResponseBuilders;
using static RestAssured.Dsl;

/// <summary>
/// Examples of RestAssuredNet usage.
/// </summary>
[TestFixture]
public class NtlmAuthenticationTests : TestBase
{
/// <summary>
/// A test demonstrating RestAssuredNet syntax for including
/// NTLM authentication details with default credentials with the request.
/// </summary>
[Test]
public void NtlmUsingDefaultNetworkCredentialsCanBeSpecified()
{
this.CreateStubForNtlmAuthenticationVerification();

Given()
.Ntlm()
.When()
.Get($"{MOCK_SERVER_BASE_URL}/ntlm-authentication")
.Then()
.StatusCode(200);
}

/// <summary>
/// A test demonstrating RestAssuredNet syntax for including
/// NTLM authentication details with specified credentials with the request.
/// </summary>
[Test]
public void NtlmUsingSpecifiedNetworkCredentialsCanBeSpecified()
{
this.CreateStubForNtlmAuthenticationVerification();

Given()
.Ntlm("username", "password", "domain")
.When()
.Get($"{MOCK_SERVER_BASE_URL}/ntlm-authentication")
.Then()
.StatusCode(200);
}

/// <summary>
/// Creates the stub response for the example using NTLM authentication
/// with default network credentials.
/// </summary>
private void CreateStubForNtlmAuthenticationVerification()
{
this.Server?.Given(Request.Create().WithPath("/ntlm-authentication").UsingGet())
.RespondWith(Response.Create()
.WithStatusCode(200));
}
}
}
26 changes: 25 additions & 1 deletion RestAssured.Net/Request/ExecutableRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public class ExecutableRequest : IDisposable
private JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings();
private List<string> sensitiveRequestHeadersAndCookies = new List<string>();
private HttpCompletionOption httpCompletionOption = HttpCompletionOption.ResponseContentRead;
private NetworkCredential? networkCredential = null;
private bool disableSslCertificateValidation = false;
private bool disposed = false;

Expand Down Expand Up @@ -242,6 +243,29 @@ public ExecutableRequest OAuth2(string token)
return this;
}

/// <summary>
/// Adds NTLM authentication to the request using cached default network credentials.
/// </summary>
/// <returns>The current <see cref="ExecutableRequest"/> object.</returns>
public ExecutableRequest Ntlm()
{
this.networkCredential = CredentialCache.DefaultNetworkCredentials;
return this;
}

/// <summary>
/// Adds NTLM authentication to the request using specified NTLM credentials.
/// </summary>
/// <param name="username">The username to use when authenticating via NTLM.</param>
/// <param name="password">The password to use when authenticating via NTLM.</param>
/// <param name="domain">The domain to use when authenticating via NTLM.</param>
/// <returns>The current <see cref="ExecutableRequest"/> object.</returns>
public ExecutableRequest Ntlm(string username = "", string password = "", string domain = "")
{
this.networkCredential = new NetworkCredential(username, password, domain);
return this;
}

/// <summary>
/// Adds a cookie to the request.
/// </summary>
Expand Down Expand Up @@ -638,7 +662,7 @@ private VerifiableResponse Send(HttpMethod httpMethod, string endpoint)
bool disableSslChecks = this.disableSslCertificateValidation || (this.requestSpecification?.DisableSslCertificateValidation ?? false);

// Create the HTTP request processor that sends the request and set its properties
HttpRequestProcessor httpRequestProcessor = new HttpRequestProcessor(this.httpClient, this.proxy ?? this.requestSpecification?.Proxy, disableSslChecks);
HttpRequestProcessor httpRequestProcessor = new HttpRequestProcessor(this.httpClient, this.proxy ?? this.requestSpecification?.Proxy, disableSslChecks, this.networkCredential);

Check warning on line 665 in RestAssured.Net/Request/ExecutableRequest.cs

View workflow job for this annotation

GitHub Actions / build (6.0.x)

Possible null reference argument for parameter 'networkCredential' in 'HttpRequestProcessor.HttpRequestProcessor(HttpClient? httpClient, IWebProxy? proxy, bool disableSslCertificateValidation, NetworkCredential networkCredential)'.

Check warning on line 665 in RestAssured.Net/Request/ExecutableRequest.cs

View workflow job for this annotation

GitHub Actions / build (7.0.x)

Possible null reference argument for parameter 'networkCredential' in 'HttpRequestProcessor.HttpRequestProcessor(HttpClient? httpClient, IWebProxy? proxy, bool disableSslCertificateValidation, NetworkCredential networkCredential)'.

Check warning on line 665 in RestAssured.Net/Request/ExecutableRequest.cs

View workflow job for this annotation

GitHub Actions / build (8.0.x)

Possible null reference argument for parameter 'networkCredential' in 'HttpRequestProcessor.HttpRequestProcessor(HttpClient? httpClient, IWebProxy? proxy, bool disableSslCertificateValidation, NetworkCredential networkCredential)'.

// Timeout set in test has precedence over timeout set in request specification
// If both are null, use default timeout for HttpClient (= 100.000 milliseconds).
Expand Down
5 changes: 4 additions & 1 deletion RestAssured.Net/Request/HttpRequestProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,16 @@ public virtual void Dispose(bool disposing)
/// <param name="httpClient">The <see cref="HttpClient"/> to use when sending requests.</param>
/// <param name="proxy">The <see cref="IWebProxy"/> to set on the <see cref="HttpClientHandler"/> used with the <see cref="HttpClient"/>.</param>
/// <param name="disableSslCertificateValidation">If set to true, SSL certificate validation is disabled.</param>
internal HttpRequestProcessor(HttpClient? httpClient, IWebProxy? proxy, bool disableSslCertificateValidation)
/// <param name="networkCredential">The <see cref="NetworkCredential"/> to add to the <see cref="HttpClientHandler"/> to use in the request.</param>
internal HttpRequestProcessor(HttpClient? httpClient, IWebProxy? proxy, bool disableSslCertificateValidation, NetworkCredential networkCredential)
{
if (disableSslCertificateValidation)
{
this.httpClientHandler = new HttpClientHandler
{
CookieContainer = this.cookieContainer,
Proxy = proxy,
Credentials = networkCredential,
ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => true,
};
}
Expand All @@ -86,6 +88,7 @@ internal HttpRequestProcessor(HttpClient? httpClient, IWebProxy? proxy, bool dis
{
CookieContainer = this.cookieContainer,
Proxy = proxy,
Credentials = networkCredential,
ServerCertificateCustomValidationCallback = ServerCertificateCustomValidation,
};
}
Expand Down
2 changes: 1 addition & 1 deletion RestAssured.Net/RestAssured.Net.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>enable</Nullable>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Version>4.3.1</Version>
<Version>4.4.0-beta.1</Version>
<Authors>Bas Dijkstra</Authors>
<Company>On Test Automation</Company>
<Description>C# port of the popular REST Assured library for writing tests for HTTP APIs.</Description>
Expand Down

0 comments on commit 2b4a105

Please sign in to comment.