Skip to content

Commit

Permalink
#33 Add custom header extractor to ASPNETCORE sample.
Browse files Browse the repository at this point in the history
  • Loading branch information
David Lievrouw committed Mar 7, 2021
1 parent b58035e commit 6d2272c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Net.Http.Headers;
using Dalion.HttpMessageSigning.Verification.AspNetCore;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Primitives;

namespace WebApplication {
public class CustomHeaderSupportingAuthenticationHeaderExtractor : IAuthenticationHeaderExtractor {
private readonly string _headerName;

public CustomHeaderSupportingAuthenticationHeaderExtractor(string headerName = "Authorization") {
_headerName = headerName ?? throw new ArgumentNullException(nameof(headerName));
}

public AuthenticationHeaderValue Extract(HttpRequest request) {
if (request == null) throw new ArgumentNullException(nameof(request));

var authHeader = request.Headers[_headerName];
if (authHeader == StringValues.Empty) {
return null;
}

var rawAuthHeader = (string) authHeader;
var separatorIndex = rawAuthHeader.IndexOf(' ');
if (separatorIndex < 0) {
return new AuthenticationHeaderValue(rawAuthHeader);
}

var authScheme = rawAuthHeader.Substring(0, separatorIndex);

if (separatorIndex >= rawAuthHeader.Length - 1) {
return new AuthenticationHeaderValue(authScheme);
}

var authParam = rawAuthHeader.Substring(separatorIndex + 1);

return new AuthenticationHeaderValue(authScheme, authParam);
}
}
}
9 changes: 6 additions & 3 deletions src/AspNetCoreWebApplication/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,20 @@ public void ConfigureServices(IServiceCollection services) {
})*/

/* Sample for storing Clients and Nonces on FileSystem instead of in-memory */
.UseFileSystemClientStore(provider => new FileSystemClientStoreSettings {
/*.UseFileSystemClientStore(provider => new FileSystemClientStoreSettings {
FilePath = Path.Combine(Path.GetTempPath(), "Clients.xml"),
SharedSecretEncryptionKey = "The_Big_S3cr37",
ClientCacheEntryExpiration = TimeSpan.FromMinutes(3)
})
.UseFileSystemNonceStore(provider => new FileSystemNonceStoreSettings {
FilePath = Path.Combine(Path.GetTempPath(), "Nonces.xml")
})
})*/

.UseClaimsPrincipalFactory<CustomClaimsPrincipalFactory>().Services
.UseClaimsPrincipalFactory<CustomClaimsPrincipalFactory>()
.UseAuthenticationHeaderExtractor<CustomHeaderSupportingAuthenticationHeaderExtractor>()
.Services
.AddSingleton<CustomClaimsPrincipalFactory>()
.AddSingleton<CustomHeaderSupportingAuthenticationHeaderExtractor>()
.AddHttpContextAccessor();
}

Expand Down

0 comments on commit 6d2272c

Please sign in to comment.