private void CreateEncryptedRoute(HttpConfiguration configuration) { var section = (NameValueCollection)ConfigurationManager.GetSection("Api2CryptoGraphySettings"); if (section == null) { throw new ApplicationException("Config section 'RestfulWebService' has not been set."); } // arrange. var settings = new CryptoGraphySettings(section); var cryptoHandler = new ServerCryptoHandler(settings.SecretKey, settings.InitialVector, settings.HashKey); var injection = new DelegatingHandler[] { new ServerMessageDumper(), cryptoHandler }; // dump decrypted request & plain response. var handler = HttpClientFactory.CreatePipeline(new HttpControllerDispatcher(configuration), injection); // register encrypted HTTP route. configuration.Routes.MapHttpRoute("Encrypted Route", "api2/{controller}/{action}", null, null, handler); // register timestamp as a route. var timestampProvider = new DefaultTimestampProvider(TimeSpan.FromMinutes(15)) as ITimestampProvider<string>; var timestampHandler = new HttpTimestampHandler<string>(timestampProvider); configuration.Routes.MapHttpRoute("Timestamp Route", "api3/!timestamp!/get", null, null, timestampHandler); // register global timestamp service, it should align with encrypted HTTP route or will not work. configuration.MessageHandlers.Add(new HttpTimestampHandler<string>(configuration, "api2/!timestamp!/get", timestampProvider)); }
public void DefaultTimestampProviderTest_GetTimestamp() { ITimestampProvider<long> target = new DefaultTimestampProvider(TimeSpan.FromMinutes(1)); var actual = target.GetTimestamp(); Assert.IsNotNull(actual); }
public ServerCryptoHandler(string secretKeyPassword, string initialVectorPassword, string hashKeyString) { var symmetricAlgorithm = new AES(secretKeyPassword, initialVectorPassword); var hashAlgorithm = new HMACSHA512(hashKeyString); var timestampProvider = new DefaultTimestampProvider(TimeSpan.FromMinutes(15)) as ITimestampProvider<string>; this.messageCryptoService = new DefaultHttpMessageCryptoService(symmetricAlgorithm, hashAlgorithm, timestampProvider); }
public void DefaultTimestampProviderTest_Validate() { var input = DateTime.UtcNow.AddMinutes(-1).Ticks; ITimestampProvider<long> target = new DefaultTimestampProvider(TimeSpan.FromMinutes(2)); target.Validate(input); }
public void DefaultTimestampProvider_GetTimestamp_ReturnsValueInRange() { ITimestampProvider provider = new DefaultTimestampProvider(); long timestamp = provider.GetCurrentTimestampInMilliseconds(); Assert.That(timestamp, Is.AtMost(IF_MORE_BASE_YEAR_MAY_BE_WRONG).And.AtLeast(IF_LESS_UNITS_ARE_WRONG)); }
internal EncryptedHttpRouteWrapper(string baseAddress) { var configuration = new HttpSelfHostConfiguration(baseAddress); // configuration.Services.Replace(typeof(ITraceWriter), new TraceWriter()); ITimestampProvider<string> timestampProvider = new DefaultTimestampProvider(TimeSpan.FromMinutes(15)); this.RegisterEncryptedRoute(configuration, timestampProvider); this.RegisterTimestampRoute(configuration, timestampProvider); // startup local HTTP server. this.inner = new HttpSelfHostServer(configuration); this.inner.OpenAsync().Wait(); this.configuration = configuration; }
internal EncryptedHttpServerWrapper(string baseAddress) { var configuration = new HttpSelfHostConfiguration(baseAddress); // arrange. var timestampProvider = new DefaultTimestampProvider(TimeSpan.FromMinutes(15)); var cryptoHandler = new ServerCryptoHandler("secretKeyPassword", "initialVectorPassword", "hashKeyString", timestampProvider); // register handlers. configuration.MessageHandlers.Add(new ServerMessageDumper()); configuration.MessageHandlers.Add(new HttpTimestampHandler<long>(configuration, "api2/!timestamp!/get", timestampProvider)); configuration.Routes.MapHttpRoute("Fake Timestamp Route", "api2/!timestamp!/{action}"); // startup local HTTP server. this.inner = new HttpSelfHostServer(configuration); this.inner.OpenAsync().Wait(); this.configuration = configuration; }