Skip to content

Commit

Permalink
Fix basic authentication unauthorized result (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
ttu committed May 17, 2022
1 parent 30c6a73 commit 7256e1e
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changelog

### [Unreleased]
* FIXED: Basic Authentication unauthorized result
* ADDED: API key authentication
* CHANGED: For empty collections return 200 status code instead of 404
* FIXED: Swagger authentication
Expand Down
55 changes: 55 additions & 0 deletions FakeServer.Test/Authentication/BasicAuthAuthenticationSpecs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;
using System.Net.Http.Headers;
using System.Net;
using System.Threading.Tasks;
using Xunit;

namespace FakeServer.Test.Authentication
{
[Collection("Integration collection")]
[Trait("category", "integration")]
[Trait("category", "authentication")]
public class BasicAuthAuthenticationSpecs : IDisposable
{
private readonly IntegrationFixture _fixture;

public BasicAuthAuthenticationSpecs(IntegrationFixture fixture)
{
_fixture = fixture;
_fixture.StartServer(authenticationType: "basic");
}

public void Dispose()
{
_fixture.Stop();
}

[Fact]
public async Task GetUsers_No_Header_Unauthorized()
{
var result = await _fixture.Client.GetAsync("api/users");
Assert.Equal(HttpStatusCode.Unauthorized, result.StatusCode);
}

[Fact]
public async Task GetUsers_Authorized()
{
_fixture.Client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", "YWRtaW46cm9vdA==");

var result = await _fixture.Client.GetAsync("api/users");
Assert.Equal(HttpStatusCode.OK, result.StatusCode);
}

[Theory]
[InlineData("abbaacdc1234")]
[InlineData("")]
public async Task GetUsers_Wrong_Token_Unauthorized(string token)
{
_fixture.Client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", token);


var result = await _fixture.Client.GetAsync("api/users");
Assert.Equal(HttpStatusCode.Unauthorized, result.StatusCode);
}
}
}
7 changes: 6 additions & 1 deletion FakeServer/Authentication/Basic/BasicAuthentication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public static void AddBasicAuthentication(this IServiceCollection services)
{
services.AddAuthentication(o =>
{
o.DefaultScheme = BasicAuthenticationDefaults.AuthenticationScheme;
o.DefaultAuthenticateScheme = BasicAuthenticationDefaults.AuthenticationScheme;
})
.AddBasicAuthentication();
Expand Down Expand Up @@ -95,6 +96,9 @@ public override void Validate()

public class BasicAuthenticationHandler : AuthenticationHandler<BasicTokenOptions>
{
// "Basic "
private const int HeaderMinLength = 6;

public BasicAuthenticationHandler(IOptionsMonitor<BasicTokenOptions> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock)
: base(options, logger, encoder, clock)
{ }
Expand All @@ -107,7 +111,7 @@ bool Authenticate(out string name)
{
var authenticationSettings = Context.RequestServices.GetService(typeof(IOptions<AuthenticationSettings>)) as IOptions<AuthenticationSettings>;

var token = authHeader.Substring("Basic ".Length).Trim();
var token = authHeader.Substring(HeaderMinLength).Trim();
var credentialString = Encoding.UTF8.GetString(Convert.FromBase64String(token));
var credentials = credentialString.Split(':');

Expand All @@ -123,6 +127,7 @@ bool Authenticate(out string name)

if (!string.IsNullOrEmpty(authHeader) &&
authHeader.StartsWith("basic", StringComparison.OrdinalIgnoreCase) &&
authHeader.Length > HeaderMinLength &&
Authenticate(out string loginName))
{
var claims = new[] { new Claim("name", loginName), new Claim(ClaimTypes.Role, "Admin") };
Expand Down

0 comments on commit 7256e1e

Please sign in to comment.