-
Notifications
You must be signed in to change notification settings - Fork 412
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
Keycloak Login results in Error: "Unable to exchange external code" #516
Comments
Doing some more inspection, the Auth Logs show the following error:
[
{
"_LINE_BREAK": null,
"_EXE": null,
"status": null,
"_MACHINE_ID": null,
"SYSLOG_FACILITY": null,
"request_id": null,
"project": "mrkpghsoraiqyrxtoqkl",
"_UID": null,
"remote_addr": null,
"_CAP_EFFECTIVE": null,
"_PID": null,
"SYSLOG_IDENTIFIER": null,
"__REALTIME_TIMESTAMP": null,
"_TRANSPORT": null,
"_COMM": null,
"CODE_FUNC": null,
"_SYSTEMD_UNIT": null,
"__MONOTONIC_TIMESTAMP": null,
"_STREAM_ID": null,
"host": "mrkpghsoraiqyrxtoqkl",
"source_type": "journald",
"UNIT": null,
"_BOOT_ID": null,
"_SYSTEMD_CGROUP": null,
"MESSAGE_ID": null,
"_SYSTEMD_SLICE": null,
"CODE_LINE": null,
"EXECUTABLE": null,
"level": null,
"_SYSTEMD_INVOCATION_ID": null,
"INVOCATION_ID": null,
"referer": null,
"path": null,
"CODE_FILE": null,
"_GID": null,
"PRIORITY": null,
"method": null,
"_SOURCE_REALTIME_TIMESTAMP": null,
"duration": null,
"_CMDLINE": null,
"component": null,
"_SELINUX_CONTEXT": null
}
] |
After further inspection, I've tried to use a self-hosted instance of supabase. The error disappears; Keycloak sets a session and I'll get redirected to the origin. However, a session cookie is not set. The response from Keycloak looks like this: URL after redirect
|
Hello redirect url with failure "Unable to exchange external code"
another unrelated strange thing is How can I use self hosted supabase with oauth2 providers to test it with keycloak, like @valerius21 |
@koakh When starting a local instance, add the following lines here to your
|
I will try it later (asap) and update my reply with feedback, maybe will be useful for others thanks @valerius21 update #1 testing with both supabase hosted and supabase self host with docker instructions here https://supabase.com/docs/guides/hosting/docker and suggested env GOTRUE_EXTERNAL_KEYCLOAK_ENABLED: "true"
GOTRUE_EXTERNAL_KEYCLOAK_CLIENT_ID: "<client id>"
GOTRUE_EXTERNAL_KEYCLOAK_SECRET: "<client secret>"
GOTRUE_EXTERNAL_KEYCLOAK_REDIRECT_URI: "http://localhost:<PORT of your webapp>/"
GOTRUE_EXTERNAL_KEYCLOAK_URL: "http://<keycloak instance>/auth/realms/<realm>" ex GOTRUE_EXTERNAL_KEYCLOAK_REDIRECT_URI: "http://localhost:8000/auth/v1/callback"
GOTRUE_EXTERNAL_KEYCLOAK_URL: "http://localhost:8080/auth/realms/SupaBase" first error beware one of the problems that I have was the in lets focus in the super annoying problem with hosted and self hosted I try with both images and is the same
update #2 : using keycloak in supabase docker stack and start debug # check gotrue logs
$ docker-compose logs -f auth
supabase-auth | time="2022-07-09T00:15:25Z" level=info msg="request started" component=api method=GET path=/authorize referer="http://localhost:3030/" remote_addr="192.168.96.1:45942" request_id=39397c7b-01e3-4dbb-919a-191c0c5dce8c
supabase-auth | time="2022-07-09T00:15:25Z" level=info msg="Redirecting to external provider" component=api method=GET path=/authorize provider=keycloak referer="http://localhost:3030/" remote_addr="192.168.96.1:45942" request_id=39397c7b-01e3-4dbb-919a-191c0c5dce8c
supabase-auth | time="2022-07-09T00:15:25Z" level=info msg="request completed" component=api duration=159963 method=GET path=/authorize referer="http://localhost:3030/" remote_addr="192.168.96.1:45942" request_id=39397c7b-01e3-4dbb-919a-191c0c5dce8c status=302
supabase-auth | time="2022-07-09T00:15:25Z" level=info msg="request started" component=api method=GET path=/callback referer= remote_addr="192.168.96.1:45942" request_id=4428c95b-3513-494f-82ab-c6b1c554b31b
supabase-auth | time="2022-07-09T00:15:25Z" level=error msg="500: Unable to exchange external code: dd4c2c81-f956-40a3-860a-764eaede02fb.fa0ff490-0ea9-4277-bf20-df339d7ec938.8a75907e-7b0d-4a2a-87ef-68c46447a957" component=api error="Post \"http://localhost:8080/auth/realms/SupaBase/protocol/openid-connect/token\": dial tcp 127.0.0.1:8080: connect: connection refused" method=GET path=/callback referer= remote_addr="192.168.96.1:45942" request_id=4428c95b-3513-494f-82ab-c6b1c554b31b
supabase-auth | time="2022-07-09T00:15:25Z" level=info msg="request completed" component=api duration=5801455 method=GET path=/callback referer= remote_addr="192.168.96.1:45942" request_id=4428c95b-3513-494f-82ab-c6b1c554b31b status=302 seems clear that the error is here # test keycloak endpoint
$ curl -X POST http://localhost:8080/auth/realms/SupaBase/protocol/openid-connect/token
{"error":"invalid_request","error_description":"Missing form parameter: grant_type"} enter inside gotrue container we can see that we can't have connection in localhost:8080 and 127.0.0.1:8080 only with keycloak:8080 works $ docker exec -it supabase-auth sh
/ $ wget localhost:8080
Connecting to localhost:8080 (127.0.0.1:8080)
wget: can't connect to remote host (127.0.0.1): Connection refused
/ $ wget 127.0.0.1:8080
Connecting to 127.0.0.1:8080 (127.0.0.1:8080)
wget: can't connect to remote host (127.0.0.1): Connection refused
/ $ wget keycloak:8080
Connecting to keycloak:8080 (192.168.96.6:8080)
# works here
wget: can't open 'index.html': Permission denied the question is why gotrue can connect to keycloak ? update #3: at last I find how to put it to work after more 4h digging in the shit hole the trick for inter container communication is using
GOTRUE_EXTERNAL_KEYCLOAK_REDIRECT_URI: "http://localhost:8000/auth/v1/callback"
GOTRUE_EXTERNAL_KEYCLOAK_URL: "http://keycloak:8080/auth/realms/SupaBase"
ip addr show docker0 | grep -Po 'inet \K[\d.]+'
172.17.0.1 GOTRUE_EXTERNAL_KEYCLOAK_REDIRECT_URI: "http://localhost:8000/auth/v1/callback"
GOTRUE_EXTERNAL_KEYCLOAK_URL: "http://172.17.0.1:8080/auth/realms/SupaBase"
GOTRUE_EXTERNAL_KEYCLOAK_REDIRECT_URI: "http://localhost:8000/auth/v1/callback"
GOTRUE_EXTERNAL_KEYCLOAK_URL: "https://keycloak.mydomain.com/auth/realms/SupaBase"
in keycloak Keycloak URL for hosted version: https://keycloak.mydomain.com/auth/realms/SupaBase other tricks than may trigger the error "Unable to exchange external code"
2 .(self hosted only with smtp enabled and connection problems like inside gotrue try to connect to port 53/smtp, same inter container problem) now I have local and online nextjs, supabase and keycloak working, if anyone needs help feel free to ask thanks PS: in my opinion https://supabase.com/docs/guides/auth/auth-keycloak don't help when it says quote="Obtain the issuer from the "OpenID Endpoint Configuration". This will be used as the Keycloak URL." depends if its from a localhost don't help, only create confusion, how supabase hosted and self hosted deals with localhost:8080? yes don't deal.....even if all services are running in a local machine with docker, I don't try running binaries in host machine but that will work because gotrue will reach localhost:8080 in that link maybe a note saying that the service must a public ip or domain with port 8080 exposing keycloak service helps |
@koakh, thank you very much for your write-up! I'll try to replicate everything once I find time to do it. IMO the documentation needs some updating before someone gets into the rabbit hole of trying to find out how it works. |
Thank you @koakh for the write up. I'll try to summarize it for others. When running both Keycloak and GoTrue locally, you need to make sure that both of them can actually talk to each other over the operating system's network layer. There is no one way to fix the problem, depending on how you have set up the Keycloak or GoTrue instance to run. Note that typically things running within a docker container can't talk to things running outside of it, especially on localhost. I believe this to be the cause of the errors with the form of:
It may be just easier to host Keycloak on a public web server with a correct URL to avoid dealing with complex docker networking issues especially if you don't have sufficient knowledge or experience in dealing with them. |
@koakh Hey, I'm having the same issue with auth not being able to reach keycloak because it is running in another container: {
"component": "api",
"error": "Post \"http://localhost:8085/realms/app/protocol/openid-connect/token\": dial tcp 127.0.0.1:8085: connect: connection refused",
"level": "error",
"method": "GET",
"msg": "500: Unable to exchange external code: 330d1058-fb86-45e7-9f7a-609276ee801e.daeeb333-2aa6-440f-9598-4bbf132a530f.bda8d7d3-8395-4429-aed6-74e07b7efd64",
"path": "/callback"
} How did you solve this issue in the end then? When I set EDIT: For anyone else looking for a solution on how to get this running when both containers are running on the same machine on windows, I found a solution:
After doing these steps, the authentication process worked for me 👍 |
Hello @olee Sorry for late answear, UPDATE: my lines of keyclock env vars auth:
container_name: supabase-auth
...
image: koakh-supabase-gotrue:latest
environment:
...
# keycloak
GOTRUE_EXTERNAL_KEYCLOAK_ENABLED: "true"
GOTRUE_EXTERNAL_KEYCLOAK_CLIENT_ID: "supabase"
GOTRUE_EXTERNAL_KEYCLOAK_SECRET: "sgTbz86zgZgdL8x7YtPzthZUlkDo0Wup"
GOTRUE_EXTERNAL_KEYCLOAK_REDIRECT_URI: "http://localhost:8000/auth/v1/callback"
# this url must be accessed from client/browser url, use docker or a online public ip
# local: ip addr show docker0 | grep -Po 'inet \K[\d.]+' or use keycloak in url but required add to hosts `172.17.0.1 keycloak`, 172.17.0.1 works without need to add to hosts
GOTRUE_EXTERNAL_KEYCLOAK_URL: "http://172.17.0.1:8080/auth/realms/SupaBase"
please let me know if it works for you buddie |
I already found a quite simlar solution based on what you had and added it to my comment above. Thanks anyways and also for all the investigation you did above which helped me find the solution 👍 |
Bug report
Describe the bug
After setting up an instance of Keycloak and using current examples with their latest versions, logging in via. the thrid-party-service Keycloak on a local instance results in an error, which message is "Unable to exchange external code", found in the redirect URL.
To Reproduce
npx create-next-app --example "https://github.com/valerius21/auth-helpers/tree/main/examples/nextjs"
.env.local
file (NEXT_PUBLIC_SUPABASE_URL
andNEXT_PUBLIC_SUPABASE_ANON_KEY
)pages/index.tsx:38
fromproviders={["google", "github"]}
toproviders={["google", "github", "keycloak"]}
yarn dev
http://localhost:3000
should have three buttons, one of which has no icon inside.Expected behavior
A user session, similar to other OAuth2 providers, like Google, GitHub, etc., where querying DB content and one's user profile is possible.
Screenshots
supabase_issue.mp4
System information
Previous attempts to resolve the issue
302
requests.The text was updated successfully, but these errors were encountered: