public void ProcessMessage(IHandlerContext <RegisterArgumentCommand> context) { var message = context.Message; if (string.IsNullOrEmpty(message.Name) || string.IsNullOrEmpty(message.DataTypeName)) { return; } using (_databaseContextFactory.Create()) { var key = Argument.Key(message.Name); var existingId = _keyStore.Get(key); if (!message.Id.Equals(existingId ?? message.Id)) { return; } var stream = _eventStore.Get(message.Id); var argument = new Argument(stream.Id); stream.Apply(argument); _keyStore.Remove(Argument.Key(argument.Name)); stream.AddEvent(argument.Register(message.Name, message.DataTypeName)); _eventStore.Save(stream); _keyStore.Add(argument.Id, key); } }
public VerifiedSignature Signature(IRequest r, ISignatureSpecification spec, IKeyStore keyStore) { var authorization = r.GetHeader("authorization"); if (string.IsNullOrEmpty(authorization)) { throw new SignatureMissingException("No authorization header present"); } var signatureAuth = authorizationParser.Parse(authorization); if (spec == null) { spec = signatureAuth; } else { if (spec.Algorithm != signatureAuth.Algorithm) { throw new InvalidSignatureException(string.Format("Algorith mismatch. Wanted: {0}, found: {1}", spec.Algorithm, signatureAuth.Algorithm)); } var missingHeaders = spec.Headers.Where(h => !signatureAuth.Headers.Contains(h)).ToList(); if (missingHeaders.Any()) { throw new InvalidSignatureException(string.Format("Missing headers in signature: {0}", string.Join(",", missingHeaders))); } } var signature = CalculateSignature(r, spec, keyStore.Get(signatureAuth.KeyId)); return(new VerifiedSignature(signatureAuth, signature)); }
public AuthenticationResult Authenticate(string username, string password) { EventStream stream; Guid? userId; using (_databaseContextFactory.Create(_configuration.ProviderName, _configuration.ConnectionString)) { userId = _keyStore.Get(User.Key(username)); if (!userId.HasValue) { _log.Trace(string.Format("[username not found] : username = '******'", username)); return(AuthenticationResult.Failure()); } stream = _eventStore.Get(userId.Value); } var user = new User(userId.Value); stream.Apply(user); if (user.PasswordMatches(_hashingService.Sha256(password))) { return(AuthenticationResult.Success()); } _log.Trace(string.Format("[invalid password] : username = '******'", username)); return(AuthenticationResult.Failure()); }
public void ProcessMessage(IHandlerContext <RegisterFormulaCommand> context) { var message = context.Message; using (_databaseContextFactory.Create()) { var key = Formula.Key(message.Name); var existingId = _keyStore.Get(key); if (!message.Id.Equals(existingId ?? message.Id)) { return; } var stream = _eventStore.Get(message.Id); var formula = new Formula(stream.Id); stream.Apply(formula); _keyStore.Remove(Formula.Key(formula.Name)); stream.AddEvent(formula.Register(message.Name)); _eventStore.Save(stream); _keyStore.Add(formula.Id, key); } }
/****************************************************************************/ public async Task <IKey> GetDecryptionKey(Guid keyId) { var key = await _decryptStore.Get(keyId); if (key == null) { throw new KeyNotFoundException(); } return(key); }
/// <summary> /// Configure the AES instance /// </summary> /// <param name="streamId">Id of the stream to obtain the encryption key for</param> /// <param name="create">Whether to create a new key in case there is none in the key store</param> /// <returns>The configured AES instance</returns> private async Task <Aes> GetAes(string streamId, bool create = false) { var aes = new AesManaged { Mode = CipherMode.CBC, Padding = PaddingMode.PKCS7 }; if (!_keyCache.ContainsKey(streamId)) { if (!await _keyStore.Contains(streamId)) { if (create) { aes.GenerateKey(); await _keyStore.Add(streamId, aes.Key); _keyCache.Add(streamId, aes.Key); } else { throw new KeyDeletedException("The encryption key has been deleted"); } } else { var value = await _keyStore.Get(streamId); _keyCache.Add(streamId, value); } } aes.Key = _keyCache[streamId]; return(aes); }
public VerifiedSignature Signature(IRequest r, ISignatureSpecification spec, IKeyStore keyStore) { var authorization = r.GetHeader("authorization"); if (string.IsNullOrEmpty(authorization)) throw new SignatureMissingException("No authorization header present"); var signatureAuth = authorizationParser.Parse(authorization); if (spec == null) { spec = signatureAuth; } else { if (spec.Algorithm != signatureAuth.Algorithm) { throw new InvalidSignatureException(string.Format("Algorith mismatch. Wanted: {0}, found: {1}", spec.Algorithm, signatureAuth.Algorithm)); } var missingHeaders = spec.Headers.Where(h=> !signatureAuth.Headers.Contains(h)).ToList(); if (missingHeaders.Any()) { throw new InvalidSignatureException(string.Format("Missing headers in signature: {0}", string.Join(",", missingHeaders))); } } var signature = CalculateSignature(r, spec, keyStore.Get(signatureAuth.KeyId)); return new VerifiedSignature(signatureAuth, signature); }