Use Domain class in :feature:autodiscovery:autoconfig module

This commit is contained in:
cketti 2023-05-19 18:29:40 +02:00
parent 8b8bc409e8
commit 57e3db5443
17 changed files with 119 additions and 54 deletions

View file

@ -6,3 +6,5 @@ value class Domain(val value: String) {
requireNotNull(HostNameUtils.isLegalHostName(value)) { "Not a valid domain name: '$value'" }
}
}
fun String.toDomain() = Domain(this)

View file

@ -2,6 +2,7 @@ package app.k9mail.autodiscovery.autoconfig
import app.k9mail.autodiscovery.api.ConnectionSettingsDiscovery
import app.k9mail.autodiscovery.api.DiscoveryResults
import app.k9mail.core.common.net.toDomain
import com.fsck.k9.helper.EmailHelper
class AutoconfigDiscovery(
@ -11,7 +12,7 @@ class AutoconfigDiscovery(
) : ConnectionSettingsDiscovery {
override fun discover(email: String): DiscoveryResults? {
val domain = requireNotNull(EmailHelper.getDomainFromEmailAddress(email)) {
val domain = requireNotNull(EmailHelper.getDomainFromEmailAddress(email)?.toDomain()) {
"Couldn't extract domain from email address: $email"
}

View file

@ -1,7 +1,8 @@
package app.k9mail.autodiscovery.autoconfig
import app.k9mail.core.common.net.Domain
import okhttp3.HttpUrl
interface AutoconfigUrlProvider {
fun getAutoconfigUrls(domain: String, email: String? = null): List<HttpUrl>
fun getAutoconfigUrls(domain: Domain, email: String? = null): List<HttpUrl>
}

View file

@ -1,10 +1,12 @@
package app.k9mail.autodiscovery.autoconfig
import app.k9mail.core.common.net.Domain
/**
* Extract the base domain from a host name.
*
* An implementation needs to respect the [Public Suffix List](https://publicsuffix.org/).
*/
interface BaseDomainExtractor {
fun extractBaseDomain(domain: String): String
fun extractBaseDomain(domain: Domain): Domain
}

View file

@ -1,18 +1,19 @@
package app.k9mail.autodiscovery.autoconfig
import app.k9mail.core.common.net.Domain
import okhttp3.HttpUrl
import okhttp3.HttpUrl.Companion.toHttpUrl
class IspDbAutoconfigUrlProvider : AutoconfigUrlProvider {
override fun getAutoconfigUrls(domain: String, email: String?): List<HttpUrl> {
override fun getAutoconfigUrls(domain: Domain, email: String?): List<HttpUrl> {
return listOf(createIspDbUrl(domain))
}
private fun createIspDbUrl(domain: String): HttpUrl {
private fun createIspDbUrl(domain: Domain): HttpUrl {
// https://autoconfig.thunderbird.net/v1.1/{domain}
return "https://autoconfig.thunderbird.net/v1.1/".toHttpUrl()
.newBuilder()
.addPathSegment(domain)
.addPathSegment(domain.value)
.build()
}
}

View file

@ -1,13 +1,15 @@
package app.k9mail.autodiscovery.autoconfig
import app.k9mail.core.common.net.Domain
import app.k9mail.core.common.net.toDomain
import org.minidns.hla.ResolverApi
import org.minidns.record.MX
class MiniDnsMxResolver : MxResolver {
override fun lookup(domain: String): List<String> {
val result = ResolverApi.INSTANCE.resolve(domain, MX::class.java)
override fun lookup(domain: Domain): List<Domain> {
val result = ResolverApi.INSTANCE.resolve(domain.value, MX::class.java)
return result.answersOrEmptySet
.sortedBy { it.priority }
.map { it.target.toString() }
.map { it.target.toString().toDomain() }
}
}

View file

@ -2,6 +2,8 @@ package app.k9mail.autodiscovery.autoconfig
import app.k9mail.autodiscovery.api.ConnectionSettingsDiscovery
import app.k9mail.autodiscovery.api.DiscoveryResults
import app.k9mail.core.common.net.Domain
import app.k9mail.core.common.net.toDomain
import com.fsck.k9.helper.EmailHelper
class MxLookupAutoconfigDiscovery(
@ -15,7 +17,7 @@ class MxLookupAutoconfigDiscovery(
@Suppress("ReturnCount")
override fun discover(email: String): DiscoveryResults? {
val domain = requireNotNull(EmailHelper.getDomainFromEmailAddress(email)) {
val domain = requireNotNull(EmailHelper.getDomainFromEmailAddress(email)?.toDomain()) {
"Couldn't extract domain from email address: $email"
}
@ -42,16 +44,16 @@ class MxLookupAutoconfigDiscovery(
.firstOrNull()
}
private fun mxLookup(domain: String): String? {
private fun mxLookup(domain: Domain): Domain? {
// Only return the most preferred entry to match Thunderbird's behavior.
return mxResolver.lookup(domain).firstOrNull()
}
private fun getMxBaseDomain(mxHostName: String): String {
private fun getMxBaseDomain(mxHostName: Domain): Domain {
return baseDomainExtractor.extractBaseDomain(mxHostName)
}
private fun getNextSubDomain(domain: String): String? {
private fun getNextSubDomain(domain: Domain): Domain? {
return subDomainExtractor.extractSubDomain(domain)
}
}

View file

@ -1,8 +1,10 @@
package app.k9mail.autodiscovery.autoconfig
import app.k9mail.core.common.net.Domain
/**
* Look up MX records for a domain.
*/
interface MxResolver {
fun lookup(domain: String): List<String>
fun lookup(domain: Domain): List<Domain>
}

View file

@ -1,10 +1,12 @@
package app.k9mail.autodiscovery.autoconfig
import app.k9mail.core.common.net.Domain
import app.k9mail.core.common.net.toDomain
import okhttp3.HttpUrl
class OkHttpBaseDomainExtractor : BaseDomainExtractor {
override fun extractBaseDomain(domain: String): String {
return domain.toHttpUrlOrNull().topPrivateDomain() ?: domain
override fun extractBaseDomain(domain: Domain): Domain {
return domain.value.toHttpUrlOrNull().topPrivateDomain()?.toDomain() ?: domain
}
private fun String.toHttpUrlOrNull(): HttpUrl {

View file

@ -1,9 +1,10 @@
package app.k9mail.autodiscovery.autoconfig
import app.k9mail.core.common.net.Domain
import okhttp3.HttpUrl
class ProviderAutoconfigUrlProvider(private val config: AutoconfigUrlConfig) : AutoconfigUrlProvider {
override fun getAutoconfigUrls(domain: String, email: String?): List<HttpUrl> {
override fun getAutoconfigUrls(domain: Domain, email: String?): List<HttpUrl> {
return buildList {
add(createProviderUrl(domain, email, useHttps = true))
add(createDomainUrl(domain, email, useHttps = true))
@ -15,12 +16,12 @@ class ProviderAutoconfigUrlProvider(private val config: AutoconfigUrlConfig) : A
}
}
private fun createProviderUrl(domain: String, email: String?, useHttps: Boolean): HttpUrl {
private fun createProviderUrl(domain: Domain, 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()
.scheme(if (useHttps) "https" else "http")
.host("autoconfig.$domain")
.host("autoconfig.${domain.value}")
.addEncodedPathSegments("mail/config-v1.1.xml")
.apply {
if (email != null && config.includeEmailAddress) {
@ -30,12 +31,12 @@ class ProviderAutoconfigUrlProvider(private val config: AutoconfigUrlConfig) : A
.build()
}
private fun createDomainUrl(domain: String, email: String?, useHttps: Boolean): HttpUrl {
private fun createDomainUrl(domain: Domain, 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()
.scheme(if (useHttps) "https" else "http")
.host(domain)
.host(domain.value)
.addEncodedPathSegments(".well-known/autoconfig/mail/config-v1.1.xml")
.apply {
if (email != null && config.includeEmailAddress) {

View file

@ -1,15 +1,19 @@
package app.k9mail.autodiscovery.autoconfig
import app.k9mail.core.common.net.Domain
import app.k9mail.core.common.net.toDomain
class RealSubDomainExtractor(private val baseDomainExtractor: BaseDomainExtractor) : SubDomainExtractor {
@Suppress("ReturnCount")
override fun extractSubDomain(domain: String): String? {
override fun extractSubDomain(domain: Domain): Domain? {
val baseDomain = baseDomainExtractor.extractBaseDomain(domain)
if (baseDomain == domain) {
// The domain doesn't have a sub domain.
return null
}
val domainPrefix = domain.removeSuffix(".$baseDomain")
val baseDomainString = baseDomain.value
val domainPrefix = domain.value.removeSuffix(".$baseDomainString")
val index = domainPrefix.indexOf('.')
if (index == -1) {
// The prefix is the sub domain. When we remove it only the base domain remains.
@ -17,6 +21,6 @@ class RealSubDomainExtractor(private val baseDomainExtractor: BaseDomainExtracto
}
val prefixWithoutFirstLabel = domainPrefix.substring(index + 1)
return "$prefixWithoutFirstLabel.$baseDomain"
return "$prefixWithoutFirstLabel.$baseDomainString".toDomain()
}
}

View file

@ -1,10 +1,12 @@
package app.k9mail.autodiscovery.autoconfig
import app.k9mail.core.common.net.Domain
/**
* Extract the sub domain from a host name.
*
* An implementation needs to respect the [Public Suffix List](https://publicsuffix.org/).
*/
interface SubDomainExtractor {
fun extractSubDomain(domain: String): String?
fun extractSubDomain(domain: Domain): Domain?
}

View file

@ -1,7 +1,9 @@
package app.k9mail.autodiscovery.autoconfig
import app.k9mail.core.common.net.toDomain
import assertk.assertThat
import assertk.assertions.containsExactly
import assertk.assertions.extracting
import org.junit.Test
class IspDbAutoconfigUrlProviderTest {
@ -9,9 +11,11 @@ class IspDbAutoconfigUrlProviderTest {
@Test
fun `getAutoconfigUrls with ASCII email address`() {
val autoconfigUrls = urlProvider.getAutoconfigUrls(domain = "domain.example")
val domain = "domain.example".toDomain()
assertThat(autoconfigUrls.map { it.toString() }).containsExactly(
val autoconfigUrls = urlProvider.getAutoconfigUrls(domain)
assertThat(autoconfigUrls).extracting { it.toString() }.containsExactly(
"https://autoconfig.thunderbird.net/v1.1/domain.example",
)
}

View file

@ -1,8 +1,10 @@
package app.k9mail.autodiscovery.autoconfig
import app.k9mail.core.common.net.toDomain
import assertk.all
import assertk.assertThat
import assertk.assertions.containsExactlyInAnyOrder
import assertk.assertions.extracting
import assertk.assertions.index
import assertk.assertions.isEqualTo
import kotlin.test.Ignore
@ -14,9 +16,11 @@ class MiniDnsMxResolverTest {
@Test
@Ignore("Requires internet")
fun `MX lookup for known domain`() {
val result = resolver.lookup("thunderbird.net")
val domain = "thunderbird.net".toDomain()
assertThat(result).all {
val result = resolver.lookup(domain)
assertThat(result).extracting { it.value }.all {
index(0).isEqualTo("aspmx.l.google.com")
containsExactlyInAnyOrder(
"aspmx.l.google.com",

View file

@ -1,5 +1,6 @@
package app.k9mail.autodiscovery.autoconfig
import app.k9mail.core.common.net.toDomain
import assertk.assertThat
import assertk.assertions.isEqualTo
import org.junit.Test
@ -9,21 +10,37 @@ class OkHttpBaseDomainExtractorTest {
@Test
fun `basic domain`() {
assertThat(baseDomainExtractor.extractBaseDomain("domain.example")).isEqualTo("domain.example")
val domain = "domain.example".toDomain()
val result = baseDomainExtractor.extractBaseDomain(domain)
assertThat(result).isEqualTo(domain)
}
@Test
fun `basic subdomain`() {
assertThat(baseDomainExtractor.extractBaseDomain("subdomain.domain.example")).isEqualTo("domain.example")
val domain = "subdomain.domain.example".toDomain()
val result = baseDomainExtractor.extractBaseDomain(domain)
assertThat(result).isEqualTo("domain.example".toDomain())
}
@Test
fun `domain with public suffix`() {
assertThat(baseDomainExtractor.extractBaseDomain("example.co.uk")).isEqualTo("example.co.uk")
val domain = "example.co.uk".toDomain()
val result = baseDomainExtractor.extractBaseDomain(domain)
assertThat(result).isEqualTo(domain)
}
@Test
fun `subdomain with public suffix`() {
assertThat(baseDomainExtractor.extractBaseDomain("subdomain.example.co.uk")).isEqualTo("example.co.uk")
val domain = "subdomain.example.co.uk".toDomain()
val result = baseDomainExtractor.extractBaseDomain(domain)
assertThat(result).isEqualTo("example.co.uk".toDomain())
}
}

View file

@ -1,17 +1,21 @@
package app.k9mail.autodiscovery.autoconfig
import app.k9mail.core.common.net.toDomain
import assertk.assertThat
import assertk.assertions.containsExactly
import org.junit.Test
class ProviderAutoconfigUrlProviderTest {
private val domain = "domain.example".toDomain()
private val email = "test@domain.example"
@Test
fun `getAutoconfigUrls with http allowed and email address included`() {
val urlProvider = ProviderAutoconfigUrlProvider(
AutoconfigUrlConfig(httpsOnly = false, includeEmailAddress = true),
)
val autoconfigUrls = urlProvider.getAutoconfigUrls(domain = "domain.example", email = "test@domain.example")
val autoconfigUrls = urlProvider.getAutoconfigUrls(domain, email)
assertThat(autoconfigUrls.map { it.toString() }).containsExactly(
"https://autoconfig.domain.example/mail/config-v1.1.xml?emailaddress=test%40domain.example",
@ -27,7 +31,7 @@ class ProviderAutoconfigUrlProviderTest {
AutoconfigUrlConfig(httpsOnly = true, includeEmailAddress = true),
)
val autoconfigUrls = urlProvider.getAutoconfigUrls(domain = "domain.example", email = "test@domain.example")
val autoconfigUrls = urlProvider.getAutoconfigUrls(domain, email)
assertThat(autoconfigUrls.map { it.toString() }).containsExactly(
"https://autoconfig.domain.example/mail/config-v1.1.xml?emailaddress=test%40domain.example",
@ -41,7 +45,7 @@ class ProviderAutoconfigUrlProviderTest {
AutoconfigUrlConfig(httpsOnly = true, includeEmailAddress = false),
)
val autoconfigUrls = urlProvider.getAutoconfigUrls(domain = "domain.example", email = "test@domain.example")
val autoconfigUrls = urlProvider.getAutoconfigUrls(domain, email)
assertThat(autoconfigUrls.map { it.toString() }).containsExactly(
"https://autoconfig.domain.example/mail/config-v1.1.xml",
@ -55,7 +59,7 @@ class ProviderAutoconfigUrlProviderTest {
AutoconfigUrlConfig(httpsOnly = false, includeEmailAddress = false),
)
val autoconfigUrls = urlProvider.getAutoconfigUrls(domain = "domain.example", email = "test@domain.example")
val autoconfigUrls = urlProvider.getAutoconfigUrls(domain, email)
assertThat(autoconfigUrls.map { it.toString() }).containsExactly(
"https://autoconfig.domain.example/mail/config-v1.1.xml",
@ -71,7 +75,7 @@ class ProviderAutoconfigUrlProviderTest {
AutoconfigUrlConfig(httpsOnly = false, includeEmailAddress = true),
)
val autoconfigUrls = urlProvider.getAutoconfigUrls(domain = "domain.example")
val autoconfigUrls = urlProvider.getAutoconfigUrls(domain)
assertThat(autoconfigUrls.map { it.toString() }).containsExactly(
"https://autoconfig.domain.example/mail/config-v1.1.xml",

View file

@ -1,5 +1,7 @@
package app.k9mail.autodiscovery.autoconfig
import app.k9mail.core.common.net.Domain
import app.k9mail.core.common.net.toDomain
import assertk.assertThat
import assertk.assertions.isEqualTo
import assertk.assertions.isNull
@ -11,71 +13,83 @@ class RealSubDomainExtractorTest {
@Test
fun `input has one more label than the base domain`() {
val result = baseSubDomainExtractor.extractSubDomain("subdomain.domain.example")
val domain = "subdomain.domain.example".toDomain()
assertThat(result).isEqualTo("domain.example")
val result = baseSubDomainExtractor.extractSubDomain(domain)
assertThat(result).isEqualTo("domain.example".toDomain())
}
@Test
fun `input has two more labels than the base domain`() {
val result = baseSubDomainExtractor.extractSubDomain("more.subdomain.domain.example")
val domain = "more.subdomain.domain.example".toDomain()
assertThat(result).isEqualTo("subdomain.domain.example")
val result = baseSubDomainExtractor.extractSubDomain(domain)
assertThat(result).isEqualTo("subdomain.domain.example".toDomain())
}
@Test
fun `input has three more labels than the base domain`() {
val result = baseSubDomainExtractor.extractSubDomain("three.two.one.domain.example")
val domain = "three.two.one.domain.example".toDomain()
assertThat(result).isEqualTo("two.one.domain.example")
val result = baseSubDomainExtractor.extractSubDomain(domain)
assertThat(result).isEqualTo("two.one.domain.example".toDomain())
}
@Test
fun `no sub domain available`() {
val result = baseSubDomainExtractor.extractSubDomain("domain.example")
val domain = "domain.example".toDomain()
val result = baseSubDomainExtractor.extractSubDomain(domain)
assertThat(result).isNull()
}
@Test
fun `input has one more label than the base domain with public suffix`() {
val domain = "subdomain.example.co.uk".toDomain()
testBaseDomainExtractor.baseDomain = "example.co.uk"
val result = baseSubDomainExtractor.extractSubDomain("subdomain.example.co.uk")
val result = baseSubDomainExtractor.extractSubDomain(domain)
assertThat(result).isEqualTo("example.co.uk")
assertThat(result).isEqualTo("example.co.uk".toDomain())
}
@Test
fun `input has two more labels than the base domain with public suffix`() {
val domain = "more.subdomain.example.co.uk".toDomain()
testBaseDomainExtractor.baseDomain = "example.co.uk"
val result = baseSubDomainExtractor.extractSubDomain("more.subdomain.example.co.uk")
val result = baseSubDomainExtractor.extractSubDomain(domain)
assertThat(result).isEqualTo("subdomain.example.co.uk")
assertThat(result).isEqualTo("subdomain.example.co.uk".toDomain())
}
@Test
fun `input has three more labels than the base domain with public suffix`() {
val domain = "three.two.one.example.co.uk".toDomain()
testBaseDomainExtractor.baseDomain = "example.co.uk"
val result = baseSubDomainExtractor.extractSubDomain("three.two.one.example.co.uk")
val result = baseSubDomainExtractor.extractSubDomain(domain)
assertThat(result).isEqualTo("two.one.example.co.uk")
assertThat(result).isEqualTo("two.one.example.co.uk".toDomain())
}
@Test
fun `no sub domain available with public suffix`() {
val domain = "example.co.uk".toDomain()
testBaseDomainExtractor.baseDomain = "example.co.uk"
val result = baseSubDomainExtractor.extractSubDomain("example.co.uk")
val result = baseSubDomainExtractor.extractSubDomain(domain)
assertThat(result).isNull()
}
}
private class TestBaseDomainExtractor(var baseDomain: String) : BaseDomainExtractor {
override fun extractBaseDomain(domain: String): String {
return baseDomain
override fun extractBaseDomain(domain: Domain): Domain {
return Domain(baseDomain)
}
}