107 lines
4.0 KiB
Java
107 lines
4.0 KiB
Java
package org.mercury_im.messenger.core.store.crypto;
|
|
|
|
import org.bouncycastle.openpgp.PGPException;
|
|
import org.bouncycastle.openpgp.PGPPublicKeyRingCollection;
|
|
import org.bouncycastle.openpgp.PGPSecretKeyRingCollection;
|
|
import org.jivesoftware.smackx.ox.store.abstr.AbstractOpenPgpKeyStore;
|
|
import org.jivesoftware.smackx.ox.store.abstr.AbstractOpenPgpMetadataStore;
|
|
import org.jivesoftware.smackx.ox.store.abstr.AbstractOpenPgpStore;
|
|
import org.jivesoftware.smackx.ox.store.abstr.AbstractOpenPgpTrustStore;
|
|
import org.jxmpp.jid.BareJid;
|
|
import org.mercury_im.messenger.core.SchedulersFacade;
|
|
import org.mercury_im.messenger.core.data.repository.OpenPgpRepository;
|
|
import org.pgpainless.key.OpenPgpV4Fingerprint;
|
|
|
|
import java.io.IOException;
|
|
import java.util.Date;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
import java.util.NoSuchElementException;
|
|
|
|
import javax.inject.Inject;
|
|
|
|
public class MercuryOpenPgpStore extends AbstractOpenPgpStore {
|
|
|
|
private final OpenPgpRepository repository;
|
|
private final SchedulersFacade schedulers;
|
|
|
|
@Inject
|
|
public MercuryOpenPgpStore(OpenPgpRepository repository, SchedulersFacade schedulers) {
|
|
super(new AbstractOpenPgpKeyStore() {
|
|
@Override
|
|
protected PGPPublicKeyRingCollection readPublicKeysOf(BareJid owner)
|
|
throws IOException, PGPException {
|
|
try {
|
|
return repository.loadPublicKeysOf(owner.asEntityBareJidIfPossible())
|
|
.blockingGet();
|
|
} catch (NoSuchElementException e) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
@Override
|
|
protected void writePublicKeysOf(BareJid owner, PGPPublicKeyRingCollection publicKeys)
|
|
throws IOException {
|
|
repository.storePublicKeysOf(owner.asEntityBareJidIfPossible(), publicKeys)
|
|
.subscribeOn(schedulers.getIoScheduler()).subscribe();
|
|
}
|
|
|
|
@Override
|
|
protected PGPSecretKeyRingCollection readSecretKeysOf(BareJid owner)
|
|
throws IOException, PGPException {
|
|
try {
|
|
return repository.loadSecretKeysOf(owner.asEntityBareJidIfPossible())
|
|
.blockingGet();
|
|
} catch (NoSuchElementException e) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
@Override
|
|
protected void writeSecretKeysOf(BareJid owner, PGPSecretKeyRingCollection secretKeys)
|
|
throws IOException {
|
|
repository.storeSecretKeysOf(owner.asEntityBareJidIfPossible(), secretKeys)
|
|
.subscribeOn(schedulers.getIoScheduler()).subscribe();
|
|
}
|
|
|
|
@Override
|
|
protected Map<OpenPgpV4Fingerprint, Date> readKeyFetchDates(BareJid owner)
|
|
throws IOException {
|
|
return new HashMap<>();
|
|
}
|
|
|
|
@Override
|
|
protected void writeKeyFetchDates(BareJid owner, Map<OpenPgpV4Fingerprint, Date> dates)
|
|
throws IOException {
|
|
|
|
}
|
|
}, new AbstractOpenPgpMetadataStore() {
|
|
@Override
|
|
protected Map<OpenPgpV4Fingerprint, Date> readAnnouncedFingerprintsOf(BareJid contact)
|
|
throws IOException {
|
|
return null;
|
|
}
|
|
|
|
@Override
|
|
protected void writeAnnouncedFingerprintsOf(BareJid contact, Map<OpenPgpV4Fingerprint, Date> metadata)
|
|
throws IOException {
|
|
|
|
}
|
|
}, new AbstractOpenPgpTrustStore() {
|
|
@Override
|
|
protected Trust readTrust(BareJid owner, OpenPgpV4Fingerprint fingerprint)
|
|
throws IOException {
|
|
return Trust.trusted;
|
|
}
|
|
|
|
@Override
|
|
protected void writeTrust(BareJid owner, OpenPgpV4Fingerprint fingerprint, Trust trust)
|
|
throws IOException {
|
|
|
|
}
|
|
});
|
|
this.repository = repository;
|
|
this.schedulers = schedulers;
|
|
}
|
|
}
|