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
4 changes: 2 additions & 2 deletions .build/release.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
<Authors>Arturo Martinez</Authors>
<Company>DarkLoop</Company>
<PackageId>DarkLoop.Azure.Functions.Authorize</PackageId>
<IsPreview>false</IsPreview>
<IsPreview>true</IsPreview>
<AssemblyVersion>3.0.0.0</AssemblyVersion>
<Version>3.1.0</Version>
<Version>3.1.1</Version>
<FileVersion>$(Version).0</FileVersion>
<RepositoryUrl>https://github.com/dark-loop/functions-authorize</RepositoryUrl>
<License>https://github.com/dark-loop/functions-authorize/blob/master/LICENSE</License>
Expand Down
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[*.cs]

# CS0618: Type or member is obsolete
dotnet_diagnostic.CS0618.severity = silent
9 changes: 7 additions & 2 deletions Functions-Authorize.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.28606.126
# Visual Studio Version 17
VisualStudioVersion = 17.4.32804.182
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{D96FC724-6F6E-400E-BCA9-21A8FD44CA1C}"
EndProject
Expand All @@ -27,6 +27,11 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "sample", "sample", "{53EC58
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DarkLoop.Azure.Functions.Authorize.SampleFunctions", "sample\DarkLoop.Azure.Functions.Authorize.SampleFunctions\DarkLoop.Azure.Functions.Authorize.SampleFunctions.csproj", "{9AB1B297-FA02-406C-A3E2-979A7CC5C706}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{6C3D01C4-AFF0-4AE3-ACA1-FDCDF8FD6CE1}"
ProjectSection(SolutionItems) = preProject
.editorconfig = .editorconfig
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@

<Import Project="..\..\.build\release.props" Condition=" '$(Configuration)' == 'Release' " />

<ItemGroup>
<None Include="..\..\.editorconfig" Link=".editorconfig" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Authorization" Version="3.0.3" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Runtime.Serialization;
using System.Text;

namespace DarkLoop.Azure.Functions.Authorize
{
public sealed class FunctionAuthorizationException : Exception
{
private readonly HttpStatusCode _statusCode;

internal FunctionAuthorizationException(HttpStatusCode status)
: base($"{ValidateStatus(status)} authorization error encountered. This is the only way to stop function execution. The correct status has been communicated to caller")
{
_statusCode = status;
}

public FunctionAuthorizationException(SerializationInfo info, StreamingContext context) : base(info, context) { }

public HttpStatusCode AuthorizationStatus => _statusCode;

private static int ValidateStatus(HttpStatusCode status)
{
if (status != HttpStatusCode.Unauthorized && status != HttpStatusCode.Forbidden)
{
throw new ArgumentException("Only unauthorized and forbidden status are accepted for this exception.");
}

return (int)status;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System;
using System.Linq;
using System.Net.Http;
using System.Runtime.ExceptionServices;
using System.Threading;
using System.Threading.Tasks;
using DarkLoop.Azure.Functions.Authorize.Filters;
using DarkLoop.Azure.Functions.Authorize.Security;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
Expand All @@ -15,7 +17,6 @@ namespace DarkLoop.Azure.Functions.Authorize
/// Represents authorization logic that needs to be applied to a function.
/// </summary>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
[Obsolete("This class is dependent on Azure Functions preview features.")]
public class FunctionAuthorizeAttribute : FunctionInvocationFilterAttribute, IFunctionInvocationFilter, IAuthorizeData
{
public FunctionAuthorizeAttribute() { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public async Task OnAuthorizingFunctionInstance(FunctionExecutingContext functio
await SetResponseAsync("Unauthorized", httpContext.Response);

// need to make sure function stops executing. At this moment this is the only way.
BombFunctionInstance((int)HttpStatusCode.Unauthorized);
BombFunctionInstance(HttpStatusCode.Unauthorized);
}

if (context.Result is ForbidResult forbid)
Expand All @@ -71,7 +71,7 @@ public async Task OnAuthorizingFunctionInstance(FunctionExecutingContext functio
await SetResponseAsync("Forbidden", httpContext.Response);

// need to make sure function stops executing. At this moment this is the only way.
BombFunctionInstance((int)HttpStatusCode.Forbidden);
BombFunctionInstance(HttpStatusCode.Forbidden);
}
}

Expand All @@ -86,10 +86,9 @@ private async Task SetResponseAsync(string message, HttpResponse response)
await response.Body.FlushAsync();
}

private void BombFunctionInstance(int status)
private void BombFunctionInstance(HttpStatusCode status)
{
throw new Exception(
$"{status} Authorization error encountered. This is the only way to stop function execution. The correct status has been communicated to caller");
throw new FunctionAuthorizationException(status);
}
}
}