Skip to content

Commit 4a2bd25

Browse files
authored
Merge pull request #24 from dark-loop/disable-authorization
adding helper method to detect local development
2 parents 6ebb3f2 + 118dbb9 commit 4a2bd25

File tree

4 files changed

+41
-3
lines changed

4 files changed

+41
-3
lines changed

README.md

+10-2
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ Adding change log starting with version 3.1.3
106106

107107
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:
108108
```c#
109-
builder.Services.Configure<FunctionsAuthorizationOptions>(Configuration.GetSection("FunctionsAuthorization"));
109+
builder.Services.Configure<FunctionsAuthorizationOptions>(
110+
Configuration.GetSection("FunctionsAuthorization"));
110111
```
111112

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

119-
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.
120+
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:
121+
```c#
122+
if (builder.IsLocalAuthorizationContext())
123+
{
124+
builder.Services.Configure<FunctionsAuthorizationOptions>(
125+
options => options.AuthorizationDisabled = true);
126+
}
127+
```
120128

121129
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:
122130

sample/Darkloop.Azure.Functions.Authorize.SampleFunctions.V4/Startup.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
77
using Microsoft.Extensions.Configuration;
88
using Microsoft.Extensions.DependencyInjection;
9+
using Microsoft.Extensions.Logging;
910
using System;
1011
using System.Collections.Generic;
1112
using System.Text;
@@ -63,7 +64,10 @@ public override void Configure(IFunctionsHostBuilder builder)
6364
// decorated with FunctionAuthorizeAttribute you can add the following configuration.
6465
// If you bind it to configuration, you can modify the setting remotely using
6566
// Azure App Configuration or other configuration providers without the need to restart app.
66-
builder.Services.Configure<FunctionsAuthorizationOptions>(Configuration.GetSection("AuthOptions"));
67+
if (builder.IsLocalAuthorizationContext())
68+
{
69+
builder.Services.Configure<FunctionsAuthorizationOptions>(Configuration.GetSection("AuthOptions"));
70+
}
6771
}
6872

6973
public override void ConfigureAppConfiguration(IFunctionsConfigurationBuilder builder)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using DarkLoop.Azure.Functions.Authorize.Security;
2+
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Text;
6+
7+
namespace Microsoft.Azure.Functions.Extensions.DependencyInjection
8+
{
9+
/// <summary>
10+
/// Extension methods for <see cref="IFunctionsHostBuilder"/>.
11+
/// </summary>
12+
public static class FunctionsHostBuilderExtensions
13+
{
14+
/// <summary>
15+
/// Returns a value indicating whether the current environment is local development.
16+
/// </summary>
17+
/// <param name="builder">The current builder.</param>
18+
/// <returns></returns>
19+
public static bool IsLocalAuthorizationContext(this IFunctionsHostBuilder builder)
20+
{
21+
return AuthHelper.IsLocalDevelopment;
22+
}
23+
}
24+
}

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

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ internal class AuthHelper
1818

1919
internal static bool EnableAuth { get; private set; }
2020

21+
internal static bool IsLocalDevelopment => !EnableAuth;
22+
2123
static AuthHelper()
2224
{
2325
var entry = Assembly.GetEntryAssembly();

0 commit comments

Comments
 (0)