【问题标题】:Idenity Server 4 Injecting additional claims in IAuthorizeInteractionResponseGeneratorIdentity Server 4 在 AuthorizeInteractionResponseGenerator 中注入额外的声明
【发布时间】:2020-07-29 15:32:15
【问题描述】:

我正在尝试从 IdentityServer3 移植代码,该代码使用 PreAuthenticate 为我们的管理员提供临时模拟用户到客户端应用程序。

here 中的线程之后,我将在自定义 IAuthorizeInteractionResponseGenerator 中覆盖 ProcessInteractionAsync。这很好用,我一切正常,但是,我无法添加额外的声明以发送回客户端应用程序,以便它知道这是一个模拟的 id_token。在我的覆盖中替换 ValidatedAuthorizeRequest 上的主题时,我添加了一个附加声明,该声明指定了开始模拟的用户,但此声明在 id_token 或访问令牌中没有遵循。这是我的覆盖:

public override async Task<InteractionResponse> ProcessInteractionAsync(ValidatedAuthorizeRequest request, ConsentResponse consent = null)
{
    string impersonatedUserName = request.GetPrefixedAcrValue("Impersonate:");
    if (!string.IsNullOrWhiteSpace(impersonatedUserName))
    {
        if (request.Client.AllowedScopes.Contains(Constants.ClaimTypes.Impersonation))
        {
            var currentUser;
            var impersonatedUser;

            //Omited code to verify eligibility to impersonate user

            if (impersonatedUser != null)
            {
                IEnumerable<string> requestedClaimTypes = request.Client.AllowedScopes;

                IdentityServerUser idSrvUser = new IdentityServerUser(impersonatedUser.Id.ToString())
                {
                    AuthenticationTime = Clock.UtcNow.UtcDateTime,
                    DisplayName = impersonatedUser.UserName,
                    IdentityProvider = !string.IsNullOrEmpty(impersonatedUser.PasswordHash) ? IdentityServerConstants.LocalIdentityProvider : "external"
                };

                ProfileDataRequestContext context = new ProfileDataRequestContext(
                    idSrvUser.CreatePrincipal(),
                    request.Client,
                    nameof(AuthorizeInteractionResponseGenerator),
                    requestedClaimTypes);

                await Profile.GetProfileDataAsync(context);

                //Need this claim to flow through to client
                context.IssuedClaims.Add(new Claim(Constants.ClaimTypes.Impersonation, currentUser.UserName));

                foreach (Claim claim in context.IssuedClaims)
                {
                    idSrvUser.AdditionalClaims.Add(claim);
                }

                ClaimsPrincipal newSubject = idSrvUser.CreatePrincipal();

                request.Subject = newSubject;

                Logger.LogInformation("Impersonation set, returning response");

                return new InteractionResponse();
            }
            else
            {
                Logger.LogWarning("Invalid attempt to impersonate user");
                return new InteractionResponse { Error = "Invalid attempt to impersonate user" };
            }
        }
        else
        {
            Logger.LogWarning("Client does not support impersonation!");
            return new InteractionResponse { Error = "Client does not support impersonation" };
        }
    }

    return await base.ProcessInteractionAsync(request, consent);
}

我为客户请求的这个特殊声明添加了一个范围,但它仍然没有被包括在内。我觉得我在这里遗漏了一些明显的东西,如何为其中一个令牌添加额外的声明?或者有没有更好的方法来向开始模拟的客户发出信号?

【问题讨论】:

    标签: openid identityserver4 openid-connect


    【解决方案1】:

    所以我最终采取了与 Identity Server 3 中略有不同的方法来解决这个要求。我没有在 Identity Server 中附加用户名,而是将在客户端开始模拟的用户的用户名存储在临时 cookie 中。然后在 AuthorizationCodeReceived 通知中,我检查 cookie 值并将声明附加到在那里创建的主体。

    不像在身份中附加声明那样万无一失,但由于所有主要的安全检查仍然会发生,而且我想知道谁作为模拟用户在做的事情如果没有 cookie 将会失败,这将起作用。如果其他人遇到这种情况并且确实弄清楚如何在身份服务器中附加声明,请回复,我会给你答案

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-16
      • 2020-06-30
      • 2015-05-29
      • 1970-01-01
      • 2019-01-03
      • 2017-05-14
      • 2019-12-28
      • 2018-04-29
      相关资源
      最近更新 更多