Skip to content

Commit 2839d14

Browse files
authored
Feature/5575 custom auth params for sign up (#5577)
Add a fun `RegistrationWizard.registrationCustom()` to be able to use any parameters during the registration. Move terms converter into `api` package.
1 parent 96350b0 commit 2839d14

File tree

11 files changed

+268
-138
lines changed

11 files changed

+268
-138
lines changed

changelog.d/5575.sdk

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- Added registrationCustom into RegistrationWizard to send custom auth params for sign up
2+
- Moved terms converter into api package to make it accessible in sdk

vector/src/main/java/im/vector/app/features/login/terms/UrlAndName.kt renamed to matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/auth/UrlAndName.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019 New Vector Ltd
2+
* Copyright 2020 The Matrix.org Foundation C.I.C.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package im.vector.app.features.login.terms
17+
package org.matrix.android.sdk.api.auth
1818

1919
data class UrlAndName(
2020
val url: String,
Original file line numberDiff line numberDiff line change
@@ -1,127 +1,127 @@
1-
/*
2-
* Copyright 2019 New Vector Ltd
3-
*
4-
* Licensed under the Apache License, Version 2.0 (the "License");
5-
* you may not use this file except in compliance with the License.
6-
* You may obtain a copy of the License at
7-
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
10-
* Unless required by applicable law or agreed to in writing, software
11-
* distributed under the License is distributed on an "AS IS" BASIS,
12-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
* See the License for the specific language governing permissions and
14-
* limitations under the License.
15-
*/
16-
17-
package im.vector.app.features.login.terms
18-
19-
import org.matrix.android.sdk.api.auth.data.LocalizedFlowDataLoginTerms
20-
import org.matrix.android.sdk.api.auth.registration.TermPolicies
21-
22-
/**
23-
* This method extract the policies from the login terms parameter, regarding the user language.
24-
* For each policy, if user language is not found, the default language is used and if not found, the first url and name are used (not predictable)
25-
*
26-
* Example of Data:
27-
* <pre>
28-
* "m.login.terms": {
29-
* "policies": {
30-
* "privacy_policy": {
31-
* "version": "1.0",
32-
* "en": {
33-
* "url": "http:\/\/matrix.org\/_matrix\/consent?v=1.0",
34-
* "name": "Terms and Conditions"
35-
* }
36-
* }
37-
* }
38-
* }
39-
*</pre>
40-
*
41-
* @param userLanguage the user language
42-
* @param defaultLanguage the default language to use if the user language is not found for a policy in registrationFlowResponse
43-
*/
44-
fun TermPolicies.toLocalizedLoginTerms(userLanguage: String,
45-
defaultLanguage: String = "en"): List<LocalizedFlowDataLoginTerms> {
46-
val result = ArrayList<LocalizedFlowDataLoginTerms>()
47-
48-
val policies = get("policies")
49-
if (policies is Map<*, *>) {
50-
policies.keys.forEach { policyName ->
51-
val localizedFlowDataLoginTermsPolicyName = policyName as String
52-
var localizedFlowDataLoginTermsVersion: String? = null
53-
var localizedFlowDataLoginTermsLocalizedUrl: String? = null
54-
var localizedFlowDataLoginTermsLocalizedName: String? = null
55-
56-
val policy = policies[policyName]
57-
58-
// Enter this policy
59-
if (policy is Map<*, *>) {
60-
// Version
61-
localizedFlowDataLoginTermsVersion = policy["version"] as String?
62-
63-
var userLanguageUrlAndName: UrlAndName? = null
64-
var defaultLanguageUrlAndName: UrlAndName? = null
65-
var firstUrlAndName: UrlAndName? = null
66-
67-
// Search for language
68-
policy.keys.forEach { policyKey ->
69-
when (policyKey) {
70-
"version" -> Unit // Ignore
71-
userLanguage -> {
72-
// We found the data for the user language
73-
userLanguageUrlAndName = extractUrlAndName(policy[policyKey])
74-
}
75-
defaultLanguage -> {
76-
// We found default language
77-
defaultLanguageUrlAndName = extractUrlAndName(policy[policyKey])
78-
}
79-
else -> {
80-
if (firstUrlAndName == null) {
81-
// Get at least some data
82-
firstUrlAndName = extractUrlAndName(policy[policyKey])
83-
}
84-
}
85-
}
86-
}
87-
88-
// Copy found language data by priority
89-
when {
90-
userLanguageUrlAndName != null -> {
91-
localizedFlowDataLoginTermsLocalizedUrl = userLanguageUrlAndName!!.url
92-
localizedFlowDataLoginTermsLocalizedName = userLanguageUrlAndName!!.name
93-
}
94-
defaultLanguageUrlAndName != null -> {
95-
localizedFlowDataLoginTermsLocalizedUrl = defaultLanguageUrlAndName!!.url
96-
localizedFlowDataLoginTermsLocalizedName = defaultLanguageUrlAndName!!.name
97-
}
98-
firstUrlAndName != null -> {
99-
localizedFlowDataLoginTermsLocalizedUrl = firstUrlAndName!!.url
100-
localizedFlowDataLoginTermsLocalizedName = firstUrlAndName!!.name
101-
}
102-
}
103-
}
104-
105-
result.add(LocalizedFlowDataLoginTerms(
106-
policyName = localizedFlowDataLoginTermsPolicyName,
107-
version = localizedFlowDataLoginTermsVersion,
108-
localizedUrl = localizedFlowDataLoginTermsLocalizedUrl,
109-
localizedName = localizedFlowDataLoginTermsLocalizedName
110-
))
111-
}
112-
}
113-
114-
return result
115-
}
116-
117-
private fun extractUrlAndName(policyData: Any?): UrlAndName? {
118-
if (policyData is Map<*, *>) {
119-
val url = policyData["url"] as String?
120-
val name = policyData["name"] as String?
121-
122-
if (url != null && name != null) {
123-
return UrlAndName(url, name)
124-
}
125-
}
126-
return null
127-
}
1+
/*
2+
* Copyright 2020 The Matrix.org Foundation C.I.C.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.matrix.android.sdk.api.auth
18+
19+
import org.matrix.android.sdk.api.auth.data.LocalizedFlowDataLoginTerms
20+
import org.matrix.android.sdk.api.auth.registration.TermPolicies
21+
22+
/**
23+
* This method extract the policies from the login terms parameter, regarding the user language.
24+
* For each policy, if user language is not found, the default language is used and if not found, the first url and name are used (not predictable)
25+
*
26+
* Example of Data:
27+
* <pre>
28+
* "m.login.terms": {
29+
* "policies": {
30+
* "privacy_policy": {
31+
* "version": "1.0",
32+
* "en": {
33+
* "url": "http:\/\/matrix.org\/_matrix\/consent?v=1.0",
34+
* "name": "Terms and Conditions"
35+
* }
36+
* }
37+
* }
38+
* }
39+
*</pre>
40+
*
41+
* @param userLanguage the user language
42+
* @param defaultLanguage the default language to use if the user language is not found for a policy in registrationFlowResponse
43+
*/
44+
fun TermPolicies.toLocalizedLoginTerms(userLanguage: String,
45+
defaultLanguage: String = "en"): List<LocalizedFlowDataLoginTerms> {
46+
val result = ArrayList<LocalizedFlowDataLoginTerms>()
47+
48+
val policies = get("policies")
49+
if (policies is Map<*, *>) {
50+
policies.keys.forEach { policyName ->
51+
val localizedFlowDataLoginTermsPolicyName = policyName as String
52+
var localizedFlowDataLoginTermsVersion: String? = null
53+
var localizedFlowDataLoginTermsLocalizedUrl: String? = null
54+
var localizedFlowDataLoginTermsLocalizedName: String? = null
55+
56+
val policy = policies[policyName]
57+
58+
// Enter this policy
59+
if (policy is Map<*, *>) {
60+
// Version
61+
localizedFlowDataLoginTermsVersion = policy["version"] as String?
62+
63+
var userLanguageUrlAndName: UrlAndName? = null
64+
var defaultLanguageUrlAndName: UrlAndName? = null
65+
var firstUrlAndName: UrlAndName? = null
66+
67+
// Search for language
68+
policy.keys.forEach { policyKey ->
69+
when (policyKey) {
70+
"version" -> Unit // Ignore
71+
userLanguage -> {
72+
// We found the data for the user language
73+
userLanguageUrlAndName = extractUrlAndName(policy[policyKey])
74+
}
75+
defaultLanguage -> {
76+
// We found default language
77+
defaultLanguageUrlAndName = extractUrlAndName(policy[policyKey])
78+
}
79+
else -> {
80+
if (firstUrlAndName == null) {
81+
// Get at least some data
82+
firstUrlAndName = extractUrlAndName(policy[policyKey])
83+
}
84+
}
85+
}
86+
}
87+
88+
// Copy found language data by priority
89+
when {
90+
userLanguageUrlAndName != null -> {
91+
localizedFlowDataLoginTermsLocalizedUrl = userLanguageUrlAndName!!.url
92+
localizedFlowDataLoginTermsLocalizedName = userLanguageUrlAndName!!.name
93+
}
94+
defaultLanguageUrlAndName != null -> {
95+
localizedFlowDataLoginTermsLocalizedUrl = defaultLanguageUrlAndName!!.url
96+
localizedFlowDataLoginTermsLocalizedName = defaultLanguageUrlAndName!!.name
97+
}
98+
firstUrlAndName != null -> {
99+
localizedFlowDataLoginTermsLocalizedUrl = firstUrlAndName!!.url
100+
localizedFlowDataLoginTermsLocalizedName = firstUrlAndName!!.name
101+
}
102+
}
103+
}
104+
105+
result.add(LocalizedFlowDataLoginTerms(
106+
policyName = localizedFlowDataLoginTermsPolicyName,
107+
version = localizedFlowDataLoginTermsVersion,
108+
localizedUrl = localizedFlowDataLoginTermsLocalizedUrl,
109+
localizedName = localizedFlowDataLoginTermsLocalizedName
110+
))
111+
}
112+
}
113+
114+
return result
115+
}
116+
117+
private fun extractUrlAndName(policyData: Any?): UrlAndName? {
118+
if (policyData is Map<*, *>) {
119+
val url = policyData["url"] as String?
120+
val name = policyData["name"] as String?
121+
122+
if (url != null && name != null) {
123+
return UrlAndName(url, name)
124+
}
125+
}
126+
return null
127+
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.matrix.android.sdk.api.auth.registration
1818

19+
import org.matrix.android.sdk.api.util.JsonDict
20+
1921
/**
2022
* Set of methods to be able to create an account on a homeserver.
2123
*
@@ -73,6 +75,13 @@ interface RegistrationWizard {
7375
*/
7476
suspend fun dummy(): RegistrationResult
7577

78+
/**
79+
* Perform custom registration stage by sending a custom JsonDict.
80+
* Current registration "session" param will be included into authParams by default.
81+
* The authParams should contain at least one entry "type" with a String value.
82+
*/
83+
suspend fun registrationCustom(authParams: JsonDict): RegistrationResult
84+
7685
/**
7786
* Perform the "m.login.email.identity" or "m.login.msisdn" stage.
7887
*

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import org.matrix.android.sdk.internal.auth.data.WebClientConfig
2626
import org.matrix.android.sdk.internal.auth.login.ResetPasswordMailConfirmed
2727
import org.matrix.android.sdk.internal.auth.registration.AddThreePidRegistrationParams
2828
import org.matrix.android.sdk.internal.auth.registration.AddThreePidRegistrationResponse
29+
import org.matrix.android.sdk.internal.auth.registration.RegistrationCustomParams
2930
import org.matrix.android.sdk.internal.auth.registration.RegistrationParams
3031
import org.matrix.android.sdk.internal.auth.registration.SuccessResult
3132
import org.matrix.android.sdk.internal.auth.registration.ValidationCodeBody
@@ -68,6 +69,14 @@ internal interface AuthAPI {
6869
@POST(NetworkConstants.URI_API_PREFIX_PATH_R0 + "register")
6970
suspend fun register(@Body registrationParams: RegistrationParams): Credentials
7071

72+
/**
73+
* Register to the homeserver, or get error 401 with a RegistrationFlowResponse object if registration is incomplete
74+
* method to perform other custom stages
75+
* Ref: https://matrix.org/docs/spec/client_server/latest#account-registration-and-management
76+
*/
77+
@POST(NetworkConstants.URI_API_PREFIX_PATH_R0 + "register")
78+
suspend fun registerCustom(@Body registrationCustomParams: RegistrationCustomParams): Credentials
79+
7180
/**
7281
* Checks to see if a username is available, and valid, for the server.
7382
*/

0 commit comments

Comments
 (0)