Skip to content

Commit

Permalink
fix(e2ei): pass challenges to usecase and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mchenani committed Jan 30, 2024
1 parent b181a34 commit c528d38
Show file tree
Hide file tree
Showing 8 changed files with 401 additions and 346 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@ import com.wire.kalium.cryptography.NewAcmeAuthz
import com.wire.kalium.network.api.base.unbound.acme.ACMEAuthorizationResponse
import com.wire.kalium.network.api.base.unbound.acme.DtoAuthorizationChallengeType

class AcmeMapper {
fun fromDto(dto: ACMEAuthorizationResponse, newAcmeAuthz: NewAcmeAuthz) = AcmeAuthorization(
interface AcmeMapper{
fun fromDto(dto: ACMEAuthorizationResponse, newAcmeAuthz: NewAcmeAuthz): AcmeAuthorization
}

class AcmeMapperImpl: AcmeMapper {
override fun fromDto(dto: ACMEAuthorizationResponse, newAcmeAuthz: NewAcmeAuthz) = AcmeAuthorization(
nonce = Nonce(dto.nonce),
location = dto.location,
response = dto.response,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
@file:Suppress("konsist.repositoriesShouldNotAccessFeaturePackageClasses", "TooManyFunctions")
@file:Suppress("TooManyFunctions")

package com.wire.kalium.logic.data.e2ei

Expand All @@ -30,6 +30,7 @@ import com.wire.kalium.logic.data.client.MLSClientProvider
import com.wire.kalium.logic.data.conversation.ClientId
import com.wire.kalium.logic.data.conversation.MLSConversationRepository
import com.wire.kalium.logic.data.id.CurrentClientIdProvider
import com.wire.kalium.logic.di.MapperProvider
import com.wire.kalium.logic.feature.e2ei.usecase.E2EIEnrollmentResult
import com.wire.kalium.logic.functional.Either
import com.wire.kalium.logic.functional.flatMap
Expand All @@ -51,7 +52,7 @@ import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json

interface E2EIRepository {
suspend fun initE2EIClient(clientId: ClientId? = null, isNewClient: Boolean = false): Either<CoreFailure, Unit>
suspend fun initFreshE2EIClient(clientId: ClientId? = null, isNewClient: Boolean = false): Either<CoreFailure, Unit>
suspend fun fetchAndSetTrustAnchors(): Either<CoreFailure, Unit>
suspend fun loadACMEDirectories(): Either<CoreFailure, AcmeDirectory>
suspend fun getACMENonce(endpoint: String): Either<CoreFailure, Nonce>
Expand Down Expand Up @@ -102,17 +103,19 @@ class E2EIRepositoryImpl(
private val currentClientIdProvider: CurrentClientIdProvider,
private val mlsConversationRepository: MLSConversationRepository,
private val userConfigRepository: UserConfigRepository,
private val acmeMapper: AcmeMapper = AcmeMapper()
private val acmeMapper: AcmeMapper = MapperProvider.acmeMapper()
) : E2EIRepository {

override suspend fun initE2EIClient(clientId: ClientId?, isNewClient: Boolean): Either<CoreFailure, Unit> =
e2EIClientProvider.getE2EIClient(clientId, isNewClient).fold({
override suspend fun initFreshE2EIClient(clientId: ClientId?, isNewClient: Boolean): Either<CoreFailure, Unit> {
nukeE2EIClient()
return e2EIClientProvider.getE2EIClient(clientId, isNewClient).fold({
kaliumLogger.w("E2EI client initialization failed: $it")
Either.Left(it)
}, {
kaliumLogger.w("E2EI client initialized for enrollment")
Either.Right(Unit)
})
}

override suspend fun fetchAndSetTrustAnchors(): Either<CoreFailure, Unit> = userConfigRepository.getE2EISettings().flatMap {
wrapApiRequest {
Expand Down Expand Up @@ -184,8 +187,8 @@ class E2EIRepositoryImpl(
): Either<CoreFailure, AuthorizationResult> {
var nonce = prevNonce
val challenges = mutableMapOf<AuthorizationChallengeType, NewAcmeAuthz>()
val oidcAuthorization: NewAcmeAuthz? = null
val dpopAuthorization: NewAcmeAuthz? = null
var oidcAuthorization: NewAcmeAuthz? = null
var dpopAuthorization: NewAcmeAuthz? = null

authorizationsEndpoints.forEach { endPoint ->
val authorizationResponse = createAuthorization(nonce, endPoint).getOrFail {
Expand All @@ -195,6 +198,9 @@ class E2EIRepositoryImpl(
challenges[authorizationResponse.challengeType] = authorizationResponse.newAcmeAuthz
}

oidcAuthorization = challenges[AuthorizationChallengeType.OIDC]
dpopAuthorization = challenges[AuthorizationChallengeType.DPoP]

if (oidcAuthorization == null || dpopAuthorization == null)
return Either.Left(CoreFailure.Unknown(Throwable("Missing ACME Challenges")))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ import com.wire.kalium.logic.data.conversation.ProtocolInfoMapper
import com.wire.kalium.logic.data.conversation.ProtocolInfoMapperImpl
import com.wire.kalium.logic.data.conversation.ReceiptModeMapper
import com.wire.kalium.logic.data.conversation.ReceiptModeMapperImpl
import com.wire.kalium.logic.data.e2ei.AcmeMapper
import com.wire.kalium.logic.data.e2ei.AcmeMapperImpl
import com.wire.kalium.logic.data.event.EventMapper
import com.wire.kalium.logic.data.featureConfig.FeatureConfigMapper
import com.wire.kalium.logic.data.featureConfig.FeatureConfigMapperImpl
Expand Down Expand Up @@ -171,4 +173,5 @@ internal object MapperProvider {
fun sendMessagePartialFailureMapper(): SendMessagePartialFailureMapper = SendMessagePartialFailureMapperImpl()
fun serviceMapper(): ServiceMapper = ServiceMapper()
fun legalHoldStatusMapper(): LegalHoldStatusMapper = LegalHoldStatusMapperImpl
fun acmeMapper(): AcmeMapper = AcmeMapperImpl()
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class EnrollE2EIUseCaseImpl internal constructor(
override suspend fun initialEnrollment(isNewClientRegistration: Boolean): Either<CoreFailure, E2EIEnrollmentResult> {
kaliumLogger.i("start E2EI Enrollment Initialization")

e2EIRepository.initE2EIClient(isNewClient = isNewClientRegistration)
e2EIRepository.initFreshE2EIClient(isNewClient = isNewClientRegistration)

e2EIRepository.fetchAndSetTrustAnchors()

Expand Down Expand Up @@ -105,7 +105,7 @@ class EnrollE2EIUseCaseImpl internal constructor(
oidcAuthorizations.challenge.url
),
dPopAuthorizations = dPopAuthorizations,
oidcAuthorizations = dPopAuthorizations,
oidcAuthorizations = oidcAuthorizations,
lastNonce = prevNonce,
orderLocation = newOrderResponse.third,
isNewClientRegistration = isNewClientRegistration
Expand Down Expand Up @@ -201,8 +201,6 @@ class EnrollE2EIUseCaseImpl internal constructor(
).toEitherLeft()
}

e2EIRepository.nukeE2EIClient()

return Either.Right(E2EIEnrollmentResult.Finalized(certificateRequest.response.decodeToString()))
}

Expand Down Expand Up @@ -235,7 +233,6 @@ class EnrollE2EIUseCaseImpl internal constructor(

sealed interface E2EIEnrollmentResult {
enum class E2EIStep {
TrustAnchors,
AcmeNonce,
AcmeDirectories,
AcmeNewAccount,
Expand All @@ -254,7 +251,7 @@ sealed interface E2EIEnrollmentResult {
}

@Suppress("LongParameterList")
class Initialized(
data class Initialized(
val target: String,
val oAuthState: String?,
val oAuthClaims: JsonObject,
Expand Down
Loading

0 comments on commit c528d38

Please sign in to comment.