Skip to content
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

[Bug]: OAuth redirect for identity linking fails #833

Open
2 of 3 tasks
jshxr opened this issue Jan 11, 2025 · 2 comments
Open
2 of 3 tasks

[Bug]: OAuth redirect for identity linking fails #833

jshxr opened this issue Jan 11, 2025 · 2 comments
Assignees
Labels
auth bug Something isn't working

Comments

@jshxr
Copy link

jshxr commented Jan 11, 2025

General Info

  • I checked for similar bug report
  • I am using the latest version (should fail on 3.0.3 aswell though)
  • I checked the troubleshooting page for similar problems

Version(s)

3.0.1

Kotlin Target(s) and their respective versions

2.0.21 / WasmJs

What happened? (include your code)

In my Compose Multiplatform webapp, calling

supabaseClient.auth.linkIdentity(Google)

to have an anonymous user sign in with Google differs greatly from calling

supabaseClient.auth.signIn(Google)

to sign in a completely new user.

While the latter works just fine, with the browser redirecting to the Google Sign In page, calling linkIdentity results in a failed request (blocked preflight due to missing headers) to the Google OAuth endpoint.

Looking at the implementation

override suspend fun linkIdentity(
provider: OAuthProvider,
redirectUrl: String?,
config: ExternalAuthConfigDefaults.() -> Unit
): String? {
val automaticallyOpen = ExternalAuthConfigDefaults().apply(config).automaticallyOpenUrl
val fetchUrl: suspend (String?) -> String = { redirectTo: String? ->
val url = getOAuthUrl(provider, redirectTo, "user/identities/authorize", config)
val response = api.rawRequest(url) {
method = HttpMethod.Get
}
response.request.url.toString()
}
if(!automaticallyOpen) {
return fetchUrl(redirectUrl ?: "")
}
startExternalAuth(
redirectUrl = redirectUrl,
getUrl = {
fetchUrl(it)
},
onSessionSuccess = {
importSession(it, source = SessionSource.UserIdentitiesChanged(it))
}
)
return null
}

it seems as if response.request.url.toString() on line 156 might be the problem.

Based on how the linkIdentity method is implemented in the official JS client,
I assume that the line tries to extract the URL returned by the /user/identities/authorize endpoint.
However, instead, the previously constructed URL used to make the request is being accessed.

A second problem is that the /user/identities/authorize endpoint doesn't return the wanted URL, but instead
tries to redirect to it right away.

Possible fix

  1. Adding the query parameter skip_http_redirect=true to the /user/identities/authorize URL.
  2. Parsing the returned URL correctly

Workaround

This is my current workaround:

@Serializable
data class SupabaseLinkIdentityResponse(val url: String)

@OptIn(SupabaseInternal::class)
suspend fun signInWithGoogle(uriHandler: UriHandler) {
    val api = supabaseClient.authenticatedSupabaseApi(supabaseClient.auth)
    val fetchUrl: suspend (String?) -> String = { redirectTo: String? ->
        val url =
            supabaseClient.auth.getOAuthUrl(
                Google,
                redirectTo,
                "user/identities/authorize"
            ) {
                queryParams["skip_http_redirect"] = "true"
            }
        val response = api.rawRequest(url) {
            method = HttpMethod.Get
        }
        Json.decodeFromString<SupabaseLinkIdentityResponse>(response.body()).url
    }

    val url = fetchUrl(null)
    uriHandler.openUri(url)
}

Note: I haven't yet tested this on platforms/targets other than Web/WasmJs.

Steps To Reproduce (optional)

No response

Relevant log output (optional)

No response

@jshxr jshxr added the bug Something isn't working label Jan 11, 2025
@jan-tennert jan-tennert self-assigned this Jan 11, 2025
@jan-tennert
Copy link
Collaborator

Thanks for reporting, this was a bug. Fixed in #d7dd01a.
Will publish a version later today for testing.

@jan-tennert
Copy link
Collaborator

jan-tennert commented Jan 13, 2025

Released 3.1.0-beta-1. Let me know if this was fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auth bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants