SwizlyPeasy.Gateway
0.2.1-beta
dotnet add package SwizlyPeasy.Gateway --version 0.2.1-beta
NuGet\Install-Package SwizlyPeasy.Gateway -Version 0.2.1-beta
<PackageReference Include="SwizlyPeasy.Gateway" Version="0.2.1-beta" />
<PackageVersion Include="SwizlyPeasy.Gateway" Version="0.2.1-beta" />
<PackageReference Include="SwizlyPeasy.Gateway" />
paket add SwizlyPeasy.Gateway --version 0.2.1-beta
#r "nuget: SwizlyPeasy.Gateway, 0.2.1-beta"
#:package SwizlyPeasy.Gateway@0.2.1-beta
#addin nuget:?package=SwizlyPeasy.Gateway&version=0.2.1-beta&prerelease
#tool nuget:?package=SwizlyPeasy.Gateway&version=0.2.1-beta&prerelease
SwizlyPeasy.Gateway
This package is part of SwizlyPeasy.Gateway project. This package contains extension methods for SwizlyPeasy.Gateway configuration.
What is SwizlyPeasy.Gateway?
SwizlyPeasy.Gateway is a small API gateway based on YARP Reverse Proxy. This gateway should support OIDC authentication and service discovery with Consul.
Introduction
Currently, YARP is the most advanced reverse proxy in .NET. The version 1 of this proxy was introduced by Microsoft at the end of 2021. I wondered if it was possible to graft the minimal functionalities to propose an API Gateway based on YARP. https://devblogs.microsoft.com/dotnet/announcing-yarp-1-0-release/
about consul: https://developer.hashicorp.com/consul/download
The solution proposed here, "SwizlyPeasy.Gateway", is for now a PoC (Proof of Concept), but who knows, the results are promising so far, maybe I will be able to propose something more robust later on.
Requirements
- The user must be able to authenticate using OpenID Connect.
- Two convenience endpoints for login/logout allow the user's redirection if the action was carried out correctly.
- Claims are transmitted to the microservices as headers. The microservices use the headers information for user authorization.
- The microservices are registered to consul. The gateway retrieve their addresses by using the service names.
- The cluster configuration is populated automatically, using service data retrieved from consul.
- The routes configuration is stored in consul KV store
Setup
The setup is straight forward
- Use the provided extension methods in
program.cs
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSwizlyPeasyGateway(builder.Configuration);
var app = builder.Build();
app.UseSwizlyPeasyGateway();
app.Run();
- Provide a
routes.config.jsonfile (please have a look at the SwizlyPeasy.Gateway.API demo project). The syntax is the same as YARP configuration for routes.
{
"Routes": {
"route1": {
"ClusterId": "DemoAPI",
"AuthorizationPolicy": "oidc",
"Match": {
"Path": "/api/v1/demo/weather"
},
"Transforms": [
{
"RequestHeader": "Accept-Language",
"Set": "de-CH"
}
]
},
"route2": {
"ClusterId": "DemoAPI",
"AuthorizationPolicy": "oidc",
"Match": {
"Path": "/api/v1/demo/weather-with-authorization"
},
"Transforms": [
{
"RequestHeader": "Accept-Language",
"Set": "de-CH"
}
]
}
}
}
- Define your configuration in appsettings
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AuthRedirectionConfig": {
"MainUrl": "/",
"IdpLogoutUrl": "https://demo.duendesoftware.com/Account/Logout/LoggedOut"
},
"OidcConfig": {
"RefreshThresholdMinutes": 1,
"Origins": [],
"Authority": "https://demo.duendesoftware.com/",
"CallbackUri": "/signin-oidc",
"ClientId": "interactive.confidential.short",
"ClientSecret": "secret",
"RedirectUri": "",
"Scopes": [ "openid", "profile", "email", "offline_access" ],
"DisableOidc": true
},
"ServiceDiscovery": {
"Scheme": "http",
"RefreshIntervalInSeconds": 20,
"LoadBalancingPolicy": "Random",
"KeyValueStoreKey": "SwizlyPeasy.Gateway",
"ServiceDiscoveryAddress": "http://localhost:8500"
},
"ClaimsConfig": {
"ClaimsHeaderPrefix": "SWIZLY-PEASY",
"ClaimsAsHeaders": [
"sub",
"email",
"name",
"family_name"
],
"JwtToIdentityClaimsMappings": {
"sub": "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier",
"email": "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress"
}
},
"RateLimiterPolicies": [
{
"PolicyName": "swizly1",
"RateLimiterType": "FixedWindowRateLimiter",
"AutoReplenishment": true,
"PermitLimit": 5,
"QueueLimit": 0,
"QueueProcessingOrder": 1,
"Window": 12
},
{
"PolicyName": "swizly2",
"RateLimiterType": "SlidingWindowRateLimiter",
"AutoReplenishment": true,
"PermitLimit": 5,
"QueueLimit": 0,
"QueueProcessingOrder": 1,
"Window": 12,
"SegmentsPerWindow": 3
},
{
"PolicyName": "swizly3",
"RateLimiterType": "ConcurrencyLimiter",
"PermitLimit": 5,
"QueueLimit": 0,
"QueueProcessingOrder": 1
},
{
"PolicyName": "swizly4",
"RateLimiterType": "TokenBucketRateLimiter",
"AutoReplenishment": true,
"QueueLimit": 0,
"QueueProcessingOrder": 1,
"ReplenishmentPeriod": 60,
"TokenLimit": 20,
"TokensPerPeriod": 10
}
]
}
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- Microsoft.AspNetCore.Mvc.NewtonsoftJson (>= 10.0.12)
- Microsoft.VisualStudio.Azure.Containers.Tools.Targets (>= 1.23.0)
- NLog.Web.AspNetCore (>= 6.2.0)
- SwizlyPeasy.Common (>= 0.2.1-beta)
- SwizlyPeasy.Consul (>= 0.2.1-beta)
- Yarp.ReverseProxy (>= 2.3.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 0.2.1-beta | 52 | 9/11/2026 |
| 0.1.5-beta | 142 | 4/21/2024 |
| 0.1.1-beta | 111 | 10/11/2023 |
| 0.1.0-beta | 130 | 9/16/2023 |
| 0.0.8-alpha | 166 | 8/9/2023 |
| 0.0.7-alpha | 166 | 8/9/2023 |
| 0.0.6-alpha | 184 | 7/24/2023 |
| 0.0.5-alpha | 184 | 7/18/2023 |
| 0.0.4-alpha | 184 | 7/14/2023 |