【发布时间】: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