SwizlyPeasy.Gateway 0.2.1-beta

This is a prerelease version of SwizlyPeasy.Gateway.
dotnet add package SwizlyPeasy.Gateway --version 0.2.1-beta
                    
NuGet\Install-Package SwizlyPeasy.Gateway -Version 0.2.1-beta
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SwizlyPeasy.Gateway" Version="0.2.1-beta" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SwizlyPeasy.Gateway" Version="0.2.1-beta" />
                    
Directory.Packages.props
<PackageReference Include="SwizlyPeasy.Gateway" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add SwizlyPeasy.Gateway --version 0.2.1-beta
                    
#r "nuget: SwizlyPeasy.Gateway, 0.2.1-beta"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package SwizlyPeasy.Gateway@0.2.1-beta
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=SwizlyPeasy.Gateway&version=0.2.1-beta&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=SwizlyPeasy.Gateway&version=0.2.1-beta&prerelease
                    
Install as a Cake Tool

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.json file (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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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