【问题标题】:Custom authorizer data with Amazon.Lambda.AspNetCoreServer使用 Amazon.Lambda.AspNetCoreServer 自定义授权方数据
【发布时间】:2017-08-29 12:42:00
【问题描述】:

我们过去曾与 Node.js 进行过广泛合作,目前正在研究 ASP.NET Core 作为替代 Lambda 平台。

过去,我们面向 API 网关的服务依赖于自定义授权方,该授权方对用户进行身份验证并从我们公司的 IAM 服务中检索基于资源的权限策略列表。授权者将该列表附加到 authContext 键。我们的服务将通过 Lambda 代理与 API Gateway 集成,并从原始代理请求中提取主体对象。

当使用 Amazon.Lambda.AspNetCoreServer 在 API Gateway 和 ASP.NET 之间进行转换时,我们无法达到类似的场景。

Amazon.Lambda.AspNetCoreServer::ApiGatewayProxyFunction::FunctionHandlerAsync(Stream responseStream, ILambdaContext lambdaContext) 或任何等效的 Lambda 处理程序签名在第一个参数中接收完整的原始请求。 可以序列化流(例如,序列化为 JSON.NET JObject)并在那里提取主体对象。

然而,真正困难的是在 ASP.NET 应用程序中访问该数据。我不相信授权方响应已传递给 HTTP 上下文。检查时,ClaimsPrincipal context.User 键不包含任何数据。

提出了几种解决方案:

  • 在覆盖的 FunctionHandlerAsync 中检索 IAM 信息并使用环境变量或会话全局存储它们
  • 创建 IAM 提供程序服务的接口和补充实现。它将公开一种检索 IAM 信息的方法。该实现将简单地返回一个反序列化的声明列表。该服务将在重写的 Init(IWebHostBuilder) 方法中进行配置。
  • 将(Claims/General)Principal 对象粘合在一起并尝试将其传递给 HTTP 上下文

有没有办法干净利落地实现这一目标?

【问题讨论】:

    标签: c# amazon-web-services asp.net-core aws-lambda


    【解决方案1】:

    我们的情况完全相同,我无法提供一个好的和干净的解决方案,但我有一个解决方法。

    如果查看请求负载,json 格式如下:

    {
        [...]
        "requestContext": {
            [...]
            "authorizer": {
                "claims": {
                    "claim1": "value1",
                    "claim2": "value2",
                    "claim3": "value3",
                }
            },
            [...]
    

    APIGatewayProxyFunction.FunctionHandlerAsync 中,他们将requestStream 反序列化为APIGatewayProxyRequest。如果您进入该类,您会发现 json 的 Authorizer 部分被反序列化为:

    public class APIGatewayCustomAuthorizerContext
    {
        public string PrincipalId { get; set; }
        public string StringKey { get; set; }
        public int? NumKey { get; set; }
        public bool? BoolKey { get; set; }
    }
    

    即所有声明都在反序列化中丢失。我在这里发布了这个问题:https://github.com/aws/aws-lambda-dotnet/issues/98

    现在是解决方法,我刚刚将一些“有效”的东西放在一起here(代码here):

    请注意,它未经测试。 :-)

    用法:

    public class LambdaEntryPoint : APIGatewayAuthorizerProxyFunction
    {
        protected override void Init(IWebHostBuilder builder)
        {
            builder
                .UseContentRoot(Directory.GetCurrentDirectory())
                .UseStartup<Startup>()
                .UseApiGateway();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-11-17
      • 2017-12-13
      • 1970-01-01
      • 2022-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多