Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
611808787e | ||
|
|
8ee297e819 | ||
|
|
bc4195e984 | ||
|
|
4a0caa1079 |
@@ -96,10 +96,10 @@ public class SimApiAuthGate(SimApiAuthGateClient simapi)
|
|||||||
/// <param name="data"></param>
|
/// <param name="data"></param>
|
||||||
/// <param name="backUrl"></param>
|
/// <param name="backUrl"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public SimApiAuthGateDto.GetCodeResponse GetAuthCode(string? scene = null, Dictionary<string, object>? data = null,
|
public SimApiAuthGateDto.GetCodeResponse GetLoginCode(string? scene = null, Dictionary<string, object>? data = null,
|
||||||
string? backUrl = null)
|
string? backUrl = null)
|
||||||
{
|
{
|
||||||
var code = simapi.SignQuery<string>("/api/auth/confirm/code",
|
var code = simapi.SignQuery<string>("/api/auth/login/code",
|
||||||
new { scene, data, backUrl });
|
new { scene, data, backUrl });
|
||||||
return new SimApiAuthGateDto.GetCodeResponse()
|
return new SimApiAuthGateDto.GetCodeResponse()
|
||||||
{
|
{
|
||||||
@@ -115,9 +115,9 @@ public class SimApiAuthGate(SimApiAuthGateClient simapi)
|
|||||||
/// <param name="code"></param>
|
/// <param name="code"></param>
|
||||||
/// <param name="scene"></param>
|
/// <param name="scene"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public SimApiAuthGateDto.AuthInfoResponse GetAuthInfo(string code, string? scene = null)
|
public SimApiAuthGateDto.LoginInfoResponse GetLoginInfo(string code, string? scene = null)
|
||||||
{
|
{
|
||||||
var resp = simapi.SignQuery<SimApiAuthGateDto.AuthInfoResponse>("/api/auth/confirm/get", new { code });
|
var resp = simapi.SignQuery<SimApiAuthGateDto.LoginInfoResponse>("/api/auth/login/get", new { code });
|
||||||
ErrorWhenNull(resp, 400232, "登录信息获取失败");
|
ErrorWhenNull(resp, 400232, "登录信息获取失败");
|
||||||
ErrorWhen(resp.Scene != scene, 403003, "登录场景不匹配");
|
ErrorWhen(resp.Scene != scene, 403003, "登录场景不匹配");
|
||||||
return resp;
|
return resp;
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
using SimApi.Configurations;
|
using Microsoft.Extensions.Logging;
|
||||||
|
using SimApi.Configurations;
|
||||||
using SimApi.Helpers;
|
using SimApi.Helpers;
|
||||||
|
|
||||||
namespace SimApi.AuthGate;
|
namespace SimApi.AuthGate;
|
||||||
|
|
||||||
public class SimApiAuthGateClient(SimApiOptions apiOptions) : SimApiHttpClient
|
public class SimApiAuthGateClient(SimApiOptions apiOptions, ILogger<SimApiAuthGateClient> logger)
|
||||||
|
: SimApiHttpClient(apiOptions, logger)
|
||||||
{
|
{
|
||||||
public override string Server { get; init; } = apiOptions.SimApiAuthGateOptions.Server ?? string.Empty;
|
public override string Server { get; init; } = apiOptions.SimApiAuthGateOptions.Server ?? string.Empty;
|
||||||
public override string AppId { get; init; } = apiOptions.SimApiAuthGateOptions.AppId ?? string.Empty;
|
public override string AppId { get; init; } = apiOptions.SimApiAuthGateOptions.AppId ?? string.Empty;
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ public class SimApiAuthGateDto
|
|||||||
public Dictionary<string, object>? Data { get; set; }
|
public Dictionary<string, object>? Data { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public class AuthInfoResponse
|
public class LoginInfoResponse
|
||||||
{
|
{
|
||||||
public string? Scene { get; set; }
|
public string? Scene { get; set; }
|
||||||
public Dictionary<string, object>? Data { get; set; }
|
public Dictionary<string, object>? Data { get; set; }
|
||||||
|
|||||||
@@ -11,4 +11,5 @@ public class SimApiAuthGateOptions
|
|||||||
/// 注意: 只有内部应用需要开启这个,也就是api通过内部网关代理后
|
/// 注意: 只有内部应用需要开启这个,也就是api通过内部网关代理后
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool UseMiddleware { get; set; }
|
public bool UseMiddleware { get; set; }
|
||||||
|
public bool UseIam { get; set; }
|
||||||
}
|
}
|
||||||
@@ -56,7 +56,6 @@ public class SimApiOptions
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public bool EnableSimApiResponseFilter { get; set; } = true;
|
public bool EnableSimApiResponseFilter { get; set; } = true;
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 开启ForwardHeaders,开启后可以透传负载均衡的Headers
|
/// 开启ForwardHeaders,开启后可以透传负载均衡的Headers
|
||||||
/// default: true
|
/// default: true
|
||||||
@@ -69,24 +68,17 @@ public class SimApiOptions
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public bool EnableLowerUrl { get; set; } = true;
|
public bool EnableLowerUrl { get; set; } = true;
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 应用可以通过 /versions 显示出应用版本和SimApi包版本
|
|
||||||
/// default: true
|
|
||||||
/// </summary>
|
|
||||||
public bool EnableVersionUrl { get; set; } = true;
|
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 启用格式化的 Console Logger
|
/// 启用格式化的 Console Logger
|
||||||
/// default: false
|
/// default: false
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool EnableLogger { get; set; } = true;
|
public bool EnableLogger { get; set; } = true;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 是否启用SimApiHttpClient
|
||||||
|
/// </summary>
|
||||||
public bool EnableSimApiHttpClient { get; set; } = false;
|
public bool EnableSimApiHttpClient { get; set; } = false;
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 配置Job
|
/// 配置Job
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -110,6 +102,13 @@ public class SimApiOptions
|
|||||||
|
|
||||||
public SimApiExceptionOptions SimApiExceptionOptions { get; set; } = new();
|
public SimApiExceptionOptions SimApiExceptionOptions { get; set; } = new();
|
||||||
|
|
||||||
|
public SimApiRouteOptions SimApiRouteOptions { get; set; } = new();
|
||||||
|
|
||||||
|
public void ConfigureSimApiRoute(Action<SimApiRouteOptions>? options = null)
|
||||||
|
{
|
||||||
|
options?.Invoke(SimApiRouteOptions);
|
||||||
|
}
|
||||||
|
|
||||||
public void ConfigureSimApiException(Action<SimApiExceptionOptions>? options = null)
|
public void ConfigureSimApiException(Action<SimApiExceptionOptions>? options = null)
|
||||||
{
|
{
|
||||||
options?.Invoke(SimApiExceptionOptions);
|
options?.Invoke(SimApiExceptionOptions);
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
namespace SimApi.Configurations;
|
||||||
|
|
||||||
|
public class SimApiRouteOptions
|
||||||
|
{
|
||||||
|
public string? VersionRoute = "/versions";
|
||||||
|
public string? LogoutRoute = "/auth/logout";
|
||||||
|
public string? UserInfoRoute = "/user/info";
|
||||||
|
}
|
||||||
@@ -5,8 +5,6 @@ using SimApi.Helpers;
|
|||||||
|
|
||||||
namespace SimApi.Controllers;
|
namespace SimApi.Controllers;
|
||||||
|
|
||||||
using static SimApiError;
|
|
||||||
|
|
||||||
public class SimApiAuthController(SimApiAuth auth) : SimApiBaseController
|
public class SimApiAuthController(SimApiAuth auth) : SimApiBaseController
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -22,4 +20,10 @@ public class SimApiAuthController(SimApiAuth auth) : SimApiBaseController
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取已登录用户信息
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
[HttpPost, SimApiAuth, SimApiDoc("认证", "获取已登录用户信息")]
|
||||||
|
public SimApiLoginItem UserInfo() => LoginInfo;
|
||||||
}
|
}
|
||||||
@@ -31,11 +31,4 @@ public class SimApiCommonController : SimApiBaseController
|
|||||||
{ "App", SimApiUtil.AppVersion }
|
{ "App", SimApiUtil.AppVersion }
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 获取已登录用户信息
|
|
||||||
/// </summary>
|
|
||||||
/// <returns></returns>
|
|
||||||
[HttpPost("/user/info"), SimApiAuth, SimApiDoc("认证", "获取已登录用户信息")]
|
|
||||||
public SimApiLoginItem UserInfo() => LoginInfo;
|
|
||||||
}
|
}
|
||||||
+30
-9
@@ -333,7 +333,10 @@ public static class SimApiExtensions
|
|||||||
{
|
{
|
||||||
builder.AddSingleton<SimApiAuthGateClient>();
|
builder.AddSingleton<SimApiAuthGateClient>();
|
||||||
builder.AddSingleton<SimApiAuthGate>();
|
builder.AddSingleton<SimApiAuthGate>();
|
||||||
builder.AddSingleton<SimApiIam>();
|
if (simApiOptions.SimApiAuthGateOptions.UseIam)
|
||||||
|
{
|
||||||
|
builder.AddSingleton<SimApiIam>();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
builder.AddSingleton(simApiOptions);
|
builder.AddSingleton(simApiOptions);
|
||||||
@@ -440,17 +443,13 @@ public static class SimApiExtensions
|
|||||||
{
|
{
|
||||||
logger.LogInformation("开始配置SimApiAuth...");
|
logger.LogInformation("开始配置SimApiAuth...");
|
||||||
builder.UseMiddleware<SimApiAuthMiddleware>();
|
builder.UseMiddleware<SimApiAuthMiddleware>();
|
||||||
builder.MapControllerRoute(name: "Logout", pattern: "/auth/logout",
|
|
||||||
defaults: new
|
|
||||||
{
|
|
||||||
controller = "SimApiAuth",
|
|
||||||
action = "Logout"
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options.EnableVersionUrl)
|
|
||||||
|
if (options.SimApiRouteOptions.VersionRoute != null)
|
||||||
{
|
{
|
||||||
builder.MapControllerRoute(name: "Versions", pattern: "/versions",
|
logger.LogInformation("注册内置Route: Versions => {}", options.SimApiRouteOptions.LogoutRoute);
|
||||||
|
builder.MapControllerRoute(name: "Versions", pattern: options.SimApiRouteOptions.VersionRoute,
|
||||||
defaults: new
|
defaults: new
|
||||||
{
|
{
|
||||||
controller = "SimApiCommon",
|
controller = "SimApiCommon",
|
||||||
@@ -458,6 +457,28 @@ public static class SimApiExtensions
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (options.SimApiRouteOptions.UserInfoRoute != null)
|
||||||
|
{
|
||||||
|
logger.LogInformation("注册内置Route: UserInfo => {}", options.SimApiRouteOptions.UserInfoRoute);
|
||||||
|
builder.MapControllerRoute(name: "UserInfo", pattern: options.SimApiRouteOptions.UserInfoRoute,
|
||||||
|
defaults: new
|
||||||
|
{
|
||||||
|
controller = "SimApiAuth",
|
||||||
|
action = "UserInfo"
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (options.SimApiRouteOptions.LogoutRoute != null)
|
||||||
|
{
|
||||||
|
logger.LogInformation("注册内置Route: Logout => {}", options.SimApiRouteOptions.LogoutRoute);
|
||||||
|
builder.MapControllerRoute(name: "Logout", pattern: options.SimApiRouteOptions.LogoutRoute,
|
||||||
|
defaults: new
|
||||||
|
{
|
||||||
|
controller = "SimApiAuth",
|
||||||
|
action = "Logout"
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
if (options.EnableSimApiDoc)
|
if (options.EnableSimApiDoc)
|
||||||
{
|
{
|
||||||
logger.LogInformation("开始配置SimApiDoc...");
|
logger.LogInformation("开始配置SimApiDoc...");
|
||||||
|
|||||||
Reference in New Issue
Block a user