Skip to content

Commit e6a5422

Browse files
authored
Adding IsAuthenticationEnabled property (#35)
1 parent c49093c commit e6a5422

File tree

7 files changed

+25
-55
lines changed

7 files changed

+25
-55
lines changed

src/Azure.WebJobs.Extensions.HttpApi/Azure.WebJobs.Extensions.HttpApi.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>net6.0;netcoreapp3.1</TargetFrameworks>
4+
<TargetFrameworks>net6.0</TargetFrameworks>
55
<LangVersion>latest</LangVersion>
66
</PropertyGroup>
77

src/Azure.WebJobs.Extensions.HttpApi/Config/HttpApiWebJobsBuilderExtensions.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@ public static class HttpApiWebJobsBuilderExtensions
1010
{
1111
public static IWebJobsBuilder AddHttpApi(this IWebJobsBuilder builder)
1212
{
13-
if (builder is null)
14-
{
15-
throw new ArgumentNullException(nameof(builder));
16-
}
13+
ArgumentNullException.ThrowIfNull(builder);
1714

1815
builder.AddExtension<RoutePrecedenceExtensionConfigProvider>();
1916

src/Azure.WebJobs.Extensions.HttpApi/Core/LocalStaticAppResult.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@ public class LocalStaticAppResult : IActionResult
1717

1818
public Task ExecuteResultAsync(ActionContext context)
1919
{
20-
if (context is null)
21-
{
22-
throw new ArgumentNullException(nameof(context));
23-
}
20+
ArgumentNullException.ThrowIfNull(context);
2421

2522
return s_executor.ExecuteAsync(context, this);
2623
}

src/Azure.WebJobs.Extensions.HttpApi/Core/ProxyResult.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ public class ProxyResult : IActionResult
1010
{
1111
public ProxyResult(string backendUri)
1212
{
13-
BackendUri = backendUri ?? throw new ArgumentNullException(nameof(backendUri));
13+
ArgumentNullException.ThrowIfNull(backendUri);
14+
15+
BackendUri = backendUri;
1416
}
1517

1618
public string BackendUri { get; }
@@ -23,10 +25,7 @@ public ProxyResult(string backendUri)
2325

2426
public Task ExecuteResultAsync(ActionContext context)
2527
{
26-
if (context is null)
27-
{
28-
throw new ArgumentNullException(nameof(context));
29-
}
28+
ArgumentNullException.ThrowIfNull(context);
3029

3130
return s_executor.ExecuteAsync(context, this);
3231
}

src/Azure.WebJobs.Extensions.HttpApi/HttpFunctionBase.cs

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ protected HttpFunctionBase(IHttpContextAccessor httpContextAccessor)
4545
protected HttpResponse Response => HttpContext?.Response;
4646
protected ClaimsPrincipal User => HttpContext?.User;
4747
protected ModelStateDictionary ModelState { get; } = new();
48+
protected bool IsAuthenticationEnabled => FunctionAppEnvironment.IsAuthenticationEnabled;
4849

4950
protected IUrlHelper Url
5051
{
@@ -134,10 +135,7 @@ protected ObjectResult Problem(string detail = null, string instance = null, int
134135

135136
protected BadRequestObjectResult ValidationProblem(ValidationProblemDetails descriptor)
136137
{
137-
if (descriptor is null)
138-
{
139-
throw new ArgumentNullException(nameof(descriptor));
140-
}
138+
ArgumentNullException.ThrowIfNull(descriptor);
141139

142140
return new BadRequestObjectResult(descriptor);
143141
}
@@ -153,20 +151,14 @@ protected ObjectResult ValidationProblem(string detail = null, string instance =
153151

154152
protected CreatedResult Created(string uri, object value)
155153
{
156-
if (uri is null)
157-
{
158-
throw new ArgumentNullException(nameof(uri));
159-
}
154+
ArgumentNullException.ThrowIfNull(uri);
160155

161156
return new CreatedResult(uri, value);
162157
}
163158

164159
protected CreatedResult Created(Uri uri, object value)
165160
{
166-
if (uri is null)
167-
{
168-
throw new ArgumentNullException(nameof(uri));
169-
}
161+
ArgumentNullException.ThrowIfNull(uri);
170162

171163
return new CreatedResult(uri, value);
172164
}
@@ -182,10 +174,7 @@ protected CreatedResult CreatedAtFunction(string functionName, object routeValue
182174

183175
protected AcceptedResult Accepted(Uri uri)
184176
{
185-
if (uri is null)
186-
{
187-
throw new ArgumentNullException(nameof(uri));
188-
}
177+
ArgumentNullException.ThrowIfNull(uri);
189178

190179
return new AcceptedResult(uri, null);
191180
}
@@ -194,10 +183,7 @@ protected AcceptedResult Accepted(Uri uri)
194183

195184
protected AcceptedResult Accepted(Uri uri, object value)
196185
{
197-
if (uri is null)
198-
{
199-
throw new ArgumentNullException(nameof(uri));
200-
}
186+
ArgumentNullException.ThrowIfNull(uri);
201187

202188
return new AcceptedResult(uri, value);
203189
}
@@ -215,20 +201,14 @@ protected AcceptedResult AcceptedAtFunction(string functionName, object routeVal
215201

216202
protected ProxyResult Proxy(string backendUri, Action<HttpRequestMessage> beforeSend = null, Action<HttpResponseMessage> afterSend = null)
217203
{
218-
if (backendUri is null)
219-
{
220-
throw new ArgumentNullException(nameof(backendUri));
221-
}
204+
ArgumentNullException.ThrowIfNull(backendUri);
222205

223206
return new ProxyResult(backendUri) { BeforeSend = beforeSend, AfterSend = afterSend };
224207
}
225208

226209
protected RemoteStaticAppResult RemoteStaticApp(string backendUri, string fallbackExclude = null)
227210
{
228-
if (backendUri is null)
229-
{
230-
throw new ArgumentNullException(nameof(backendUri));
231-
}
211+
ArgumentNullException.ThrowIfNull(backendUri);
232212

233213
return new RemoteStaticAppResult(backendUri) { FallbackExclude = fallbackExclude };
234214
}

src/Azure.WebJobs.Extensions.HttpApi/Internal/FunctionEnvironment.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,7 @@ internal static class FunctionAppEnvironment
66
{
77
public static bool IsAvailable => !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("WEBSITE_SITE_NAME"));
88

9+
public static bool IsAuthenticationEnabled => bool.TryParse(Environment.GetEnvironmentVariable("WEBSITE_AUTH_ENABLED"), out var result) && result;
10+
911
public static string RootPath => IsAvailable ? Environment.ExpandEnvironmentVariables("%HOME%/site/wwwroot") : Environment.CurrentDirectory;
1012
}

src/Azure.WebJobs.Extensions.HttpApi/Internal/HttpForwarder.cs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ private static HttpMethod GetHttpMethod(string method)
4343
{
4444
return method switch
4545
{
46-
{ } when HttpMethods.IsGet(method) => HttpMethod.Get,
47-
{ } when HttpMethods.IsPost(method) => HttpMethod.Post,
48-
{ } when HttpMethods.IsPut(method) => HttpMethod.Put,
49-
{ } when HttpMethods.IsDelete(method) => HttpMethod.Delete,
50-
{ } when HttpMethods.IsOptions(method) => HttpMethod.Options,
51-
{ } when HttpMethods.IsHead(method) => HttpMethod.Head,
52-
{ } when HttpMethods.IsPatch(method) => HttpMethod.Patch,
46+
not null when HttpMethods.IsGet(method) => HttpMethod.Get,
47+
not null when HttpMethods.IsPost(method) => HttpMethod.Post,
48+
not null when HttpMethods.IsPut(method) => HttpMethod.Put,
49+
not null when HttpMethods.IsDelete(method) => HttpMethod.Delete,
50+
not null when HttpMethods.IsOptions(method) => HttpMethod.Options,
51+
not null when HttpMethods.IsHead(method) => HttpMethod.Head,
52+
not null when HttpMethods.IsPatch(method) => HttpMethod.Patch,
5353
_ => throw new ArgumentOutOfRangeException(nameof(method), method, null)
5454
};
5555
}
@@ -64,12 +64,7 @@ private static bool HasRequestBody(HttpContext httpContext)
6464
return true;
6565
}
6666

67-
if (HttpMethods.IsGet(method) || HttpMethods.IsHead(method) || HttpMethods.IsDelete(method))
68-
{
69-
return false;
70-
}
71-
72-
return true;
67+
return !HttpMethods.IsGet(method) && !HttpMethods.IsHead(method) && !HttpMethods.IsDelete(method);
7368
}
7469

7570
private static void CopyRequestHeaders(HttpContext httpContext, HttpRequestMessage request)

0 commit comments

Comments
 (0)