-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46 from marcwittke/release/4.1.0
Release/4.1.0
- Loading branch information
Showing
16 changed files
with
447 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/abstractions/Backend.Fx/Exceptions/ForbiddenException.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
namespace Backend.Fx.Exceptions | ||
{ | ||
using System; | ||
|
||
public class ForbiddenException : ClientException | ||
{ | ||
public enum Code | ||
{ | ||
AccessDenied, | ||
NotAuthenticated, | ||
AccountLocked, | ||
InvalidLogonAttempt, | ||
} | ||
|
||
public ForbiddenException() | ||
: base("Unauthorized") | ||
{ } | ||
|
||
public ForbiddenException(params Error[] errors) | ||
: base("Unauthorized", errors) | ||
{ } | ||
|
||
public ForbiddenException(string message, params Error[] errors) | ||
: base(message, errors) | ||
{ } | ||
|
||
public ForbiddenException(string message, Exception innerException, params Error[] errors) | ||
: base(message, innerException, errors) | ||
{ } | ||
|
||
public static IExceptionBuilder UseBuilder() | ||
{ | ||
return new ExceptionBuilder<ForbiddenException>(); | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/abstractions/Backend.Fx/Exceptions/TooManyRequestsException.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using System; | ||
|
||
namespace Backend.Fx.Exceptions | ||
{ | ||
public class TooManyRequestsException : ClientException | ||
{ | ||
public TooManyRequestsException(int retryAfter) | ||
{ | ||
RetryAfter = retryAfter; | ||
} | ||
|
||
public TooManyRequestsException(int retryAfter, params Error[] errors) : base(errors) | ||
{ | ||
RetryAfter = retryAfter; | ||
} | ||
|
||
public TooManyRequestsException(int retryAfter, string message, params Error[] errors) : base(message, errors) | ||
{ | ||
RetryAfter = retryAfter; | ||
} | ||
|
||
public TooManyRequestsException(int retryAfter, string message, Exception innerException, params Error[] errors) : base(message, innerException, errors) | ||
{ | ||
RetryAfter = retryAfter; | ||
} | ||
|
||
public int RetryAfter { get; } | ||
} | ||
} |
35 changes: 11 additions & 24 deletions
35
src/abstractions/Backend.Fx/Exceptions/UnauthorizedException.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,23 @@ | ||
namespace Backend.Fx.Exceptions | ||
{ | ||
using System; | ||
using System; | ||
|
||
namespace Backend.Fx.Exceptions | ||
{ | ||
public class UnauthorizedException : ClientException | ||
{ | ||
public enum Code | ||
{ | ||
AccessDenied, | ||
NotAuthenticated, | ||
AccountLocked, | ||
InvalidLogonAttempt, | ||
} | ||
|
||
public UnauthorizedException() | ||
: base("Unauthorized") | ||
public UnauthorizedException() | ||
: base("Unauthorized") | ||
{ } | ||
|
||
public UnauthorizedException(params Error[] errors) | ||
: base("Unauthorized", errors) | ||
public UnauthorizedException(params Error[] errors) | ||
: base("Unauthorized", errors) | ||
{ } | ||
|
||
public UnauthorizedException(string message, params Error[] errors) | ||
: base(message, errors) | ||
public UnauthorizedException(string message, params Error[] errors) | ||
: base(message, errors) | ||
{ } | ||
|
||
public UnauthorizedException(string message, Exception innerException, params Error[] errors) | ||
: base(message, innerException, errors) | ||
: base(message, innerException, errors) | ||
{ } | ||
|
||
public static IExceptionBuilder UseBuilder() | ||
{ | ||
return new ExceptionBuilder<UnauthorizedException>(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/environments/Backend.Fx.AspNetCore.Mvc/Throttling/ExceptionThrottlingAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using System; | ||
using Backend.Fx.Exceptions; | ||
using Microsoft.AspNetCore.Mvc.Filters; | ||
using Microsoft.Extensions.Caching.Memory; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Backend.Fx.AspNetCore.Mvc.Throttling | ||
{ | ||
public class ExceptionThrottlingAttribute : ThrottlingBaseAttribute | ||
{ | ||
public override void OnActionExecuted(ActionExecutedContext actionContext) | ||
{ | ||
var cache = actionContext.HttpContext.RequestServices.GetRequiredService<IMemoryCache>(); | ||
var key = string.Concat(Name, "-", actionContext.HttpContext.Connection.RemoteIpAddress); | ||
|
||
if (actionContext.Exception == null) | ||
{ | ||
cache.Remove(key); | ||
return; | ||
} | ||
|
||
if (cache.TryGetValue(key, out int repetition)) | ||
{ | ||
var retryAfter = Math.Max(1, CalculateRepeatedTimeoutFactor(repetition)) * Seconds; | ||
cache.Set(key, ++repetition, TimeSpan.FromSeconds(retryAfter)); | ||
throw new TooManyRequestsException(retryAfter,"Request canceled due to throttling", new Error("Throttled", string.Format(Message, retryAfter))); | ||
} | ||
|
||
cache.Set(key, 1, TimeSpan.FromSeconds(Seconds)); | ||
} | ||
|
||
protected override int CalculateRepeatedTimeoutFactor(int repetition) | ||
{ | ||
return repetition * repetition; | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/environments/Backend.Fx.AspNetCore.Mvc/Throttling/ThrottlingAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using System; | ||
using Backend.Fx.Exceptions; | ||
using Microsoft.AspNetCore.Mvc.Filters; | ||
using Microsoft.Extensions.Caching.Memory; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Backend.Fx.AspNetCore.Mvc.Throttling | ||
{ | ||
public class ThrottlingAttribute : ThrottlingBaseAttribute | ||
{ | ||
public override void OnActionExecuting(ActionExecutingContext actionContext) | ||
{ | ||
var cache = actionContext.HttpContext.RequestServices.GetRequiredService<IMemoryCache>(); | ||
var key = string.Concat(Name, "-", actionContext.HttpContext.Connection.RemoteIpAddress); | ||
|
||
if (cache.TryGetValue(key, out int repetition)) | ||
{ | ||
repetition++; | ||
var retryAfter = Math.Max(1, CalculateRepeatedTimeoutFactor(repetition)) * Seconds; | ||
cache.Set(key, repetition, TimeSpan.FromSeconds(retryAfter)); | ||
throw new TooManyRequestsException(retryAfter, "Request canceled due to throttling", new Error("Throttled", string.Format(Message, retryAfter))); | ||
} | ||
|
||
cache.Set(key, 1, TimeSpan.FromSeconds(Seconds)); | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/environments/Backend.Fx.AspNetCore.Mvc/Throttling/ThrottlingBaseAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using Microsoft.AspNetCore.Mvc.Filters; | ||
|
||
namespace Backend.Fx.AspNetCore.Mvc.Throttling | ||
{ | ||
public abstract class ThrottlingBaseAttribute : ActionFilterAttribute | ||
{ | ||
/// <summary> | ||
/// A unique name for this Throttle. | ||
/// </summary> | ||
/// <remarks> | ||
/// We'll be inserting a Cache record based on this name and client IP, e.g. "Name-192.168.0.1" | ||
/// </remarks> | ||
public string Name { get; set; } | ||
|
||
/// <summary> | ||
/// The number of seconds clients must wait before executing this decorated route again. | ||
/// </summary> | ||
public int Seconds { get; set; } | ||
|
||
/// <summary> | ||
/// A text message that will be sent to the client upon throttling. You can include the token {0} to | ||
/// show this.Seconds in the message, e.g. "Wait {0} seconds before trying again". | ||
/// </summary> | ||
public string Message { get; set; } = "Wait {0} seconds before trying again"; | ||
|
||
protected virtual int CalculateRepeatedTimeoutFactor(int repetition) | ||
{ | ||
return 1; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
src/implementations/Backend.Fx.Log4NetLogging/Backend.Fx.Log4NetLogging.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard1.5</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="log4net" Version="2.0.8" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\abstractions\Backend.Fx\Backend.Fx.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
Oops, something went wrong.