Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
DenBond7 committed Nov 1, 2021
1 parent 5803a9e commit b53ba16
Show file tree
Hide file tree
Showing 44 changed files with 660 additions and 730 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import org.junit.runner.RunWith
*/
@MediumTest
@RunWith(AndroidJUnit4::class)
class ContactsListFragmentTest : BaseTest() {
class RecipientsListFragmentTest : BaseTest() {

override val activityScenarioRule = activityScenarioRule<SettingsActivity>(
TestGeneralUtil.genIntentForNavigationComponent(
Expand Down
12 changes: 6 additions & 6 deletions FlowCrypt/src/devTest/res/navigation/nav_graph.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
android:id="@+id/action_mainSettingsFragment_to_experimentalSettingsFragment"
app:destination="@id/experimentalSettingsFragment" />
<action
android:id="@+id/action_mainSettingsFragment_to_contactsListFragment"
app:destination="@id/contactsListFragment" />
android:id="@+id/action_mainSettingsFragment_to_recipientsListFragment"
app:destination="@id/recipientsListFragment" />
</fragment>

<fragment
Expand Down Expand Up @@ -130,15 +130,15 @@
</fragment>

<fragment
android:id="@+id/contactsListFragment"
android:name="com.flowcrypt.email.ui.activity.fragment.ContactsListFragment"
android:id="@+id/recipientsListFragment"
android:name="com.flowcrypt.email.ui.activity.fragment.RecipientsListFragment"
android:label="ContactsListFragment"
tools:layout="@layout/fragment_contacts_list">
tools:layout="@layout/fragment_recipients_list">
<deepLink
android:id="@+id/deepLink"
app:uri="flowcrypt://email.flowcrypt.com/settings/contacts" />
<action
android:id="@+id/action_contactsListFragment_to_publicKeyDetailsFragment"
android:id="@+id/action_recipientsListFragment_to_publicKeyDetailsFragment"
app:destination="@id/publicKeyDetailsFragment" />
</fragment>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,9 @@ class EmailUtil {
* @param pgpKeyDetails The key details
* @return A generated [AttachmentInfo].
*/
fun genAttInfoFromPubKey(pgpKeyDetails: PgpKeyDetails?): AttachmentInfo? {
fun genAttInfoFromPubKey(pgpKeyDetails: PgpKeyDetails?, email: String): AttachmentInfo? {
if (pgpKeyDetails != null) {
val fileName = "0x" + pgpKeyDetails.fingerprint.toUpperCase(Locale.getDefault()) + ".asc"
val fileName = "0x" + pgpKeyDetails.fingerprint.uppercase() + ".asc"

return if (!TextUtils.isEmpty(pgpKeyDetails.publicKey)) {
val attachmentInfo = AttachmentInfo()
Expand All @@ -212,7 +212,7 @@ class EmailUtil {
attachmentInfo.encodedSize = pgpKeyDetails.publicKey.length.toLong()
attachmentInfo.rawData = pgpKeyDetails.publicKey.toByteArray()
attachmentInfo.type = Constants.MIME_TYPE_PGP_KEY
attachmentInfo.email = pgpKeyDetails.primaryPgpContact.email
attachmentInfo.email = email
attachmentInfo.id = generateContentId()
attachmentInfo.isEncryptionAllowed = false

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ package com.flowcrypt.email.api.retrofit.response.model

import android.os.Parcel
import android.os.Parcelable
import com.flowcrypt.email.model.PgpContact
import com.flowcrypt.email.database.entity.relation.RecipientWithPubKeys

import com.flowcrypt.email.security.model.PgpKeyDetails

import com.google.gson.annotations.Expose
Expand All @@ -29,15 +30,16 @@ data class PublicKeyMsgBlock constructor(
@Expose
override val type: MsgBlock.Type = MsgBlock.Type.PUBLIC_KEY

var existingPgpContact: PgpContact? = null
var existingRecipientWithPubKeys: RecipientWithPubKeys? = null

constructor(parcel: Parcel) : this(
parcel.readString(),
parcel.readByte() != 0.toByte(),
parcel.readParcelable(PgpKeyDetails::class.java.classLoader),
parcel.readParcelable(MsgBlockError::class.java.classLoader),
) {
existingPgpContact = parcel.readParcelable(PgpContact::class.java.classLoader)
existingRecipientWithPubKeys =
parcel.readParcelable(RecipientWithPubKeys::class.java.classLoader)
}

override fun writeToParcel(parcel: Parcel, flags: Int) =
Expand All @@ -47,7 +49,7 @@ data class PublicKeyMsgBlock constructor(
writeInt((if (complete) 1 else 0))
writeParcelable(keyDetails, flags)
writeParcelable(error, flags)
writeParcelable(existingPgpContact, flags)
writeParcelable(existingRecipientWithPubKeys, flags)
}

override fun describeContents(): Int = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,10 @@ interface PubKeysDao : BaseDao<PublicKeyEntity> {

@Query("SELECT * FROM public_keys WHERE fingerprint = :fingerprint")
suspend fun getPublicKeysByFingerprint(fingerprint: String): List<PublicKeyEntity>

@Query("SELECT * FROM public_keys WHERE recipient = :recipient AND fingerprint = :fingerprint")
suspend fun getPublicKeysByRecipientAndFingerprint(
recipient: String,
fingerprint: String
): PublicKeyEntity?
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ interface RecipientDao : BaseDao<RecipientEntity> {
@Query("SELECT * FROM recipients WHERE email = :email")
fun getRecipientByEmail(email: String): RecipientEntity?

@Transaction
@Query("SELECT * FROM recipients WHERE email = :email")
fun getRecipientByEmailLD(email: String): LiveData<RecipientEntity?>
fun getRecipientsWithPubKeysByEmailsLD(email: String): LiveData<RecipientWithPubKeys?>

//fixed
@Transaction
Expand All @@ -68,5 +69,5 @@ interface RecipientDao : BaseDao<RecipientEntity> {

@Transaction
@Query("SELECT * FROM recipients WHERE email = :email")
fun getRecipientWithPubKeysByEmail(email: String): RecipientWithPubKeys?
suspend fun getRecipientWithPubKeysByEmail(email: String): RecipientWithPubKeys?
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import android.provider.BaseColumns
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.ForeignKey
import androidx.room.Ignore
import androidx.room.Index
import androidx.room.PrimaryKey
import com.flowcrypt.email.security.model.PgpKeyDetails

/**
* @author Denis Bondarenko
Expand Down Expand Up @@ -49,18 +51,31 @@ data class PublicKeyEntity(
@ColumnInfo(name = "fingerprint") val fingerprint: String,
@ColumnInfo(name = "public_key") val publicKey: ByteArray
) : Parcelable {

@Ignore
var pgpKeyDetails: PgpKeyDetails? = null

@Ignore
var isNotUsable: Boolean? = null

constructor(parcel: Parcel) : this(
parcel.readValue(Long::class.java.classLoader) as? Long,
requireNotNull(parcel.readString()),
requireNotNull(parcel.readString()),
requireNotNull(parcel.createByteArray())
)
) {
pgpKeyDetails = parcel.readParcelable(PgpKeyDetails::class.java.classLoader)
isNotUsable = parcel.readValue(Boolean::class.java.classLoader) as? Boolean
}


override fun writeToParcel(parcel: Parcel, flags: Int) {
parcel.writeValue(id)
parcel.writeString(recipient)
parcel.writeString(fingerprint)
parcel.writeByteArray(publicKey)
parcel.writeParcelable(pgpKeyDetails, flags)
parcel.writeValue(isNotUsable)
}

override fun describeContents(): Int {
Expand All @@ -77,6 +92,8 @@ data class PublicKeyEntity(
if (recipient != other.recipient) return false
if (fingerprint != other.fingerprint) return false
if (!publicKey.contentEquals(other.publicKey)) return false
if (pgpKeyDetails != other.pgpKeyDetails) return false
if (isNotUsable != other.isNotUsable) return false

return true
}
Expand All @@ -86,6 +103,8 @@ data class PublicKeyEntity(
result = 31 * result + recipient.hashCode()
result = 31 * result + fingerprint.hashCode()
result = 31 * result + publicKey.contentHashCode()
result = 31 * result + (pgpKeyDetails?.hashCode() ?: 0)
result = 31 * result + (isNotUsable?.hashCode() ?: 0)
return result
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@ import android.os.Parcelable
import android.provider.BaseColumns
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.Ignore
import androidx.room.Index
import androidx.room.PrimaryKey
import com.flowcrypt.email.model.PgpContact
import com.flowcrypt.email.security.model.PgpKeyDetails

/**
* @author Denis Bondarenko
Expand All @@ -37,26 +34,12 @@ data class RecipientEntity(
@ColumnInfo(name = "last_use", defaultValue = "0") val lastUse: Long = 0
) : Parcelable {

@Ignore
var pgpKeyDetails: PgpKeyDetails? = null

constructor(parcel: Parcel) : this(
parcel.readValue(Long::class.java.classLoader) as? Long,
requireNotNull(parcel.readString()),
parcel.readString(),
parcel.readLong()
) {
pgpKeyDetails = parcel.readParcelable(PgpKeyDetails::class.java.classLoader)
}

fun toPgpContact(): PgpContact {
return PgpContact(
email = email,
name = name,
lastUse = lastUse,
pgpKeyDetails = pgpKeyDetails
)
}
)

//todo-denbond7 need to think about this class.
enum class Type {
Expand All @@ -68,7 +51,6 @@ data class RecipientEntity(
parcel.writeString(email)
parcel.writeString(name)
parcel.writeLong(lastUse)
parcel.writeParcelable(pgpKeyDetails, flags)
}

override fun describeContents(): Int {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ data class RecipientWithPubKeys(
return 0
}

fun hasPgp(): Boolean {
return publicKeys.isNotEmpty()
}

fun hasNotExpiredKeys(): Boolean {
return publicKeys.any { it.pgpKeyDetails?.isExpired?.not() ?: false }
}

fun hasUsableKeys(): Boolean {
return publicKeys.any { if (it.isNotUsable != null) it.isNotUsable?.not() ?: false else true }
}

companion object CREATOR : Parcelable.Creator<RecipientWithPubKeys> {
override fun createFromParcel(parcel: Parcel) = RecipientWithPubKeys(parcel)
override fun newArray(size: Int): Array<RecipientWithPubKeys?> = arrayOfNulls(size)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package com.flowcrypt.email.extensions.org.bouncycastle.openpgp

import androidx.annotation.WorkerThread
import com.flowcrypt.email.model.PgpContact
import com.flowcrypt.email.security.model.Algo
import com.flowcrypt.email.security.model.KeyId
import com.flowcrypt.email.security.model.PgpKeyDetails
Expand Down Expand Up @@ -85,11 +84,6 @@ fun PGPKeyRing.toPgpKeyDetails(): PgpKeyDetails {
)
}

fun PGPKeyRing.pgpContacts(): List<PgpContact> {
val list = publicKey.userIDs.iterator().asSequence().toList()
return PgpContact.determinePgpContacts(list)
}

@Throws(IOException::class)
fun PGPKeyRing.armor(headers: List<Pair<String, String>>? = PgpArmor.FLOWCRYPT_HEADERS): String {
ByteArrayOutputStream().use { out ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import com.flowcrypt.email.R
import com.flowcrypt.email.api.email.model.AuthCredentials
import com.flowcrypt.email.api.retrofit.response.base.Result
import com.flowcrypt.email.database.entity.AccountEntity
import com.flowcrypt.email.jetpack.workmanager.sync.LoadContactsWorker
import com.flowcrypt.email.jetpack.workmanager.sync.LoadRecipientsWorker
import com.flowcrypt.email.security.KeyStoreCryptoManager
import com.flowcrypt.email.service.IdleService
import kotlinx.coroutines.Dispatchers
Expand Down Expand Up @@ -84,7 +84,7 @@ open class AccountViewModel(application: Application) : RoomBasicViewModel(appli
)
}

LoadContactsWorker.enqueue(getApplication())
LoadRecipientsWorker.enqueue(getApplication())

addNewAccountLiveData.value = Result.success(true)
} catch (e: Exception) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -411,22 +411,22 @@ class MsgDetailsViewModel(

private suspend fun preResultsProcessing(blocks: List<MsgBlock>) {
for (block in blocks) {
if (block is PublicKeyMsgBlock) {
val keyDetails = block.keyDetails ?: continue
val pgpContact = keyDetails.primaryPgpContact
//todo-denbond7 need to rework on this code
val recipientEntity =
roomDatabase.recipientDao().getRecipientByEmailSuspend(pgpContact.email)
//block.existingPgpContact = recipientEntity?.toPgpContact()
}
when (block) {
is PublicKeyMsgBlock -> {
val keyDetails = block.keyDetails ?: continue
val recipient = keyDetails.mimeAddresses.firstOrNull()?.address ?: continue
block.existingRecipientWithPubKeys =
roomDatabase.recipientDao().getRecipientWithPubKeysByEmail(recipient)
}

if (block is DecryptErrorMsgBlock) {
if (block.decryptErr?.details?.type == PgpDecrypt.DecryptionErrorType.NEED_PASSPHRASE) {
val fingerprints = block.decryptErr.fingerprints ?: emptyList()
if (fingerprints.isEmpty()) {
ExceptionUtil.handleError(IllegalStateException("Fingerprints were not provided"))
} else {
passphraseNeededLiveData.postValue(fingerprints)
is DecryptErrorMsgBlock -> {
if (block.decryptErr?.details?.type == PgpDecrypt.DecryptionErrorType.NEED_PASSPHRASE) {
val fingerprints = block.decryptErr.fingerprints ?: emptyList()
if (fingerprints.isEmpty()) {
ExceptionUtil.handleError(IllegalStateException("Fingerprints were not provided"))
} else {
passphraseNeededLiveData.postValue(fingerprints)
}
}
}
}
Expand Down
Loading

0 comments on commit b53ba16

Please sign in to comment.