Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(e2ei): use refresh token for idp authorization (WPB-5880) #2549

Merged
merged 4 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
}, {
if (it is E2EIEnrollmentResult.Initialized) {
initialEnrollmentResult = it
OAuthUseCase(context, it.target).launch(
OAuthUseCase(context, it.target, it.oAuthState).launch(

Check warning on line 51 in app/src/main/kotlin/com/wire/android/feature/e2ei/GetE2EICertificateUseCase.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/kotlin/com/wire/android/feature/e2ei/GetE2EICertificateUseCase.kt#L51

Added line #L51 was not covered by tests
context.getActivity()!!.activityResultRegistry,
::oAuthResultHandler
)
Expand All @@ -61,11 +61,13 @@
scope.launch {
when (oAuthResult) {
is OAuthUseCase.OAuthResult.Success -> {
enrollmentResultHandler(enrollE2EI.finalizeEnrollment(
oAuthResult.idToken,
oAuthResult.refreshToken,
initialEnrollmentResult
))
enrollmentResultHandler(
enrollE2EI.finalizeEnrollment(
oAuthResult.idToken,
oAuthResult.authState,

Check warning on line 67 in app/src/main/kotlin/com/wire/android/feature/e2ei/GetE2EICertificateUseCase.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/kotlin/com/wire/android/feature/e2ei/GetE2EICertificateUseCase.kt#L64-L67

Added lines #L64 - L67 were not covered by tests
initialEnrollmentResult
)
)
}

is OAuthUseCase.OAuthResult.Failed -> {
Expand Down
29 changes: 24 additions & 5 deletions app/src/main/kotlin/com/wire/android/feature/e2ei/OAuthUseCase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import android.content.Intent
import android.net.Uri
import android.util.Base64
import android.util.Log
import androidx.activity.result.ActivityResult
import androidx.activity.result.ActivityResultRegistry
import androidx.activity.result.contract.ActivityResultContracts
Expand Down Expand Up @@ -51,8 +52,11 @@
import javax.net.ssl.TrustManager
import javax.net.ssl.X509TrustManager

class OAuthUseCase(context: Context, private val authUrl: String) {
private var authState: AuthState = AuthState()
class OAuthUseCase(context: Context, private val authUrl: String, oAuthState: String?) {

Check warning on line 55 in app/src/main/kotlin/com/wire/android/feature/e2ei/OAuthUseCase.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/kotlin/com/wire/android/feature/e2ei/OAuthUseCase.kt#L55

Added line #L55 was not covered by tests
private var authState: AuthState = oAuthState?.let {
AuthState.jsonDeserialize(it)
} ?: AuthState()

Check warning on line 58 in app/src/main/kotlin/com/wire/android/feature/e2ei/OAuthUseCase.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/kotlin/com/wire/android/feature/e2ei/OAuthUseCase.kt#L57-L58

Added lines #L57 - L58 were not covered by tests

private var authorizationService: AuthorizationService
private lateinit var authServiceConfig: AuthorizationServiceConfiguration

Expand Down Expand Up @@ -96,6 +100,17 @@
private fun getAuthorizationRequestIntent(): Intent = authorizationService.getAuthorizationRequestIntent(getAuthorizationRequest())

fun launch(activityResultRegistry: ActivityResultRegistry, resultHandler: (OAuthResult) -> Unit) {
authState.performActionWithFreshTokens(authorizationService) { _, idToken, exception ->

Check warning on line 103 in app/src/main/kotlin/com/wire/android/feature/e2ei/OAuthUseCase.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/kotlin/com/wire/android/feature/e2ei/OAuthUseCase.kt#L103

Added line #L103 was not covered by tests
if (exception != null) {
Log.e("OAuthTokenRefreshManager", "Error refreshing tokens, continue with login!", exception)
launchLoginFlow(activityResultRegistry, resultHandler)

Check warning on line 106 in app/src/main/kotlin/com/wire/android/feature/e2ei/OAuthUseCase.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/kotlin/com/wire/android/feature/e2ei/OAuthUseCase.kt#L105-L106

Added lines #L105 - L106 were not covered by tests
} else {
resultHandler(OAuthResult.Success(idToken.toString(), authState.jsonSerializeString()))

Check warning on line 108 in app/src/main/kotlin/com/wire/android/feature/e2ei/OAuthUseCase.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/kotlin/com/wire/android/feature/e2ei/OAuthUseCase.kt#L108

Added line #L108 was not covered by tests
}
}
}

Check warning on line 111 in app/src/main/kotlin/com/wire/android/feature/e2ei/OAuthUseCase.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/kotlin/com/wire/android/feature/e2ei/OAuthUseCase.kt#L110-L111

Added lines #L110 - L111 were not covered by tests

private fun launchLoginFlow(activityResultRegistry: ActivityResultRegistry, resultHandler: (OAuthResult) -> Unit) {
val resultLauncher = activityResultRegistry.register(
OAUTH_ACTIVITY_RESULT_KEY, ActivityResultContracts.StartActivityForResult()
) { result ->
Expand Down Expand Up @@ -141,8 +156,12 @@
if (response != null) {
authState.update(response, exception)
appLogger.i("OAuth idToken: ${response.idToken}")
appLogger.i("OAuth refreshToken: ${response.refreshToken}")
resultHandler(OAuthResult.Success(response.idToken.toString(), response.refreshToken))
resultHandler(
OAuthResult.Success(
response.idToken.toString(),
authState.jsonSerializeString()

Check warning on line 162 in app/src/main/kotlin/com/wire/android/feature/e2ei/OAuthUseCase.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/kotlin/com/wire/android/feature/e2ei/OAuthUseCase.kt#L159-L162

Added lines #L159 - L162 were not covered by tests
)
)
} else {
resultHandler(OAuthResult.Failed.EmptyResponse)
}
Expand Down Expand Up @@ -182,7 +201,7 @@
}

sealed class OAuthResult {
data class Success(val idToken: String, val refreshToken: String?) : OAuthResult()
data class Success(val idToken: String, val authState: String) : OAuthResult()

Check warning on line 204 in app/src/main/kotlin/com/wire/android/feature/e2ei/OAuthUseCase.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/kotlin/com/wire/android/feature/e2ei/OAuthUseCase.kt#L204

Added line #L204 was not covered by tests
open class Failed(val reason: String) : OAuthResult() {
object Unknown : Failed("Unknown")
class InvalidActivityResult(reason: String) : Failed(reason)
Expand Down