Merge pull request #1127 from vector-im/feature/bma/finishOidc

Enable OIDC support
This commit is contained in:
Benoit Marty 2023-08-23 17:01:41 +02:00 committed by GitHub
commit 6941bc69f6
40 changed files with 488 additions and 109 deletions

View file

@ -55,9 +55,15 @@ interface MatrixClient : Closeable {
* Will close the client and delete the cache data.
*/
suspend fun clearCache()
suspend fun logout()
/**
* Logout the user.
* Returns an optional URL. When the URL is there, it should be presented to the user after logout for RP initiated logout on their account page.
*/
suspend fun logout(): String?
suspend fun loadUserDisplayName(): Result<String>
suspend fun loadUserAvatarURLString(): Result<String?>
suspend fun getAccountManagementUrl(): Result<String?>
suspend fun uploadMedia(mimeType: String, data: ByteArray, progressCallback: ProgressCallback?): Result<String>
fun roomMembershipObserver(): RoomMembershipObserver

View file

@ -22,6 +22,5 @@ sealed class AuthenticationException(message: String) : Exception(message) {
class SlidingSyncNotAvailable(message: String) : AuthenticationException(message)
class SessionMissing(message: String) : AuthenticationException(message)
class Generic(message: String) : AuthenticationException(message)
// TODO Oidc
// class OidcError(type: String, message: String) : AuthenticationException(message)
data class OidcError(val type: String, override val message: String) : AuthenticationException(message)
}

View file

@ -40,6 +40,7 @@ import io.element.android.libraries.matrix.api.user.MatrixSearchUserResults
import io.element.android.libraries.matrix.api.user.MatrixUser
import io.element.android.libraries.matrix.api.verification.SessionVerificationService
import io.element.android.libraries.matrix.impl.core.toProgressWatcher
import io.element.android.libraries.matrix.impl.mapper.toSessionData
import io.element.android.libraries.matrix.impl.media.RustMediaLoader
import io.element.android.libraries.matrix.impl.notification.RustNotificationService
import io.element.android.libraries.matrix.impl.pushers.RustPushersService
@ -119,6 +120,13 @@ class RustMatrixClient constructor(
Timber.v("didReceiveAuthError -> already cleaning up")
}
}
override fun didRefreshTokens() {
Timber.w("didRefreshTokens()")
appCoroutineScope.launch {
sessionStore.updateData(client.session().toSessionData())
}
}
}
private val rustRoomListService: RoomListService =
@ -287,21 +295,30 @@ class RustMatrixClient constructor(
baseDirectory.deleteSessionDirectory(userID = sessionId.value, deleteCryptoDb = false)
}
override suspend fun logout() = doLogout(doRequest = true)
override suspend fun logout(): String? = doLogout(doRequest = true)
private suspend fun doLogout(doRequest: Boolean) = withContext(sessionDispatcher) {
if (doRequest) {
try {
client.logout()
} catch (failure: Throwable) {
Timber.e(failure, "Fail to call logout on HS. Still delete local files.")
private suspend fun doLogout(doRequest: Boolean): String? {
var result: String? = null
withContext(sessionDispatcher) {
if (doRequest) {
try {
result = client.logout()
} catch (failure: Throwable) {
Timber.e(failure, "Fail to call logout on HS. Still delete local files.")
}
}
close()
baseDirectory.deleteSessionDirectory(userID = sessionId.value, deleteCryptoDb = true)
sessionStore.removeSession(sessionId.value)
}
close()
baseDirectory.deleteSessionDirectory(userID = sessionId.value, deleteCryptoDb = true)
sessionStore.removeSession(sessionId.value)
return result
}
override suspend fun getAccountManagementUrl(): Result<String?> = withContext(sessionDispatcher) {
runCatching {
client.accountUrl()
}
}
override suspend fun loadUserDisplayName(): Result<String> = withContext(sessionDispatcher) {
runCatching {
client.displayName()

View file

@ -75,4 +75,5 @@ private fun SessionData.toSession() = Session(
deviceId = deviceId,
homeserverUrl = homeserverUrl,
slidingSyncProxy = slidingSyncProxy,
oidcData = oidcData,
)

View file

@ -26,15 +26,12 @@ fun Throwable.mapAuthenticationException(): AuthenticationException {
is RustAuthenticationException.InvalidServerName -> AuthenticationException.InvalidServerName(this.message!!)
is RustAuthenticationException.SessionMissing -> AuthenticationException.SessionMissing(this.message!!)
is RustAuthenticationException.SlidingSyncNotAvailable -> AuthenticationException.SlidingSyncNotAvailable(this.message!!)
/* TODO Oidc
is RustAuthenticationException.OidcException -> AuthenticationException.OidcError("OidcException", message!!)
is RustAuthenticationException.OidcMetadataInvalid -> AuthenticationException.OidcError("OidcMetadataInvalid", message!!)
is RustAuthenticationException.OidcMetadataMissing -> AuthenticationException.OidcError("OidcMetadataMissing", message!!)
is RustAuthenticationException.OidcNotStarted -> AuthenticationException.OidcError("OidcNotStarted", message!!)
is RustAuthenticationException.OidcNotSupported -> AuthenticationException.OidcError("OidcNotSupported", message!!)
*/
is RustAuthenticationException.OidcCancelled -> AuthenticationException.OidcError("OidcCancelled", message!!)
is RustAuthenticationException.OidcCallbackUrlInvalid -> AuthenticationException.OidcError("OidcCallbackUrlInvalid", message!!)
else -> AuthenticationException.Generic(this.message ?: "Unknown error")
}
}

View file

@ -23,6 +23,6 @@ fun HomeserverLoginDetails.map(): MatrixHomeServerDetails = use {
MatrixHomeServerDetails(
url = url(),
supportsPasswordLogin = supportsPasswordLogin(),
supportsOidcLogin = false // TODO Oidc supportsOidcLogin(),
supportsOidcLogin = supportsOidcLogin(),
)
}

View file

@ -16,17 +16,19 @@
package io.element.android.libraries.matrix.impl.auth
// TODO Oidc
// import io.element.android.libraries.matrix.api.auth.OidcConfig
// import org.matrix.rustcomponents.sdk.OidcClientMetadata
import io.element.android.libraries.matrix.api.auth.OidcConfig
import org.matrix.rustcomponents.sdk.OidcConfiguration
/*
val oidcClientMetadata: OidcClientMetadata = OidcClientMetadata(
val oidcConfiguration: OidcConfiguration = OidcConfiguration(
clientName = "Element",
redirectUri = OidcConfig.redirectUri,
clientUri = "https://element.io",
tosUri = "https://element.io/user-terms-of-service",
policyUri = "https://element.io/privacy"
policyUri = "https://element.io/privacy",
/**
* Some homeservers/auth issuers don't support dynamic client registration, and have to be registered manually
*/
staticRegistrations = mapOf(
"https://id.thirdroom.io/realms/thirdroom" to "elementx",
),
)
*/

View file

@ -16,8 +16,6 @@
package io.element.android.libraries.matrix.impl.auth
// TODO Oidc
// import org.matrix.rustcomponents.sdk.OidcAuthenticationUrl
import com.squareup.anvil.annotations.ContributesBinding
import io.element.android.libraries.core.coroutine.CoroutineDispatchers
import io.element.android.libraries.core.extensions.mapFailure
@ -30,17 +28,16 @@ import io.element.android.libraries.matrix.api.auth.OidcDetails
import io.element.android.libraries.matrix.api.core.SessionId
import io.element.android.libraries.matrix.impl.RustMatrixClientFactory
import io.element.android.libraries.matrix.impl.exception.mapClientException
import io.element.android.libraries.matrix.impl.mapper.toSessionData
import io.element.android.libraries.network.useragent.UserAgentProvider
import io.element.android.libraries.sessionstorage.api.SessionData
import io.element.android.libraries.sessionstorage.api.SessionStore
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.withContext
import org.matrix.rustcomponents.sdk.Session
import org.matrix.rustcomponents.sdk.OidcAuthenticationData
import org.matrix.rustcomponents.sdk.use
import java.io.File
import java.util.Date
import javax.inject.Inject
import org.matrix.rustcomponents.sdk.AuthenticationService as RustAuthenticationService
@ -57,9 +54,8 @@ class RustMatrixAuthenticationService @Inject constructor(
private val authService: RustAuthenticationService = RustAuthenticationService(
basePath = baseDirectory.absolutePath,
passphrase = null,
// TODO Oidc
// oidcClientMetadata = oidcClientMetadata,
userAgent = userAgentProvider.provide(),
oidcConfiguration = oidcConfiguration,
customSlidingSyncProxy = null,
)
private var currentHomeserver = MutableStateFlow<MatrixHomeServerDetails?>(null)
@ -112,68 +108,48 @@ class RustMatrixAuthenticationService @Inject constructor(
}
}
// TODO Oidc
// private var pendingUrlForOidcLogin: OidcAuthenticationUrl? = null
private var pendingOidcAuthenticationData: OidcAuthenticationData? = null
override suspend fun getOidcUrl(): Result<OidcDetails> {
TODO("Oidc")
/*
return withContext(coroutineDispatchers.io) {
runCatching {
val urlForOidcLogin = authService.urlForOidcLogin()
val url = urlForOidcLogin.loginUrl()
pendingUrlForOidcLogin = urlForOidcLogin
val oidcAuthenticationData = authService.urlForOidcLogin()
val url = oidcAuthenticationData.loginUrl()
pendingOidcAuthenticationData = oidcAuthenticationData
OidcDetails(url)
}.mapFailure { failure ->
failure.mapAuthenticationException()
}
}
*/
}
override suspend fun cancelOidcLogin(): Result<Unit> {
TODO("Oidc")
/*
return withContext(coroutineDispatchers.io) {
runCatching {
pendingUrlForOidcLogin?.close()
pendingUrlForOidcLogin = null
pendingOidcAuthenticationData?.close()
pendingOidcAuthenticationData = null
}.mapFailure { failure ->
failure.mapAuthenticationException()
}
}
*/
}
/**
* callbackUrl should be the uriRedirect from OidcClientMetadata (with all the parameters).
*/
override suspend fun loginWithOidc(callbackUrl: String): Result<SessionId> {
TODO("Oidc")
/*
return withContext(coroutineDispatchers.io) {
runCatching {
val urlForOidcLogin = pendingUrlForOidcLogin ?: error("You need to call `getOidcUrl()` first")
val urlForOidcLogin = pendingOidcAuthenticationData ?: error("You need to call `getOidcUrl()` first")
val client = authService.loginWithOidcCallback(urlForOidcLogin, callbackUrl)
val sessionData = client.use { it.session().toSessionData() }
pendingUrlForOidcLogin = null
pendingOidcAuthenticationData?.close()
pendingOidcAuthenticationData = null
sessionStore.storeData(sessionData)
SessionId(sessionData.userId)
}.mapFailure { failure ->
failure.mapAuthenticationException()
}
}
*/
}
}
private fun Session.toSessionData() = SessionData(
userId = userId,
deviceId = deviceId,
accessToken = accessToken,
refreshToken = refreshToken,
homeserverUrl = homeserverUrl,
slidingSyncProxy = slidingSyncProxy,
loginTimestamp = Date(),
)

View file

@ -0,0 +1,32 @@
/*
* Copyright (c) 2023 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.element.android.libraries.matrix.impl.mapper
import io.element.android.libraries.sessionstorage.api.SessionData
import org.matrix.rustcomponents.sdk.Session
import java.util.Date
internal fun Session.toSessionData() = SessionData(
userId = userId,
deviceId = deviceId,
accessToken = accessToken,
refreshToken = refreshToken,
homeserverUrl = homeserverUrl,
oidcData = oidcData,
slidingSyncProxy = slidingSyncProxy,
loginTimestamp = Date(),
)

View file

@ -51,6 +51,7 @@ class FakeMatrixClient(
private val pushersService: FakePushersService = FakePushersService(),
private val notificationService: FakeNotificationService = FakeNotificationService(),
private val syncService: FakeSyncService = FakeSyncService(),
private val accountManagementUrlString: Result<String?> = Result.success(null),
) : MatrixClient {
private var ignoreUserResult: Result<Unit> = Result.success(Unit)
@ -109,9 +110,10 @@ class FakeMatrixClient(
override suspend fun clearCache() {
}
override suspend fun logout() {
override suspend fun logout(): String? {
delay(100)
logoutFailure?.let { throw it }
return null
}
override fun close() = Unit
@ -124,6 +126,9 @@ class FakeMatrixClient(
return userAvatarURLString
}
override suspend fun getAccountManagementUrl(): Result<String?> {
return accountManagementUrlString
}
override suspend fun uploadMedia(
mimeType: String,
data: ByteArray,

View file

@ -24,6 +24,7 @@ data class SessionData(
val accessToken: String,
val refreshToken: String?,
val homeserverUrl: String,
val oidcData: String?,
val slidingSyncProxy: String?,
val loginTimestamp: Date?,
)

View file

@ -23,6 +23,12 @@ interface SessionStore {
fun isLoggedIn(): Flow<Boolean>
fun sessionsFlow(): Flow<List<SessionData>>
suspend fun storeData(sessionData: SessionData)
/**
* Will update the session data matching the userId, except the value of loginTimestamp.
* No op if userId is not found in DB.
*/
suspend fun updateData(sessionData: SessionData)
suspend fun getSession(sessionId: String): SessionData?
suspend fun getAllSessions(): List<SessionData>
suspend fun getLatestSession(): SessionData?

View file

@ -38,6 +38,10 @@ class InMemorySessionStore : SessionStore {
sessionDataFlow.value = sessionData
}
override suspend fun updateData(sessionData: SessionData) {
sessionDataFlow.value = sessionData
}
override suspend fun getSession(sessionId: String): SessionData? {
return sessionDataFlow.value.takeIf { it?.userId == sessionId }
}

View file

@ -46,6 +46,24 @@ class DatabaseSessionStore @Inject constructor(
database.sessionDataQueries.insertSessionData(sessionData.toDbModel())
}
override suspend fun updateData(sessionData: SessionData) {
val result = database.sessionDataQueries.selectByUserId(sessionData.userId)
.executeAsOneOrNull()
?.toApiModel()
if (result == null) {
Timber.e("User ${sessionData.userId} not found in session database")
return
}
// Copy new data from SDK, but keep login timestamp
database.sessionDataQueries.updateSession(
sessionData.copy(
loginTimestamp = result.loginTimestamp,
).toDbModel()
)
}
override suspend fun getLatestSession(): SessionData? {
return database.sessionDataQueries.selectFirst()
.executeAsOneOrNull()

View file

@ -27,6 +27,7 @@ internal fun SessionData.toDbModel(): DbSessionData {
accessToken = accessToken,
refreshToken = refreshToken,
homeserverUrl = homeserverUrl,
oidcData = oidcData,
slidingSyncProxy = slidingSyncProxy,
loginTimestamp = loginTimestamp?.time,
)
@ -39,6 +40,7 @@ internal fun DbSessionData.toApiModel(): SessionData {
accessToken = accessToken,
refreshToken = refreshToken,
homeserverUrl = homeserverUrl,
oidcData = oidcData,
slidingSyncProxy = slidingSyncProxy,
loginTimestamp = loginTimestamp?.let { Date(it) }
)

View file

@ -5,7 +5,8 @@ CREATE TABLE SessionData (
refreshToken TEXT,
homeserverUrl TEXT NOT NULL,
slidingSyncProxy TEXT,
loginTimestamp INTEGER
loginTimestamp INTEGER,
oidcData TEXT
);
@ -23,3 +24,6 @@ INSERT INTO SessionData VALUES ?;
removeSession:
DELETE FROM SessionData WHERE userId = ?;
updateSession:
REPLACE INTO SessionData VALUES ?;

View file

@ -0,0 +1 @@
ALTER TABLE SessionData ADD COLUMN oidcData TEXT;

View file

@ -37,6 +37,7 @@ class DatabaseSessionStoreTests {
homeserverUrl = "homeserverUrl",
slidingSyncProxy = null,
loginTimestamp = null,
oidcData = "aOidcData",
)
@Before
@ -108,4 +109,45 @@ class DatabaseSessionStoreTests {
assertThat(database.sessionDataQueries.selectByUserId(aSessionData.userId).executeAsOneOrNull()).isNull()
}
@Test
fun `update session update all fields except loginTimestamp`() = runTest {
val firstSessionData = SessionData(
userId = "userId",
deviceId = "deviceId",
accessToken = "accessToken",
refreshToken = "refreshToken",
homeserverUrl = "homeserverUrl",
slidingSyncProxy = "slidingSyncProxy",
loginTimestamp = 1,
oidcData = "aOidcData",
)
val secondSessionData = SessionData(
userId = "userId",
deviceId = "deviceIdAltered",
accessToken = "accessTokenAltered",
refreshToken = "refreshTokenAltered",
homeserverUrl = "homeserverUrlAltered",
slidingSyncProxy = "slidingSyncProxyAltered",
loginTimestamp = 2,
oidcData = "aOidcDataAltered",
)
assertThat(firstSessionData.userId).isEqualTo(secondSessionData.userId)
assertThat(firstSessionData.loginTimestamp).isNotEqualTo(secondSessionData.loginTimestamp)
database.sessionDataQueries.insertSessionData(firstSessionData)
databaseSessionStore.updateData(secondSessionData.toApiModel())
// Get the altered session
val alteredSession = databaseSessionStore.getSession(firstSessionData.userId)!!.toDbModel()
assertThat(alteredSession.userId).isEqualTo(secondSessionData.userId)
assertThat(alteredSession.deviceId).isEqualTo(secondSessionData.deviceId)
assertThat(alteredSession.accessToken).isEqualTo(secondSessionData.accessToken)
assertThat(alteredSession.refreshToken).isEqualTo(secondSessionData.refreshToken)
assertThat(alteredSession.homeserverUrl).isEqualTo(secondSessionData.homeserverUrl)
assertThat(alteredSession.slidingSyncProxy).isEqualTo(secondSessionData.slidingSyncProxy)
assertThat(alteredSession.loginTimestamp).isEqualTo(/* Not altered! */ firstSessionData.loginTimestamp)
assertThat(alteredSession.oidcData).isEqualTo(secondSessionData.oidcData)
}
}

View file

@ -180,11 +180,21 @@
<string name="screen_migration_title">"Setting up your account."</string>
<string name="screen_notification_settings_additional_settings_section_title">"Additional settings"</string>
<string name="screen_notification_settings_calls_label">"Audio and video calls"</string>
<string name="screen_notification_settings_configuration_mismatch">"Configuration mismatch"</string>
<string name="screen_notification_settings_configuration_mismatch_description">"Weve simplified Notifications Settings to make options easier to find.
Some custom settings youve chosen in the past are not shown here, but theyre still active.
If you proceed, some of your settings may change."</string>
<string name="screen_notification_settings_direct_chats">"Direct chats"</string>
<string name="screen_notification_settings_edit_custom_settings_section_title">"Custom setting per chat"</string>
<string name="screen_notification_settings_edit_failed_updating_default_mode">"An error occurred while updating the notification setting."</string>
<string name="screen_notification_settings_edit_mode_all_messages">"All messages"</string>
<string name="screen_notification_settings_edit_mode_mentions_and_keywords">"Mentions and Keywords only"</string>
<string name="screen_notification_settings_edit_screen_direct_section_header">"On direct chats, notify me for"</string>
<string name="screen_notification_settings_edit_screen_group_section_header">"On group chats, notify me for"</string>
<string name="screen_notification_settings_enable_notifications">"Enable notifications on this device"</string>
<string name="screen_notification_settings_failed_fixing_configuration">"The configuration has not been corrected, please try again."</string>
<string name="screen_notification_settings_group_chats">"Group chats"</string>
<string name="screen_notification_settings_mentions_section_title">"Mentions"</string>
<string name="screen_notification_settings_mode_all">"All"</string>
@ -196,6 +206,7 @@
<string name="screen_notification_settings_system_notifications_turned_off">"System notifications turned off"</string>
<string name="screen_notification_settings_title">"Notifications"</string>
<string name="screen_report_content_block_user_hint">"Check if you want to hide all current and future messages from this user"</string>
<string name="screen_settings_oidc_account">"Account and devices"</string>
<string name="screen_share_location_title">"Share location"</string>
<string name="screen_share_my_location_action">"Share my location"</string>
<string name="screen_share_open_apple_maps">"Open in Apple Maps"</string>