【问题标题】:Extracting Identity from lambda authorizer response从 lambda 授权方响应中提取身份
【发布时间】:2019-10-07 14:20:44
【问题描述】:

我创建了一个自定义 lambda 授权器,用于验证 JWT 并返回 Allow 策略。

 var context = new APIGatewayCustomAuthorizerContextOutput();

 var tokenUse = ExtractClaims(claims, "token_use");
 context["tokenType"] = tokenUse;
 var response = new APIGatewayCustomAuthorizerResponse
 {
     PrincipalID = "asd",
     PolicyDocument = new APIGatewayCustomAuthorizerPolicy
     {
         Version = "2012-10-17",
         Statement = new List<APIGatewayCustomAuthorizerPolicy.IAMPolicyStatement>()
         {
             new APIGatewayCustomAuthorizerPolicy.IAMPolicyStatement
             {
                 Action = new HashSet<string>() {"execute-api:Invoke"},
                 Effect = "Allow",
                 Resource = new HashSet<string>() {"***"} // resource arn here
              }
          },
      },
      Context = context
 };

 return response;

现在我需要在我的资源服务器上使用这个身份。

问题是我在授权人上下文中的声明直接出现在authorizer

 "authorizer": {
            "cognito:groups": "Admin", ...
 }

但我的Amazon.Lambda.AspNetCoreServer.APIGatewayProxyFunction 期望那些在authorizer.claims 下的人。

像这样:

 "authorizer": {
            "claims": {
                "cognito:groups": "Admin", ... 
             }
  }

我知道这一点,因为当我使用内置的 Cognito 用户池授权器时它正在工作,它会像这样进行输入。

我设法发现不允许 Lambda Authorizer 将嵌套对象添加到上下文中(并测试如果我这样做它会抛出 authorizer error。)

我还发现APIGatewayProxyFunction 在提取身份时,它会查看 Authorizer.Claims。

所以我需要绕过Claims 属性在我的资源服务器上提取它们,或者将嵌套对象添加到授权方响应中,这是不允许的。

做什么?

【问题讨论】:

    标签: asp.net-core asp.net-identity amazon-cognito aws-sdk-net lambda-authorizer


    【解决方案1】:

    所以我通过覆盖我的LambdaEntryPoint : Amazon.Lambda.AspNetCoreServer.APIGatewayProxyFunction 上的PostCreateContext 方法解决了这个问题。

    protected override void PostCreateContext(
            HostingApplication.Context context,
            APIGatewayProxyRequest apiGatewayRequest, ILambdaContext lambdaContext)
    {
            // handling output from cognito user pool authorizer
            if (apiGatewayRequest?.RequestContext?.Authorizer?.Claims != null)
            {
                var identity = new ClaimsIdentity(apiGatewayRequest.RequestContext.Authorizer.Claims.Select(
                    entry => new Claim(entry.Key, entry.Value.ToString())), "AuthorizerIdentity");
    
                context.HttpContext.User = new ClaimsPrincipal(identity);
                return;
            }
    
            // handling output from lambda authorizer
            if (apiGatewayRequest?.RequestContext?.Authorizer != null)
            {
                var identity = new ClaimsIdentity(apiGatewayRequest.RequestContext.Authorizer.Select(
                    entry => new Claim(entry.Key, entry.Value.ToString())), "AuthorizerIdentity");
    
                context.HttpContext.User = new ClaimsPrincipal(identity);
            }
        }
    

    编辑:还向 aws-lambda-dotnet 库提交了拉取请求以解决此问题。

    编辑 2:我的拉取请求已经合并了一段时间,如果使用最新的 Amazon.Lambda.AspNetCoreServer,这不再是问题(不确定哪个版本首先拥有它,但 3.1.0 肯定有)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-16
      • 2019-08-02
      • 2017-10-17
      • 2012-11-12
      • 2023-01-12
      • 2022-08-24
      • 2021-05-16
      • 2017-11-12
      相关资源
      最近更新 更多