public IActionResult RequestVerify(RequestVerifyParameters vp) { Log.Info(JsonConvert.SerializeObject(vp)); var identities = _repository.KeyIdentity.GetIdentitiesByToken(vp.Token).ToList(); if (identities.Count == 0) { return(NotFound()); } if (vp.Addresses.Any(address => !identities.Any(id => id.Email.Equals(address, StringComparison.OrdinalIgnoreCase)))) { return(BadRequest("One of the specified addresses does not exist.")); } var affectedIdentities = identities.Where(id => vp.Addresses.Contains(id.Email) && id.Status == IdentityStatus.Unpublished).ToList(); // all specified addresses can be verified. var rnd = new Random(); var statusList = new Dictionary <string, IdentityStatus>(); foreach (var identity in affectedIdentities) { var verification = new AddressVerification { Token = vp.Token, Email = identity.Email, VerificationCode = (ushort)rnd.Next(10000, ushort.MaxValue) }; _repository.AddressVerification.Create(verification); statusList.Add(identity.Email, IdentityStatus.Pending); identity.Status = IdentityStatus.Pending; _repository.KeyIdentity.Update(identity); // TODO: send mail var message = new MimeMessage(); message.From.Add(new MailboxAddress("MAUTOM Portunus", ConfigManager.SmtpSettings.GetValue <string>("Sender"))); message.To.Add(new MailboxAddress(identity.Name, identity.Email)); message.Subject = "Verify your identity"; var msgBody = $@"Dear {identity.Name}, You are receiving this e-mail because you have requested the verification of your PGP key and e-mail address. Your e-mail address is: {identity.Email} Please click the link below, to verify your address and publish it on the server: {CreateVerificationUrl(vp.Token, verification.VerificationCode)} Please do not reply to this e-mail."; var emailBody = GpgKeychain.Instance.EncryptAndSign(msgBody, identity.PublicKeyFingerprint); message.Body = new TextPart(TextFormat.Plain) { Text = emailBody }; MailManager.Instance.Enqueue(message); } _repository.Save(); var response = new KeyUploadResult(identities[0].PublicKeyFingerprint, vp.Token, statusList); return(Ok(response)); }
public IActionResult Upload(KeyUploadParameters up) { if (string.IsNullOrEmpty(up.KeyText)) { return(BadRequest()); } var result = GpgKeychain.Instance.ImportArmoredKey(up.KeyText); foreach (var importResult in result.Imports) { var key = (PgpKey)GpgKeychain.Instance.KeyStore.GetKey(importResult.Fpr, false); if (_repository.PublicKey.GetPublicKeyByFingerprint(importResult.Fpr, false, false) != null) { Log.Warn($"Submission of already existing Fpr: {key.Fingerprint}. Skipping."); continue; } Log.Info( $"Imported fpr:{key.Fingerprint}, created at: {key.Uid.Signatures.Timestamp}, identities: {key.Uids.Count()}"); var armor = GpgKeychain.Instance.ExportArmoredKey(key.Fingerprint); // create new db record // create identities var verificationToken = Guid.NewGuid(); var identities = key.Uids.Select(uid => new KeyIdentity { Name = uid.Name, Email = uid.Email, Comment = uid.Comment, CreationDate = uid.Signatures.Timestamp, Status = IdentityStatus.Unpublished, VerificationToken = verificationToken }).ToList(); var statusList = identities.ToDictionary(k => k.Email, val => val.Status); // create key record var keyFingerprint = new PublicKeyFingerprint(key.Fingerprint); var flags = PublicKeyFlags.Default; if (key.Disabled) { flags |= PublicKeyFlags.Disabled; } if (key.Expired) { flags |= PublicKeyFlags.Expired; } if (key.Revoked) { flags |= PublicKeyFlags.Revoked; } var publicKey = new PublicKey { Fingerprint = keyFingerprint, LongKeyId = keyFingerprint.LongKeyId, ShortKeyId = keyFingerprint.ShortKeyId, CreationDate = key.Subkeys.Timestamp, Algorithm = (PublicKeyAlgorithm)key.Subkeys.PubkeyAlgorithm, Length = (PublicKeyLength)key.Subkeys.Length, ArmoredKey = armor, Flags = flags, KeyIdentities = identities }; _repository.PublicKey.CreatePublicKey(publicKey); _repository.Save(); key.Dispose(); // produce response var response = new KeyUploadResult(key.Fingerprint, verificationToken, statusList); return(Ok(response)); } return(Ok()); }