Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(e2ei): expose thumbprint and userhandle with e2ei cert (WPB-8606) 🍒 🍒 #2784

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ import com.wire.kalium.logic.data.user.type.DomainUserTypeMapper
import com.wire.kalium.logic.data.user.type.DomainUserTypeMapperImpl
import com.wire.kalium.logic.data.user.type.UserEntityTypeMapper
import com.wire.kalium.logic.data.user.type.UserEntityTypeMapperImpl
import com.wire.kalium.logic.feature.e2ei.CertificateStatusMapper
import com.wire.kalium.logic.feature.e2ei.CertificateStatusMapperImpl

@Suppress("TooManyFunctions")
internal object MapperProvider {
Expand Down Expand Up @@ -175,4 +177,5 @@ internal object MapperProvider {
fun serviceMapper(): ServiceMapper = ServiceMapper()
fun legalHoldStatusMapper(): LegalHoldStatusMapper = LegalHoldStatusMapperImpl
fun acmeMapper(): AcmeMapper = AcmeMapperImpl()
fun certificateStatusMapper(): CertificateStatusMapper = CertificateStatusMapperImpl()
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,28 @@
package com.wire.kalium.logic.feature.e2ei

import com.wire.kalium.cryptography.WireIdentity
import com.wire.kalium.logic.di.MapperProvider
import kotlinx.datetime.Instant

data class E2eiCertificate(
var userHandle: String,
val status: CertificateStatus,
val serialNumber: String,
val certificateDetail: String,
val thumbprint: String,
val endAt: Instant
) {
companion object {
fun fromWireIdentity(identity: WireIdentity, certificateStatusMapper: CertificateStatusMapper): E2eiCertificate? =
private val certificateStatusMapper = MapperProvider.certificateStatusMapper()

fun fromWireIdentity(identity: WireIdentity): E2eiCertificate? =
identity.certificate?.let {
E2eiCertificate(
userHandle = it.handle.handle,
status = certificateStatusMapper.toCertificateStatus(identity.status),
serialNumber = it.serialNumber,
certificateDetail = it.certificate,
thumbprint = it.thumbprint,
endAt = Instant.fromEpochSeconds(it.endTimestampSeconds)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package com.wire.kalium.logic.feature.e2ei.usecase

import com.wire.kalium.logic.data.conversation.ClientId
import com.wire.kalium.logic.data.conversation.MLSConversationRepository
import com.wire.kalium.logic.feature.e2ei.CertificateStatusMapper
import com.wire.kalium.logic.feature.e2ei.E2eiCertificate
import com.wire.kalium.logic.functional.fold

Expand All @@ -31,15 +30,14 @@ interface GetE2eiCertificateUseCase {
}

class GetE2eiCertificateUseCaseImpl internal constructor(
private val mlsConversationRepository: MLSConversationRepository,
private val certificateStatusMapper: CertificateStatusMapper
private val mlsConversationRepository: MLSConversationRepository
) : GetE2eiCertificateUseCase {
override suspend operator fun invoke(clientId: ClientId): GetE2EICertificateUseCaseResult =
mlsConversationRepository.getClientIdentity(clientId).fold(
{ GetE2EICertificateUseCaseResult.Failure },
{
it?.let {
E2eiCertificate.fromWireIdentity(it, certificateStatusMapper)?.let { certificate ->
E2eiCertificate.fromWireIdentity(it)?.let { certificate ->
GetE2EICertificateUseCaseResult.Success(certificate)
}
} ?: GetE2EICertificateUseCaseResult.NotActivated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import com.wire.kalium.logic.data.conversation.MLSConversationRepository
import com.wire.kalium.logic.data.id.ConversationId
import com.wire.kalium.logic.data.user.UserId
import com.wire.kalium.logic.feature.e2ei.CertificateStatus
import com.wire.kalium.logic.feature.e2ei.CertificateStatusMapper
import com.wire.kalium.logic.feature.e2ei.E2eiCertificate
import com.wire.kalium.logic.functional.fold

Expand All @@ -35,15 +34,14 @@ interface GetMembersE2EICertificateStatusesUseCase {
}

class GetMembersE2EICertificateStatusesUseCaseImpl internal constructor(
private val mlsConversationRepository: MLSConversationRepository,
private val certificateStatusMapper: CertificateStatusMapper
private val mlsConversationRepository: MLSConversationRepository
) : GetMembersE2EICertificateStatusesUseCase {
override suspend operator fun invoke(conversationId: ConversationId, userIds: List<UserId>): Map<UserId, CertificateStatus?> =
mlsConversationRepository.getMembersIdentities(conversationId, userIds).fold(
{ mapOf() },
{
it.mapValues { (_, identities) ->
identities.getUserCertificateStatus(certificateStatusMapper)
identities.getUserCertificateStatus()
}
}
)
Expand All @@ -55,9 +53,9 @@ class GetMembersE2EICertificateStatusesUseCaseImpl internal constructor(
* [CertificateStatus.EXPIRED] if any certificate is expired;
* [CertificateStatus.VALID] otherwise.
*/
fun List<WireIdentity>.getUserCertificateStatus(certificateStatusMapper: CertificateStatusMapper): CertificateStatus? {
fun List<WireIdentity>.getUserCertificateStatus(): CertificateStatus? {
val certificates = this.map {
E2eiCertificate.fromWireIdentity(it, certificateStatusMapper)
E2eiCertificate.fromWireIdentity(it)
}
return if (certificates.isEmpty() || certificates.any { it == null }) {
null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package com.wire.kalium.logic.feature.e2ei.usecase
import com.wire.kalium.logic.data.conversation.ClientId
import com.wire.kalium.logic.data.conversation.MLSConversationRepository
import com.wire.kalium.logic.data.user.UserId
import com.wire.kalium.logic.feature.e2ei.CertificateStatusMapper
import com.wire.kalium.logic.feature.e2ei.E2eiCertificate
import com.wire.kalium.logic.feature.user.IsE2EIEnabledUseCase
import com.wire.kalium.logic.functional.getOrElse
Expand All @@ -36,15 +35,14 @@ interface GetUserE2eiCertificatesUseCase {

class GetUserE2eiCertificatesUseCaseImpl internal constructor(
private val mlsConversationRepository: MLSConversationRepository,
private val isE2EIEnabledUseCase: IsE2EIEnabledUseCase,
private val certificateStatusMapper: CertificateStatusMapper
private val isE2EIEnabledUseCase: IsE2EIEnabledUseCase
) : GetUserE2eiCertificatesUseCase {
override suspend operator fun invoke(userId: UserId): Map<String, E2eiCertificate> =
if (isE2EIEnabledUseCase()) {
mlsConversationRepository.getUserIdentity(userId).map { identities ->
val result = mutableMapOf<String, E2eiCertificate>()
identities.forEach {
E2eiCertificate.fromWireIdentity(it, certificateStatusMapper)?.let { certificate ->
E2eiCertificate.fromWireIdentity(it)?.let { certificate ->
result[it.clientId.value] = certificate
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package com.wire.kalium.logic.feature.e2ei.usecase
import com.wire.kalium.logic.data.conversation.MLSConversationRepository
import com.wire.kalium.logic.data.user.UserId
import com.wire.kalium.logic.feature.e2ei.CertificateStatus
import com.wire.kalium.logic.feature.e2ei.CertificateStatusMapper
import com.wire.kalium.logic.feature.user.IsE2EIEnabledUseCase
import com.wire.kalium.logic.functional.fold

Expand All @@ -33,7 +32,6 @@ interface GetUserE2eiCertificateStatusUseCase {

class GetUserE2eiCertificateStatusUseCaseImpl internal constructor(
private val mlsConversationRepository: MLSConversationRepository,
private val certificateStatusMapper: CertificateStatusMapper,
private val isE2EIEnabledUseCase: IsE2EIEnabledUseCase
) : GetUserE2eiCertificateStatusUseCase {
override suspend operator fun invoke(userId: UserId): GetUserE2eiCertificateStatusResult =
Expand All @@ -43,7 +41,7 @@ class GetUserE2eiCertificateStatusUseCaseImpl internal constructor(
GetUserE2eiCertificateStatusResult.Failure.NotActivated
},
{ identities ->
identities.getUserCertificateStatus(certificateStatusMapper)?.let {
identities.getUserCertificateStatus()?.let {
GetUserE2eiCertificateStatusResult.Success(it)
} ?: GetUserE2eiCertificateStatusResult.Failure.NotActivated
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ import com.wire.kalium.logic.feature.client.FinalizeMLSClientAfterE2EIEnrollment
import com.wire.kalium.logic.feature.conversation.GetAllContactsNotInConversationUseCase
import com.wire.kalium.logic.feature.e2ei.CertificateRevocationListCheckWorker
import com.wire.kalium.logic.feature.e2ei.CertificateRevocationListCheckWorkerImpl
import com.wire.kalium.logic.feature.e2ei.CertificateStatusMapperImpl
import com.wire.kalium.logic.feature.e2ei.usecase.EnrollE2EIUseCase
import com.wire.kalium.logic.feature.e2ei.usecase.EnrollE2EIUseCaseImpl
import com.wire.kalium.logic.feature.e2ei.usecase.GetE2eiCertificateUseCase
Expand Down Expand Up @@ -119,7 +118,6 @@ class UserScope internal constructor(
val observeUserInfo: ObserveUserInfoUseCase get() = ObserveUserInfoUseCaseImpl(userRepository, teamRepository)
val uploadUserAvatar: UploadUserAvatarUseCase get() = UploadUserAvatarUseCaseImpl(userRepository, assetRepository)

private val certificateStatusMapper by lazy { CertificateStatusMapperImpl() }
val getPublicAsset: GetAvatarAssetUseCase get() = GetAvatarAssetUseCaseImpl(assetRepository, userRepository)
val enrollE2EI: EnrollE2EIUseCase get() = EnrollE2EIUseCaseImpl(e2EIRepository)

Expand All @@ -130,25 +128,21 @@ class UserScope internal constructor(
)
val getE2EICertificate: GetE2eiCertificateUseCase
get() = GetE2eiCertificateUseCaseImpl(
mlsConversationRepository = mlsConversationRepository,
certificateStatusMapper = certificateStatusMapper
mlsConversationRepository = mlsConversationRepository
)
val getUserE2eiCertificateStatus: GetUserE2eiCertificateStatusUseCase
get() = GetUserE2eiCertificateStatusUseCaseImpl(
mlsConversationRepository = mlsConversationRepository,
certificateStatusMapper = certificateStatusMapper,
isE2EIEnabledUseCase = isE2EIEnabledUseCase
)
val getUserE2eiCertificates: GetUserE2eiCertificatesUseCase
get() = GetUserE2eiCertificatesUseCaseImpl(
mlsConversationRepository = mlsConversationRepository,
certificateStatusMapper = certificateStatusMapper,
isE2EIEnabledUseCase = isE2EIEnabledUseCase
)
val getMembersE2EICertificateStatuses: GetMembersE2EICertificateStatusesUseCase
get() = GetMembersE2EICertificateStatusesUseCaseImpl(
mlsConversationRepository = mlsConversationRepository,
certificateStatusMapper = certificateStatusMapper
mlsConversationRepository = mlsConversationRepository
)
val deleteAsset: DeleteAssetUseCase get() = DeleteAssetUseCaseImpl(assetRepository)
val setUserHandle: SetUserHandleUseCase get() = SetUserHandleUseCase(accountRepository, validateUserHandleUseCase, syncManager)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,9 +367,11 @@ class ObserveE2EIRequiredUseCaseTest {
companion object {
private val MLS_E2EI_SETTING = E2EISettings(true, "some_url", null)
private val VALID_CERTIFICATE = E2eiCertificate(
userHandle = "userHandle",
serialNumber = "serialNumber",
certificateDetail = "certificateDetail",
status = CertificateStatus.VALID,
thumbprint = "thumbprint",
endAt = DateTimeUtil.currentInstant().plus(1.days)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ class GetE2eiCertificateUseCaseTest {
runTest {
val (arrangement, getE2eiCertificateUseCase) = Arrangement()
.withRepositoryValidCertificate(IDENTITY)
.withMapperReturning(CertificateStatus.EXPIRED)
.arrange()

val result = getE2eiCertificateUseCase.invoke(CLIENT_ID)
Expand All @@ -89,10 +88,6 @@ class GetE2eiCertificateUseCaseTest {
arrangement.mlsConversationRepository.getClientIdentity(any())
}.wasInvoked(once)

verify {
arrangement.certificateStatusMapper.toCertificateStatus(any())
}.wasInvoked(once)

assertEquals(true, result is GetE2EICertificateUseCaseResult.Success)
}

Expand All @@ -109,10 +104,6 @@ class GetE2eiCertificateUseCaseTest {
arrangement.mlsConversationRepository.getClientIdentity(any())
}.wasInvoked(once)

verify {
arrangement.certificateStatusMapper.toCertificateStatus(any())
}.wasNotInvoked()

assertEquals(true, result is GetE2EICertificateUseCaseResult.NotActivated)
}

Expand All @@ -121,12 +112,8 @@ class GetE2eiCertificateUseCaseTest {
@Mock
val mlsConversationRepository = mock(MLSConversationRepository::class)

@Mock
val certificateStatusMapper = mock(CertificateStatusMapper::class)

fun arrange() = this to GetE2eiCertificateUseCaseImpl(
mlsConversationRepository = mlsConversationRepository,
certificateStatusMapper = certificateStatusMapper
mlsConversationRepository = mlsConversationRepository
)

suspend fun withRepositoryFailure(failure: CoreFailure = E2EIFailure.Generic(Exception())) = apply {
Expand All @@ -140,12 +127,6 @@ class GetE2eiCertificateUseCaseTest {
mlsConversationRepository.getClientIdentity(any())
}.returns(Either.Right(identity))
}

fun withMapperReturning(status: CertificateStatus) = apply {
every {
certificateStatusMapper.toCertificateStatus(any())
}.returns(status)
}
}

companion object {
Expand All @@ -155,7 +136,14 @@ class GetE2eiCertificateUseCaseTest {
CryptoQualifiedClientId("clientId", USER_ID.toCrypto())

val e2eiCertificate =
E2eiCertificate(CertificateStatus.EXPIRED, "serialNumber", "certificateDetail", Instant.DISTANT_FUTURE)
E2eiCertificate(
userHandle = "handle",
status = CertificateStatus.EXPIRED,
serialNumber = "serialNumber",
certificateDetail = "certificateDetail",
endAt = Instant.DISTANT_FUTURE,
thumbprint = "thumbprint"
)
val IDENTITY = WireIdentity(
CRYPTO_QUALIFIED_CLIENT_ID,
handle = "alic_test",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ import com.wire.kalium.logic.data.id.toCrypto
import com.wire.kalium.logic.data.user.UserId
import com.wire.kalium.logic.feature.e2ei.usecase.GetMembersE2EICertificateStatusesUseCaseImpl
import com.wire.kalium.logic.functional.Either
import com.wire.kalium.logic.util.arrangement.mls.CertificateStatusMapperArrangement
import com.wire.kalium.logic.util.arrangement.mls.CertificateStatusMapperArrangementImpl
import com.wire.kalium.logic.util.arrangement.mls.MLSConversationRepositoryArrangement
import com.wire.kalium.logic.util.arrangement.mls.MLSConversationRepositoryArrangementImpl
import io.mockative.matchers.EqualsMatcher
Expand Down Expand Up @@ -108,27 +106,12 @@ class GetMembersE2EICertificateStatusesUseCaseTest {
}

private class Arrangement(private val block: suspend Arrangement.() -> Unit) :
MLSConversationRepositoryArrangement by MLSConversationRepositoryArrangementImpl(),
CertificateStatusMapperArrangement by CertificateStatusMapperArrangementImpl() {
MLSConversationRepositoryArrangement by MLSConversationRepositoryArrangementImpl() {

fun arrange() = run {
withCertificateStatusMapperReturning(
CertificateStatus.VALID,
EqualsMatcher(CryptoCertificateStatus.VALID)
)
withCertificateStatusMapperReturning(
CertificateStatus.EXPIRED,
EqualsMatcher(CryptoCertificateStatus.EXPIRED)
)
withCertificateStatusMapperReturning(
CertificateStatus.REVOKED,
EqualsMatcher(CryptoCertificateStatus.REVOKED)
)

runBlocking { block() }
this@Arrangement to GetMembersE2EICertificateStatusesUseCaseImpl(
mlsConversationRepository = mlsConversationRepository,
certificateStatusMapper = certificateStatusMapper
mlsConversationRepository = mlsConversationRepository
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,12 @@ import com.wire.kalium.logic.data.id.toCrypto
import com.wire.kalium.logic.data.user.UserId
import com.wire.kalium.logic.feature.e2ei.usecase.GetUserE2eiCertificatesUseCaseImpl
import com.wire.kalium.logic.functional.Either
import com.wire.kalium.logic.util.arrangement.mls.CertificateStatusMapperArrangement
import com.wire.kalium.logic.util.arrangement.mls.CertificateStatusMapperArrangementImpl
import com.wire.kalium.logic.util.arrangement.mls.IsE2EIEnabledUseCaseArrangement
import com.wire.kalium.logic.util.arrangement.mls.IsE2EIEnabledUseCaseArrangementImpl
import com.wire.kalium.logic.util.arrangement.mls.MLSConversationRepositoryArrangement
import com.wire.kalium.logic.util.arrangement.mls.MLSConversationRepositoryArrangementImpl
import io.mockative.any
import io.mockative.coVerify
import io.mockative.matchers.EqualsMatcher
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.runTest
import kotlin.test.Test
Expand Down Expand Up @@ -116,27 +113,12 @@ class GetUserE2eiAllCertificateStatusesUseCaseTest {

private class Arrangement(private val block: suspend Arrangement.() -> Unit) :
MLSConversationRepositoryArrangement by MLSConversationRepositoryArrangementImpl(),
CertificateStatusMapperArrangement by CertificateStatusMapperArrangementImpl(),
IsE2EIEnabledUseCaseArrangement by IsE2EIEnabledUseCaseArrangementImpl() {

fun arrange() = run {
withCertificateStatusMapperReturning(
CertificateStatus.VALID,
EqualsMatcher(CryptoCertificateStatus.VALID)
)
withCertificateStatusMapperReturning(
CertificateStatus.EXPIRED,
EqualsMatcher(CryptoCertificateStatus.EXPIRED)
)
withCertificateStatusMapperReturning(
CertificateStatus.REVOKED,
EqualsMatcher(CryptoCertificateStatus.REVOKED)
)

runBlocking { block() }
this@Arrangement to GetUserE2eiCertificatesUseCaseImpl(
mlsConversationRepository = mlsConversationRepository,
certificateStatusMapper = certificateStatusMapper,
isE2EIEnabledUseCase = isE2EIEnabledUseCase
)
}
Expand Down
Loading
Loading