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

Ability to set state #20

Merged
merged 4 commits into from
Nov 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions lib/ueberauth/strategy/vk.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ defmodule Ueberauth.Strategy.VK do

use Ueberauth.Strategy, default_scope: "",
default_display: "page",
default_state: "",
profile_fields: "",
uid_field: :uid,
allowed_request_params: [
:display,
:scope
:scope,
:state
]

alias OAuth2.{Response, Error, Client}
Expand All @@ -30,6 +32,7 @@ defmodule Ueberauth.Strategy.VK do
|> maybe_replace_param(conn, "auth_type", :auth_type)
|> maybe_replace_param(conn, "scope", :default_scope)
|> maybe_replace_param(conn, "display", :default_display)
|> maybe_replace_param(conn, "state", :default_state)
|> Enum.filter(fn {k, _} -> Enum.member?(allowed_params, k) end)
|> Enum.map(fn {k, v} -> {String.to_existing_atom(k), v} end)
|> Keyword.put(:redirect_uri, callback_url(conn))
Expand All @@ -41,7 +44,7 @@ defmodule Ueberauth.Strategy.VK do
@doc """
Handles the callback from VK.
"""
def handle_callback!(%Plug.Conn{params: %{"code" => code}} = conn) do
def handle_callback!(%Plug.Conn{params: %{"code" => code, "state" => state}} = conn) do
opts = [redirect_uri: callback_url(conn)]
client = OAuth.get_token!([code: code], opts)
token = client.token
Expand All @@ -51,7 +54,7 @@ defmodule Ueberauth.Strategy.VK do
desc = token.other_params["error_description"]
set_errors!(conn, [error(err, desc)])
else
fetch_user(conn, client)
fetch_user(conn, client, state)
end
end

Expand Down Expand Up @@ -146,8 +149,10 @@ defmodule Ueberauth.Strategy.VK do
end
end

defp fetch_user(conn, client) do
conn = put_private(conn, :vk_token, client.token)
defp fetch_user(conn, client, state) do
conn = conn
|> put_private(:vk_token, client.token)
|> put_private(:vk_state, state)
path = user_query(conn)

case Client.get(client, path) do
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/cassettes/httpoison_get.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[
{
"request": {
"body": "client_id=appid&client_secret=secret&code=code_abc&grant_type=authorization_code&redirect_uri=http%3A%2F%2Fwww.example.com%2Fauth%2Fvk%2Fcallback",
"body": "client_id=appid&client_secret=secret&code=code_abc&grant_type=authorization_code&redirect_uri=http%3A%2F%2Fwww.example.com%2Fauth%2Fvk%2Fcallback&state=abc",
"headers": {
"Accept": "application/x-www-form-urlencoded",
"Content-Type": "application/x-www-form-urlencoded"
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/vk_response.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<html><body>You are being <a href="https://oauth.vk.com/authorize?client_id=appid&amp;display=page&amp;redirect_uri=http%3A%2F%2Fwww.example.com%2Fauth%2Fvk%2Fcallback&amp;response_type=code&amp;scope=">redirected</a>.</body></html>
<html><body>You are being <a href="https://oauth.vk.com/authorize?client_id=appid&amp;display=page&amp;redirect_uri=http%3A%2F%2Fwww.example.com%2Fauth%2Fvk%2Fcallback&amp;response_type=code&amp;scope=&amp;state=">redirected</a>.</body></html>
2 changes: 1 addition & 1 deletion test/ueberauth/strategy/vk_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ defmodule Ueberauth.Strategy.VKTest do
end

test "default callback phase" do
query = %{code: "code_abc"} |> URI.encode_query
query = %{code: "code_abc", state: "abc"} |> URI.encode_query

use_cassette "httpoison_get" do
conn =
Expand Down