Skip to content

Commit

Permalink
Fixes #40
Browse files Browse the repository at this point in the history
  • Loading branch information
blowdart committed Feb 16, 2020
1 parent ffbe12f commit 005919a
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/idunno.Authentication.Basic/BasicAuthenticationHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.Net.Http.Headers;
using System.Globalization;

namespace idunno.Authentication.Basic
{
Expand Down Expand Up @@ -70,15 +71,31 @@ protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
try
{
string decodedCredentials = string.Empty;
byte[] base64DecodedCredentials;
try
{
decodedCredentials = Encoding.UTF8.GetString(Convert.FromBase64String(encodedCredentials));
base64DecodedCredentials = Convert.FromBase64String(encodedCredentials);
}
catch (FormatException)
{
const string failedToDecodeCredentials = "Cannot convert credentials from Base64.";
Logger.LogInformation(failedToDecodeCredentials);
return AuthenticateResult.Fail(failedToDecodeCredentials);
}

try
{
decodedCredentials = Encoding.UTF8.GetString(base64DecodedCredentials);
}
catch (Exception ex)
{
throw new Exception($"Failed to decode credentials : {encodedCredentials}", ex);
const string failedToDecodeCredentials = "Cannot build credentials from decoded base64 value, exception {0} encountered.";
var logMessage = string.Format(CultureInfo.InvariantCulture, failedToDecodeCredentials, ex.Message);
Logger.LogInformation(logMessage);
return AuthenticateResult.Fail(logMessage);
}


var delimiterIndex = decodedCredentials.IndexOf(':');
if (delimiterIndex == -1)
{
Expand Down
33 changes: 33 additions & 0 deletions test/idunno.Authentication.Test/BasicAuthenticationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,17 @@ public async Task ValidateAuthenticationFailsIfOnValidateCredentialsFails()
Assert.Equal(HttpStatusCode.Unauthorized, transaction.Response.StatusCode);
}

[Fact]
public async Task ValidateAuthenticationFailsWhenAnInvalidUTF8AuthenticationHeaderIsSent()
{
var server = CreateServer(new BasicAuthenticationOptions
{
});

var transaction = await SendAsyncWithRawHeaderValue(server, "https://example.com/challenge", "%%%%%");
Assert.Equal(HttpStatusCode.Unauthorized, transaction.Response.StatusCode);
}

[Fact]
public async Task ValidateSupressionOfWWWAuthenticationHeader()
{
Expand Down Expand Up @@ -427,6 +438,28 @@ private static async Task<Transaction> SendAsyncWithHeaderValue(TestServer serve
return transaction;
}

private static async Task<Transaction> SendAsyncWithRawHeaderValue(TestServer server, string uri, string authorizationHeaderValue, string scheme = "Basic")
{
var request = new HttpRequestMessage(HttpMethod.Get, uri);
request.Headers.Add(HeaderNames.Authorization, scheme + " " + authorizationHeaderValue);

var transaction = new Transaction
{
Request = request,
Response = await server.CreateClient().SendAsync(request),
};
transaction.ResponseText = await transaction.Response.Content.ReadAsStringAsync();

if (transaction.Response.Content != null &&
transaction.Response.Content.Headers.ContentType != null &&
transaction.Response.Content.Headers.ContentType.MediaType == "text/xml")
{
transaction.ResponseElement = XElement.Parse(transaction.ResponseText);
}
return transaction;
}


private class Transaction
{
public HttpRequestMessage Request { get; set; }
Expand Down

0 comments on commit 005919a

Please sign in to comment.