예제 #1
0
        public async Task GenerateClientEndpoint(string userId, Claim[] claims, string appName)
        {
            var endpoints  = FakeEndpointUtils.GetFakeEndpoint(3).ToArray();
            var routerMock = new Mock <IEndpointRouter>();

            routerMock.SetupSequence(router => router.GetNegotiateEndpoint(null, endpoints))
            .Returns(endpoints[0])
            .Returns(endpoints[1])
            .Returns(endpoints[2]);
            var router   = routerMock.Object;
            var provider = new ServiceCollection().AddSignalRServiceManager()
                           .Configure <ServiceManagerOptions>(o =>
            {
                o.ApplicationName  = appName;
                o.ServiceEndpoints = endpoints;
            })
                           .AddSingleton(router).BuildServiceProvider();
            var negotiateProcessor = provider.GetRequiredService <NegotiateProcessor>();

            for (int i = 0; i < 3; i++)
            {
                var negotiationResponse = await negotiateProcessor.NegotiateAsync(HubName, null, userId, claims, _tokenLifeTime);

                var tokenString = negotiationResponse.AccessToken;
                var token       = JwtTokenHelper.JwtHandler.ReadJwtToken(tokenString);

                string expectedToken = JwtTokenHelper.GenerateJwtBearer(ClientEndpointUtils.GetExpectedClientEndpoint(HubName, appName, endpoints[i].Endpoint), ClaimsUtility.BuildJwtClaims(null, userId, () => claims), token.ValidTo, token.ValidFrom, token.ValidFrom, endpoints[i].AccessKey);

                Assert.Equal(ClientEndpointUtils.GetExpectedClientEndpoint(HubName, appName, endpoints[i].Endpoint), negotiationResponse.Url);
                Assert.Equal(expectedToken, tokenString);
            }
        }
예제 #2
0
        public async Task Call_NegotiateAsync_After_WithEndpoints(ServiceTransportType serviceTransportType)
        {
            var serviceManager = new ServiceManagerBuilder()
                                 .WithOptions(o =>
            {
                o.ServiceTransportType = serviceTransportType;
                o.ServiceEndpoints     = ServiceEndpoints;
            })
                                 .BuildServiceManager();
            var hubContext = await serviceManager.CreateHubContextAsync(Hub, default);

            for (var i = 0; i < 5; i++)
            {
                var randomEndpoint      = ServiceEndpoints[StaticRandom.Next(0, Count)];
                var negotiationResponse = await(hubContext as IInternalServiceHubContext)
                                          .WithEndpoints(new ServiceEndpoint[] { randomEndpoint })
                                          .NegotiateAsync();

                Assert.Equal(ClientEndpointUtils.GetExpectedClientEndpoint(Hub, null, randomEndpoint.Endpoint), negotiationResponse.Url);
                var tokenString   = negotiationResponse.AccessToken;
                var token         = JwtTokenHelper.JwtHandler.ReadJwtToken(tokenString);
                var expectedToken = JwtTokenHelper.GenerateJwtBearer(
                    ClientEndpointUtils.GetExpectedClientEndpoint(Hub, null, randomEndpoint.Endpoint),
                    ClaimsUtility.BuildJwtClaims(null, null, null), token.ValidTo, token.ValidFrom, token.ValidFrom, randomEndpoint.AccessKey);
                Assert.Equal(expectedToken, tokenString);
            }
        }
        public async Task ColdStartNegotiateTest()
        {
            var hubName = "hub";
            ServiceCollection services = new ServiceCollection();

            services.AddSignalRServiceManager();

            //configure two fake service endpoints and one real endpoints.
            services.Configure <ServiceManagerOptions>(o =>
            {
                o.ConnectionString = TestConfiguration.Instance.ConnectionString;
                o.ServiceEndpoints = FakeEndpointUtils.GetFakeEndpoint(3).ToArray();
            });

            //enable test output
            services.AddSingleton <ILoggerFactory>(new LoggerFactory(new List <ILoggerProvider> {
                new XunitLoggerProvider(_outputHelper)
            }));
            var manager    = services.BuildServiceProvider().GetRequiredService <IServiceManager>();
            var hubContext = await manager.CreateHubContextAsync(hubName);

            var realEndpoint = new ServiceEndpoint(TestConfiguration.Instance.ConnectionString).Endpoint;

            //reduce the effect of randomness
            for (int i = 0; i < 5; i++)
            {
                var clientEndoint = await(hubContext as IInternalServiceHubContext).NegotiateAsync();
                var expectedUrl   = ClientEndpointUtils.GetExpectedClientEndpoint(hubName, null, realEndpoint);
                Assert.Equal(expectedUrl, clientEndoint.Url);
            }
        }
        internal void GenerateClientAccessTokenTest(string userId, Claim[] claims, string appName)
        {
            var builder = new ServiceManagerBuilder()
                          .WithOptions(o =>
            {
                o.ApplicationName  = appName;
                o.ConnectionString = _testConnectionString;
            });
            var manager     = builder.Build();
            var tokenString = manager.GenerateClientAccessToken(HubName, userId, claims, _tokenLifeTime);
            var token       = JwtTokenHelper.JwtHandler.ReadJwtToken(tokenString);

            string expectedToken = JwtTokenHelper.GenerateExpectedAccessToken(token, ClientEndpointUtils.GetExpectedClientEndpoint(HubName, appName), AccessKey, claims);

            Assert.Equal(expectedToken, tokenString);
        }