Skip to content

Commit 2878baf

Browse files
authored
Merge pull request #23 from dark-loop/force-remove-builtin-bearer
Removing existing Bearer configuration by default when adding custom bearer token
2 parents 7ff193a + 411e0b7 commit 2878baf

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,7 @@ public class Functions
9393
## Change log
9494
Adding change log starting with version 3.1.3
9595

96-
### 3.1.3
96+
### 3.1.3
97+
- #### Remove Functions bult-in JwtBearer configuration by default (Breaking change?)
98+
Azure Functions recently [added configuration](https://github.com/Azure/azure-functions-host/pull/9678) for issuer and audience validation for the default authentication flows, not the one supported by this package through `FunctionAuthorizeAttribute`, which interferes with token validation when using our own Bearer scheme token configuration.
99+
In prior versions, this package has functionality to clear Functions built-in configuration, but it was not enabled by default when using `AddJwtBearer(Action<JwtBearerOptions> configure, bool removeBuiltInConfig = false)`. Since the use of this package is commonly used for custom JWT token, the default value of `removeBuiltInConfig` is now `true`.

src/DarkLoop.Azure.Functions.Authorize/Security/FunctionsAuthenticationBuilder.cs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ internal FunctionsAuthenticationBuilder(IServiceCollection services)
2121
/// and all HTTP functions are applied the Admin level after a token is validated.
2222
/// </summary>
2323
/// <param name="removeBuiltInConfig">A value indicating whether remove the built-in configuration for JWT.
24-
/// Bearer scheme is still in place, but Admin level is not set incoming requests.</param>
24+
/// Bearer scheme is still in place, but Admin level is not set for incoming requests.
25+
/// <para>When setting this value to <c>true</c> (default) all existing configuration will be removed.</para></param>
2526
/// <returns>A instance of the <see cref="FunctionsAuthenticationBuilder"/></returns>
26-
public FunctionsAuthenticationBuilder AddJwtBearer(bool removeBuiltInConfig = false)
27+
public FunctionsAuthenticationBuilder AddJwtBearer(bool removeBuiltInConfig = true)
2728
{
2829
return this.AddJwtBearer(delegate { }, removeBuiltInConfig);
2930
}
@@ -35,18 +36,25 @@ public FunctionsAuthenticationBuilder AddJwtBearer(bool removeBuiltInConfig = fa
3536
/// <param name="configureOptions">An action configuring the JWT options for authentication.
3637
/// <para>When <see cref="removeBuiltInConfig"/> is set to false, it enhances the built-in configuration for the scheme</para></param>
3738
/// <param name="removeBuiltInConfig">A value indicating whether remove the built-in configuration for JWT.
38-
/// Bearer scheme is still in place, but Admin level is not set incoming requests.</param>
39+
/// Bearer scheme is still in place, but Admin level is not set incoming requests.
40+
/// <para>When setting this value to <c>true</c> (default) all existing configuration will be removed.</para></param>
3941
/// <returns>A instance of the <see cref="FunctionsAuthenticationBuilder"/></returns>
40-
public FunctionsAuthenticationBuilder AddJwtBearer(Action<JwtBearerOptions> configureOptions, bool removeBuiltInConfig = false)
42+
public FunctionsAuthenticationBuilder AddJwtBearer(Action<JwtBearerOptions> configureOptions, bool removeBuiltInConfig = true)
4143
{
4244
if(removeBuiltInConfig)
4345
{
44-
var descriptor = Services.FirstOrDefault(s => s.ServiceType == typeof(IConfigureOptions<JwtBearerOptions>));
45-
var instance = descriptor?.ImplementationInstance as ConfigureNamedOptions<JwtBearerOptions>;
46+
var descriptors = Services
47+
.Where(s => s.ServiceType == typeof(IConfigureOptions<JwtBearerOptions>))
48+
.ToList();
4649

47-
if (instance?.Name == "Bearer")
50+
foreach (var descriptor in descriptors)
4851
{
49-
Services.Remove(descriptor);
52+
var instance = descriptor?.ImplementationInstance as ConfigureNamedOptions<JwtBearerOptions>;
53+
54+
if (instance?.Name == "Bearer")
55+
{
56+
Services.Remove(descriptor);
57+
}
5058
}
5159
}
5260

0 commit comments

Comments
 (0)