79 lines
2.8 KiB
Java
79 lines
2.8 KiB
Java
package org.mercury_im.messenger.data.repository;
|
|
|
|
import org.bouncycastle.openpgp.PGPPublicKeyRingCollection;
|
|
import org.bouncycastle.openpgp.PGPSecretKeyRingCollection;
|
|
import org.jxmpp.jid.EntityBareJid;
|
|
import org.mercury_im.messenger.core.data.repository.OpenPgpRepository;
|
|
import org.mercury_im.messenger.data.model.OpenPgpPublicKeyRing;
|
|
import org.mercury_im.messenger.data.model.OpenPgpSecretKeyRing;
|
|
import org.pgpainless.PGPainless;
|
|
|
|
import javax.inject.Inject;
|
|
|
|
import io.reactivex.Completable;
|
|
import io.reactivex.Single;
|
|
import io.requery.Persistable;
|
|
import io.requery.reactivex.ReactiveEntityStore;
|
|
|
|
public class RxOpenPgpRepository implements OpenPgpRepository {
|
|
|
|
private final ReactiveEntityStore<Persistable> data;
|
|
|
|
@Inject
|
|
public RxOpenPgpRepository(ReactiveEntityStore<Persistable> data) {
|
|
this.data = data;
|
|
}
|
|
|
|
@Override
|
|
public Completable storePublicKeysOf(EntityBareJid owner, PGPPublicKeyRingCollection keys) {
|
|
return Single.fromCallable(() -> {
|
|
OpenPgpPublicKeyRing keyRing = new OpenPgpPublicKeyRing();
|
|
keyRing.setOwner(owner);
|
|
keyRing.setBytes(keys.getEncoded());
|
|
return keyRing;
|
|
}).flatMap(data::upsert).ignoreElement();
|
|
}
|
|
|
|
@Override
|
|
public Single<PGPPublicKeyRingCollection> loadPublicKeysOf(EntityBareJid owner) {
|
|
return data.select(OpenPgpPublicKeyRing.class)
|
|
.where(OpenPgpPublicKeyRing.OWNER.eq(owner))
|
|
.limit(1).get()
|
|
.maybe().toSingle()
|
|
.map(keyring -> PGPainless.readKeyRing().publicKeyRingCollection(keyring.getBytes()));
|
|
}
|
|
|
|
@Override
|
|
public Single<Integer> deletePublicKeysOf(EntityBareJid owner) {
|
|
return data.delete(OpenPgpPublicKeyRing.class)
|
|
.where(OpenPgpPublicKeyRing.OWNER.eq(owner))
|
|
.get().single();
|
|
}
|
|
|
|
@Override
|
|
public Completable storeSecretKeysOf(EntityBareJid owner, PGPSecretKeyRingCollection keys) {
|
|
return Single.fromCallable(() -> {
|
|
OpenPgpSecretKeyRing keyRing = new OpenPgpSecretKeyRing();
|
|
keyRing.setOwner(owner);
|
|
keyRing.setBytes(keys.getEncoded());
|
|
return keyRing;
|
|
}).flatMap(data::upsert).ignoreElement();
|
|
}
|
|
|
|
@Override
|
|
public Single<PGPSecretKeyRingCollection> loadSecretKeysOf(EntityBareJid owner) {
|
|
return data.select(OpenPgpSecretKeyRing.class)
|
|
.where(OpenPgpSecretKeyRing.OWNER.eq(owner))
|
|
.limit(1).get()
|
|
.maybe().toSingle()
|
|
.map(keyring -> PGPainless.readKeyRing().secretKeyRingCollection(keyring.getBytes()));
|
|
}
|
|
|
|
@Override
|
|
public Single<Integer> deleteSecretKeysOf(EntityBareJid owner) {
|
|
return data.delete(OpenPgpSecretKeyRing.class)
|
|
.where(OpenPgpPublicKeyRing.OWNER.eq(owner))
|
|
.get().single();
|
|
}
|
|
}
|