generated from bitwarden/template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Hosting extensions * Packaging * Documentation * Merge into single new project * Test project * Self-hosted attribute with tests * Hyphens * Move to new attributes folder
- Loading branch information
1 parent
053b659
commit 0e2fa35
Showing
13 changed files
with
554 additions
and
1 deletion.
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
60 changes: 60 additions & 0 deletions
60
extensions/Bitwarden.Extensions.Hosting/src/AssemblyHelpers.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,60 @@ | ||
using System.Reflection; | ||
|
||
namespace Bitwarden.Extensions.Hosting; | ||
|
||
/// <summary> | ||
/// Helper class for working with assembly attributes. | ||
/// </summary> | ||
public static class AssemblyHelpers | ||
{ | ||
private const string _gitHashAssemblyKey = "GitHash"; | ||
|
||
private static readonly IEnumerable<AssemblyMetadataAttribute> _assemblyMetadataAttributes; | ||
private static readonly AssemblyInformationalVersionAttribute? _assemblyInformationalVersionAttributes; | ||
private static string? _version; | ||
private static string? _gitHash; | ||
|
||
static AssemblyHelpers() | ||
{ | ||
_assemblyMetadataAttributes = Assembly.GetEntryAssembly()! | ||
.GetCustomAttributes<AssemblyMetadataAttribute>(); | ||
_assemblyInformationalVersionAttributes = Assembly.GetEntryAssembly()! | ||
.GetCustomAttribute<AssemblyInformationalVersionAttribute>(); | ||
} | ||
|
||
/// <summary> | ||
/// Gets the version of the entry assembly. | ||
/// </summary> | ||
/// <returns></returns> | ||
public static string? GetVersion() | ||
{ | ||
if (string.IsNullOrWhiteSpace(_version)) | ||
{ | ||
_version = _assemblyInformationalVersionAttributes?.InformationalVersion; | ||
} | ||
|
||
return _version; | ||
} | ||
|
||
/// <summary> | ||
/// Gets the Git hash of the entry assembly. | ||
/// </summary> | ||
/// <returns></returns> | ||
public static string? GetGitHash() | ||
{ | ||
if (string.IsNullOrWhiteSpace(_gitHash)) | ||
{ | ||
try | ||
{ | ||
_gitHash = _assemblyMetadataAttributes.First(i => | ||
i.Key == _gitHashAssemblyKey).Value; | ||
} | ||
catch (Exception) | ||
{ | ||
// suppress | ||
} | ||
} | ||
|
||
return _gitHash; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
extensions/Bitwarden.Extensions.Hosting/src/Attributes/SelfHostedAttribute.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,38 @@ | ||
using Bitwarden.Extensions.Hosting.Exceptions; | ||
using Microsoft.AspNetCore.Mvc.Filters; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Bitwarden.Extensions.Hosting.Attributes; | ||
|
||
/// <summary> | ||
/// Attribute to indicate that an instance is self-hosted. | ||
/// </summary> | ||
public class SelfHostedAttribute : ActionFilterAttribute | ||
{ | ||
/// <summary> | ||
/// Gets or sets a value indicating whether the attribute is only allowed when self-hosted. | ||
/// </summary> | ||
public bool SelfHostedOnly { get; init; } | ||
/// <summary> | ||
/// Gets or sets a value indicating whether the attribute is only allowed when not self-hosted. | ||
/// </summary> | ||
public bool NotSelfHostedOnly { get; init; } | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="SelfHostedAttribute"/> class. | ||
/// </summary> | ||
/// <param name="context">Action context.</param> | ||
/// <exception cref="BadRequestException"></exception> | ||
public override void OnActionExecuting(ActionExecutingContext context) | ||
{ | ||
var globalSettings = context.HttpContext.RequestServices.GetRequiredService<GlobalSettingsBase>(); | ||
if (SelfHostedOnly && !globalSettings.IsSelfHosted) | ||
{ | ||
throw new BadRequestException("Only allowed when self-hosted."); | ||
} | ||
else if (NotSelfHostedOnly && globalSettings.IsSelfHosted) | ||
{ | ||
throw new BadRequestException("Only allowed when not self-hosted."); | ||
} | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
extensions/Bitwarden.Extensions.Hosting/src/Bitwarden.Extensions.Hosting.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,47 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<IsPackable>true</IsPackable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup> | ||
<Authors>Bitwarden</Authors> | ||
<Description>.NET hosting extensions library</Description> | ||
<PackageProjectUrl>https://github.com/bitwarden/dotnet-extensions</PackageProjectUrl> | ||
<PackageReleaseNotes>https://github.com/bitwarden/dotnet-extensions/releases</PackageReleaseNotes> | ||
<PackageLicenseExpression>GPL-3.0-only</PackageLicenseExpression> | ||
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance> | ||
<PackageReadmeFile>README.md</PackageReadmeFile> | ||
<GenerateDocumentationFile>true</GenerateDocumentationFile> | ||
<PublishRepositoryUrl>true</PublishRepositoryUrl> | ||
<EmbedUntrackedSources>true</EmbedUntrackedSources> | ||
<DebugType>embedded</DebugType> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" /> | ||
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.8.1" /> | ||
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.8.1" /> | ||
<PackageReference Include="Serilog.Extensions.Hosting" Version="8.0.0" /> | ||
<PackageReference Include="Serilog.Formatting.Compact" Version="2.0.0" /> | ||
<PackageReference Include="Serilog.Settings.Configuration" Version="8.0.0" /> | ||
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.1" /> | ||
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<FrameworkReference Include="Microsoft.AspNetCore.App" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Serilog.AspNetCore" Version="8.0.1" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<None Include=".\README.md" Pack="true" PackagePath="\" /> | ||
</ItemGroup> | ||
|
||
</Project> |
16 changes: 16 additions & 0 deletions
16
extensions/Bitwarden.Extensions.Hosting/src/BitwardenHostOptions.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,16 @@ | ||
namespace Bitwarden.Extensions.Hosting; | ||
|
||
/// <summary> | ||
/// Options for configuring the Bitwarden host. | ||
/// </summary> | ||
public class BitwardenHostOptions | ||
{ | ||
/// <summary> | ||
/// Gets or sets a value indicating whether to include request logging. | ||
/// </summary> | ||
public bool IncludeLogging { get; set; } = true; | ||
/// <summary> | ||
/// Gets or sets a value indicating whether to include metrics. | ||
/// </summary> | ||
public bool IncludeMetrics { get; set; } = true; | ||
} |
14 changes: 14 additions & 0 deletions
14
extensions/Bitwarden.Extensions.Hosting/src/BitwardenWebHostOptions.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,14 @@ | ||
using Bitwarden.Extensions.Hosting; | ||
|
||
namespace Bitwarden.Extensions.WebHosting; | ||
|
||
/// <summary> | ||
/// Options for configuring the web host. | ||
/// </summary> | ||
public class BitwardenWebHostOptions : BitwardenHostOptions | ||
{ | ||
/// <summary> | ||
/// Gets or sets a value indicating whether to include request logging. | ||
/// </summary> | ||
public bool IncludeRequestLogging { get; set; } | ||
} |
15 changes: 15 additions & 0 deletions
15
extensions/Bitwarden.Extensions.Hosting/src/Exceptions/BadRequestException.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,15 @@ | ||
namespace Bitwarden.Extensions.Hosting.Exceptions; | ||
|
||
/// <summary> | ||
/// Exception for when a request is invalid. | ||
/// </summary> | ||
public class BadRequestException : Exception | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="BadRequestException"/> class. | ||
/// </summary> | ||
/// <param name="message">Error message.</param> | ||
public BadRequestException(string message) | ||
: base(message) | ||
{ } | ||
} |
12 changes: 12 additions & 0 deletions
12
extensions/Bitwarden.Extensions.Hosting/src/GlobalSettingsBase.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,12 @@ | ||
namespace Bitwarden.Extensions.Hosting; | ||
|
||
/// <summary> | ||
/// Global settings. | ||
/// </summary> | ||
public class GlobalSettingsBase | ||
{ | ||
/// <summary> | ||
/// Gets or sets a value indicating whether the application is self-hosted. | ||
/// </summary> | ||
public bool IsSelfHosted { get; set; } | ||
} |
Oops, something went wrong.