using IdentityServer4.Models;
using JSMachine.WMS.Authorization.Model;
namespace JSMachine.WMS.Authorization
{
///
/// 从授权配置中解析 IdentityServer 的身份资源、API 作用域和客户端信息。
///
public class AuthorizationConfigResolver
{
private static IConfiguration _configuration;
///
/// 设置后续解析操作使用的配置对象。
///
/// 包含 AppSettings 配置节的配置对象。
public static void SetConfig(IConfiguration configuration)
{
_configuration = configuration;
}
///
/// 获取 OpenID Connect 所需的标准身份资源。
///
public static IEnumerable IdentityResources =>
new List
{
new IdentityResources.OpenId(),
new IdentityResources.Profile()
};
///
/// 从 AppSettings:ApiScopes 读取 API 作用域配置。
///
/// 配置存在时返回 API 作用域集合,否则返回空值。
public static IEnumerable? GetApiScopes()
{
return _configuration
.GetSection("AppSettings")
.GetSection("ApiScopes")
.Get()
?.Select(p => new ApiScope(p));
}
///
/// 从 AppSettings:Clients 读取客户端配置,并转换为客户端凭据模式定义。
///
/// 配置存在时返回客户端集合,否则返回空值。
public static IEnumerable? GetClients()
{
List clients = _configuration
.GetSection("AppSettings")
.GetSection("Clients")
.Get>();
return clients?.Select(p => new Client
{
ClientId = p.ClientId,
//用于身份验证的密钥
ClientSecrets = new Secret[] { new Secret(p.Secret.Sha256()) },
//客户端有权访问的范围
AllowedScopes = p.AllowedScopes,
//过期时间(秒)
AccessTokenLifetime = p.AccessTokenLifetime,
//没有交互式用户,使用 clientid/secret 进行身份验证
AllowedGrantTypes = GrantTypes.ClientCredentials
});
}
}
}