diff --git a/.build/release.props b/.build/release.props index 09f6dfd..aedd538 100644 --- a/.build/release.props +++ b/.build/release.props @@ -4,9 +4,9 @@ Arturo Martinez DarkLoop DarkLoop.Azure.Functions.Authorize - false + true 3.0.0.0 - 3.1.0 + 3.1.1 $(Version).0 https://github.com/dark-loop/functions-authorize https://github.com/dark-loop/functions-authorize/blob/master/LICENSE diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..5e51eda --- /dev/null +++ b/.editorconfig @@ -0,0 +1,4 @@ +[*.cs] + +# CS0618: Type or member is obsolete +dotnet_diagnostic.CS0618.severity = silent diff --git a/Functions-Authorize.sln b/Functions-Authorize.sln index 08cc588..1a6a265 100644 --- a/Functions-Authorize.sln +++ b/Functions-Authorize.sln @@ -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 @@ -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 diff --git a/src/DarkLoop.Azure.Functions.Authorize/DarkLoop.Azure.Functions.Authorize.csproj b/src/DarkLoop.Azure.Functions.Authorize/DarkLoop.Azure.Functions.Authorize.csproj index 3e495b8..9cd1b94 100644 --- a/src/DarkLoop.Azure.Functions.Authorize/DarkLoop.Azure.Functions.Authorize.csproj +++ b/src/DarkLoop.Azure.Functions.Authorize/DarkLoop.Azure.Functions.Authorize.csproj @@ -21,6 +21,10 @@ + + + + diff --git a/src/DarkLoop.Azure.Functions.Authorize/FunctionAuthorizationException.cs b/src/DarkLoop.Azure.Functions.Authorize/FunctionAuthorizationException.cs new file mode 100644 index 0000000..8703ab2 --- /dev/null +++ b/src/DarkLoop.Azure.Functions.Authorize/FunctionAuthorizationException.cs @@ -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; + } + } +} diff --git a/src/DarkLoop.Azure.Functions.Authorize/FunctionAuthorizeAttribute.cs b/src/DarkLoop.Azure.Functions.Authorize/FunctionAuthorizeAttribute.cs index 0b436bd..39663f6 100644 --- a/src/DarkLoop.Azure.Functions.Authorize/FunctionAuthorizeAttribute.cs +++ b/src/DarkLoop.Azure.Functions.Authorize/FunctionAuthorizeAttribute.cs @@ -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; @@ -15,7 +17,6 @@ namespace DarkLoop.Azure.Functions.Authorize /// Represents authorization logic that needs to be applied to a function. /// [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() { } diff --git a/src/DarkLoop.Azure.Functions.Authorize/Security/FunctionsHttpAuthorizationHandler.cs b/src/DarkLoop.Azure.Functions.Authorize/Security/FunctionsHttpAuthorizationHandler.cs index 3d223c2..22b0a35 100644 --- a/src/DarkLoop.Azure.Functions.Authorize/Security/FunctionsHttpAuthorizationHandler.cs +++ b/src/DarkLoop.Azure.Functions.Authorize/Security/FunctionsHttpAuthorizationHandler.cs @@ -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) @@ -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); } } @@ -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); } } }