forked from thunderbird/thunderbird-android
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract GetOAuthRequestIntent into own use case for use in the new ac…
…count flow
- Loading branch information
Showing
10 changed files
with
206 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 7 additions & 3 deletions
10
app/ui/legacy/src/main/java/com/fsck/k9/activity/KoinModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,16 @@ | ||
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()) } | ||
viewModel { | ||
AuthViewModel( | ||
application = get(), | ||
accountManager = get(), | ||
getOAuthRequestIntent = get(), | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 1 addition & 2 deletions
3
core/common/src/main/kotlin/app/k9mail/core/common/oauth/OAuthConfigurationProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
package app.k9mail.core.common.oauth | ||
|
||
interface OAuthConfigurationProvider { | ||
|
||
fun interface OAuthConfigurationProvider { | ||
fun getConfiguration(hostname: String): OAuthConfiguration? | ||
} |
29 changes: 29 additions & 0 deletions
29
feature/account/oauth/src/main/kotlin/app/k9mail/feature/account/oauth/AccountOAuthModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package app.k9mail.feature.account.oauth | ||
|
||
import app.k9mail.core.common.coreCommonModule | ||
import app.k9mail.feature.account.oauth.domain.DomainContract.UseCase | ||
import app.k9mail.feature.account.oauth.domain.usecase.GetOAuthRequestIntent | ||
import app.k9mail.feature.account.oauth.domain.usecase.SuggestServerName | ||
import net.openid.appauth.AuthorizationService | ||
import org.koin.android.ext.koin.androidApplication | ||
import org.koin.core.module.Module | ||
import org.koin.dsl.module | ||
|
||
val featureAccountOAuthModule: Module = module { | ||
includes(coreCommonModule) | ||
|
||
factory { | ||
AuthorizationService( | ||
androidApplication(), | ||
) | ||
} | ||
|
||
factory<UseCase.SuggestServerName> { SuggestServerName() } | ||
|
||
factory<UseCase.GetOAuthRequestIntent> { | ||
GetOAuthRequestIntent( | ||
service = get(), | ||
configurationProvider = get(), | ||
) | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...e/account/oauth/src/main/kotlin/app/k9mail/feature/account/oauth/domain/DomainContract.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,24 @@ | ||
package app.k9mail.feature.account.oauth.domain | ||
|
||
import android.content.Intent | ||
|
||
interface DomainContract { | ||
|
||
interface UseCase { | ||
fun interface SuggestServerName { | ||
fun suggest(protocol: String, domain: String): String | ||
} | ||
|
||
fun interface GetOAuthRequestIntent { | ||
suspend fun execute(hostname: String, emailAddress: String): GetOAuthRequestIntentResult | ||
|
||
sealed interface GetOAuthRequestIntentResult { | ||
object NotSupported : GetOAuthRequestIntentResult | ||
|
||
data class Success( | ||
val intent: Intent, | ||
) : GetOAuthRequestIntentResult | ||
} | ||
} | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
.../src/main/kotlin/app/k9mail/feature/account/oauth/domain/usecase/GetOAuthRequestIntent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package app.k9mail.feature.account.oauth.domain.usecase | ||
|
||
import android.content.Intent | ||
import androidx.core.net.toUri | ||
import app.k9mail.core.common.oauth.OAuthConfiguration | ||
import app.k9mail.core.common.oauth.OAuthConfigurationProvider | ||
import app.k9mail.feature.account.oauth.domain.DomainContract.UseCase.GetOAuthRequestIntent | ||
import app.k9mail.feature.account.oauth.domain.DomainContract.UseCase.GetOAuthRequestIntent.GetOAuthRequestIntentResult | ||
import net.openid.appauth.AuthorizationRequest | ||
import net.openid.appauth.AuthorizationService | ||
import net.openid.appauth.AuthorizationServiceConfiguration | ||
import net.openid.appauth.ResponseTypeValues | ||
|
||
internal class GetOAuthRequestIntent( | ||
private val service: AuthorizationService, | ||
private val configurationProvider: OAuthConfigurationProvider, | ||
) : GetOAuthRequestIntent { | ||
override suspend fun execute(hostname: String, emailAddress: String): GetOAuthRequestIntentResult { | ||
val configuration = configurationProvider.getConfiguration(hostname) | ||
?: return GetOAuthRequestIntentResult.NotSupported | ||
|
||
return GetOAuthRequestIntentResult.Success(createAuthorizationRequestIntent(emailAddress, configuration)) | ||
} | ||
|
||
private fun createAuthorizationRequestIntent(emailAddress: String, configuration: OAuthConfiguration): Intent { | ||
val serviceConfig = AuthorizationServiceConfiguration( | ||
configuration.authorizationEndpoint.toUri(), | ||
configuration.tokenEndpoint.toUri(), | ||
) | ||
|
||
val authRequestBuilder = AuthorizationRequest.Builder( | ||
serviceConfig, | ||
configuration.clientId, | ||
ResponseTypeValues.CODE, | ||
configuration.redirectUri.toUri(), | ||
) | ||
|
||
val authRequest = authRequestBuilder | ||
.setScope(configuration.scopes.joinToString(" ")) | ||
.setCodeVerifier(null) | ||
.setLoginHint(emailAddress) | ||
.build() | ||
|
||
return service.getAuthorizationRequestIntent(authRequest) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
.../test/kotlin/app/k9mail/feature/account/oauth/domain/usecase/GetOAuthRequestIntentTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package app.k9mail.feature.account.oauth.domain.usecase | ||
|
||
import android.content.Intent | ||
import androidx.core.net.toUri | ||
import app.k9mail.core.common.oauth.OAuthConfiguration | ||
import app.k9mail.feature.account.oauth.domain.DomainContract.UseCase.GetOAuthRequestIntent | ||
import assertk.all | ||
import assertk.assertThat | ||
import assertk.assertions.isEqualTo | ||
import assertk.assertions.isNull | ||
import assertk.assertions.prop | ||
import kotlinx.coroutines.test.runTest | ||
import net.openid.appauth.AuthorizationRequest | ||
import net.openid.appauth.AuthorizationService | ||
import net.openid.appauth.AuthorizationServiceConfiguration | ||
import net.openid.appauth.ResponseTypeValues | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.mockito.kotlin.argumentCaptor | ||
import org.mockito.kotlin.mock | ||
import org.mockito.kotlin.stub | ||
import org.robolectric.RobolectricTestRunner | ||
|
||
@RunWith(RobolectricTestRunner::class) | ||
class GetOAuthRequestIntentTest { | ||
|
||
private val service: AuthorizationService = mock<AuthorizationService>() | ||
|
||
@Test | ||
fun `should return NotSupported when hostname has no oauth configuration`() = runTest { | ||
val testSubject = GetOAuthRequestIntent( | ||
service = service, | ||
configurationProvider = { null }, | ||
) | ||
val hostname = "hostname" | ||
val emailAddress = "emailAddress" | ||
|
||
val result = testSubject.execute(hostname, emailAddress) | ||
|
||
assertThat(result).isEqualTo(GetOAuthRequestIntent.GetOAuthRequestIntentResult.NotSupported) | ||
} | ||
|
||
@Test | ||
fun `should return Success with intent when hostname has oauth configuration`() = runTest { | ||
val testSubject = GetOAuthRequestIntent( | ||
service = service, | ||
configurationProvider = { oAuthConfiguration }, | ||
) | ||
val hostname = "hostname" | ||
val emailAddress = "emailAddress" | ||
val intent = Intent() | ||
val authRequestCapture = argumentCaptor<AuthorizationRequest>().apply { | ||
service.stub { on { getAuthorizationRequestIntent(capture()) }.thenReturn(intent) } | ||
} | ||
|
||
// When | ||
val result = testSubject.execute(hostname, emailAddress) | ||
|
||
// Then | ||
assertThat(result).isEqualTo( | ||
GetOAuthRequestIntent.GetOAuthRequestIntentResult.Success( | ||
intent = intent, | ||
), | ||
) | ||
assertThat(authRequestCapture.firstValue).all { | ||
prop(AuthorizationRequest::configuration).all { | ||
prop(AuthorizationServiceConfiguration::authorizationEndpoint).isEqualTo( | ||
oAuthConfiguration.authorizationEndpoint.toUri(), | ||
) | ||
prop(AuthorizationServiceConfiguration::tokenEndpoint).isEqualTo( | ||
oAuthConfiguration.tokenEndpoint.toUri(), | ||
) | ||
} | ||
prop(AuthorizationRequest::clientId).isEqualTo(oAuthConfiguration.clientId) | ||
prop(AuthorizationRequest::responseType).isEqualTo(ResponseTypeValues.CODE) | ||
prop(AuthorizationRequest::redirectUri).isEqualTo(oAuthConfiguration.redirectUri.toUri()) | ||
prop(AuthorizationRequest::scope).isEqualTo("scope scope2") | ||
prop(AuthorizationRequest::codeVerifier).isNull() | ||
prop(AuthorizationRequest::loginHint).isEqualTo(emailAddress) | ||
} | ||
} | ||
|
||
private companion object { | ||
val oAuthConfiguration = OAuthConfiguration( | ||
clientId = "clientId", | ||
scopes = listOf("scope", "scope2"), | ||
authorizationEndpoint = "auth.example.com", | ||
tokenEndpoint = "token.example.com", | ||
redirectUri = "redirect.example.com", | ||
) | ||
} | ||
} |