-
-
Notifications
You must be signed in to change notification settings - Fork 404
Replace WebView login with Chrome Custom Tabs #6694
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bhatganeshdarshan
wants to merge
9
commits into
streetcomplete:master
Choose a base branch
from
bhatganeshdarshan:feature/chrome-custom-tabs-oauth
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+242
−132
Open
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
af55c72
Fix: Redirect to OSM authorization for newly confirmed accounts
bhatganeshdarshan f4313ac
Implement Chrome Custom Tabs for OAuth authentication flow
bhatganeshdarshan 7b04d2f
Fix OAuth login cancel handling
bhatganeshdarshan 3123b64
fix: use androidx custom tabs action in queries
bhatganeshdarshan b4d1d39
fix(oauth): clear stale grants on logout and suppress misleading toast
bhatganeshdarshan 150a970
Merge branch 'master' into feature/chrome-custom-tabs-oauth
bhatganeshdarshan 8f4fcbe
Fix : Use LocalUriHandler instead of Chrome custom tabs
bhatganeshdarshan 333d7f0
remove dealy and unused imports
bhatganeshdarshan ea5d4bb
remove webview dependency
bhatganeshdarshan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
...idMain/kotlin/de/westnordost/streetcomplete/screens/user/login/ChromeCustomTabLauncher.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package de.westnordost.streetcomplete.screens.user.login | ||
|
|
||
| import android.content.Context | ||
| import android.content.Intent | ||
| import androidx.browser.customtabs.CustomTabsIntent | ||
| import androidx.core.content.ContextCompat | ||
| import de.westnordost.streetcomplete.R | ||
| import androidx.core.net.toUri | ||
|
|
||
| /** | ||
| * Utility class to launch URLs in Chrome Custom Tabs for OAuth flows. | ||
| */ | ||
| object ChromeCustomTabLauncher { | ||
|
|
||
| fun launchUrl(context: Context, url: String): Boolean { | ||
| return try { | ||
| val uri = url.toUri() | ||
| val customTabsIntent = CustomTabsIntent.Builder() | ||
| .setToolbarColor(ContextCompat.getColor(context, R.color.primary)) | ||
| .setShowTitle(true) | ||
| .build() | ||
|
|
||
| customTabsIntent.launchUrl(context, uri) | ||
| true | ||
| } catch (e: Exception) { | ||
| // Fallback to default browser if Custom Tabs fail | ||
| try { | ||
| val browserIntent = Intent(Intent.ACTION_VIEW, url.toUri()) | ||
| context.startActivity(browserIntent) | ||
| true | ||
| } catch (e: Exception) { | ||
| false | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
app/src/commonMain/kotlin/de/westnordost/streetcomplete/data/user/OAuthCallbackHandler.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| package de.westnordost.streetcomplete.data.user | ||
|
|
||
| import de.westnordost.streetcomplete.data.user.oauth.OAuthAuthorizationParams | ||
| import de.westnordost.streetcomplete.util.logs.Log | ||
| import kotlinx.coroutines.flow.MutableStateFlow | ||
| import kotlinx.coroutines.flow.StateFlow | ||
| import kotlinx.coroutines.flow.getAndUpdate | ||
| import kotlinx.coroutines.flow.update | ||
|
|
||
| /** | ||
| * When the user completes the OAuth flow and returns to the app via the callback URI, | ||
| * this handler processes the authorization response. | ||
| */ | ||
| class OAuthCallbackHandler { | ||
| private val _oAuthCallbackUri = MutableStateFlow<String?>(null) | ||
| val oAuthCallbackUri: StateFlow<String?> = _oAuthCallbackUri | ||
|
|
||
| // Store the OAuth params so the same codeVerifier can be used during token exchange | ||
| private var storedOAuthParams: OAuthAuthorizationParams? = null | ||
|
|
||
| fun storeOAuthParams(params: OAuthAuthorizationParams) { | ||
| storedOAuthParams = params | ||
| } | ||
|
|
||
| fun getStoredOAuthParams(): OAuthAuthorizationParams? = storedOAuthParams | ||
|
|
||
| // Process a potential OAuth callback URI | ||
| fun handleUri(uriString: String): Boolean { | ||
| return if (isOAuthCallback(uriString)) { | ||
| _oAuthCallbackUri.update { uriString } | ||
| true | ||
| } else { | ||
| false | ||
| } | ||
| } | ||
|
|
||
| // checks if the uri is oauth callback | ||
| private fun isOAuthCallback(uriString: String): Boolean { | ||
| return uriString.startsWith("$OAUTH2_CALLBACK_SCHEME://$OAUTH2_CALLBACK_HOST") | ||
| } | ||
|
|
||
| fun consumeCallback(): String? { | ||
| return _oAuthCallbackUri.getAndUpdate { null } | ||
| } | ||
|
|
||
| fun clearStoredParams() { | ||
| Log.i(TAG, "Clearing stored OAuth params") | ||
| storedOAuthParams = null | ||
| } | ||
|
|
||
| companion object { | ||
| private const val TAG = "OAuthCallbackHandler" | ||
| } | ||
| } | ||
|
|
44 changes: 44 additions & 0 deletions
44
app/src/commonMain/kotlin/de/westnordost/streetcomplete/data/user/OAuthLoginCompleter.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package de.westnordost.streetcomplete.data.user | ||
|
|
||
| import de.westnordost.streetcomplete.data.user.oauth.OAuthApiClient | ||
| import de.westnordost.streetcomplete.data.user.oauth.OAuthException | ||
| import de.westnordost.streetcomplete.util.logs.Log | ||
|
|
||
|
|
||
| // Finishes the OAuth login flow when an authorization callback URL is received. | ||
| class OAuthLoginCompleter( | ||
| private val oAuthApiClient: OAuthApiClient, | ||
| private val userLoginController: UserLoginController, | ||
| private val oAuthCallbackHandler: OAuthCallbackHandler | ||
| ) { | ||
|
|
||
| suspend fun processCallback(authorizationResponseUrl: String): Boolean { | ||
|
|
||
| val oAuthParams = oAuthCallbackHandler.getStoredOAuthParams() ?: return false | ||
|
|
||
| return try { | ||
| val tokenResponse = oAuthApiClient.getAccessToken(oAuthParams, authorizationResponseUrl) | ||
| if (tokenResponse.grantedScopes?.containsAll(OAUTH2_REQUIRED_SCOPES) == false) { | ||
| return false | ||
| } | ||
|
|
||
| userLoginController.logIn(tokenResponse.accessToken) | ||
|
|
||
| // Clear stored params after successful login | ||
| oAuthCallbackHandler.clearStoredParams() | ||
| true | ||
| } catch (e: Exception) { | ||
| if (e is OAuthException && e.error == "access_denied") { | ||
| Log.w(TAG, "OAuth access denied by user") | ||
| } else { | ||
| Log.e(TAG, "Error finishing OAuth login: ${e.message}", e) | ||
| } | ||
| false | ||
| } | ||
| } | ||
|
|
||
| companion object { | ||
| private const val TAG = "OAuthLoginCompleter" | ||
| } | ||
| } | ||
|
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, that's the big issue with this PR. This library is not multi platform. I cannot merge a PR that replaces functionality that already works with multiplatform (i.e. with iOS) with an implementation that doesn't.
Looking at the source code, it is also completely written in Java and also depends on a couple of Java and Android stuff ( https://android.googlesource.com/platform/frameworks/support/+/androidx-main/browser/browser/build.gradle#39 ), so it doesn't look like it would be trivial to migrate for them. I don't think it is on their agenda.
In short, we can't use that library.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The only multiplatform way to open an URL in the browser via compose that I know is
LocalUriHandler. This doesn't however, allow to pass any (launch) parameters to the browser.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perfect got it !! So if we dont want to use
implementation("androidx.browser:browser:1.9.0"),we can open it in a browser without custom tabs ,
currently in my approach if chrome custom tabs are available , i m using that else it opens it in browser
so if we remove custom tabs and only open in browser like this :
it'll work ig :
WhatsApp.Video.2026-01-17.at.19.03.36.mp4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but Context is Android only and hence that will not even compile on platforms other than Android
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As you said, the only way would be to use
val uriHandler = LocalUriHandler.currentright?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In a nutshell, yes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That I know of, that is.