diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 7df9400..9048b05 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -36,6 +36,8 @@ android:layout_margin="32dp" android:minHeight="48dp" android:visibility="gone" + android:focusable="true" + android:focusableInTouchMode="true" android:backgroundTint="@color/black" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" diff --git a/core/src/main/java/com/web3auth/core/CustomChromeTabsActivity.kt b/core/src/main/java/com/web3auth/core/CustomChromeTabsActivity.kt index 6493b6f..2aba94a 100644 --- a/core/src/main/java/com/web3auth/core/CustomChromeTabsActivity.kt +++ b/core/src/main/java/com/web3auth/core/CustomChromeTabsActivity.kt @@ -1,54 +1,82 @@ package com.web3auth.core -import android.content.Intent import android.net.Uri import android.os.Bundle -import androidx.activity.result.ActivityResultLauncher -import androidx.activity.result.contract.ActivityResultContracts +import android.webkit.WebView +import android.webkit.WebViewClient import androidx.appcompat.app.AppCompatActivity -import androidx.browser.customtabs.CustomTabsIntent +import com.google.gson.GsonBuilder +import com.web3auth.core.types.REDIRECT_URL +import com.web3auth.core.types.SessionResponse import com.web3auth.core.types.WEBVIEW_URL +import com.web3auth.core.types.WebViewResultCallback class CustomChromeTabsActivity : AppCompatActivity() { - private lateinit var customTabLauncher: ActivityResultLauncher + private lateinit var webView: WebView + private val gson = GsonBuilder().disableHtmlEscaping().create() + + companion object { + var webViewResultCallback: WebViewResultCallback? = null + } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) + supportActionBar?.hide() setContentView(R.layout.activity_cct) + webView = findViewById(R.id.webView) - customTabLauncher = - registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result -> - if (result.resultCode == RESULT_CANCELED) { - Web3Auth.setCustomTabsClosed(true) - finish() - } - } - + // Handle loading URL from intent extras val extras = intent.extras if (extras != null) { val webViewUrl = extras.getString(WEBVIEW_URL) + val redirectUrl = extras.getString(REDIRECT_URL) if (webViewUrl != null) { - launchCustomTabs(webViewUrl) + webView.webViewClient = object : WebViewClient() { + override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean { + if (redirectUrl?.isNotEmpty() == true) { + if (url?.contains(redirectUrl) == true) { + val uri = Uri.parse(url) + val hashUri = Uri.parse(uri.host + "?" + uri.fragment) + val b64Params = hashUri.getQueryParameter("b64Params") + val b64ParamString = + decodeBase64URLString(b64Params!!).toString(Charsets.UTF_8) + val sessionResponse = + gson.fromJson(b64ParamString, SessionResponse::class.java) + println("Session Response: $sessionResponse") + webViewResultCallback?.onSessionResponseReceived(sessionResponse) + //WebViewActivity.webViewResultCallback?.onSignResponseReceived(signResponse) + finish() + return true + } + } + return false + } + + override fun onPageFinished(view: WebView?, url: String?) { + + } + } + } + + if (webViewUrl != null) { + webView.loadUrl(webViewUrl) } } + + val webSettings = webView.settings + webSettings.javaScriptEnabled = true + webSettings.domStorageEnabled = true + webSettings.setSupportMultipleWindows(true) + webView.settings.userAgentString = null + } - private fun launchCustomTabs(url: String) { - val defaultBrowser = this.getDefaultBrowser() - val customTabsBrowsers = this.getCustomTabsBrowsers() - if (customTabsBrowsers.contains(defaultBrowser)) { - val intent = CustomTabsIntent.Builder().build().intent - intent.data = Uri.parse(url) - intent.`package` = defaultBrowser - customTabLauncher.launch(intent) - } else if (customTabsBrowsers.isNotEmpty()) { - val intent = CustomTabsIntent.Builder().build().intent - intent.data = Uri.parse(url) - intent.`package` = customTabsBrowsers[0] - customTabLauncher.launch(intent) + override fun onBackPressed() { + if (webView.canGoBack()) { + webView.goBack() } else { - startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(url))) + super.onBackPressed() } } } \ No newline at end of file diff --git a/core/src/main/java/com/web3auth/core/Web3Auth.kt b/core/src/main/java/com/web3auth/core/Web3Auth.kt index 1c3d6da..ab4c53d 100644 --- a/core/src/main/java/com/web3auth/core/Web3Auth.kt +++ b/core/src/main/java/com/web3auth/core/Web3Auth.kt @@ -52,6 +52,7 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions, context: Context) : WebViewResu private var web3AuthResponse: Web3AuthResponse? = null private var web3AuthOption = web3AuthOptions + private var context = context private var sessionManager: SessionManager = SessionManager( context, web3AuthOptions.sessionTime ?: 600, @@ -107,6 +108,7 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions, context: Context) : WebViewResu private fun processRequest( actionType: String, params: LoginParams? ) { + CustomChromeTabsActivity.webViewResultCallback = this val sdkUrl = Uri.parse(web3AuthOption.sdkUrl) val initOptions = JSONObject(gson.toJson(getInitOptions())) val initParams = JSONObject(gson.toJson(getInitParams(params))) @@ -147,6 +149,7 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions, context: Context) : WebViewResu //print("url: => $url") val intent = Intent(baseContext, CustomChromeTabsActivity::class.java) intent.putExtra(WEBVIEW_URL, url.toString()) + intent.putExtra(REDIRECT_URL, web3AuthOption.redirectUrl.toString()) baseContext.startActivity(intent) } } @@ -691,6 +694,52 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions, context: Context) : WebViewResu } } + override fun onSessionResponseReceived(sessionResponse: SessionResponse?) { + val sessionId = sessionResponse?.sessionId + if (sessionId?.isNotBlank() == true && sessionId.isNotEmpty()) { + SessionManager.saveSessionIdToStorage(sessionId) + sessionManager.setSessionId(sessionId) + + //Rehydrate Session + this.authorizeSession(web3AuthOption.redirectUrl.toString(), this.context) + .whenComplete { resp, error -> + runOnUIThread { + if (error == null) { + web3AuthResponse = resp + if (web3AuthResponse?.error?.isNotBlank() == true) { + throwLoginError(ErrorCode.SOMETHING_WENT_WRONG) + throwEnableMFAError(ErrorCode.SOMETHING_WENT_WRONG) + } else if (web3AuthResponse?.privKey.isNullOrBlank() && web3AuthResponse?.factorKey.isNullOrBlank()) { + throwLoginError(ErrorCode.SOMETHING_WENT_WRONG) + throwEnableMFAError(ErrorCode.SOMETHING_WENT_WRONG) + } else { + web3AuthResponse?.sessionId?.let { + SessionManager.saveSessionIdToStorage(it) + sessionManager.setSessionId(it) + } + + if (web3AuthResponse?.userInfo?.dappShare?.isNotEmpty() == true) { + KeyStoreManagerUtils.encryptData( + web3AuthResponse?.userInfo?.verifier.plus(" | ") + .plus(web3AuthResponse?.userInfo?.verifierId), + web3AuthResponse?.userInfo?.dappShare!!, + ) + } + loginCompletableFuture.complete(web3AuthResponse) + if (::enableMfaCompletableFuture.isInitialized) + enableMfaCompletableFuture.complete(true) + } + } else { + print(error) + } + } + } + } else { + throwLoginError(ErrorCode.SOMETHING_WENT_WRONG) + throwEnableMFAError(ErrorCode.SOMETHING_WENT_WRONG) + } + } + override fun onWebViewCancelled() { signMsgCF.completeExceptionally(Exception("User cancelled the operation.")) } diff --git a/core/src/main/java/com/web3auth/core/WebViewActivity.kt b/core/src/main/java/com/web3auth/core/WebViewActivity.kt index e112f3a..a95a3d5 100644 --- a/core/src/main/java/com/web3auth/core/WebViewActivity.kt +++ b/core/src/main/java/com/web3auth/core/WebViewActivity.kt @@ -57,11 +57,9 @@ class WebViewActivity : AppCompatActivity() { gson.fromJson(b64ParamString, SignResponse::class.java) webViewResultCallback?.onSignResponseReceived(signResponse) finish() + return true } } - if (webViewUrl != null) { - view?.loadUrl(webViewUrl) - } return false } diff --git a/core/src/main/java/com/web3auth/core/types/WebViewResultCallback.kt b/core/src/main/java/com/web3auth/core/types/WebViewResultCallback.kt index d3dc456..49bce83 100644 --- a/core/src/main/java/com/web3auth/core/types/WebViewResultCallback.kt +++ b/core/src/main/java/com/web3auth/core/types/WebViewResultCallback.kt @@ -2,5 +2,6 @@ package com.web3auth.core.types interface WebViewResultCallback { fun onSignResponseReceived(signResponse: SignResponse?) + fun onSessionResponseReceived(sessionResponse: SessionResponse?) fun onWebViewCancelled() } \ No newline at end of file diff --git a/core/src/main/res/layout/activity_cct.xml b/core/src/main/res/layout/activity_cct.xml index ed583c7..e75916c 100644 --- a/core/src/main/res/layout/activity_cct.xml +++ b/core/src/main/res/layout/activity_cct.xml @@ -4,6 +4,9 @@ android:layout_height="match_parent" tools:context=".WebViewActivity"> - + \ No newline at end of file