-
Notifications
You must be signed in to change notification settings - Fork 976
Support rendering URLs inside sync barcodes #5944
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
Open
CDRussell
wants to merge
1
commit into
develop
Choose a base branch
from
feature/craig/sync_barcode_url_support
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
sync/sync-impl/src/main/java/com/duckduckgo/sync/impl/ui/qrcode/SyncBarcodeDecorator.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* | ||
* Copyright (c) 2025 DuckDuckGo | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.duckduckgo.sync.impl.ui.qrcode | ||
|
||
import com.duckduckgo.common.utils.DispatcherProvider | ||
import com.duckduckgo.di.scopes.AppScope | ||
import com.duckduckgo.sync.impl.SyncDeviceIds | ||
import com.duckduckgo.sync.impl.SyncFeature | ||
import com.duckduckgo.sync.impl.applyUrlSafetyFromB64 | ||
import com.duckduckgo.sync.impl.ui.qrcode.SyncBarcodeDecorator.CodeType | ||
import com.duckduckgo.sync.impl.ui.qrcode.SyncBarcodeDecorator.CodeType.Connect | ||
import com.duckduckgo.sync.impl.ui.qrcode.SyncBarcodeDecorator.CodeType.Exchange | ||
import com.squareup.anvil.annotations.ContributesBinding | ||
import java.net.URLEncoder | ||
import javax.inject.Inject | ||
import kotlinx.coroutines.withContext | ||
import timber.log.Timber | ||
|
||
interface SyncBarcodeDecorator { | ||
|
||
/** | ||
* Will accept a sync code and potentially modify it depending on feature flagged capabilities. | ||
* Not all code types can be modified so the type of code must be provided. | ||
* | ||
* @param originalCodeB64Encoded the original base64-encoded code to be potentially modified. | ||
* @param codeType the type of code to be decorated | ||
*/ | ||
suspend fun decorateCode( | ||
originalCodeB64Encoded: String, | ||
codeType: CodeType, | ||
): String | ||
|
||
sealed interface CodeType { | ||
data object Connect : CodeType | ||
data object Exchange : CodeType | ||
data object Recovery : CodeType | ||
} | ||
} | ||
|
||
@ContributesBinding(AppScope::class) | ||
class SyncBarcodeUrlDecorator @Inject constructor( | ||
private val syncDeviceIds: SyncDeviceIds, | ||
private val syncFeature: SyncFeature, | ||
private val dispatchers: DispatcherProvider, | ||
) : SyncBarcodeDecorator { | ||
|
||
override suspend fun decorateCode(originalCodeB64Encoded: String, codeType: CodeType): String { | ||
return withContext(dispatchers.io()) { | ||
// can only wrap codes in a URL if the feature is enabled | ||
if (!urlFeatureSupported()) { | ||
return@withContext originalCodeB64Encoded | ||
} | ||
|
||
// only `Connect` and `Exchange` codes can be wrapped in a URL | ||
when (codeType) { | ||
is Connect -> originalCodeB64Encoded.wrapInUrl() | ||
is Exchange -> originalCodeB64Encoded.wrapInUrl() | ||
else -> originalCodeB64Encoded | ||
} | ||
}.also { | ||
Timber.v("Sync: code to include in the barcode is $it") | ||
} | ||
} | ||
|
||
private fun urlFeatureSupported(): Boolean { | ||
return syncFeature.syncSetupBarcodeIsUrlBased().isEnabled() | ||
} | ||
|
||
private fun String.wrapInUrl(): String { | ||
return kotlin.runCatching { | ||
val urlSafeCode = this.applyUrlSafetyFromB64() | ||
SyncBarcodeUrl(webSafeB64EncodedCode = urlSafeCode, urlEncodedDeviceName = getDeviceName()).asUrl() | ||
}.getOrElse { | ||
Timber.w("Sync-url: Failed to encode string for use inside a URL; returning original code") | ||
this | ||
} | ||
} | ||
|
||
private fun getDeviceName(): String { | ||
val deviceName = syncDeviceIds.deviceName() | ||
return URLEncoder.encode(deviceName, "UTF-8") | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
sync/sync-impl/src/main/java/com/duckduckgo/sync/impl/ui/qrcode/SyncBarcodeUrl.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright (c) 2025 DuckDuckGo | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.duckduckgo.sync.impl.ui.qrcode | ||
|
||
import androidx.core.net.toUri | ||
|
||
data class SyncBarcodeUrl( | ||
val webSafeB64EncodedCode: String, | ||
val urlEncodedDeviceName: String? = null, | ||
) { | ||
|
||
fun asUrl(): String { | ||
val sb = StringBuilder(URL_BASE) | ||
.append("&") | ||
.append(CODE_PARAM).append("=").append(webSafeB64EncodedCode) | ||
|
||
if (urlEncodedDeviceName?.isNotBlank() == true) { | ||
sb.append("&") | ||
sb.append(DEVICE_NAME_PARAM).append("=").append(urlEncodedDeviceName) | ||
} | ||
|
||
return sb.toString() | ||
} | ||
|
||
companion object { | ||
const val URL_BASE = "https://duckduckgo.com/sync/pairing/#" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this might need to change in a later PR because we’ll need to be able to access this from |
||
private const val CODE_PARAM = "code" | ||
private const val DEVICE_NAME_PARAM = "deviceName" | ||
|
||
fun parseUrl(fullSyncUrl: String): SyncBarcodeUrl? { | ||
return kotlin.runCatching { | ||
if (!fullSyncUrl.startsWith(URL_BASE)) { | ||
return null | ||
} | ||
|
||
val uri = fullSyncUrl.toUri() | ||
val fragment = uri.fragment ?: return null | ||
val fragmentParts = fragment.split("&") | ||
|
||
val code = fragmentParts | ||
.find { it.startsWith("code=") } | ||
?.substringAfter("code=") | ||
?: return null | ||
|
||
val deviceName = fragmentParts | ||
.find { it.startsWith("deviceName=") } | ||
?.substringAfter("deviceName=") | ||
|
||
SyncBarcodeUrl(webSafeB64EncodedCode = code, urlEncodedDeviceName = deviceName) | ||
}.getOrNull() | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
want to make sure we use the underlying code and not the URL