Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update references, replace reflection w/ extensions #7

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
10 changes: 5 additions & 5 deletions IsolatedFunctionAuth/IsolatedFunctionAuth.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.0.13" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.3.0" OutputItemType="Analyzer" />
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.6.0" />
<PackageReference Include="Microsoft.IdentityModel.Protocols.OpenIdConnect" Version="6.14.1" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.14.1" />
<PackageReference Include="System.Text.Json" Version="6.0.0-rc.2.21480.5" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.7.0" OutputItemType="Analyzer" />
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.10.0" />
<PackageReference Include="Microsoft.IdentityModel.Protocols.OpenIdConnect" Version="6.24.0" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.24.0" />
<PackageReference Include="System.Text.Json" Version="6.0.6" />
</ItemGroup>
<ItemGroup>
<None Update="host.json">
Expand Down
6 changes: 3 additions & 3 deletions IsolatedFunctionAuth/Middleware/AuthenticationMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ public async Task Invoke(
if (!TryGetTokenFromHeaders(context, out var token))
{
// Unable to get token from headers
context.SetHttpResponseStatusCode(HttpStatusCode.Unauthorized);
await context.SetHttpResponseStatusCode(HttpStatusCode.Unauthorized);
return;
}

if (!_tokenValidator.CanReadToken(token))
{
// Token is malformed
context.SetHttpResponseStatusCode(HttpStatusCode.Unauthorized);
await context.SetHttpResponseStatusCode(HttpStatusCode.Unauthorized);
return;
}

Expand All @@ -73,7 +73,7 @@ public async Task Invoke(
catch (SecurityTokenException)
{
// Token is not valid (expired etc.)
context.SetHttpResponseStatusCode(HttpStatusCode.Unauthorized);
await context.SetHttpResponseStatusCode(HttpStatusCode.Unauthorized);
return;
}
}
Expand Down
2 changes: 1 addition & 1 deletion IsolatedFunctionAuth/Middleware/AuthorizationMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public async Task Invoke(
var principalFeature = context.Features.Get<JwtPrincipalFeature>();
if (!AuthorizePrincipal(context, principalFeature.Principal))
{
context.SetHttpResponseStatusCode(HttpStatusCode.Forbidden);
await context.SetHttpResponseStatusCode(HttpStatusCode.Forbidden);
return;
}

Expand Down
26 changes: 9 additions & 17 deletions IsolatedFunctionAuth/Middleware/FunctionContextExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,33 +1,25 @@
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using System;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Threading.Tasks;

namespace IsolatedFunctionAuth.Middleware
{
public static class FunctionContextExtensions
{
public static void SetHttpResponseStatusCode(
public static async Task SetHttpResponseStatusCode(
this FunctionContext context,
HttpStatusCode statusCode)
{
// Terrible reflection code since I haven't found a nicer way to do this...
// For some reason the types are marked as internal
// If there's code that will break in this sample,
// it's probably here.
var coreAssembly = Assembly.Load("Microsoft.Azure.Functions.Worker.Core");
var featureInterfaceName = "Microsoft.Azure.Functions.Worker.Context.Features.IFunctionBindingsFeature";
var featureInterfaceType = coreAssembly.GetType(featureInterfaceName);
var bindingsFeature = context.Features.Single(
f => f.Key.FullName == featureInterfaceType.FullName).Value;
var invocationResultProp = featureInterfaceType.GetProperty("InvocationResult");

var grpcAssembly = Assembly.Load("Microsoft.Azure.Functions.Worker.Grpc");
var responseDataType = grpcAssembly.GetType("Microsoft.Azure.Functions.Worker.GrpcHttpResponseData");
var responseData = Activator.CreateInstance(responseDataType, context, statusCode);

invocationResultProp.SetMethod.Invoke(bindingsFeature, new object[] { responseData });
var req = await context.GetHttpRequestDataAsync();
if (req == null) { return; }
var response = HttpResponseData.CreateResponse(req);
response.StatusCode = statusCode;
var result = context.GetInvocationResult();
result.Value = response;
}

public static MethodInfo GetTargetFunctionMethod(this FunctionContext context)
Expand Down