Pass HttpContext during negotiation (#183)

This commit is contained in:
yzt 2021-03-17 16:10:28 +08:00 коммит произвёл GitHub
Родитель d6bdff0bcd
Коммит e7cb9e4b08
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
3 изменённых файлов: 35 добавлений и 34 удалений

Просмотреть файл

@ -34,38 +34,33 @@ namespace Microsoft.Azure.WebJobs.Extensions.SignalRService
{
var azureSignalRClient = Utils.GetAzureSignalRClient(attrResolved.ConnectionStringSetting, attrResolved.HubName);
if (!bindingData.ContainsKey(HttpRequestName) || securityTokenValidator == null)
bindingData.TryGetValue(HttpRequestName, out var requestObj);
var request = requestObj as HttpRequest;
var httpContext = request?.HttpContext;
if (bindingData.ContainsKey(HttpRequestName) && securityTokenValidator != null)
{
var info = await azureSignalRClient.GetClientConnectionInfoAsync(attrResolved.UserId, attrResolved.IdToken,
attrResolved.ClaimTypeList);
return new SignalRConnectionInfoValueProvider(info, userType, "");
var tokenResult = securityTokenValidator.ValidateToken(request);
if (tokenResult.Status != SecurityTokenStatus.Valid)
{
return new SignalRConnectionInfoValueProvider(null, userType, "");
}
var signalRConnectionDetail = new SignalRConnectionDetail
{
UserId = attrResolved.UserId,
Claims = azureSignalRClient.GetCustomClaims(attrResolved.IdToken, attrResolved.ClaimTypeList),
};
signalRConnectionInfoConfigurer?.Configure(tokenResult, request, signalRConnectionDetail);
var customizedInfo = await azureSignalRClient.GetClientConnectionInfoAsync(signalRConnectionDetail.UserId,
signalRConnectionDetail.Claims, httpContext);
return new SignalRConnectionInfoValueProvider(customizedInfo, userType, "");
}
var request = bindingData[HttpRequestName] as HttpRequest;
var tokenResult = securityTokenValidator.ValidateToken(request);
if (tokenResult.Status != SecurityTokenStatus.Valid)
{
return new SignalRConnectionInfoValueProvider(null, userType, "");
}
if (signalRConnectionInfoConfigurer == null)
{
var info = await azureSignalRClient.GetClientConnectionInfoAsync(attrResolved.UserId, attrResolved.IdToken,
attrResolved.ClaimTypeList);
return new SignalRConnectionInfoValueProvider(info, userType, "");
}
var signalRConnectionDetail = new SignalRConnectionDetail
{
UserId = attrResolved.UserId,
Claims = azureSignalRClient.GetCustomClaims(attrResolved.IdToken, attrResolved.ClaimTypeList),
};
signalRConnectionInfoConfigurer.Configure(tokenResult, request, signalRConnectionDetail);
var customizedInfo = await azureSignalRClient.GetClientConnectionInfoAsync(signalRConnectionDetail.UserId,
signalRConnectionDetail.Claims);
return new SignalRConnectionInfoValueProvider(customizedInfo, userType, "");
var info = await azureSignalRClient.GetClientConnectionInfoAsync(attrResolved.UserId, attrResolved.IdToken,
attrResolved.ClaimTypeList, httpContext);
return new SignalRConnectionInfoValueProvider(info, userType, "");
}
}
}

Просмотреть файл

@ -7,6 +7,7 @@ using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Azure.SignalR;
using Microsoft.Azure.SignalR.Management;
@ -40,16 +41,21 @@ namespace Microsoft.Azure.WebJobs.Extensions.SignalRService
this.connectionStringKey = connectionStringKey;
}
public Task<SignalRConnectionInfo> GetClientConnectionInfoAsync(string userId, string idToken, string[] claimTypeList)
public Task<SignalRConnectionInfo> GetClientConnectionInfoAsync(string userId, string idToken, string[] claimTypeList, HttpContext httpContext)
{
var customerClaims = GetCustomClaims(idToken, claimTypeList);
return GetClientConnectionInfoAsync(userId, customerClaims);
return GetClientConnectionInfoAsync(userId, customerClaims, httpContext);
}
public async Task<SignalRConnectionInfo> GetClientConnectionInfoAsync(string userId, IList<Claim> claims)
public async Task<SignalRConnectionInfo> GetClientConnectionInfoAsync(string userId, IList<Claim> claims, HttpContext httpContext)
{
var serviceHubContext = await GetHubContextAsync();
var negotiateResponse = await serviceHubContext.NegotiateAsync(new NegotiationOptions() { UserId = userId, Claims = BuildJwtClaims(claims, AzureSignalRUserPrefix).ToList() });
var negotiateResponse = await serviceHubContext.NegotiateAsync(new NegotiationOptions()
{
UserId = userId,
Claims = BuildJwtClaims(claims, AzureSignalRUserPrefix).ToList(),
HttpContext = httpContext
});
return new SignalRConnectionInfo
{
Url = negotiateResponse.Url,

Просмотреть файл

@ -39,7 +39,7 @@ namespace SignalRServiceExtension.Tests
var configuration = new ConfigurationBuilder().AddInMemoryCollection(configDict).Build();
var serviceManagerStore = new ServiceManagerStore(configuration, NullLoggerFactory.Instance, new TestRouter());
var azureSignalRClient = new AzureSignalRClient(serviceManagerStore, connectionStringKey, hubName);
var connectionInfo = await azureSignalRClient.GetClientConnectionInfoAsync(userId, idToken, claimTypeList);
var connectionInfo = await azureSignalRClient.GetClientConnectionInfoAsync(userId, idToken, claimTypeList, null);
Assert.Equal(connectionInfo.Url, $"{hubUrl}/client/?hub={hubName.ToLower()}");