Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ Adding change log starting with version 3.1.3

Optionally you can bind it to configuration to rely on providers like User Secrets or Azure App Configuration to disable and re-enable without having to restart your application:
```c#
builder.Services.Configure<FunctionsAuthorizationOptions>(Configuration.GetSection("FunctionsAuthorization"));
builder.Services.Configure<FunctionsAuthorizationOptions>(
Configuration.GetSection("FunctionsAuthorization"));
```

For function apps targeting .NET 7 or greater, you can also use `AuthorizationBuilder` to set this value:
Expand All @@ -116,7 +117,14 @@ Adding change log starting with version 3.1.3
.DisableAuthorization(Configuration.GetValue<bool>("AuthOptions:DisableAuthorization"));
```

Its always recommended to encapsulate this logic within checks for environments to ensure that if the configuration setting is unintentionally moved to a non-desired environment, it would not affect security of our HTTP triggered functions.
Its always recommended to encapsulate this logic within checks for environments to ensure that if the configuration setting is unintentionally moved to a non-desired environment, it would not affect security of our HTTP triggered functions. This change adds a helper method to identify if you are running the function app in the local environment:
```c#
if (builder.IsLocalAuthorizationContext())
{
builder.Services.Configure<FunctionsAuthorizationOptions>(
options => options.AuthorizationDisabled = true);
}
```

If you want to output warnings emitted by the library remember to set the log level to `Warning` or lower for `Darkloop` category in your `host.json` file:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Text;
Expand Down Expand Up @@ -63,7 +64,10 @@ public override void Configure(IFunctionsHostBuilder builder)
// decorated with FunctionAuthorizeAttribute you can add the following configuration.
// If you bind it to configuration, you can modify the setting remotely using
// Azure App Configuration or other configuration providers without the need to restart app.
builder.Services.Configure<FunctionsAuthorizationOptions>(Configuration.GetSection("AuthOptions"));
if (builder.IsLocalAuthorizationContext())
{
builder.Services.Configure<FunctionsAuthorizationOptions>(Configuration.GetSection("AuthOptions"));
}
}

public override void ConfigureAppConfiguration(IFunctionsConfigurationBuilder builder)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using DarkLoop.Azure.Functions.Authorize.Security;
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Text;

namespace Microsoft.Azure.Functions.Extensions.DependencyInjection
{
/// <summary>
/// Extension methods for <see cref="IFunctionsHostBuilder"/>.
/// </summary>
public static class FunctionsHostBuilderExtensions
{
/// <summary>
/// Returns a value indicating whether the current environment is local development.
/// </summary>
/// <param name="builder">The current builder.</param>
/// <returns></returns>
public static bool IsLocalAuthorizationContext(this IFunctionsHostBuilder builder)
{
return AuthHelper.IsLocalDevelopment;
}
}
}
2 changes: 2 additions & 0 deletions src/DarkLoop.Azure.Functions.Authorize/Security/AuthHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ internal class AuthHelper

internal static bool EnableAuth { get; private set; }

internal static bool IsLocalDevelopment => !EnableAuth;

static AuthHelper()
{
var entry = Assembly.GetEntryAssembly();
Expand Down