Skip to content

Commit 0236396

Browse files
committed
Add optional deviceId to the login API
1 parent f2c22c1 commit 0236396

File tree

9 files changed

+72
-34
lines changed

9 files changed

+72
-34
lines changed

changelog.d/4334.removal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add optional deviceId to the login API

matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/auth/AuthenticationService.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,15 @@ interface AuthenticationService {
105105
/**
106106
* Authenticate with a matrixId and a password
107107
* Usually call this after a successful call to getWellKnownData()
108+
* @param homeServerConnectionConfig the information about the homeserver and other configuration
109+
* @param matrixId the matrixId of the user
110+
* @param password the password of the account
111+
* @param initialDeviceName the initial device name
112+
* @param deviceId the device id, optional. If not provided or null, the server will generate one.
108113
*/
109114
suspend fun directAuthentication(homeServerConnectionConfig: HomeServerConnectionConfig,
110115
matrixId: String,
111116
password: String,
112-
initialDeviceName: String): Session
117+
initialDeviceName: String,
118+
deviceId: String? = null): Session
113119
}

matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/auth/login/LoginWizard.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,14 @@ interface LoginWizard {
3434
*
3535
* @param login the login field. Can be a user name, or a msisdn (email or phone number) associated to the account
3636
* @param password the password of the account
37-
* @param deviceName the initial device name
37+
* @param initialDeviceName the initial device name
38+
* @param deviceId the device id, optional. If not provided or null, the server will generate one.
3839
* @return a [Session] if the login is successful
3940
*/
4041
suspend fun login(login: String,
4142
password: String,
42-
deviceName: String): Session
43+
initialDeviceName: String,
44+
deviceId: String? = null): Session
4345

4446
/**
4547
* Exchange a login token to an access token.

matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/DefaultAuthenticationService.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -388,8 +388,15 @@ internal class DefaultAuthenticationService @Inject constructor(
388388
override suspend fun directAuthentication(homeServerConnectionConfig: HomeServerConnectionConfig,
389389
matrixId: String,
390390
password: String,
391-
initialDeviceName: String): Session {
392-
return directLoginTask.execute(DirectLoginTask.Params(homeServerConnectionConfig, matrixId, password, initialDeviceName))
391+
initialDeviceName: String,
392+
deviceId: String?): Session {
393+
return directLoginTask.execute(DirectLoginTask.Params(
394+
homeServerConnectionConfig = homeServerConnectionConfig,
395+
userId = matrixId,
396+
password = password,
397+
deviceName = initialDeviceName,
398+
deviceId = deviceId
399+
))
393400
}
394401

395402
private fun buildAuthAPI(homeServerConnectionConfig: HomeServerConnectionConfig): AuthAPI {

matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/data/PasswordLoginParams.kt

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -49,51 +49,54 @@ internal data class PasswordLoginParams(
4949

5050
fun userIdentifier(user: String,
5151
password: String,
52-
deviceDisplayName: String? = null,
53-
deviceId: String? = null): PasswordLoginParams {
52+
deviceDisplayName: String?,
53+
deviceId: String?): PasswordLoginParams {
5454
return PasswordLoginParams(
55-
mapOf(
55+
identifier = mapOf(
5656
IDENTIFIER_KEY_TYPE to IDENTIFIER_KEY_TYPE_USER,
5757
IDENTIFIER_KEY_USER to user
5858
),
59-
password,
60-
LoginFlowTypes.PASSWORD,
61-
deviceDisplayName,
62-
deviceId)
59+
password = password,
60+
type = LoginFlowTypes.PASSWORD,
61+
deviceDisplayName = deviceDisplayName,
62+
deviceId = deviceId
63+
)
6364
}
6465

6566
fun thirdPartyIdentifier(medium: String,
6667
address: String,
6768
password: String,
68-
deviceDisplayName: String? = null,
69-
deviceId: String? = null): PasswordLoginParams {
69+
deviceDisplayName: String?,
70+
deviceId: String?): PasswordLoginParams {
7071
return PasswordLoginParams(
71-
mapOf(
72+
identifier = mapOf(
7273
IDENTIFIER_KEY_TYPE to IDENTIFIER_KEY_TYPE_THIRD_PARTY,
7374
IDENTIFIER_KEY_MEDIUM to medium,
7475
IDENTIFIER_KEY_ADDRESS to address
7576
),
76-
password,
77-
LoginFlowTypes.PASSWORD,
78-
deviceDisplayName,
79-
deviceId)
77+
password = password,
78+
type = LoginFlowTypes.PASSWORD,
79+
deviceDisplayName = deviceDisplayName,
80+
deviceId = deviceId
81+
)
8082
}
8183

8284
fun phoneIdentifier(country: String,
8385
phone: String,
8486
password: String,
85-
deviceDisplayName: String? = null,
86-
deviceId: String? = null): PasswordLoginParams {
87+
deviceDisplayName: String?,
88+
deviceId: String?): PasswordLoginParams {
8789
return PasswordLoginParams(
88-
mapOf(
90+
identifier = mapOf(
8991
IDENTIFIER_KEY_TYPE to IDENTIFIER_KEY_TYPE_PHONE,
9092
IDENTIFIER_KEY_COUNTRY to country,
9193
IDENTIFIER_KEY_PHONE to phone
9294
),
93-
password,
94-
LoginFlowTypes.PASSWORD,
95-
deviceDisplayName,
96-
deviceId)
95+
password = password,
96+
type = LoginFlowTypes.PASSWORD,
97+
deviceDisplayName = deviceDisplayName,
98+
deviceId = deviceId
99+
)
97100
}
98101
}
99102
}

matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/login/DefaultLoginWizard.kt

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,23 @@ internal class DefaultLoginWizard(
5252

5353
override suspend fun login(login: String,
5454
password: String,
55-
deviceName: String): Session {
55+
initialDeviceName: String,
56+
deviceId: String?): Session {
5657
val loginParams = if (Patterns.EMAIL_ADDRESS.matcher(login).matches()) {
57-
PasswordLoginParams.thirdPartyIdentifier(ThreePidMedium.EMAIL, login, password, deviceName)
58+
PasswordLoginParams.thirdPartyIdentifier(
59+
medium = ThreePidMedium.EMAIL,
60+
address = login,
61+
password = password,
62+
deviceDisplayName = initialDeviceName,
63+
deviceId = deviceId
64+
)
5865
} else {
59-
PasswordLoginParams.userIdentifier(login, password, deviceName)
66+
PasswordLoginParams.userIdentifier(
67+
user = login,
68+
password = password,
69+
deviceDisplayName = initialDeviceName,
70+
deviceId = deviceId
71+
)
6072
}
6173
val credentials = executeRequest(null) {
6274
authAPI.login(loginParams)

matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/login/DirectLoginTask.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ internal interface DirectLoginTask : Task<DirectLoginTask.Params, Session> {
3737
val homeServerConnectionConfig: HomeServerConnectionConfig,
3838
val userId: String,
3939
val password: String,
40-
val deviceName: String
40+
val deviceName: String,
41+
val deviceId: String?
4142
)
4243
}
4344

@@ -55,7 +56,12 @@ internal class DefaultDirectLoginTask @Inject constructor(
5556
val authAPI = retrofitFactory.create(client, homeServerUrl)
5657
.create(AuthAPI::class.java)
5758

58-
val loginParams = PasswordLoginParams.userIdentifier(params.userId, params.password, params.deviceName)
59+
val loginParams = PasswordLoginParams.userIdentifier(
60+
user = params.userId,
61+
password = params.password,
62+
deviceDisplayName = params.deviceName,
63+
deviceId = params.deviceId
64+
)
5965

6066
val credentials = try {
6167
executeRequest(null) {

matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/signout/SignInAgainTask.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,12 @@ internal class DefaultSignInAgainTask @Inject constructor(
4242
signOutAPI.loginAgain(
4343
PasswordLoginParams.userIdentifier(
4444
// Reuse the same userId
45-
sessionParams.userId,
46-
params.password,
45+
user = sessionParams.userId,
46+
password = params.password,
4747
// The spec says the initial device name will be ignored
4848
// https://matrix.org/docs/spec/client_server/latest#post-matrix-client-r0-login
4949
// but https://github.com/matrix-org/synapse/issues/6525
50+
deviceDisplayName = null,
5051
// Reuse the same deviceId
5152
deviceId = sessionParams.deviceId
5253
)

vector/src/main/java/im/vector/app/features/login2/LoginViewModel2.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ class LoginViewModel2 @AssistedInject constructor(
547547
safeLoginWizard.login(
548548
login = login,
549549
password = password,
550-
deviceName = stringProvider.getString(R.string.login_default_session_public_name)
550+
initialDeviceName = stringProvider.getString(R.string.login_default_session_public_name)
551551
)
552552
} catch (failure: Throwable) {
553553
_viewEvents.post(LoginViewEvents2.Failure(failure))

0 commit comments

Comments
 (0)