public async Task TestGenerateServerAccessTokenWithSpecifedAlgorithm(AccessTokenAlgorithm algorithm) { var connectionString = "Endpoint=http://localhost;AccessKey=ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789;Port=8080;Version=1.0"; var provider = new ServiceEndpointProvider(new DefaultServerNameProvider(), new ServiceEndpoint(connectionString), new ServiceOptions() { AccessTokenAlgorithm = algorithm }); var generatedToken = await provider.GenerateServerAccessTokenAsync("hub1", "user1"); var handler = new JwtSecurityTokenHandler(); var token = handler.ReadJwtToken(generatedToken); Assert.Equal(algorithm.ToString(), token.SignatureAlgorithm); }
public void TestGenerateServerAccessToken(string connectionString, string expectedAudience) { var provider = new ServiceEndpointProvider(new ServiceEndpoint(connectionString)); var clientToken = provider.GenerateServerAccessToken("hub1", "user1"); var handler = new JwtSecurityTokenHandler(); var principal = handler.ValidateToken(clientToken, new TokenValidationParameters { ValidateIssuer = false, IssuerSigningKey = SecurityKey, ValidAudience = expectedAudience }, out var token); Assert.Equal("user1", principal.FindFirst(ClaimTypes.NameIdentifier).Value); }
public void GenerateMutlipleAccessTokenShouldBeUnique() { var count = 1000; var sep = new ServiceEndpointProvider(new ServiceEndpoint(DefaultConnectionString)); var userId = Guid.NewGuid().ToString(); var tokens = new List <string>(); for (int i = 0; i < count; i++) { tokens.Add(sep.GenerateClientAccessToken()); tokens.Add(sep.GenerateServerAccessToken("test1", userId)); } var distinct = tokens.Distinct(); Assert.Equal(tokens.Count, distinct.Count()); }
internal ServiceManager(ServiceManagerOptions serviceManagerOptions, string productInfo) { _serviceManagerOptions = serviceManagerOptions; _endpoint = new ServiceEndpoint(_serviceManagerOptions.ConnectionString, EndpointType.Secondary); _serverNameProvider = new DefaultServerNameProvider(); var serviceOptions = Options.Create(new ServiceOptions { ApplicationName = _serviceManagerOptions.ApplicationName, Proxy = serviceManagerOptions.Proxy }).Value; _endpointProvider = new ServiceEndpointProvider(_serverNameProvider, _endpoint, serviceOptions); _productInfo = productInfo; _restClient = new RestClient(); _restApiProvider = new RestApiProvider(_serviceManagerOptions.ConnectionString); }
public async Task TestGenerateServerAccessToken(string connectionString, string expectedAudience) { var provider = new ServiceEndpointProvider(new DefaultServerNameProvider(), new ServiceEndpoint(connectionString), new ServiceOptions() { }, NullLoggerFactory.Instance); var clientToken = await provider.GenerateServerAccessTokenAsync("hub1", "user1"); var handler = new JwtSecurityTokenHandler(); var principal = handler.ValidateToken(clientToken, new TokenValidationParameters { ValidateIssuer = false, IssuerSigningKey = SecurityKey, ValidAudience = expectedAudience }, out _); Assert.Equal("user1", principal.FindFirst(ClaimTypes.NameIdentifier).Value); }
internal ServiceManager(ServiceManagerContext context, RestClientFactory restClientFactory) { _endpoint = context.ServiceEndpoints.Single();//temp solution _serverNameProvider = new DefaultServerNameProvider(); var serviceOptions = Options.Create(new ServiceOptions { ApplicationName = context.ApplicationName, Proxy = context.Proxy }).Value; _endpointProvider = new ServiceEndpointProvider(_serverNameProvider, _endpoint, serviceOptions); _productInfo = context.ProductInfo; _context = context; _restClientFactory = restClientFactory; }
public async Task TestGenerateServerAccessTokenWIthPrefix(string connectionString, string expectedAudience) { var provider = new ServiceEndpointProvider(new ServiceEndpoint(connectionString), new ServiceOptions() { ApplicationName = "prefix" }); var serverToken = await provider.GenerateServerAccessTokenAsync("hub1", "user1"); var handler = new JwtSecurityTokenHandler(); var principal = handler.ValidateToken(serverToken, new TokenValidationParameters { ValidateIssuer = false, IssuerSigningKey = SecurityKey, ValidAudience = expectedAudience }, out var token); Assert.Equal("user1", principal.FindFirst(ClaimTypes.NameIdentifier).Value); }
public async Task GenerateMutlipleAccessTokenShouldBeUnique() { var count = 1000; var sep = new ServiceEndpointProvider(new DefaultServerNameProvider(), new ServiceEndpoint(DefaultConnectionString), new ServiceOptions() { }, NullLoggerFactory.Instance); var userId = Guid.NewGuid().ToString(); var tokens = new List <string>(); for (int i = 0; i < count; i++) { tokens.Add(await sep.GenerateClientAccessTokenAsync()); tokens.Add(await sep.GenerateServerAccessTokenAsync("test1", userId)); } var distinct = tokens.Distinct(); Assert.Equal(tokens.Count, distinct.Count()); }
private static void RegisterServiceObjects(HubConfiguration configuration, ServiceOptions options, string applicationName, IReadOnlyList <string> hubs) { // TODO: Using IOptions looks wierd, thinking of a way removing it // share the same object all through var serviceOptions = Options.Create(options); // For safety, ALWAYS register abstract classes or interfaces // Some third-party DI frameworks such as Ninject, implicit self-binding concrete types: // https://github.com/ninject/ninject/wiki/dependency-injection-with-ninject#skipping-the-type-binding-bit--implicit-self-binding-of-concrete-types configuration.Resolver.Register(typeof(IOptions <ServiceOptions>), () => serviceOptions); var serviceProtocol = new ServiceProtocol(); configuration.Resolver.Register(typeof(IServiceProtocol), () => serviceProtocol); var provider = new EmptyProtectedData(); configuration.Resolver.Register(typeof(IProtectedData), () => provider); var endpoint = new ServiceEndpointProvider(serviceOptions.Value); configuration.Resolver.Register(typeof(IServiceEndpointProvider), () => endpoint); var scm = new ServiceConnectionManager(applicationName, hubs); configuration.Resolver.Register(typeof(IServiceConnectionManager), () => scm); var ccm = new ClientConnectionManager(configuration); configuration.Resolver.Register(typeof(IClientConnectionManager), () => ccm); var atm = new AzureTransportManager(configuration.Resolver); configuration.Resolver.Register(typeof(ITransportManager), () => atm); var parser = new SignalRMessageParser(hubs, configuration.Resolver); configuration.Resolver.Register(typeof(IMessageParser), () => parser); var smb = new ServiceMessageBus(configuration.Resolver); configuration.Resolver.Register(typeof(IMessageBus), () => smb); }
public void TestGenerateClientAccessToken(string connectionString, string expectedAudience) { var provider = new ServiceEndpointProvider(new ServiceEndpoint(connectionString)); var clientToken = provider.GenerateClientAccessToken(null, new Claim[] { new Claim("type1", "value1") }); var handler = new JwtSecurityTokenHandler(); var principal = handler.ValidateToken(clientToken, new TokenValidationParameters { ValidateIssuer = false, IssuerSigningKey = SecurityKey, ValidAudience = expectedAudience }, out var token); var customClaims = principal.FindAll("type1").ToList(); Assert.Single(customClaims); Assert.Equal("value1", customClaims[0].Value); }
public async Task TestGenerateClientAccessToken(string connectionString, string expectedAudience) { var provider = new ServiceEndpointProvider(new DefaultServerNameProvider(), new ServiceEndpoint(connectionString), new ServiceOptions() { }, NullLoggerFactory.Instance); var clientToken = await provider.GenerateClientAccessTokenAsync(null, new Claim[] { new Claim("type1", "value1") }); var handler = new JwtSecurityTokenHandler(); var principal = handler.ValidateToken(clientToken, new TokenValidationParameters { ValidateIssuer = false, IssuerSigningKey = SecurityKey, ValidAudience = expectedAudience }, out var token); var customClaims = principal.FindAll("type1").ToList(); Assert.Single(customClaims); Assert.Equal("value1", customClaims[0].Value); }
internal ServiceManager(ServiceManagerOptions serviceManagerOptions) { _serviceManagerOptions = serviceManagerOptions; _endpoint = new ServiceEndpoint(_serviceManagerOptions.ConnectionString, EndpointType.Secondary); _endpointProvider = new ServiceEndpointProvider(_endpoint); }
internal ServiceManager(ServiceManagerOptions serviceManagerOptions) { _serviceManagerOptions = serviceManagerOptions; _endpoint = new ServiceEndpointProvider(new ServiceEndpoint(_serviceManagerOptions.ConnectionString)); }