-
Notifications
You must be signed in to change notification settings - Fork 41
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
"get_token" code blows on bad "code" #71
Comments
Related to: #16 🔗 |
It's even written on the code!. Sorry about that, I delete my remark. |
So maybe the full correction since there are only limited possibilities of failure with 4 inputs that boil down to 400,401,404 def parse_status(request) do
case request do
{:ok, %{status_code: 200} = response} ->
parse_body_response({:ok, response})
{:ok, %{status_code: 404}} ->
{:error, :wrong_url}
{:ok, %{status_code: 401}} ->
{:error, :unauthorized_with_bad_secret}
{:ok, %{status_code: 400}} ->
{:error, :bad_code}
end
end def get_profile(code, conn) do
Jason.encode!(%{
client_id: google_client_id(),
client_secret: google_client_secret(),
redirect_uri: generate_redirect_uri(conn),
grant_type: "authorization_code",
code: code
})
|> then(fn body ->
inject_poison().post(@google_token_url, body)
|> parse_status()
end)
|> then(fn {status, response} ->
case {status, response} do
{:error, response} ->
{:error, response}
{:ok, response} ->
get_user_profile(response.access_token)
end
end)
end
def get_user_profile(access_token) do
access_token
|> encode()
|>then(fn params ->
(@google_user_profile <> "?" <> params)
|> inject_poison().get()
|> parse_status()
end)
end
defp encode(token), do: URI.encode_query(%{access_token: token}, :rfc3986) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Test:
The controller that receives Google's code goes like this:
When I run a test with a "bad" code:
I receive:
The fix in the package ElxirAuthGoogle is:
and it is sufficient to pass directly
access_token
to the next functionElixirAuthGoogle.get_user_profile(access_token)
.If you keep your controller, since you have a "two staged" function, you now have a (MatchError) in case of error but you can capture it now.
The controller should be:
The error
{:error, :invalid_code_value}
is now captured, ready for theaction_callback
.I would even refactor to expose only one function, say
get_profile
:so that in the controller, you do:
The text was updated successfully, but these errors were encountered: