add BASIC Auth (#22)

This commit is contained in:
Stef Heyenrath
2017-02-10 20:04:06 +01:00
parent ba86d81a17
commit bb35f55bbb
4 changed files with 58 additions and 18 deletions

View File

@@ -19,6 +19,8 @@ namespace WireMock.Net.ConsoleApplication
var server = FluentMockServer.StartWithAdminInterface(url1, url2, url3);
Console.WriteLine("FluentMockServer listening at {0}", string.Join(" and ", server.Urls));
server.SetBasicAuthentication("a", "b");
server.AllowPartialMapping();
server

View File

@@ -73,5 +73,13 @@ namespace WireMock
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;
}
}

View File

@@ -142,7 +142,7 @@ namespace WireMock.Server
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)

View File

@@ -4,10 +4,12 @@ using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using JetBrains.Annotations;
using WireMock.Http;
using WireMock.Logging;
using WireMock.Matchers;
using WireMock.Matchers.Request;
using WireMock.Validation;
@@ -34,22 +36,27 @@ namespace WireMock.Server
private bool _allowPartialMapping;
private IMatcher _authorizationMatcher;
/// <summary>
/// Gets the ports.
/// </summary>
/// <value>
/// The ports.
/// </value>
[PublicAPI]
public List<int> Ports { get; }
/// <summary>
/// Gets the urls.
/// </summary>
[PublicAPI]
public string[] Urls { get; }
/// <summary>
/// Gets the request logs.
/// </summary>
[PublicAPI]
public IEnumerable<LogEntry> LogEntries
{
get
@@ -64,6 +71,7 @@ namespace WireMock.Server
/// <summary>
/// Gets the mappings.
/// </summary>
[PublicAPI]
public IEnumerable<Mapping> Mappings
{
get
@@ -159,6 +167,7 @@ namespace WireMock.Server
/// <summary>
/// Stop this server.
/// </summary>
[PublicAPI]
public void Stop()
{
_httpServer.Stop();
@@ -167,6 +176,7 @@ namespace WireMock.Server
/// <summary>
/// Resets LogEntries and Mappings.
/// </summary>
[PublicAPI]
public void Reset()
{
ResetLogEntries();
@@ -177,6 +187,7 @@ namespace WireMock.Server
/// <summary>
/// Resets the LogEntries.
/// </summary>
[PublicAPI]
public void ResetLogEntries()
{
lock (((ICollection)_logEntries).SyncRoot)
@@ -209,6 +220,7 @@ namespace WireMock.Server
/// <summary>
/// Resets the Mappings.
/// </summary>
[PublicAPI]
public void ResetMappings()
{
lock (((ICollection)_mappings).SyncRoot)
@@ -243,6 +255,7 @@ namespace WireMock.Server
/// </summary>
/// <param name="matcher">The matcher.</param>
/// <returns>The <see cref="IEnumerable"/>.</returns>
[PublicAPI]
public IEnumerable<LogEntry> SearchLogsFor(IRequestMatcher matcher)
{
lock (((ICollection)_logEntries).SyncRoot)
@@ -258,6 +271,7 @@ namespace WireMock.Server
/// <param name="delay">
/// The delay.
/// </param>
[PublicAPI]
public void AddRequestProcessingDelay(TimeSpan delay)
{
lock (_syncRoot)
@@ -269,6 +283,7 @@ namespace WireMock.Server
/// <summary>
/// Allows the partial mapping.
/// </summary>
[PublicAPI]
public void AllowPartialMapping()
{
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>
/// The given.
/// </summary>
/// <param name="requestMatcher">The request matcher.</param>
/// <returns>The <see cref="IRespondWithAProvider"/>.</returns>
[PublicAPI]
public IRespondWithAProvider Given(IRequestMatcher requestMatcher)
{
return new RespondWithAProvider(RegisterMapping, requestMatcher);
@@ -344,10 +375,7 @@ namespace WireMock.Server
if (_allowPartialMapping)
{
var partialMappings = mappings
.Where(pm =>
(pm.Mapping.Provider is DynamicResponseProvider && pm.MatchResult.IsPerfectMatch) ||
!(pm.Mapping.Provider is DynamicResponseProvider)
)
.Where(pm => pm.Mapping.IsAdminInterface && pm.MatchResult.IsPerfectMatch || !pm.Mapping.IsAdminInterface)
.OrderBy(m => m.MatchResult)
.ThenBy(m => m.Mapping.Priority)
.ToList();
@@ -369,24 +397,26 @@ namespace WireMock.Server
if (targetMapping == null)
{
response = new ResponseMessage
{
StatusCode = 404,
Body = "No mapping found"
};
response = new ResponseMessage { StatusCode = 404, Body = "No mapping found" };
return;
}
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)
{
response = new ResponseMessage
{
StatusCode = 500,
Body = ex.ToString()
};
response = new ResponseMessage { StatusCode = 500, Body = ex.ToString() };
}
finally
{
@@ -406,4 +436,4 @@ namespace WireMock.Server
}
}
}
}
}