|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 DuckDuckGo |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.duckduckgo.sync.impl.ui.qrcode |
| 18 | + |
| 19 | +import com.duckduckgo.common.utils.DispatcherProvider |
| 20 | +import com.duckduckgo.di.scopes.AppScope |
| 21 | +import com.duckduckgo.sync.impl.SyncDeviceIds |
| 22 | +import com.duckduckgo.sync.impl.SyncFeature |
| 23 | +import com.duckduckgo.sync.impl.applyUrlSafetyFromB64 |
| 24 | +import com.duckduckgo.sync.impl.ui.qrcode.SyncBarcodeDecorator.CodeType |
| 25 | +import com.duckduckgo.sync.impl.ui.qrcode.SyncBarcodeDecorator.CodeType.Connect |
| 26 | +import com.duckduckgo.sync.impl.ui.qrcode.SyncBarcodeDecorator.CodeType.Exchange |
| 27 | +import com.squareup.anvil.annotations.ContributesBinding |
| 28 | +import java.net.URLEncoder |
| 29 | +import javax.inject.Inject |
| 30 | +import kotlinx.coroutines.withContext |
| 31 | +import timber.log.Timber |
| 32 | + |
| 33 | +interface SyncBarcodeDecorator { |
| 34 | + |
| 35 | + /** |
| 36 | + * Will accept a sync code and potentially modify it depending on feature flagged capabilities. |
| 37 | + * Not all code types can be modified so the type of code must be provided. |
| 38 | + * |
| 39 | + * @param originalCodeB64Encoded the original base64-encoded code to be potentially modified. |
| 40 | + * @param codeType the type of code to be decorated |
| 41 | + */ |
| 42 | + suspend fun decorateCode( |
| 43 | + originalCodeB64Encoded: String, |
| 44 | + codeType: CodeType, |
| 45 | + ): String |
| 46 | + |
| 47 | + sealed interface CodeType { |
| 48 | + data object Connect : CodeType |
| 49 | + data object Exchange : CodeType |
| 50 | + data object Recovery : CodeType |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +@ContributesBinding(AppScope::class) |
| 55 | +class SyncBarcodeUrlDecorator @Inject constructor( |
| 56 | + private val syncDeviceIds: SyncDeviceIds, |
| 57 | + private val syncFeature: SyncFeature, |
| 58 | + private val dispatchers: DispatcherProvider, |
| 59 | +) : SyncBarcodeDecorator { |
| 60 | + |
| 61 | + override suspend fun decorateCode(originalCodeB64Encoded: String, codeType: CodeType): String { |
| 62 | + return withContext(dispatchers.io()) { |
| 63 | + // can only wrap codes in a URL if the feature is enabled |
| 64 | + if (!urlFeatureSupported()) { |
| 65 | + return@withContext originalCodeB64Encoded |
| 66 | + } |
| 67 | + |
| 68 | + // only `Connect` and `Exchange` codes can be wrapped in a URL |
| 69 | + when (codeType) { |
| 70 | + is Connect -> originalCodeB64Encoded.wrapInUrl() |
| 71 | + is Exchange -> originalCodeB64Encoded.wrapInUrl() |
| 72 | + else -> originalCodeB64Encoded |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + private fun urlFeatureSupported(): Boolean { |
| 78 | + return syncFeature.syncSetupBarcodeIsUrlBased().isEnabled() |
| 79 | + } |
| 80 | + |
| 81 | + private fun String.wrapInUrl(): String { |
| 82 | + return kotlin.runCatching { |
| 83 | + val urlSafeCode = this.applyUrlSafetyFromB64() |
| 84 | + SyncBarcodeUrl(webSafeB64EncodedCode = urlSafeCode, urlEncodedDeviceName = getDeviceName()).asUrl() |
| 85 | + }.getOrElse { |
| 86 | + Timber.w("Sync-url: Failed to encode string for use inside a URL; returning original code") |
| 87 | + this |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + private fun getDeviceName(): String { |
| 92 | + val deviceName = syncDeviceIds.deviceName() |
| 93 | + return URLEncoder.encode(deviceName, "UTF-8") |
| 94 | + } |
| 95 | +} |
0 commit comments