mirror of
https://github.com/bitwarden/android.git
synced 2025-03-16 03:08:50 +03:00
Clean up environment urls (#3364)
This commit is contained in:
parent
67874655fa
commit
732ebb61fb
3 changed files with 92 additions and 47 deletions
|
@ -1,8 +1,9 @@
|
|||
package com.x8bit.bitwarden.data.platform.datasource.network.interceptor
|
||||
|
||||
import com.x8bit.bitwarden.data.platform.repository.model.Environment
|
||||
import com.x8bit.bitwarden.data.platform.repository.util.baseApiUrl
|
||||
import com.x8bit.bitwarden.data.platform.repository.util.baseEventsUrl
|
||||
import com.x8bit.bitwarden.data.platform.repository.util.baseIdentityUrl
|
||||
import com.x8bit.bitwarden.ui.platform.base.util.orNullIfBlank
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
|
@ -39,24 +40,8 @@ class BaseUrlInterceptors @Inject constructor() {
|
|||
|
||||
private fun updateBaseUrls(environment: Environment) {
|
||||
val environmentUrlData = environment.environmentUrlData
|
||||
val baseUrl = environmentUrlData.base.trim()
|
||||
|
||||
// Determine the required base URLs
|
||||
val apiUrl: String
|
||||
val eventsUrl: String
|
||||
if (baseUrl.isNotEmpty()) {
|
||||
apiUrl = "$baseUrl/api"
|
||||
eventsUrl = "$baseUrl/events"
|
||||
} else {
|
||||
apiUrl =
|
||||
environmentUrlData.api.orNullIfBlank() ?: "https://api.bitwarden.com"
|
||||
eventsUrl =
|
||||
environmentUrlData.events.orNullIfBlank() ?: "https://events.bitwarden.com"
|
||||
}
|
||||
|
||||
// Update the base URLs
|
||||
apiInterceptor.baseUrl = apiUrl
|
||||
apiInterceptor.baseUrl = environmentUrlData.baseApiUrl
|
||||
identityInterceptor.baseUrl = environmentUrlData.baseIdentityUrl
|
||||
eventsInterceptor.baseUrl = eventsUrl
|
||||
eventsInterceptor.baseUrl = environmentUrlData.baseEventsUrl
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,21 +4,36 @@ import com.x8bit.bitwarden.data.auth.datasource.disk.model.EnvironmentUrlDataJso
|
|||
import com.x8bit.bitwarden.data.platform.repository.model.Environment
|
||||
import java.net.URI
|
||||
|
||||
private const val DEFAULT_API_URL: String = "https://api.bitwarden.com"
|
||||
private const val DEFAULT_EVENTS_URL: String = "https://events.bitwarden.com"
|
||||
private const val DEFAULT_IDENTITY_URL: String = "https://identity.bitwarden.com"
|
||||
private const val DEFAULT_WEB_VAULT_URL: String = "https://vault.bitwarden.com"
|
||||
private const val DEFAULT_WEB_SEND_URL: String = "https://send.bitwarden.com/#"
|
||||
private const val DEFAULT_ICON_URL: String = "https://icons.bitwarden.net/"
|
||||
private const val DEFAULT_ICON_URL: String = "https://icons.bitwarden.net"
|
||||
|
||||
/**
|
||||
* Returns the base api URL or the default value if one is not present.
|
||||
*/
|
||||
val EnvironmentUrlDataJson.baseApiUrl: String
|
||||
get() = this.base.sanitizeUrl?.let { "$it/api" }
|
||||
?: this.api.sanitizeUrl
|
||||
?: DEFAULT_API_URL
|
||||
|
||||
/**
|
||||
* Returns the base events URL or the default value if one is not present.
|
||||
*/
|
||||
val EnvironmentUrlDataJson.baseEventsUrl: String
|
||||
get() = this.base.sanitizeUrl?.let { "$it/events" }
|
||||
?: this.events.sanitizeUrl
|
||||
?: DEFAULT_EVENTS_URL
|
||||
|
||||
/**
|
||||
* Returns the base identity URL or the default value if one is not present.
|
||||
*/
|
||||
val EnvironmentUrlDataJson.baseIdentityUrl: String
|
||||
get() =
|
||||
this
|
||||
.identity
|
||||
.takeIf { !it.isNullOrBlank() }
|
||||
?: base.takeIf { it.isNotBlank() }?.let { "$it/identity" }
|
||||
?: DEFAULT_IDENTITY_URL
|
||||
get() = this.identity.sanitizeUrl
|
||||
?: this.base.sanitizeUrl?.let { "$it/identity" }
|
||||
?: DEFAULT_IDENTITY_URL
|
||||
|
||||
/**
|
||||
* Returns the base web vault URL. This will check for a custom [EnvironmentUrlDataJson.webVault]
|
||||
|
@ -26,11 +41,8 @@ val EnvironmentUrlDataJson.baseIdentityUrl: String
|
|||
* null or blank.
|
||||
*/
|
||||
val EnvironmentUrlDataJson.baseWebVaultUrlOrNull: String?
|
||||
get() =
|
||||
this
|
||||
.webVault
|
||||
.takeIf { !it.isNullOrBlank() }
|
||||
?: base.takeIf { it.isNotBlank() }
|
||||
get() = this.webVault.sanitizeUrl
|
||||
?: this.base.sanitizeUrl
|
||||
|
||||
/**
|
||||
* Returns the base web vault URL or the default value if one is not present.
|
||||
|
@ -63,12 +75,9 @@ val EnvironmentUrlDataJson.toBaseWebVaultImportUrl: String
|
|||
* Returns a base icon url based on the environment or the default value if values are missing.
|
||||
*/
|
||||
val EnvironmentUrlDataJson.baseIconUrl: String
|
||||
get() =
|
||||
this
|
||||
.icon
|
||||
.takeIf { !it.isNullOrBlank() }
|
||||
?: base.takeIf { it.isNotBlank() }?.let { "$it/icons" }
|
||||
?: DEFAULT_ICON_URL
|
||||
get() = this.icon.sanitizeUrl
|
||||
?: this.base.sanitizeUrl?.let { "$it/icons" }
|
||||
?: DEFAULT_ICON_URL
|
||||
|
||||
/**
|
||||
* Returns the appropriate pre-defined labels for environments matching the known US/EU values.
|
||||
|
@ -98,13 +107,16 @@ val EnvironmentUrlDataJson.labelOrBaseUrlHost: String
|
|||
* all self-host environment URLs are null.
|
||||
*/
|
||||
private fun EnvironmentUrlDataJson.getSelfHostedUrlOrNull(): String? =
|
||||
webVault.takeIf { !it.isNullOrBlank() }
|
||||
?: base
|
||||
.takeIf { it.isNotBlank() }
|
||||
?: api
|
||||
.takeIf { !it.isNullOrBlank() }
|
||||
?: identity
|
||||
.takeIf { !it.isNullOrBlank() }
|
||||
this.webVault.sanitizeUrl
|
||||
?: this.base.sanitizeUrl
|
||||
?: this.api.sanitizeUrl
|
||||
?: this.identity.sanitizeUrl
|
||||
|
||||
/**
|
||||
* A helper method to filter out blank urls and remove any trailing forward slashes.
|
||||
*/
|
||||
private val String?.sanitizeUrl: String?
|
||||
get() = this?.trimEnd('/').takeIf { !it.isNullOrBlank() }
|
||||
|
||||
/**
|
||||
* Converts a raw [EnvironmentUrlDataJson] to an externally-consumable [Environment].
|
||||
|
|
|
@ -7,21 +7,69 @@ import org.junit.jupiter.api.Assertions.assertNull
|
|||
import org.junit.jupiter.api.Test
|
||||
|
||||
class EnvironmentUrlsDataJsonExtensionsTest {
|
||||
@Test
|
||||
fun `baseApiUrl should return base if it is present`() {
|
||||
assertEquals(
|
||||
"base/api",
|
||||
DEFAULT_CUSTOM_ENVIRONMENT_URL_DATA.baseApiUrl,
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `baseApiUrl should return api value if base is empty`() {
|
||||
assertEquals(
|
||||
"api",
|
||||
DEFAULT_CUSTOM_ENVIRONMENT_URL_DATA.copy(base = "").baseApiUrl,
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `baseApiUrl should return default url if base is empty and api is null`() {
|
||||
assertEquals(
|
||||
"https://api.bitwarden.com",
|
||||
DEFAULT_CUSTOM_ENVIRONMENT_URL_DATA.copy(base = "", api = null).baseApiUrl,
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `baseEventsUrl should return base if it is present`() {
|
||||
assertEquals(
|
||||
"base/events",
|
||||
DEFAULT_CUSTOM_ENVIRONMENT_URL_DATA.baseEventsUrl,
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `baseEventsUrl should return events value if base is empty`() {
|
||||
assertEquals(
|
||||
"events",
|
||||
DEFAULT_CUSTOM_ENVIRONMENT_URL_DATA.copy(base = "").baseEventsUrl,
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `baseEventsUrl should return default url if base is empty and events is null`() {
|
||||
assertEquals(
|
||||
"https://events.bitwarden.com",
|
||||
DEFAULT_CUSTOM_ENVIRONMENT_URL_DATA.copy(base = "", events = null).baseEventsUrl,
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `baseIdentityUrl should return identity if value is present`() {
|
||||
assertEquals(
|
||||
DEFAULT_CUSTOM_ENVIRONMENT_URL_DATA.baseIdentityUrl,
|
||||
"identity",
|
||||
DEFAULT_CUSTOM_ENVIRONMENT_URL_DATA.baseIdentityUrl,
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `baseIdentityUrl should return base value if identity is null`() {
|
||||
assertEquals(
|
||||
"base/identity",
|
||||
DEFAULT_CUSTOM_ENVIRONMENT_URL_DATA
|
||||
.copy(identity = null)
|
||||
.baseIdentityUrl,
|
||||
"base/identity",
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -242,7 +290,7 @@ class EnvironmentUrlsDataJsonExtensionsTest {
|
|||
|
||||
@Test
|
||||
fun `toIconBaseurl should return default url if base is empty and icon is null`() {
|
||||
val expectedUrl = "https://icons.bitwarden.net/"
|
||||
val expectedUrl = "https://icons.bitwarden.net"
|
||||
|
||||
assertEquals(
|
||||
expectedUrl,
|
||||
|
|
Loading…
Add table
Reference in a new issue