-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reshuffle NuGet packages to make it easier to consume them (#55)
* Reshuffle NuGet packages * Revert bad version ref * REFACTOR: (re)move ISecretProvider extension on ConfigurationBuil… (#58) * Reshuffle NuGet packages * Revert bad version ref * Remove empty project * Remove old project file Signed-off-by: Tom Kerkhove <kerkhove.tom@gmail.com>
- Loading branch information
1 parent
c052624
commit c0facca
Showing
26 changed files
with
182 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/Arcus.WebApi.Security.Authentication/Arcus.WebApi.Security.Authentication.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
<Authors>Arcus</Authors> | ||
<Company>Arcus</Company> | ||
<RepositoryType>Git</RepositoryType> | ||
<PackageTags>Azure;WebAPI;App Services;Web App;Web;API</PackageTags> | ||
<Description>Provides capabilities to easily build secure Web APIs running in Azure.</Description> | ||
<Copyright>Copyright (c) Arcus</Copyright> | ||
<PackageLicenseUrl>https://github.com/arcus-azure/arcus.webapi/blob/master/LICENSE</PackageLicenseUrl> | ||
<PackageProjectUrl>https://github.com/arcus-azure/arcus.webapi</PackageProjectUrl> | ||
<RepositoryUrl>https://github.com/arcus-azure/arcus.webapi</RepositoryUrl> | ||
<PackageIconUrl>https://raw.githubusercontent.com/arcus-azure/arcus/master/media/arcus.png</PackageIconUrl> | ||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild> | ||
<GenerateDocumentationFile>true</GenerateDocumentationFile> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.2.0" /> | ||
<PackageReference Include="System.Web.Http" Version="4.0.0" /> | ||
<PackageReference Include="Guard.Net" Version="1.2.0" /> | ||
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" /> | ||
<PackageReference Include="Arcus.Security.Secrets.AzureKeyVault" Version="0.1.0" /> | ||
</ItemGroup> | ||
|
||
</Project> |
2 changes: 1 addition & 1 deletion
2
...ion/CertificateAuthenticationAttribute.cs → ...tes/CertificateAuthenticationAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...CertificateAuthenticationConfigBuilder.cs → ...CertificateAuthenticationConfigBuilder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
....Security/Authentication/ConfiguredKey.cs → ...hentication/Certificates/ConfiguredKey.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...uthentication/ExpectedCertificateValue.cs → .../Certificates/ExpectedCertificateValue.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...thentication/X509ValidationRequirement.cs → ...Certificates/X509ValidationRequirement.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
....WebApi.Security.Authentication/SharedAccessKey/SharedAccessKeyAuthenticationAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using System; | ||
using Arcus.Security.Secrets.Core.Interfaces; | ||
using GuardNet; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace Arcus.WebApi.Security.Authentication.SharedAccessKey | ||
{ | ||
/// <summary> | ||
/// Authentication filter to secure HTTP requests with shared access keys. | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)] | ||
public class SharedAccessKeyAuthenticationAttribute : TypeFilterAttribute | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="SharedAccessKeyAuthenticationAttribute"/> class. | ||
/// </summary> | ||
/// <param name="secretName">The name of the request header which value must match the stored secret with the same name as the header.</param> | ||
public SharedAccessKeyAuthenticationAttribute(string secretName) : this(secretName, secretName) { } | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="SharedAccessKeyAuthenticationAttribute"/> class. | ||
/// </summary> | ||
/// <param name="headerName">The name of the request header which value must match the stored secret.</param> | ||
/// <param name="secretName">The name of the secret that's being retrieved using the <see cref="ISecretProvider.Get"/> call.</param> | ||
public SharedAccessKeyAuthenticationAttribute(string headerName, string secretName) : base(typeof(SharedAccessKeyAuthenticationFilter)) | ||
{ | ||
Guard.NotNullOrWhitespace(headerName, nameof(headerName), "Header name cannot be blank"); | ||
Guard.NotNullOrWhitespace(secretName, nameof(secretName), "Secret name cannot be blank"); | ||
|
||
Arguments = new object[] { headerName, secretName }; | ||
} | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
...cus.WebApi.Security.Authentication/SharedAccessKey/SharedAccessKeyAuthenticationFilter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Arcus.Security.Secrets.Core.Exceptions; | ||
using Arcus.Security.Secrets.Core.Interfaces; | ||
using GuardNet; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.Filters; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Primitives; | ||
|
||
namespace Arcus.WebApi.Security.Authentication.SharedAccessKey | ||
{ | ||
/// <summary> | ||
/// Authentication filter to secure HTTP requests with shared access keys. | ||
/// </summary> | ||
/// <remarks> | ||
/// Please provide an <see cref="ISecretProvider"/> implementation in the configured services of the request. | ||
/// </remarks> | ||
public class SharedAccessKeyAuthenticationFilter : IAsyncAuthorizationFilter | ||
{ | ||
private readonly string _headerName, _secretName; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="SharedAccessKeyAuthenticationFilter"/> class. | ||
/// </summary> | ||
/// <param name="headerName">The name of the request header which value must match the stored secret.</param> | ||
/// <param name="secretName">The name of the secret that's being retrieved using the <see cref="ISecretProvider.Get"/> call.</param> | ||
public SharedAccessKeyAuthenticationFilter(string headerName, string secretName) | ||
{ | ||
Guard.NotNullOrWhitespace(headerName, nameof(headerName), "Header name cannot be blank"); | ||
Guard.NotNullOrWhitespace(secretName, nameof(secretName), "Secret name cannot be blank"); | ||
|
||
_headerName = headerName; | ||
_secretName = secretName; | ||
} | ||
|
||
/// <summary> | ||
/// Called early in the filter pipeline to confirm request is authorized. | ||
/// </summary> | ||
/// <param name="context">The <see cref="T:Microsoft.AspNetCore.Mvc.Filters.AuthorizationFilterContext" />.</param> | ||
/// <returns> | ||
/// A <see cref="T:System.Threading.Tasks.Task" /> that on completion indicates the filter has executed. | ||
/// </returns> | ||
public async Task OnAuthorizationAsync(AuthorizationFilterContext context) | ||
{ | ||
Guard.NotNull(context, nameof(context)); | ||
Guard.NotNull(context.HttpContext, nameof(context.HttpContext)); | ||
Guard.For<ArgumentException>(() => context.HttpContext.Request == null, "Invalid action context given without any HTTP request"); | ||
Guard.For<ArgumentException>(() => context.HttpContext.Request.Headers == null, "Invalid action context given without any HTTP request headers"); | ||
Guard.For<ArgumentException>(() => context.HttpContext.RequestServices == null, "Invalid action context given without any HTTP request services"); | ||
|
||
if (context.HttpContext.Request.Headers | ||
.TryGetValue(_headerName, out StringValues requestSecretHeaders)) | ||
{ | ||
ISecretProvider userDefinedSecretProvider = | ||
context.HttpContext.RequestServices.GetService<ICachedSecretProvider>() | ||
?? context.HttpContext.RequestServices.GetService<ISecretProvider>(); | ||
|
||
if (userDefinedSecretProvider == null) | ||
{ | ||
throw new KeyNotFoundException( | ||
$"No configured {nameof(ICachedSecretProvider)} or {nameof(ISecretProvider)} implementation found in the request service container. " | ||
+ "Please configure such an implementation (ex. in the Startup) of your application"); | ||
} | ||
|
||
string foundSecret = await userDefinedSecretProvider.Get(_secretName); | ||
if (foundSecret == null) | ||
{ | ||
throw new SecretNotFoundException(_secretName); | ||
} | ||
|
||
if (requestSecretHeaders.Any(headerValue => !String.Equals(headerValue, foundSecret))) | ||
{ | ||
context.Result = new UnauthorizedResult(); | ||
} | ||
} | ||
else | ||
{ | ||
context.Result = new UnauthorizedResult(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
src/Arcus.WebApi.Security/Authentication/SharedAccessKeyAuthenticationAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/Arcus.WebApi.Unit/Security/Authentication/CertificateAuthenticationOnMethodController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 1 addition & 2 deletions
3
src/Arcus.WebApi.Unit/Security/Doubles/StubX509ValidationLocation.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.