Add optional deviceId to the login API

This commit is contained in:
Benoit Marty 2021-10-26 11:15:33 +02:00
parent f2c22c1985
commit 0236396c59
9 changed files with 72 additions and 34 deletions

1
changelog.d/4334.removal Normal file
View File

@ -0,0 +1 @@
Add optional deviceId to the login API

View File

@ -105,9 +105,15 @@ interface AuthenticationService {
/** /**
* Authenticate with a matrixId and a password * Authenticate with a matrixId and a password
* Usually call this after a successful call to getWellKnownData() * Usually call this after a successful call to getWellKnownData()
* @param homeServerConnectionConfig the information about the homeserver and other configuration
* @param matrixId the matrixId of the user
* @param password the password of the account
* @param initialDeviceName the initial device name
* @param deviceId the device id, optional. If not provided or null, the server will generate one.
*/ */
suspend fun directAuthentication(homeServerConnectionConfig: HomeServerConnectionConfig, suspend fun directAuthentication(homeServerConnectionConfig: HomeServerConnectionConfig,
matrixId: String, matrixId: String,
password: String, password: String,
initialDeviceName: String): Session initialDeviceName: String,
deviceId: String? = null): Session
} }

View File

@ -34,12 +34,14 @@ interface LoginWizard {
* *
* @param login the login field. Can be a user name, or a msisdn (email or phone number) associated to the account * @param login the login field. Can be a user name, or a msisdn (email or phone number) associated to the account
* @param password the password of the account * @param password the password of the account
* @param deviceName the initial device name * @param initialDeviceName the initial device name
* @param deviceId the device id, optional. If not provided or null, the server will generate one.
* @return a [Session] if the login is successful * @return a [Session] if the login is successful
*/ */
suspend fun login(login: String, suspend fun login(login: String,
password: String, password: String,
deviceName: String): Session initialDeviceName: String,
deviceId: String? = null): Session
/** /**
* Exchange a login token to an access token. * Exchange a login token to an access token.

View File

@ -388,8 +388,15 @@ internal class DefaultAuthenticationService @Inject constructor(
override suspend fun directAuthentication(homeServerConnectionConfig: HomeServerConnectionConfig, override suspend fun directAuthentication(homeServerConnectionConfig: HomeServerConnectionConfig,
matrixId: String, matrixId: String,
password: String, password: String,
initialDeviceName: String): Session { initialDeviceName: String,
return directLoginTask.execute(DirectLoginTask.Params(homeServerConnectionConfig, matrixId, password, initialDeviceName)) deviceId: String?): Session {
return directLoginTask.execute(DirectLoginTask.Params(
homeServerConnectionConfig = homeServerConnectionConfig,
userId = matrixId,
password = password,
deviceName = initialDeviceName,
deviceId = deviceId
))
} }
private fun buildAuthAPI(homeServerConnectionConfig: HomeServerConnectionConfig): AuthAPI { private fun buildAuthAPI(homeServerConnectionConfig: HomeServerConnectionConfig): AuthAPI {

View File

@ -49,51 +49,54 @@ internal data class PasswordLoginParams(
fun userIdentifier(user: String, fun userIdentifier(user: String,
password: String, password: String,
deviceDisplayName: String? = null, deviceDisplayName: String?,
deviceId: String? = null): PasswordLoginParams { deviceId: String?): PasswordLoginParams {
return PasswordLoginParams( return PasswordLoginParams(
mapOf( identifier = mapOf(
IDENTIFIER_KEY_TYPE to IDENTIFIER_KEY_TYPE_USER, IDENTIFIER_KEY_TYPE to IDENTIFIER_KEY_TYPE_USER,
IDENTIFIER_KEY_USER to user IDENTIFIER_KEY_USER to user
), ),
password, password = password,
LoginFlowTypes.PASSWORD, type = LoginFlowTypes.PASSWORD,
deviceDisplayName, deviceDisplayName = deviceDisplayName,
deviceId) deviceId = deviceId
)
} }
fun thirdPartyIdentifier(medium: String, fun thirdPartyIdentifier(medium: String,
address: String, address: String,
password: String, password: String,
deviceDisplayName: String? = null, deviceDisplayName: String?,
deviceId: String? = null): PasswordLoginParams { deviceId: String?): PasswordLoginParams {
return PasswordLoginParams( return PasswordLoginParams(
mapOf( identifier = mapOf(
IDENTIFIER_KEY_TYPE to IDENTIFIER_KEY_TYPE_THIRD_PARTY, IDENTIFIER_KEY_TYPE to IDENTIFIER_KEY_TYPE_THIRD_PARTY,
IDENTIFIER_KEY_MEDIUM to medium, IDENTIFIER_KEY_MEDIUM to medium,
IDENTIFIER_KEY_ADDRESS to address IDENTIFIER_KEY_ADDRESS to address
), ),
password, password = password,
LoginFlowTypes.PASSWORD, type = LoginFlowTypes.PASSWORD,
deviceDisplayName, deviceDisplayName = deviceDisplayName,
deviceId) deviceId = deviceId
)
} }
fun phoneIdentifier(country: String, fun phoneIdentifier(country: String,
phone: String, phone: String,
password: String, password: String,
deviceDisplayName: String? = null, deviceDisplayName: String?,
deviceId: String? = null): PasswordLoginParams { deviceId: String?): PasswordLoginParams {
return PasswordLoginParams( return PasswordLoginParams(
mapOf( identifier = mapOf(
IDENTIFIER_KEY_TYPE to IDENTIFIER_KEY_TYPE_PHONE, IDENTIFIER_KEY_TYPE to IDENTIFIER_KEY_TYPE_PHONE,
IDENTIFIER_KEY_COUNTRY to country, IDENTIFIER_KEY_COUNTRY to country,
IDENTIFIER_KEY_PHONE to phone IDENTIFIER_KEY_PHONE to phone
), ),
password, password = password,
LoginFlowTypes.PASSWORD, type = LoginFlowTypes.PASSWORD,
deviceDisplayName, deviceDisplayName = deviceDisplayName,
deviceId) deviceId = deviceId
)
} }
} }
} }

View File

@ -52,11 +52,23 @@ internal class DefaultLoginWizard(
override suspend fun login(login: String, override suspend fun login(login: String,
password: String, password: String,
deviceName: String): Session { initialDeviceName: String,
deviceId: String?): Session {
val loginParams = if (Patterns.EMAIL_ADDRESS.matcher(login).matches()) { val loginParams = if (Patterns.EMAIL_ADDRESS.matcher(login).matches()) {
PasswordLoginParams.thirdPartyIdentifier(ThreePidMedium.EMAIL, login, password, deviceName) PasswordLoginParams.thirdPartyIdentifier(
medium = ThreePidMedium.EMAIL,
address = login,
password = password,
deviceDisplayName = initialDeviceName,
deviceId = deviceId
)
} else { } else {
PasswordLoginParams.userIdentifier(login, password, deviceName) PasswordLoginParams.userIdentifier(
user = login,
password = password,
deviceDisplayName = initialDeviceName,
deviceId = deviceId
)
} }
val credentials = executeRequest(null) { val credentials = executeRequest(null) {
authAPI.login(loginParams) authAPI.login(loginParams)

View File

@ -37,7 +37,8 @@ internal interface DirectLoginTask : Task<DirectLoginTask.Params, Session> {
val homeServerConnectionConfig: HomeServerConnectionConfig, val homeServerConnectionConfig: HomeServerConnectionConfig,
val userId: String, val userId: String,
val password: String, val password: String,
val deviceName: String val deviceName: String,
val deviceId: String?
) )
} }
@ -55,7 +56,12 @@ internal class DefaultDirectLoginTask @Inject constructor(
val authAPI = retrofitFactory.create(client, homeServerUrl) val authAPI = retrofitFactory.create(client, homeServerUrl)
.create(AuthAPI::class.java) .create(AuthAPI::class.java)
val loginParams = PasswordLoginParams.userIdentifier(params.userId, params.password, params.deviceName) val loginParams = PasswordLoginParams.userIdentifier(
user = params.userId,
password = params.password,
deviceDisplayName = params.deviceName,
deviceId = params.deviceId
)
val credentials = try { val credentials = try {
executeRequest(null) { executeRequest(null) {

View File

@ -42,11 +42,12 @@ internal class DefaultSignInAgainTask @Inject constructor(
signOutAPI.loginAgain( signOutAPI.loginAgain(
PasswordLoginParams.userIdentifier( PasswordLoginParams.userIdentifier(
// Reuse the same userId // Reuse the same userId
sessionParams.userId, user = sessionParams.userId,
params.password, password = params.password,
// The spec says the initial device name will be ignored // The spec says the initial device name will be ignored
// https://matrix.org/docs/spec/client_server/latest#post-matrix-client-r0-login // https://matrix.org/docs/spec/client_server/latest#post-matrix-client-r0-login
// but https://github.com/matrix-org/synapse/issues/6525 // but https://github.com/matrix-org/synapse/issues/6525
deviceDisplayName = null,
// Reuse the same deviceId // Reuse the same deviceId
deviceId = sessionParams.deviceId deviceId = sessionParams.deviceId
) )

View File

@ -547,7 +547,7 @@ class LoginViewModel2 @AssistedInject constructor(
safeLoginWizard.login( safeLoginWizard.login(
login = login, login = login,
password = password, password = password,
deviceName = stringProvider.getString(R.string.login_default_session_public_name) initialDeviceName = stringProvider.getString(R.string.login_default_session_public_name)
) )
} catch (failure: Throwable) { } catch (failure: Throwable) {
_viewEvents.post(LoginViewEvents2.Failure(failure)) _viewEvents.post(LoginViewEvents2.Failure(failure))