mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-24 18:32:13 +01:00
add BASIC Auth (#22)
This commit is contained in:
@@ -19,6 +19,8 @@ namespace WireMock.Net.ConsoleApplication
|
|||||||
var server = FluentMockServer.StartWithAdminInterface(url1, url2, url3);
|
var server = FluentMockServer.StartWithAdminInterface(url1, url2, url3);
|
||||||
Console.WriteLine("FluentMockServer listening at {0}", string.Join(" and ", server.Urls));
|
Console.WriteLine("FluentMockServer listening at {0}", string.Join(" and ", server.Urls));
|
||||||
|
|
||||||
|
server.SetBasicAuthentication("a", "b");
|
||||||
|
|
||||||
server.AllowPartialMapping();
|
server.AllowPartialMapping();
|
||||||
|
|
||||||
server
|
server
|
||||||
|
|||||||
@@ -73,5 +73,13 @@ namespace WireMock
|
|||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets a value indicating whether this mapping is an Admin Interface.
|
||||||
|
/// </summary>
|
||||||
|
/// <value>
|
||||||
|
/// <c>true</c> if this mapping is an Admin Interface; otherwise, <c>false</c>.
|
||||||
|
/// </value>
|
||||||
|
public bool IsAdminInterface => Provider is DynamicResponseProvider;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -142,7 +142,7 @@ namespace WireMock.Server
|
|||||||
return new ResponseMessage { StatusCode = 500, Body = e.ToString() };
|
return new ResponseMessage { StatusCode = 500, Body = e.ToString() };
|
||||||
}
|
}
|
||||||
|
|
||||||
return new ResponseMessage { Body = "Mapping added" };
|
return new ResponseMessage { StatusCode = 201, Body = "Mapping added" };
|
||||||
}
|
}
|
||||||
|
|
||||||
private void DeserializeAndAddMapping(string json, Guid? guid = null)
|
private void DeserializeAndAddMapping(string json, Guid? guid = null)
|
||||||
|
|||||||
@@ -4,10 +4,12 @@ using System.Collections.Generic;
|
|||||||
using System.Collections.ObjectModel;
|
using System.Collections.ObjectModel;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using JetBrains.Annotations;
|
using JetBrains.Annotations;
|
||||||
using WireMock.Http;
|
using WireMock.Http;
|
||||||
using WireMock.Logging;
|
using WireMock.Logging;
|
||||||
|
using WireMock.Matchers;
|
||||||
using WireMock.Matchers.Request;
|
using WireMock.Matchers.Request;
|
||||||
using WireMock.Validation;
|
using WireMock.Validation;
|
||||||
|
|
||||||
@@ -34,22 +36,27 @@ namespace WireMock.Server
|
|||||||
|
|
||||||
private bool _allowPartialMapping;
|
private bool _allowPartialMapping;
|
||||||
|
|
||||||
|
private IMatcher _authorizationMatcher;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the ports.
|
/// Gets the ports.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <value>
|
/// <value>
|
||||||
/// The ports.
|
/// The ports.
|
||||||
/// </value>
|
/// </value>
|
||||||
|
[PublicAPI]
|
||||||
public List<int> Ports { get; }
|
public List<int> Ports { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the urls.
|
/// Gets the urls.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
[PublicAPI]
|
||||||
public string[] Urls { get; }
|
public string[] Urls { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the request logs.
|
/// Gets the request logs.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
[PublicAPI]
|
||||||
public IEnumerable<LogEntry> LogEntries
|
public IEnumerable<LogEntry> LogEntries
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
@@ -64,6 +71,7 @@ namespace WireMock.Server
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the mappings.
|
/// Gets the mappings.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
[PublicAPI]
|
||||||
public IEnumerable<Mapping> Mappings
|
public IEnumerable<Mapping> Mappings
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
@@ -159,6 +167,7 @@ namespace WireMock.Server
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Stop this server.
|
/// Stop this server.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
[PublicAPI]
|
||||||
public void Stop()
|
public void Stop()
|
||||||
{
|
{
|
||||||
_httpServer.Stop();
|
_httpServer.Stop();
|
||||||
@@ -167,6 +176,7 @@ namespace WireMock.Server
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Resets LogEntries and Mappings.
|
/// Resets LogEntries and Mappings.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
[PublicAPI]
|
||||||
public void Reset()
|
public void Reset()
|
||||||
{
|
{
|
||||||
ResetLogEntries();
|
ResetLogEntries();
|
||||||
@@ -177,6 +187,7 @@ namespace WireMock.Server
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Resets the LogEntries.
|
/// Resets the LogEntries.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
[PublicAPI]
|
||||||
public void ResetLogEntries()
|
public void ResetLogEntries()
|
||||||
{
|
{
|
||||||
lock (((ICollection)_logEntries).SyncRoot)
|
lock (((ICollection)_logEntries).SyncRoot)
|
||||||
@@ -209,6 +220,7 @@ namespace WireMock.Server
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Resets the Mappings.
|
/// Resets the Mappings.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
[PublicAPI]
|
||||||
public void ResetMappings()
|
public void ResetMappings()
|
||||||
{
|
{
|
||||||
lock (((ICollection)_mappings).SyncRoot)
|
lock (((ICollection)_mappings).SyncRoot)
|
||||||
@@ -243,6 +255,7 @@ namespace WireMock.Server
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="matcher">The matcher.</param>
|
/// <param name="matcher">The matcher.</param>
|
||||||
/// <returns>The <see cref="IEnumerable"/>.</returns>
|
/// <returns>The <see cref="IEnumerable"/>.</returns>
|
||||||
|
[PublicAPI]
|
||||||
public IEnumerable<LogEntry> SearchLogsFor(IRequestMatcher matcher)
|
public IEnumerable<LogEntry> SearchLogsFor(IRequestMatcher matcher)
|
||||||
{
|
{
|
||||||
lock (((ICollection)_logEntries).SyncRoot)
|
lock (((ICollection)_logEntries).SyncRoot)
|
||||||
@@ -258,6 +271,7 @@ namespace WireMock.Server
|
|||||||
/// <param name="delay">
|
/// <param name="delay">
|
||||||
/// The delay.
|
/// The delay.
|
||||||
/// </param>
|
/// </param>
|
||||||
|
[PublicAPI]
|
||||||
public void AddRequestProcessingDelay(TimeSpan delay)
|
public void AddRequestProcessingDelay(TimeSpan delay)
|
||||||
{
|
{
|
||||||
lock (_syncRoot)
|
lock (_syncRoot)
|
||||||
@@ -269,6 +283,7 @@ namespace WireMock.Server
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Allows the partial mapping.
|
/// Allows the partial mapping.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
[PublicAPI]
|
||||||
public void AllowPartialMapping()
|
public void AllowPartialMapping()
|
||||||
{
|
{
|
||||||
lock (_syncRoot)
|
lock (_syncRoot)
|
||||||
@@ -277,11 +292,27 @@ namespace WireMock.Server
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sets the basic authentication.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="username">The username.</param>
|
||||||
|
/// <param name="password">The password.</param>
|
||||||
|
[PublicAPI]
|
||||||
|
public void SetBasicAuthentication([NotNull] string username, [NotNull] string password)
|
||||||
|
{
|
||||||
|
Check.NotNull(username, nameof(username));
|
||||||
|
Check.NotNull(password, nameof(password));
|
||||||
|
|
||||||
|
string authorization = Convert.ToBase64String(Encoding.GetEncoding("ISO-8859-1").GetBytes(username + ":" + password));
|
||||||
|
_authorizationMatcher = new RegexMatcher("^(?i)BASIC " + authorization + "$");
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The given.
|
/// The given.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="requestMatcher">The request matcher.</param>
|
/// <param name="requestMatcher">The request matcher.</param>
|
||||||
/// <returns>The <see cref="IRespondWithAProvider"/>.</returns>
|
/// <returns>The <see cref="IRespondWithAProvider"/>.</returns>
|
||||||
|
[PublicAPI]
|
||||||
public IRespondWithAProvider Given(IRequestMatcher requestMatcher)
|
public IRespondWithAProvider Given(IRequestMatcher requestMatcher)
|
||||||
{
|
{
|
||||||
return new RespondWithAProvider(RegisterMapping, requestMatcher);
|
return new RespondWithAProvider(RegisterMapping, requestMatcher);
|
||||||
@@ -344,10 +375,7 @@ namespace WireMock.Server
|
|||||||
if (_allowPartialMapping)
|
if (_allowPartialMapping)
|
||||||
{
|
{
|
||||||
var partialMappings = mappings
|
var partialMappings = mappings
|
||||||
.Where(pm =>
|
.Where(pm => pm.Mapping.IsAdminInterface && pm.MatchResult.IsPerfectMatch || !pm.Mapping.IsAdminInterface)
|
||||||
(pm.Mapping.Provider is DynamicResponseProvider && pm.MatchResult.IsPerfectMatch) ||
|
|
||||||
!(pm.Mapping.Provider is DynamicResponseProvider)
|
|
||||||
)
|
|
||||||
.OrderBy(m => m.MatchResult)
|
.OrderBy(m => m.MatchResult)
|
||||||
.ThenBy(m => m.Mapping.Priority)
|
.ThenBy(m => m.Mapping.Priority)
|
||||||
.ToList();
|
.ToList();
|
||||||
@@ -369,24 +397,26 @@ namespace WireMock.Server
|
|||||||
|
|
||||||
if (targetMapping == null)
|
if (targetMapping == null)
|
||||||
{
|
{
|
||||||
response = new ResponseMessage
|
response = new ResponseMessage { StatusCode = 404, Body = "No mapping found" };
|
||||||
{
|
return;
|
||||||
StatusCode = 404,
|
|
||||||
Body = "No mapping found"
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
else
|
|
||||||
|
if (targetMapping.IsAdminInterface && _authorizationMatcher != null)
|
||||||
{
|
{
|
||||||
response = await targetMapping.ResponseTo(request);
|
string authorization;
|
||||||
|
bool present = request.Headers.TryGetValue("Authorization", out authorization);
|
||||||
|
if (!present || _authorizationMatcher.IsMatch(authorization) < 1.0)
|
||||||
|
{
|
||||||
|
response = new ResponseMessage { StatusCode = 401 };
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
response = await targetMapping.ResponseTo(request);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
response = new ResponseMessage
|
response = new ResponseMessage { StatusCode = 500, Body = ex.ToString() };
|
||||||
{
|
|
||||||
StatusCode = 500,
|
|
||||||
Body = ex.ToString()
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
@@ -406,4 +436,4 @@ namespace WireMock.Server
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user