Fix for Discord authentication invalid code in request error #5907
wildstoats
started this conversation in
Guides
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello friends. Pouring through the open discussions it looks like many people are encountering
Invalid "code" in request
when attempting to use Discord authentication. I also encountered this and believe I have a solution.TL;DR Version
Conditions
System -> SSL
,Redirect HTTP requests to HTTPS
is set totrue
Site -> General
, theSite URL
starts withhttp://
Steps to Resolve
Site URL
fromhttp://butt.holdings
to thehttps://butt.holdings
docker restart wiki
.Invalid OAuth2 redirect_uri
. YMMV.Modules -> Authentication -> Active Strategies -> Discord
and confirm that theConfiguration Reference
section shows URLs that begin withhttps://
Caveats
http://
and then disableRedirect HTTP requests to HTTPS
but I HIGHLY recommend NOT doing this as it is insecure.Longer Version
So why is this happening?
This screenshot shows a subset of the requests made during the Discord authentication process.
You'll notice that there's an
HTTP 302 Found
and then anHTTP 500 Internal Server Error
. This is important. Let's dig into the redirect a bit.What we see here is that Discord is making a login request on our behalf. It makes this request to
http://butt.holdings/login/0037038c-a5ff-4bfa-9700-bf8efe19b260/callback?code=ZM62DtZwfY7yU1YaUBN9picTnWj2ID
. Note that it includes the OAuth2 auth code.However, we are configured to redirect HTTP requests to HTTPS since we want to be safe on the internet. So the server returns an
HTTP 302
response telling our browser to make the same requests against thehttps://
URI scheme. This is where the problem arises.For some reason the
Location
header in theHTTP 302
response is appendingcode
again. So you'll see something like:https://butt.holdings/login/0037038c-a5ff-4bfa-9700-bf8efe19b260/callback?code=ZM62DtZwfY7yU1YaUBN9picTnWj2ID?code=ZM62DtZwfY7yU1YaUBN9picTnWj2ID
. The callback endpoint can't handle this hence theinvalid "code" in request
error and subsequent inability to auth via Discord.This is also why changing the site scheme to
https://
allows Discord authentication to work properly. We aren't redirecting and munging up the request URL for the auth login callback endpoint.What's to be done?
A more permanent upstream fix would be to change whatever is generating the
HTTP 302
redirect to generate more well-formed URI query params. In this case ensuring thatcode
isn't added twice and if there are multiple params to use&
instead of two?
s.Beta Was this translation helpful? Give feedback.
All reactions