Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(open-api): api signature is passed from the request header #1034

Merged
merged 1 commit into from
Nov 22, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,18 @@ protected async virtual Task<bool> ValidateClientIpAddress(HttpContext httpConte

protected async virtual Task<bool> ValidatAppDescriptor(HttpContext httpContext)
{
httpContext.Request.Headers.TryGetValue(AbpOpenApiConsts.AppKeyFieldName, out var appKey);
httpContext.Request.Headers.TryGetValue(AbpOpenApiConsts.SignatureFieldName, out var sign);
httpContext.Request.Headers.TryGetValue(AbpOpenApiConsts.NonceFieldName, out var nonce);
httpContext.Request.Headers.TryGetValue(AbpOpenApiConsts.TimeStampFieldName, out var timeStampString);
httpContext.Request.Headers.TryGetValue(AbpOpenApiConsts.HEADER_APP_KEY, out var appKey);
httpContext.Request.Headers.TryGetValue(AbpOpenApiConsts.HEADER_SIGNATURE, out var sign);
httpContext.Request.Headers.TryGetValue(AbpOpenApiConsts.HEADER_NONCE, out var nonce);
httpContext.Request.Headers.TryGetValue(AbpOpenApiConsts.HEADER_TIMESTAMP, out var timeStampString);


if (StringValues.IsNullOrEmpty(appKey))
{
var exception = new BusinessException(
AbpOpenApiConsts.InvalidAccessWithAppKeyNotFound,
$"{AbpOpenApiConsts.AppKeyFieldName} Not Found",
$"{AbpOpenApiConsts.AppKeyFieldName} Not Found");
$"{AbpOpenApiConsts.HEADER_APP_KEY} Not Found",
$"{AbpOpenApiConsts.HEADER_APP_KEY} Not Found");
await Unauthorized(httpContext, exception);
return false;
}
Expand All @@ -104,8 +104,8 @@ protected async virtual Task<bool> ValidatAppDescriptor(HttpContext httpContext)
{
var exception = new BusinessException(
AbpOpenApiConsts.InvalidAccessWithNonceNotFound,
$"{AbpOpenApiConsts.NonceFieldName} Not Found",
$"{AbpOpenApiConsts.NonceFieldName} Not Found");
$"{AbpOpenApiConsts.HEADER_NONCE} Not Found",
$"{AbpOpenApiConsts.HEADER_NONCE} Not Found");

await Unauthorized(httpContext, exception);
return false;
Expand All @@ -115,8 +115,8 @@ protected async virtual Task<bool> ValidatAppDescriptor(HttpContext httpContext)
{
var exception = new BusinessException(
AbpOpenApiConsts.InvalidAccessWithSignNotFound,
$"{AbpOpenApiConsts.SignatureFieldName} Not Found",
$"{AbpOpenApiConsts.SignatureFieldName} Not Found");
$"{AbpOpenApiConsts.HEADER_SIGNATURE} Not Found",
$"{AbpOpenApiConsts.HEADER_SIGNATURE} Not Found");

await Unauthorized(httpContext, exception);
return false;
Expand All @@ -126,8 +126,8 @@ protected async virtual Task<bool> ValidatAppDescriptor(HttpContext httpContext)
{
var exception = new BusinessException(
AbpOpenApiConsts.InvalidAccessWithTimestampNotFound,
$"{AbpOpenApiConsts.TimeStampFieldName} Not Found",
$"{AbpOpenApiConsts.TimeStampFieldName} Not Found");
$"{AbpOpenApiConsts.HEADER_TIMESTAMP} Not Found",
$"{AbpOpenApiConsts.HEADER_TIMESTAMP} Not Found");

await Unauthorized(httpContext, exception);
return false;
Expand Down Expand Up @@ -264,7 +264,8 @@ protected async virtual Task Unauthorized(HttpContext context, Exception excepti
private static string CalculationSignature(string url, IDictionary<string, string> queryDictionary)
{
var queryString = BuildQuery(queryDictionary);
var encodeUrl = UrlEncode(string.Concat(url, "?", queryString));
// %20 替换 +
var encodeUrl = UrlEncode(string.Concat(url, "?", queryString)).Replace("+", "%20");

return encodeUrl.ToMd5();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ public static class AbpOpenApiConsts
{
public const string SecurityChecking = "_AbpOpenApiSecurityChecking";

public const string AppKeyFieldName = "X-API-APPKEY";
public const string SignatureFieldName = "X-API-SIGN";
public const string NonceFieldName = "X-API-NONCE";
public const string TimeStampFieldName = "X-API-TIMESTAMP";
public const string HEADER_APP_KEY = "X-API-APPKEY";
public const string HEADER_SIGNATURE = "X-API-SIGN";
public const string HEADER_NONCE = "X-API-NONCE";
public const string HEADER_TIMESTAMP = "X-API-TIMESTAMP";

public const string KeyPrefix = "AbpOpenApi";
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
global using LINGYUN.MicroService.OpenApi.Gateway;
global using Microsoft.AspNetCore.Builder;
global using Microsoft.AspNetCore.Hosting;
global using Microsoft.Extensions.DependencyInjection;
global using Microsoft.Extensions.Hosting;
global using Serilog;
global using System;
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>LINGYUN.MicroService.Internal.Gateway</RootNamespace>
<Language>latest</Language>
<ImplicitUsings >enable</ImplicitUsings >
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,12 @@
using LINGYUN.Abp.Serilog.Enrichers.UniqueId;
using LINGYUN.Abp.Wrapper;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.AspNetCore.WebSockets;
using Microsoft.Extensions.Caching.StackExchangeRedis;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Models;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using Volo.Abp;
using Volo.Abp.AspNetCore.Mvc;
using Volo.Abp.AspNetCore.Mvc.ApiExploring;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,59 +1,42 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Serilog;
using System;
using System.Threading.Tasks;

namespace LINGYUN.MicroService.OpenApi.Gateway;

public class Program
try
{
public static async Task<int> Main(string[] args)
{
try
Log.Information("Starting OpenApi ApiGateway.");
var builder = WebApplication.CreateBuilder(args);
builder.Host.AddAppSettingsSecretsJson()
.UseAutofac()
.AddYarpJson()
.ConfigureAppConfiguration((context, config) =>
{
Log.Information("Starting OpenApi ApiGateway.");
var builder = WebApplication.CreateBuilder(args);
builder.Host.AddAppSettingsSecretsJson()
.UseAutofac()
.AddYarpJson()
.ConfigureAppConfiguration((context, config) =>
{
var configuration = config.Build();
var agileConfigEnabled = configuration["AgileConfig:IsEnabled"];
if (agileConfigEnabled.IsNullOrEmpty() || bool.Parse(agileConfigEnabled))
{
config.AddAgileConfig(new AgileConfig.Client.ConfigClient(configuration));
}
})
.UseSerilog((context, provider, config) =>
{
config.ReadFrom.Configuration(context.Configuration);
});

await builder.AddApplicationAsync<OpenApiGatewayModule>(options =>
var configuration = config.Build();
var agileConfigEnabled = configuration["AgileConfig:IsEnabled"];
if (agileConfigEnabled.IsNullOrEmpty() || bool.Parse(agileConfigEnabled))
{
OpenApiGatewayModule.ApplicationName = Environment.GetEnvironmentVariable("APPLICATION_NAME")
?? OpenApiGatewayModule.ApplicationName;
options.ApplicationName = OpenApiGatewayModule.ApplicationName;
});
var app = builder.Build();
await app.InitializeApplicationAsync();
await app.RunAsync();

return 0;
}
catch (Exception ex)
{
Log.Fatal(ex, "Starting OpenApi ApiGateway terminated unexpectedly!");
return 1;
}
finally
config.AddAgileConfig(new AgileConfig.Client.ConfigClient(configuration));
}
})
.UseSerilog((context, provider, config) =>
{
Log.CloseAndFlush();
}
}
config.ReadFrom.Configuration(context.Configuration);
});

await builder.AddApplicationAsync<OpenApiGatewayModule>(options =>
{
OpenApiGatewayModule.ApplicationName = Environment.GetEnvironmentVariable("APPLICATION_NAME")
?? OpenApiGatewayModule.ApplicationName;
options.ApplicationName = OpenApiGatewayModule.ApplicationName;
});
var app = builder.Build();
await app.InitializeApplicationAsync();
await app.RunAsync();

return 0;
}
catch (Exception ex)
{
Log.Fatal(ex, "Starting OpenApi ApiGateway terminated unexpectedly!");
return 1;
}
finally
{
Log.CloseAndFlush();
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
"launchBrowser": false,
"dotnetRunMessages": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
"ASPNETCORE_ENVIRONMENT": "Production"
},
"applicationUrl": "http://localhost:30000"
"applicationUrl": "http://0.0.0.0:30000"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"tag": "BackendAdmin"
},
"App": {
"CorsOrigins": "http://127.0.0.1:3100",
"CorsOrigins": "http://127.0.0.1:3100,http://localhost:9010",
"ShowPii": true
},
"ConnectionStrings": {
Expand Down
Loading
Loading