Skip to content

Commit

Permalink
Merge pull request #33 from marcominerva/develop
Browse files Browse the repository at this point in the history
Minor fix & libraries update
  • Loading branch information
marcominerva authored Nov 3, 2022
2 parents 4d78af0 + 8f7faeb commit 0cd245c
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 15 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ Authentication can be totally configured adding an _Authentication_ section in t
},
"ApiKey": {
"SchemeName": "MyApiKeyScheme", // Default: ApiKey
// You can specify either HeaderName, QueryName or both
// You can specify either HeaderName, QueryStringKey or both
"HeaderName": "x-api-key",
"QueryName": "code",
"QueryStringKey": "code",
// Uncomment this line if you want to validate the API Key against a fixed value.
// Otherwise, you need to register an IApiKeyValidator implementation that will be used
// to validate the API Key.
Expand Down
4 changes: 2 additions & 2 deletions samples/ApiKeySample/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"DefaultScheme": "ApiKey", // Optional
"ApiKey": {
"SchemeName": "ApiKey", // Default: ApiKey
// You can specify either HeaderName, QueryName or both
// You can specify either HeaderName, QueryStringKey or both
"HeaderName": "x-api-key",
"QueryName": "code",
"QueryStringKey": "code",
// Uncomment this line if you want to validate the API Key against a fixed value.
// Otherwise, you need to register an IApiKeyValidator implementation that will be used
// to validate the API Key.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
var request = Context.Request;

var isApiKeyAvailable = (!string.IsNullOrWhiteSpace(Options.HeaderName) && request.Headers.ContainsKey(Options.HeaderName))
|| (!string.IsNullOrWhiteSpace(Options.QueryName) && request.Query.ContainsKey(Options.QueryName));
|| (!string.IsNullOrWhiteSpace(Options.QueryStringKey) && request.Query.ContainsKey(Options.QueryStringKey));

if (!isApiKeyAvailable)
{
Expand All @@ -31,7 +31,7 @@ protected override async Task<AuthenticateResult> HandleAuthenticateAsync()

if (!request.Headers.TryGetValue(Options.HeaderName ?? string.Empty, out var value))
{
request.Query.TryGetValue(Options.QueryName ?? string.Empty, out value);
request.Query.TryGetValue(Options.QueryStringKey ?? string.Empty, out value);
}

if (string.IsNullOrWhiteSpace(Options.ApiKeyValue))
Expand Down
6 changes: 3 additions & 3 deletions src/SimpleAuthentication/ApiKey/ApiKeySettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ public class ApiKeySettings : AuthenticationSchemeOptions
/// <summary>
/// Gets or sets the name of the header that contains the API key.
/// </summary>
/// <remarks>You can specify also the <see cref="QueryName"/> to support both header and query string authentication</remarks>
/// <seealso cref="QueryName"/>
/// <remarks>You can specify also the <see cref="QueryStringKey"/> to support both header and query string authentication</remarks>
/// <seealso cref="QueryStringKey"/>
public string? HeaderName { get; set; }

/// <summary>
/// Gets or sets the name of the query string parameter that contains the API key.
/// </summary>
/// <remarks>You can specify also the <see cref="HeaderName"/> to support both header and query string authentication</remarks>
/// <seealso cref="HeaderName"/>
public string? QueryName { get; set; }
public string? QueryStringKey { get; set; }

/// <summary>
/// Gets or sets a fixed value to compare the API key against. If you need to perform custom checks to validate the API key, you should leave this value <see langword="null"/> and register an <see cref="IApiKeyValidator"/> service.
Expand Down
2 changes: 1 addition & 1 deletion src/SimpleAuthentication/SimpleAuthentication.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Nerdbank.GitVersioning" Version="3.5.113">
<PackageReference Include="Nerdbank.GitVersioning" Version="3.5.119">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
8 changes: 4 additions & 4 deletions src/SimpleAuthentication/SimpleAuthenticationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ static void CheckAddApiKey(AuthenticationBuilder builder, IConfigurationSection

ArgumentNullException.ThrowIfNull(settings.SchemeName, nameof(ApiKeySettings.SchemeName));

if (string.IsNullOrWhiteSpace(settings.HeaderName) && string.IsNullOrWhiteSpace(settings.QueryName))
if (string.IsNullOrWhiteSpace(settings.HeaderName) && string.IsNullOrWhiteSpace(settings.QueryStringKey))
{
throw new ArgumentException("You need to specify either a header name or a query string parameter name");
}
Expand All @@ -128,7 +128,7 @@ static void CheckAddApiKey(AuthenticationBuilder builder, IConfigurationSection
{
options.SchemeName = settings.SchemeName;
options.HeaderName = settings.HeaderName;
options.QueryName = settings.QueryName;
options.QueryStringKey = settings.QueryStringKey;
options.ApiKeyValue = settings.ApiKeyValue;
options.DefaultUserName = settings.DefaultUserName;
});
Expand Down Expand Up @@ -195,9 +195,9 @@ static void CheckAddApiKey(SwaggerGenOptions options, IConfigurationSection sect
AddApiKeyAuthentication(options, $"{settings.SchemeName} in Header", ParameterLocation.Header, settings.HeaderName, "Insert the API Key");
}

if (!string.IsNullOrWhiteSpace(settings.QueryName))
if (!string.IsNullOrWhiteSpace(settings.QueryStringKey))
{
AddApiKeyAuthentication(options, $"{settings.SchemeName} in Query String", ParameterLocation.Query, settings.QueryName, "Insert the API Key");
AddApiKeyAuthentication(options, $"{settings.SchemeName} in Query String", ParameterLocation.Query, settings.QueryStringKey, "Insert the API Key");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void Apply(OpenApiOperation operation, OperationFilterContext context)
CheckAddSecurityRequirement(operation, hasJwtBearerAuthentication ? jwtBearerSettings.SchemeName : null);

var hasApiKeyHeaderAuthentication = !string.IsNullOrWhiteSpace(apiKeySettings.HeaderName);
var hasApiKeyQueryAuthentication = !string.IsNullOrWhiteSpace(apiKeySettings.QueryName);
var hasApiKeyQueryAuthentication = !string.IsNullOrWhiteSpace(apiKeySettings.QueryStringKey);
CheckAddSecurityRequirement(operation, hasApiKeyHeaderAuthentication ? $"{apiKeySettings.SchemeName} in Header" : null);
CheckAddSecurityRequirement(operation, hasApiKeyQueryAuthentication ? $"{apiKeySettings.SchemeName} in Query String" : null);

Expand Down

0 comments on commit 0cd245c

Please sign in to comment.