Change AutoconfigUrlProvider to accept a domain

The email address is now an optional parameter.
This commit is contained in:
cketti 2023-05-09 14:57:35 +02:00
parent 8376909043
commit 510950da65
6 changed files with 38 additions and 21 deletions

View file

@ -2,6 +2,7 @@ package app.k9mail.autodiscovery.autoconfig
import app.k9mail.autodiscovery.api.ConnectionSettingsDiscovery
import app.k9mail.autodiscovery.api.DiscoveryResults
import com.fsck.k9.helper.EmailHelper
class AutoconfigDiscovery(
private val urlProvider: AutoconfigUrlProvider,
@ -10,7 +11,11 @@ class AutoconfigDiscovery(
) : ConnectionSettingsDiscovery {
override fun discover(email: String): DiscoveryResults? {
val autoconfigUrls = urlProvider.getAutoconfigUrls(email)
val domain = requireNotNull(EmailHelper.getDomainFromEmailAddress(email)) {
"Couldn't extract domain from email address: $email"
}
val autoconfigUrls = urlProvider.getAutoconfigUrls(domain, email)
return autoconfigUrls
.asSequence()

View file

@ -3,5 +3,5 @@ package app.k9mail.autodiscovery.autoconfig
import okhttp3.HttpUrl
interface AutoconfigUrlProvider {
fun getAutoconfigUrls(email: String): List<HttpUrl>
fun getAutoconfigUrls(domain: String, email: String? = null): List<HttpUrl>
}

View file

@ -1,14 +1,10 @@
package app.k9mail.autodiscovery.autoconfig
import com.fsck.k9.helper.EmailHelper
import okhttp3.HttpUrl
import okhttp3.HttpUrl.Companion.toHttpUrl
class IspDbAutoconfigUrlProvider : AutoconfigUrlProvider {
override fun getAutoconfigUrls(email: String): List<HttpUrl> {
val domain = EmailHelper.getDomainFromEmailAddress(email)
requireNotNull(domain) { "Couldn't extract domain from email address: $email" }
override fun getAutoconfigUrls(domain: String, email: String?): List<HttpUrl> {
return listOf(createIspDbUrl(domain))
}

View file

@ -1,13 +1,9 @@
package app.k9mail.autodiscovery.autoconfig
import com.fsck.k9.helper.EmailHelper
import okhttp3.HttpUrl
class ProviderAutoconfigUrlProvider(private val config: AutoconfigUrlConfig) : AutoconfigUrlProvider {
override fun getAutoconfigUrls(email: String): List<HttpUrl> {
val domain = EmailHelper.getDomainFromEmailAddress(email)
requireNotNull(domain) { "Couldn't extract domain from email address: $email" }
override fun getAutoconfigUrls(domain: String, email: String?): List<HttpUrl> {
return buildList {
add(createProviderUrl(domain, email, useHttps = true))
add(createDomainUrl(domain, email, useHttps = true))
@ -19,7 +15,7 @@ class ProviderAutoconfigUrlProvider(private val config: AutoconfigUrlConfig) : A
}
}
private fun createProviderUrl(domain: String, email: String, useHttps: Boolean): HttpUrl {
private fun createProviderUrl(domain: String, email: String?, useHttps: Boolean): HttpUrl {
// https://autoconfig.{domain}/mail/config-v1.1.xml?emailaddress={email}
// http://autoconfig.{domain}/mail/config-v1.1.xml?emailaddress={email}
return HttpUrl.Builder()
@ -27,14 +23,14 @@ class ProviderAutoconfigUrlProvider(private val config: AutoconfigUrlConfig) : A
.host("autoconfig.$domain")
.addEncodedPathSegments("mail/config-v1.1.xml")
.apply {
if (config.includeEmailAddress) {
if (email != null && config.includeEmailAddress) {
addQueryParameter("emailaddress", email)
}
}
.build()
}
private fun createDomainUrl(domain: String, email: String, useHttps: Boolean): HttpUrl {
private fun createDomainUrl(domain: String, email: String?, useHttps: Boolean): HttpUrl {
// https://{domain}/.well-known/autoconfig/mail/config-v1.1.xml?emailaddress={email}
// http://{domain}/.well-known/autoconfig/mail/config-v1.1.xml?emailaddress={email}
return HttpUrl.Builder()
@ -42,7 +38,7 @@ class ProviderAutoconfigUrlProvider(private val config: AutoconfigUrlConfig) : A
.host(domain)
.addEncodedPathSegments(".well-known/autoconfig/mail/config-v1.1.xml")
.apply {
if (config.includeEmailAddress) {
if (email != null && config.includeEmailAddress) {
addQueryParameter("emailaddress", email)
}
}

View file

@ -9,7 +9,7 @@ class IspDbAutoconfigUrlProviderTest {
@Test
fun `getAutoconfigUrls with ASCII email address`() {
val autoconfigUrls = urlProvider.getAutoconfigUrls("test@domain.example")
val autoconfigUrls = urlProvider.getAutoconfigUrls(domain = "domain.example")
assertThat(autoconfigUrls.map { it.toString() }).containsExactly(
"https://autoconfig.thunderbird.net/v1.1/domain.example",

View file

@ -10,7 +10,8 @@ class ProviderAutoconfigUrlProviderTest {
val urlProvider = ProviderAutoconfigUrlProvider(
AutoconfigUrlConfig(httpsOnly = false, includeEmailAddress = true),
)
val autoconfigUrls = urlProvider.getAutoconfigUrls("test@domain.example")
val autoconfigUrls = urlProvider.getAutoconfigUrls(domain = "domain.example", email = "test@domain.example")
assertThat(autoconfigUrls.map { it.toString() }).containsExactly(
"https://autoconfig.domain.example/mail/config-v1.1.xml?emailaddress=test%40domain.example",
@ -25,7 +26,8 @@ class ProviderAutoconfigUrlProviderTest {
val urlProvider = ProviderAutoconfigUrlProvider(
AutoconfigUrlConfig(httpsOnly = true, includeEmailAddress = true),
)
val autoconfigUrls = urlProvider.getAutoconfigUrls("test@domain.example")
val autoconfigUrls = urlProvider.getAutoconfigUrls(domain = "domain.example", email = "test@domain.example")
assertThat(autoconfigUrls.map { it.toString() }).containsExactly(
"https://autoconfig.domain.example/mail/config-v1.1.xml?emailaddress=test%40domain.example",
@ -38,7 +40,8 @@ class ProviderAutoconfigUrlProviderTest {
val urlProvider = ProviderAutoconfigUrlProvider(
AutoconfigUrlConfig(httpsOnly = true, includeEmailAddress = false),
)
val autoconfigUrls = urlProvider.getAutoconfigUrls("test@domain.example")
val autoconfigUrls = urlProvider.getAutoconfigUrls(domain = "domain.example", email = "test@domain.example")
assertThat(autoconfigUrls.map { it.toString() }).containsExactly(
"https://autoconfig.domain.example/mail/config-v1.1.xml",
@ -51,7 +54,24 @@ class ProviderAutoconfigUrlProviderTest {
val urlProvider = ProviderAutoconfigUrlProvider(
AutoconfigUrlConfig(httpsOnly = false, includeEmailAddress = false),
)
val autoconfigUrls = urlProvider.getAutoconfigUrls("test@domain.example")
val autoconfigUrls = urlProvider.getAutoconfigUrls(domain = "domain.example", email = "test@domain.example")
assertThat(autoconfigUrls.map { it.toString() }).containsExactly(
"https://autoconfig.domain.example/mail/config-v1.1.xml",
"https://domain.example/.well-known/autoconfig/mail/config-v1.1.xml",
"http://autoconfig.domain.example/mail/config-v1.1.xml",
"http://domain.example/.well-known/autoconfig/mail/config-v1.1.xml",
)
}
@Test
fun `getAutoconfigUrls with http allowed and email address included, but none provided`() {
val urlProvider = ProviderAutoconfigUrlProvider(
AutoconfigUrlConfig(httpsOnly = false, includeEmailAddress = true),
)
val autoconfigUrls = urlProvider.getAutoconfigUrls(domain = "domain.example")
assertThat(autoconfigUrls.map { it.toString() }).containsExactly(
"https://autoconfig.domain.example/mail/config-v1.1.xml",