public async Task GetClientConnectionInfo() { var hubName = "TestHub"; var hubUrl = "http://localhost"; var accessKey = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; var connectionString = $"Endpoint={hubUrl};AccessKey={accessKey};Version=1.0;"; var userId = "User"; var idToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"; var expectedName = "John Doe"; var expectedIat = "1516239022"; var claimTypeList = new string[] { "name", "iat" }; var connectionStringKey = Constants.AzureSignalRConnectionStringName; var configDict = new Dictionary <string, string>() { { Constants.ServiceTransportTypeName, "Transient" }, { connectionStringKey, connectionString } }; var configuration = new ConfigurationBuilder().AddInMemoryCollection(configDict).Build(); var serviceManagerStore = new ServiceManagerStore(configuration, NullLoggerFactory.Instance, SingletonAzureComponentFactory.Instance, new TestRouter()); var azureSignalRClient = await SignalRUtils.GetAzureSignalRClientAsync(connectionStringKey, hubName, serviceManagerStore); var connectionInfo = await azureSignalRClient.GetClientConnectionInfoAsync(userId, idToken, claimTypeList, null); Assert.Equal(connectionInfo.Url, $"{hubUrl}/client/?hub={hubName.ToLower()}"); var claims = new JwtSecurityTokenHandler().ReadJwtToken(connectionInfo.AccessToken).Claims; Assert.Equal(expectedName, GetClaimValue(claims, "name")); Assert.Equal(expectedIat, GetClaimValue(claims, $"{AzureSignalRClient.AzureSignalRUserPrefix}iat")); }
protected override async Task <IValueProvider> BuildAsync(SignalRConnectionInfoAttribute attrResolved, IReadOnlyDictionary <string, object> bindingData) { var azureSignalRClient = await Utils.GetAzureSignalRClientAsync(attrResolved.ConnectionStringSetting, attrResolved.HubName, _managerStore).ConfigureAwait(false); bindingData.TryGetValue(HttpRequestName, out var requestObj); var request = requestObj as HttpRequest; var httpContext = request?.HttpContext; if (bindingData.ContainsKey(HttpRequestName) && _securityTokenValidator != null) { 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).ConfigureAwait(false); return(new SignalRConnectionInfoValueProvider(customizedInfo, _userType, "")); } var info = await azureSignalRClient.GetClientConnectionInfoAsync(attrResolved.UserId, attrResolved.IdToken, attrResolved.ClaimTypeList, httpContext).ConfigureAwait(false); return(new SignalRConnectionInfoValueProvider(info, _userType, "")); }
public async Task ServiceEndpointsNotSet() { var rootHubContextMock = new Mock <ServiceHubContext>().As <IInternalServiceHubContext>(); var childHubContextMock = new Mock <ServiceHubContext>(); rootHubContextMock.Setup(c => c.WithEndpoints(It.IsAny <ServiceEndpoint[]>())).Returns(childHubContextMock.Object); rootHubContextMock.Setup(c => c.Clients.All.SendCoreAsync(It.IsAny <string>(), It.IsAny <object[]>(), It.IsAny <CancellationToken>())).Returns(Task.CompletedTask); var serviceManagerStore = Mock.Of <IServiceManagerStore>(s => s.GetOrAddByConnectionStringKey(It.IsAny <string>()).GetAsync(It.IsAny <string>()) == new ValueTask <IServiceHubContext>(rootHubContextMock.Object)); var azureSignalRClient = await SignalRUtils.GetAzureSignalRClientAsync("key", "hub", serviceManagerStore); var data = new SignalRData { Target = "target", Arguments = new object[] { "arg1" } }; await azureSignalRClient.SendToAll(data); rootHubContextMock.Verify(c => c.Clients.All.SendCoreAsync(data.Target, data.Arguments, default), Times.Once); childHubContextMock.Verify(c => c.Clients.All.SendCoreAsync(It.IsAny <string>(), It.IsAny <object[]>(), It.IsAny <CancellationToken>()), Times.Never); }
public async Task <IAsyncCollector <T> > ConvertAsync(SignalRAttribute input, CancellationToken cancellationToken) { var client = await Utils.GetAzureSignalRClientAsync(input.ConnectionStringSetting, input.HubName, _managerStore); return(new SignalRAsyncCollector <T>(client)); }