use of org.jivesoftware.smackx.omemo.util.MessageOrOmemoMessage in project Smack by igniterealtime.
the class OmemoMamDecryptionTest method mamDecryptionTest.
@SmackIntegrationTest
public void mamDecryptionTest() throws XMPPException.XMPPErrorException, SmackException.NotLoggedInException, SmackException.NotConnectedException, InterruptedException, SmackException.NoResponseException, CryptoFailedException, UndecidedOmemoIdentityException, IOException {
// Make sure, Bobs server stores messages in the archive
MamManager bobsMamManager = MamManager.getInstanceFor(bob.getConnection());
bobsMamManager.enableMamForAllMessages();
bobsMamManager.setDefaultBehavior(MamPrefsIQ.DefaultBehavior.always);
// Prevent bob from automatically decrypting MAM messages.
bob.stopStanzaAndPEPListeners();
String body = "This message will be stored in MAM!";
OmemoMessage.Sent encrypted = alice.encrypt(bob.getOwnJid(), body);
XMPPConnection alicesConnection = alice.getConnection();
MessageBuilder messageBuilder = alicesConnection.getStanzaFactory().buildMessageStanza();
alicesConnection.sendStanza(encrypted.buildMessage(messageBuilder, bob.getOwnJid()));
MamManager.MamQuery query = bobsMamManager.queryArchive(MamManager.MamQueryArgs.builder().limitResultsToJid(alice.getOwnJid()).build());
assertEquals(1, query.getMessageCount());
List<MessageOrOmemoMessage> decryptedMamQuery = bob.decryptMamQueryResult(query);
assertEquals(1, decryptedMamQuery.size());
assertEquals(body, decryptedMamQuery.get(decryptedMamQuery.size() - 1).getOmemoMessage().getBody());
}
use of org.jivesoftware.smackx.omemo.util.MessageOrOmemoMessage in project Smack by igniterealtime.
the class OmemoService method decryptMamQueryResult.
/**
* Decrypt a possible OMEMO encrypted messages in a {@link MamManager.MamQuery}.
* The returned list contains wrappers that either hold an {@link OmemoMessage} in case the message was decrypted
* properly, otherwise it contains the message itself.
*
* @param managerGuard authenticated OmemoManager.
* @param mamQuery Mam archive query
* @return list of {@link MessageOrOmemoMessage MessageOrOmemoMessages}.
*
* @throws IOException if an I/O error occurred.
*/
List<MessageOrOmemoMessage> decryptMamQueryResult(OmemoManager.LoggedInOmemoManager managerGuard, MamManager.MamQuery mamQuery) throws IOException {
List<MessageOrOmemoMessage> result = new ArrayList<>();
for (Message message : mamQuery.getMessages()) {
if (OmemoManager.stanzaContainsOmemoElement(message)) {
OmemoElement element = (OmemoElement) message.getExtensionElement(OmemoElement.NAME_ENCRYPTED, OmemoConstants.OMEMO_NAMESPACE_V_AXOLOTL);
// Decrypt OMEMO messages
try {
OmemoMessage.Received omemoMessage = decryptMessage(managerGuard, message.getFrom().asBareJid(), element);
result.add(new MessageOrOmemoMessage(omemoMessage));
} catch (NoRawSessionException | CorruptedOmemoKeyException | CryptoFailedException e) {
LOGGER.log(Level.WARNING, "decryptMamQueryResult failed to decrypt message from " + message.getFrom() + " due to corrupted session/key: " + e.getMessage());
result.add(new MessageOrOmemoMessage(message));
}
} else {
// Wrap cleartext messages
result.add(new MessageOrOmemoMessage(message));
}
}
return result;
}
Aggregations