Skip to content

Commit 577dff7

Browse files
committed
feat: manage MFA initial commit
Signed-off-by: Gaurav Goel <[email protected]>
1 parent 57ecfc6 commit 577dff7

File tree

5 files changed

+87
-5
lines changed

5 files changed

+87
-5
lines changed

app/src/main/java/com/web3auth/app/MainActivity.kt

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ class MainActivity : AppCompatActivity(), AdapterView.OnItemClickListener {
123123
val launchWalletButton = findViewById<Button>(R.id.launchWalletButton)
124124
val signMsgButton = findViewById<Button>(R.id.signMsgButton)
125125
val btnSetUpMfa = findViewById<Button>(R.id.btnSetUpMfa)
126+
val btnManageMfa = findViewById<Button>(R.id.btn_manageMfa)
126127
val spinner = findViewById<TextInputLayout>(R.id.verifierList)
127128
val hintEmailEditText = findViewById<EditText>(R.id.etEmailHint)
128129
var key: String? = null
@@ -145,6 +146,7 @@ class MainActivity : AppCompatActivity(), AdapterView.OnItemClickListener {
145146
launchWalletButton.visibility = View.VISIBLE
146147
signMsgButton.visibility = View.VISIBLE
147148
btnSetUpMfa.visibility = View.VISIBLE
149+
btnManageMfa.visibility = View.VISIBLE
148150
spinner.visibility = View.GONE
149151
hintEmailEditText.visibility = View.GONE
150152
} else {
@@ -153,6 +155,7 @@ class MainActivity : AppCompatActivity(), AdapterView.OnItemClickListener {
153155
signInButton.visibility = View.VISIBLE
154156
signOutButton.visibility = View.GONE
155157
btnSetUpMfa.visibility = View.GONE
158+
btnManageMfa.visibility = View.GONE
156159
launchWalletButton.visibility = View.GONE
157160
signMsgButton.visibility = View.GONE
158161
spinner.visibility = View.VISIBLE
@@ -184,7 +187,7 @@ class MainActivity : AppCompatActivity(), AdapterView.OnItemClickListener {
184187
clientId = "d84f6xvbdV75VTGmHiMWfZLeSPk8M07C",
185188
)
186189
),
187-
buildEnv = BuildEnv.PRODUCTION,
190+
buildEnv = BuildEnv.STAGING,
188191
sessionTime = 86400,
189192
)
190193

@@ -272,6 +275,18 @@ class MainActivity : AppCompatActivity(), AdapterView.OnItemClickListener {
272275
}
273276
}
274277

278+
val btnManageMfa = findViewById<Button>(R.id.btn_manageMfa)
279+
btnManageMfa.setOnClickListener {
280+
val manageMfaCf = web3Auth.manageMFA()
281+
manageMfaCf.whenComplete { _, error ->
282+
if (error == null) {
283+
Log.d("MainActivity_Web3Auth", "MFA manage successfully")
284+
} else {
285+
Log.d("MainActivity_Web3Auth", error.message ?: "Something went wrong")
286+
}
287+
}
288+
}
289+
275290
val spinner = findViewById<AutoCompleteTextView>(R.id.spinnerTextView)
276291
val loginVerifierList: List<String> = verifierList.map { item ->
277292
item.name

app/src/main/res/layout/activity_main.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,16 @@
114114
app:layout_constraintBottom_toBottomOf="parent"
115115
app:layout_constraintStart_toStartOf="parent" />
116116

117+
<Button
118+
android:id="@+id/btn_manageMfa"
119+
android:layout_width="wrap_content"
120+
android:layout_height="wrap_content"
121+
android:layout_marginStart="10dp"
122+
android:text="Manage MFA"
123+
android:textAllCaps="false"
124+
android:visibility="gone"
125+
app:layout_constraintBottom_toBottomOf="parent"
126+
app:layout_constraintEnd_toEndOf="parent"
127+
app:layout_constraintStart_toStartOf="parent" />
128+
117129
</androidx.constraintlayout.widget.ConstraintLayout>

core/src/main/java/com/web3auth/core/Web3Auth.kt

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions, context: Context) : WebViewResu
4848

4949
private lateinit var loginCompletableFuture: CompletableFuture<Web3AuthResponse>
5050
private lateinit var enableMfaCompletableFuture: CompletableFuture<Boolean>
51+
private lateinit var manageMfaCompletableFuture: CompletableFuture<Boolean>
5152
private lateinit var signMsgCF: CompletableFuture<SignResponse>
5253

5354
private var web3AuthResponse: Web3AuthResponse? = null
@@ -117,7 +118,7 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions, context: Context) : WebViewResu
117118
)
118119
paramMap.put("actionType", actionType)
119120

120-
if (actionType == "enable_mfa") {
121+
if (actionType == "enable_mfa" || actionType == "manage_mfa") {
121122
val userInfo = web3AuthResponse?.userInfo
122123
initParams.put("loginProvider", userInfo?.typeOfLogin)
123124
var extraOptionsString = ""
@@ -205,17 +206,25 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions, context: Context) : WebViewResu
205206
val hashUri = Uri.parse(uri?.host + "?" + uri?.fragment)
206207
val error = uri?.getQueryParameter("error")
207208
if (error != null) {
208-
loginCompletableFuture.completeExceptionally(UnKnownException(error))
209+
if (::loginCompletableFuture.isInitialized) loginCompletableFuture.completeExceptionally(
210+
UnKnownException(error)
211+
)
212+
209213
if (::enableMfaCompletableFuture.isInitialized) enableMfaCompletableFuture.completeExceptionally(
210214
UnKnownException(error)
211215
)
216+
217+
if (::manageMfaCompletableFuture.isInitialized) manageMfaCompletableFuture.completeExceptionally(
218+
UnKnownException(error)
219+
)
212220
return
213221
}
214222

215223
val b64Params = hashUri.getQueryParameter("b64Params")
216224
if (b64Params.isNullOrBlank()) {
217225
throwLoginError(ErrorCode.INVALID_LOGIN)
218226
throwEnableMFAError(ErrorCode.INVALID_LOGIN)
227+
throwManageMFAError(ErrorCode.INVALID_LOGIN)
219228
return
220229
}
221230
val b64ParamString = decodeBase64URLString(b64Params).toString(Charsets.UTF_8)
@@ -235,9 +244,11 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions, context: Context) : WebViewResu
235244
if (web3AuthResponse?.error?.isNotBlank() == true) {
236245
throwLoginError(ErrorCode.SOMETHING_WENT_WRONG)
237246
throwEnableMFAError(ErrorCode.SOMETHING_WENT_WRONG)
247+
throwManageMFAError(ErrorCode.SOMETHING_WENT_WRONG)
238248
} else if (web3AuthResponse?.privKey.isNullOrBlank() && web3AuthResponse?.factorKey.isNullOrBlank()) {
239249
throwLoginError(ErrorCode.SOMETHING_WENT_WRONG)
240250
throwEnableMFAError(ErrorCode.SOMETHING_WENT_WRONG)
251+
throwManageMFAError(ErrorCode.SOMETHING_WENT_WRONG)
241252
} else {
242253
web3AuthResponse?.sessionId?.let {
243254
SessionManager.saveSessionIdToStorage(it)
@@ -251,9 +262,14 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions, context: Context) : WebViewResu
251262
web3AuthResponse?.userInfo?.dappShare!!,
252263
)
253264
}
254-
loginCompletableFuture.complete(web3AuthResponse)
265+
if (::loginCompletableFuture.isInitialized)
266+
loginCompletableFuture.complete(web3AuthResponse)
267+
255268
if (::enableMfaCompletableFuture.isInitialized)
256269
enableMfaCompletableFuture.complete(true)
270+
271+
if (::manageMfaCompletableFuture.isInitialized)
272+
manageMfaCompletableFuture.complete(true)
257273
}
258274
} else {
259275
print(error)
@@ -263,6 +279,7 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions, context: Context) : WebViewResu
263279
} else {
264280
throwLoginError(ErrorCode.SOMETHING_WENT_WRONG)
265281
throwEnableMFAError(ErrorCode.SOMETHING_WENT_WRONG)
282+
throwManageMFAError(ErrorCode.SOMETHING_WENT_WRONG)
266283
}
267284
}
268285

@@ -334,6 +351,22 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions, context: Context) : WebViewResu
334351
return enableMfaCompletableFuture
335352
}
336353

354+
355+
fun manageMFA(loginParams: LoginParams? = null): CompletableFuture<Boolean> {
356+
manageMfaCompletableFuture = CompletableFuture()
357+
if (web3AuthResponse?.userInfo?.isMfaEnabled == false) {
358+
throwManageMFAError(ErrorCode.MFA_NOT_ENABLED)
359+
return manageMfaCompletableFuture
360+
}
361+
val sessionId = sessionManager.getSessionId()
362+
if (sessionId.isBlank()) {
363+
throwManageMFAError(ErrorCode.NOUSERFOUND)
364+
return manageMfaCompletableFuture
365+
}
366+
processRequest("manage_mfa", loginParams)
367+
return manageMfaCompletableFuture
368+
}
369+
337370
/**
338371
* Authorize User session in order to avoid re-login
339372
*/
@@ -590,6 +623,17 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions, context: Context) : WebViewResu
590623
)
591624
}
592625

626+
private fun throwManageMFAError(error: ErrorCode) {
627+
if (::manageMfaCompletableFuture.isInitialized)
628+
manageMfaCompletableFuture.completeExceptionally(
629+
Exception(
630+
Web3AuthError.getError(
631+
error
632+
)
633+
)
634+
)
635+
}
636+
593637
private fun throwLoginError(error: ErrorCode) {
594638
if (::loginCompletableFuture.isInitialized) {
595639
loginCompletableFuture.completeExceptionally(

core/src/main/java/com/web3auth/core/types/Web3AuthError.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,23 @@ object Web3AuthError {
1919
ErrorCode.RUNTIME_ERROR -> {
2020
"Runtime Error"
2121
}
22+
2223
ErrorCode.APP_CANCELLED -> {
2324
"App Cancelled"
2425
}
26+
2527
ErrorCode.INVALID_LOGIN -> {
2628
"Invalid Login"
2729
}
30+
2831
ErrorCode.MFA_ALREADY_ENABLED -> {
2932
"MFA already enabled"
3033
}
34+
35+
ErrorCode.MFA_NOT_ENABLED -> {
36+
"MFA is not enabled. Please enable MFA first."
37+
}
38+
3139
ErrorCode.USER_CANCELLED -> {
3240
"User Cancelled"
3341
}
@@ -44,5 +52,6 @@ enum class ErrorCode {
4452
SOMETHING_WENT_WRONG,
4553
INVALID_LOGIN,
4654
MFA_ALREADY_ENABLED,
55+
MFA_NOT_ENABLED,
4756
USER_CANCELLED
4857
}

core/src/main/java/com/web3auth/core/types/Web3AuthOptions.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,6 @@ const val authServiceVersion = "v9"
5656
const val walletServicesVersion = "v3"
5757
const val WEBVIEW_URL = "walletUrl"
5858
const val REDIRECT_URL = "redirectUrl"
59-
const val CUSTOM_TABS_URL = "customTabsUrl"
59+
const val CUSTOM_TABS_URL = "customTabsUrl"
60+
const val ENABLE_MFA = "enable_mfa"
61+
const val MANAGE_MFA = "manage_mfa"

0 commit comments

Comments
 (0)