refactor: Align User-Agent with what official Bitwarden clients send #324

This commit is contained in:
Artem Chepurnoy 2024-06-30 20:55:52 +03:00
parent eb925db5ae
commit 98de0b7cfb
No known key found for this signature in database
GPG Key ID: FAC37D0CF674043E
5 changed files with 47 additions and 10 deletions

View File

@ -0,0 +1,8 @@
package com.artemchep.keyguard.platform.util
import android.os.Build
import com.artemchep.keyguard.platform.Platform
import com.artemchep.keyguard.provider.bitwarden.api.BitwardenPersona
actual val Platform.userAgent: String
get() = "Bitwarden_Mobile/${BitwardenPersona.CLIENT_VERSION} (Android ${Build.VERSION.RELEASE}; SDK ${Build.VERSION.SDK_INT}; Model ${Build.MODEL})"

View File

@ -0,0 +1,5 @@
package com.artemchep.keyguard.platform.util
import com.artemchep.keyguard.platform.Platform
expect val Platform.userAgent: String

View File

@ -11,6 +11,7 @@ import com.artemchep.keyguard.common.util.int
import com.artemchep.keyguard.core.store.bitwarden.BitwardenToken
import com.artemchep.keyguard.platform.CurrentPlatform
import com.artemchep.keyguard.platform.Platform
import com.artemchep.keyguard.platform.util.userAgent
import com.artemchep.keyguard.provider.bitwarden.ServerEnv
import com.artemchep.keyguard.provider.bitwarden.ServerTwoFactorToken
import com.artemchep.keyguard.provider.bitwarden.api.builder.api
@ -49,43 +50,53 @@ data class BitwardenPersona(
val clientVersion: String,
val deviceType: String,
val deviceName: String,
val userAgent: String,
) {
companion object {
private const val CLIENT_VERSION = "2024.4.0"
const val CLIENT_VERSION = "2024.4.0"
fun of(platform: Platform) = when (platform) {
is Platform.Mobile -> desktopLinux()
is Platform.Mobile -> {
Platform.Desktop.Linux.bitwardenPersona()
}
is Platform.Desktop -> when (platform) {
is Platform.Desktop.Windows -> desktopWindows()
is Platform.Desktop.MacOS -> desktopMacOs()
is Platform.Desktop.Windows -> platform.bitwardenPersona()
is Platform.Desktop.MacOS -> platform.bitwardenPersona()
is Platform.Desktop.Other,
is Platform.Desktop.Linux,
-> desktopLinux()
-> Platform.Desktop.Linux.bitwardenPersona()
}
}
private fun desktopLinux() = BitwardenPersona(
private fun Platform.Desktop.Linux.bitwardenPersona(
) = BitwardenPersona(
clientId = "desktop",
clientName = "desktop",
clientVersion = CLIENT_VERSION,
deviceType = "8",
deviceName = "linux",
userAgent = userAgent,
)
private fun desktopMacOs() = BitwardenPersona(
private fun Platform.Desktop.MacOS.bitwardenPersona(
) = BitwardenPersona(
clientId = "desktop",
clientName = "desktop",
clientVersion = CLIENT_VERSION,
deviceType = "7",
deviceName = "macos",
userAgent = userAgent,
)
private fun desktopWindows() = BitwardenPersona(
private fun Platform.Desktop.Windows.bitwardenPersona(
) = BitwardenPersona(
clientId = "desktop",
clientName = "desktop",
clientVersion = CLIENT_VERSION,
deviceType = "6",
deviceName = "windows",
userAgent = userAgent,
)
}
}

View File

@ -0,0 +1,12 @@
package com.artemchep.keyguard.platform.util
import com.artemchep.keyguard.platform.Platform
// Taken from:
// https://releases.electronjs.org/releases/stable
private const val CHROME_VERSION = "126.0.6478.114"
// Seems like desktop clients always use the Windows user-agents for
// privacy reasons.
actual val Platform.userAgent: String
get() = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/$CHROME_VERSION Safari/537.36"

View File

@ -359,7 +359,9 @@ import com.artemchep.keyguard.core.store.bitwarden.BitwardenCipher
import com.artemchep.keyguard.crypto.CipherEncryptorImpl
import com.artemchep.keyguard.crypto.CryptoGeneratorJvm
import com.artemchep.keyguard.crypto.FileEncryptorImpl
import com.artemchep.keyguard.platform.CurrentPlatform
import com.artemchep.keyguard.platform.util.isRelease
import com.artemchep.keyguard.platform.util.userAgent
import com.artemchep.keyguard.provider.bitwarden.usecase.CipherUrlCheckImpl
import com.artemchep.keyguard.provider.bitwarden.usecase.CipherUrlDuplicateCheckImpl
import io.ktor.client.HttpClient
@ -1293,8 +1295,7 @@ fun globalModuleJvm() = DI.Module(
val okHttpClient: OkHttpClient = instance()
HttpClient(OkHttp) {
install(UserAgent) {
agent =
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36"
agent = CurrentPlatform.userAgent
}
engine {
preconfigured = okHttpClient