Merge pull request #7021 from thundernest/add_oauth_module

Add OAuth module
This commit is contained in:
Wolf-Martell Montwé 2023-06-23 17:13:36 +02:00 committed by GitHub
commit cc0572967f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 113 additions and 70 deletions

View file

@ -35,6 +35,8 @@ android {
"OAUTH_MICROSOFT_REDIRECT_URI_ID",
"\"VZF2DYuLYAu4TurFd6usQB2JPts%3D\"",
)
manifestPlaceholders["appAuthRedirectScheme"] = "com.fsck.k9.debug"
}
packaging {

View file

@ -8,7 +8,6 @@ import com.fsck.k9.mail.ssl.LocalKeyStore
import com.fsck.k9.mail.ssl.TrustManagerFactory
import com.fsck.k9.mail.ssl.TrustedSocketFactory
import com.fsck.k9.mailstore.LocalStoreProvider
import com.fsck.k9.setup.ServerNameSuggester
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.GlobalScope
import kotlinx.datetime.Clock
@ -34,7 +33,6 @@ val mainModule = module {
single { LocalKeyStoreManager(get()) }
single<TrustedSocketFactory> { DefaultTrustedSocketFactory(get(), get()) }
single<Clock> { Clock.System }
factory { ServerNameSuggester() }
factory { EmailAddressValidator() }
factory { ServerSettingsSerializer() }
}

View file

@ -1,12 +0,0 @@
package com.fsck.k9.setup
import app.k9mail.core.common.mail.Protocols
class ServerNameSuggester {
fun suggestServerName(serverType: String, domainPart: String): String = when (serverType) {
Protocols.IMAP -> "imap.$domainPart"
Protocols.SMTP -> "smtp.$domainPart"
Protocols.POP3 -> "pop3.$domainPart"
else -> throw AssertionError("Missed case: $serverType")
}
}

View file

@ -1,49 +0,0 @@
package com.fsck.k9.setup;
import app.k9mail.core.common.mail.Protocols;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class ServerNameSuggesterTest {
private ServerNameSuggester serverNameSuggester;
@Before
public void setUp() throws Exception {
serverNameSuggester = new ServerNameSuggester();
}
@Test
public void suggestServerName_forImapServer() throws Exception {
String serverType = Protocols.IMAP;
String domainPart = "example.org";
String result = serverNameSuggester.suggestServerName(serverType, domainPart);
assertEquals("imap.example.org", result);
}
@Test
public void suggestServerName_forPop3Server() throws Exception {
String serverType = Protocols.POP3;
String domainPart = "example.org";
String result = serverNameSuggester.suggestServerName(serverType, domainPart);
assertEquals("pop3.example.org", result);
}
@Test
public void suggestServerName_forSmtpServer() throws Exception {
String serverType = Protocols.SMTP;
String domainPart = "example.org";
String result = serverNameSuggester.suggestServerName(serverType, domainPart);
assertEquals("smtp.example.org", result);
}
}

View file

@ -10,6 +10,7 @@ dependencies {
implementation(projects.feature.autodiscovery.providersxml)
implementation(projects.mail.common)
implementation(projects.uiUtils.toolbarBottomSheet)
implementation(projects.feature.account.oauth)
// Remove AccountSetupIncoming's dependency on these
compileOnly(projects.mail.protocols.imap)

View file

@ -1,10 +1,12 @@
package com.fsck.k9.activity
import app.k9mail.feature.account.oauth.domain.usecase.SuggestServerName
import com.fsck.k9.activity.setup.AuthViewModel
import org.koin.androidx.viewmodel.dsl.viewModel
import org.koin.dsl.module
val activityModule = module {
single { MessageLoaderHelperFactory(messageViewInfoExtractorFactory = get(), htmlSettingsProvider = get()) }
factory { SuggestServerName() }
viewModel { AuthViewModel(application = get(), accountManager = get(), oAuthConfigurationProvider = get()) }
}

View file

@ -5,13 +5,13 @@ import android.content.Intent
import android.os.Bundle
import android.view.View
import app.k9mail.core.common.mail.Protocols
import app.k9mail.feature.account.oauth.domain.usecase.SuggestServerName
import com.fsck.k9.Account
import com.fsck.k9.Preferences
import com.fsck.k9.helper.EmailHelper.getDomainFromEmailAddress
import com.fsck.k9.mail.ConnectionSecurity
import com.fsck.k9.mail.ServerSettings
import com.fsck.k9.mailstore.SpecialLocalFoldersCreator
import com.fsck.k9.setup.ServerNameSuggester
import com.fsck.k9.ui.R
import com.fsck.k9.ui.base.K9Activity
import org.koin.android.ext.android.inject
@ -23,7 +23,7 @@ import org.koin.android.ext.android.inject
*/
class AccountSetupAccountType : K9Activity() {
private val preferences: Preferences by inject()
private val serverNameSuggester: ServerNameSuggester by inject()
private val serverNameSuggester: SuggestServerName by inject()
private val localFoldersCreator: SpecialLocalFoldersCreator by inject()
private lateinit var account: Account
@ -71,7 +71,7 @@ class AccountSetupAccountType : K9Activity() {
}
private fun initializeIncomingServerSettings(serverType: String, domainPart: String) {
val suggestedStoreServerName = serverNameSuggester.suggestServerName(serverType, domainPart)
val suggestedStoreServerName = serverNameSuggester.suggest(serverType, domainPart)
val storeServer = ServerSettings(
serverType,
suggestedStoreServerName,
@ -86,7 +86,7 @@ class AccountSetupAccountType : K9Activity() {
}
private fun initializeOutgoingServerSettings(domainPart: String) {
val suggestedTransportServerName = serverNameSuggester.suggestServerName(Protocols.SMTP, domainPart)
val suggestedTransportServerName = serverNameSuggester.suggest(Protocols.SMTP, domainPart)
val transportServer = ServerSettings(
Protocols.SMTP,
suggestedTransportServerName,

View file

@ -29,7 +29,7 @@ import com.fsck.k9.LocalKeyStoreManager;
import com.fsck.k9.Preferences;
import com.fsck.k9.account.AccountCreator;
import com.fsck.k9.helper.EmailHelper;
import com.fsck.k9.setup.ServerNameSuggester;
import app.k9mail.feature.account.oauth.domain.usecase.SuggestServerName;
import com.fsck.k9.ui.base.K9Activity;
import com.fsck.k9.activity.setup.AccountSetupCheckSettings.CheckDirection;
import com.fsck.k9.helper.Utility;
@ -61,7 +61,7 @@ public class AccountSetupIncoming extends K9Activity implements OnClickListener
private static final String STATE_AUTH_TYPE_POSITION = "authTypePosition";
private final AccountCreator accountCreator = DI.get(AccountCreator.class);
private final ServerNameSuggester serverNameSuggester = DI.get(ServerNameSuggester.class);
private final SuggestServerName serverNameSuggester = DI.get(SuggestServerName.class);
private String mStoreType;
private TextInputEditText mUsernameView;
@ -524,7 +524,7 @@ public class AccountSetupIncoming extends K9Activity implements OnClickListener
}
String domain = EmailHelper.getDomainFromEmailAddress(mAccount.getEmail());
String host = serverNameSuggester.suggestServerName(Protocols.SMTP, domain);
String host = serverNameSuggester.suggest(Protocols.SMTP, domain);
ServerSettings transportServer = new ServerSettings(Protocols.SMTP, host,
-1, ConnectionSecurity.SSL_TLS_REQUIRED, authType, username, password,
clientCertificateAlias);

View file

@ -0,0 +1,26 @@
plugins {
id(ThunderbirdPlugins.Library.androidCompose)
}
android {
namespace = "app.k9mail.feature.account.oauth"
resourcePrefix = "account_oauth_"
buildTypes {
debug {
manifestPlaceholders["appAuthRedirectScheme"] = "FIXME: override this in your app project"
}
release {
manifestPlaceholders["appAuthRedirectScheme"] = "FIXME: override this in your app project"
}
}
}
dependencies {
implementation(projects.core.ui.compose.designsystem)
implementation(projects.core.common)
implementation(libs.appauth)
testImplementation(projects.core.ui.compose.testing)
}

View file

@ -0,0 +1,10 @@
package app.k9mail.feature.account.oauth.domain
interface DomainContract {
interface UseCase {
fun interface SuggestServerName {
fun suggest(protocol: String, domain: String): String
}
}
}

View file

@ -0,0 +1,13 @@
package app.k9mail.feature.account.oauth.domain.usecase
import app.k9mail.core.common.mail.Protocols
import app.k9mail.feature.account.oauth.domain.DomainContract.UseCase
class SuggestServerName : UseCase.SuggestServerName {
override fun suggest(protocol: String, domain: String): String = when (protocol) {
Protocols.IMAP -> "imap.$domain"
Protocols.SMTP -> "smtp.$domain"
Protocols.POP3 -> "pop3.$domain"
else -> throw AssertionError("Missed case: $protocol")
}
}

View file

@ -0,0 +1,41 @@
package app.k9mail.feature.account.oauth.domain.usecase
import app.k9mail.core.common.mail.Protocols
import assertk.assertThat
import assertk.assertions.isEqualTo
import org.junit.Test
class SuggestServerNameTest {
private val testSubject = SuggestServerName()
@Test
fun `should suggest server name for IMAP server`() {
val serverType = Protocols.IMAP
val domain = "example.org"
val result = testSubject.suggest(serverType, domain)
assertThat(result).isEqualTo("imap.example.org")
}
@Test
fun `should suggest server name for POP3 server`() {
val serverType = Protocols.POP3
val domain = "example.org"
val result = testSubject.suggest(serverType, domain)
assertThat(result).isEqualTo("pop3.example.org")
}
@Test
fun `should suggest server name for SMTP server`() {
val serverType = Protocols.SMTP
val domain = "example.org"
val result = testSubject.suggest(serverType, domain)
assertThat(result).isEqualTo("smtp.example.org")
}
}

View file

@ -5,6 +5,15 @@ plugins {
android {
namespace = "app.k9mail.feature.account.setup"
resourcePrefix = "account_setup_"
buildTypes {
debug {
manifestPlaceholders["appAuthRedirectScheme"] = "FIXME: override this in your app project"
}
release {
manifestPlaceholders["appAuthRedirectScheme"] = "FIXME: override this in your app project"
}
}
}
dependencies {
@ -12,6 +21,7 @@ dependencies {
implementation(projects.core.common)
implementation(projects.feature.autodiscovery.service)
implementation(projects.feature.account.oauth)
testImplementation(projects.core.ui.compose.testing)
}

View file

@ -40,6 +40,7 @@ include(
include(
":feature:account:setup",
":feature:account:oauth",
":feature:onboarding",
":feature:autodiscovery:api",
":feature:autodiscovery:providersxml",