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
* 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,
matrixId: 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 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
*/
suspend fun login(login: String,
password: String,
deviceName: String): Session
initialDeviceName: String,
deviceId: String? = null): Session
/**
* 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,
matrixId: String,
password: String,
initialDeviceName: String): Session {
return directLoginTask.execute(DirectLoginTask.Params(homeServerConnectionConfig, matrixId, password, initialDeviceName))
initialDeviceName: String,
deviceId: String?): Session {
return directLoginTask.execute(DirectLoginTask.Params(
homeServerConnectionConfig = homeServerConnectionConfig,
userId = matrixId,
password = password,
deviceName = initialDeviceName,
deviceId = deviceId
))
}
private fun buildAuthAPI(homeServerConnectionConfig: HomeServerConnectionConfig): AuthAPI {

View file

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

View file

@ -52,11 +52,23 @@ internal class DefaultLoginWizard(
override suspend fun login(login: String,
password: String,
deviceName: String): Session {
initialDeviceName: String,
deviceId: String?): Session {
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 {
PasswordLoginParams.userIdentifier(login, password, deviceName)
PasswordLoginParams.userIdentifier(
user = login,
password = password,
deviceDisplayName = initialDeviceName,
deviceId = deviceId
)
}
val credentials = executeRequest(null) {
authAPI.login(loginParams)

View file

@ -37,7 +37,8 @@ internal interface DirectLoginTask : Task<DirectLoginTask.Params, Session> {
val homeServerConnectionConfig: HomeServerConnectionConfig,
val userId: 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)
.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 {
executeRequest(null) {

View file

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

View file

@ -547,7 +547,7 @@ class LoginViewModel2 @AssistedInject constructor(
safeLoginWizard.login(
login = login,
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) {
_viewEvents.post(LoginViewEvents2.Failure(failure))