Skip to content

Instantly share code, notes, and snippets.

@almays
Created January 13, 2018 01:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save almays/b7d2031201e1a80c1e7ec780b8721c68 to your computer and use it in GitHub Desktop.
Save almays/b7d2031201e1a80c1e7ec780b8721c68 to your computer and use it in GitHub Desktop.
defmodule CoherenceAssent.Strategy.VK do
alias CoherenceAssent.Strategy.Helpers
alias CoherenceAssent.Strategies.OAuth2, as: OAuth2Helper
def authorize_url(conn, config) do
OAuth2Helper.authorize_url(conn, set_config(config))
end
def callback(conn, config, params) do
config = set_config(config)
%Plug.Conn{params: %{"code" => code, "state" => state}} = conn
client = OAuth2.Client.new(config)
params = params
|> Keyword.put(:code, code)
conn
|> OAuth2Helper.check_conn(client, params)
|> OAuth2Helper.get_access_token(config, params)
|> OAuth2Helper.get_user(config[:user_url])
|> normalize
end
defp set_config(config) do
[
site: "https://api.vk.com/method",
authorize_url: "https://oauth.vk.com/authorize",
token_url: "https://oauth.vk.com/access_token",
user_url: "/users.get",
authorization_params: [scope: "email"],
user_url_request_fields: "screen_name,about,verified"
]
|> Keyword.merge(config)
|> Keyword.put(:strategy, OAuth2.Strategy.AuthCode)
end
defp normalize({:ok, %{conn: conn, client: client, user: user}}) do
user = %{"uid" => user["id"],
"nickname" => user["screen_name"],
"email" => user["email"],
"first_name" => user["first_name"],
"last_name" => user["last_name"],
"description" => user["about"],
"verified" => user["verified"]}
|> Helpers.prune
{:ok, %{conn: conn, client: client, user: user}}
end
defp normalize({:error, _} = error), do: error
end
@danschultzer
Copy link

danschultzer commented Jan 13, 2018

This is how I would set it up:

defmodule CoherenceAssent.Strategy.VK do
  @moduledoc """
  VK.com OAuth 2.0 strategy.
  """

  alias CoherenceAssent.Strategy.Helpers
  alias CoherenceAssent.Strategies.OAuth2, as: OAuth2Helper

  def authorize_url(conn, config) do
    OAuth2Helper.authorize_url(conn, set_config(config))
  end

  def callback(conn, config, params) do
    config = set_config(config)

    conn
    |> OAuth2Helper.callback(config, params)
    |> normalize
  end

  defp set_config(config) do
    [
      site: "https://api.vk.com/method",
      authorize_url: "https://oauth.vk.com/authorize",
      token_url: "https://oauth.vk.com/access_token",
      user_url: "/account.getProfileInfo",
      authorization_params: [scope: "email"]
    ]
    |> Keyword.merge(config)
    |> Keyword.put(:strategy, OAuth2.Strategy.AuthCode)
  end

  defp normalize({:ok, %{conn: conn, client: client, user: user}}) do
    user = %{"uid"         => user["id"],
             "nickname"    => user["screen_name"],
             "email"       => user["email"],
             "first_name"  => user["first_name"],
             "last_name"   => user["last_name"],
             "description" => user["about"],
             "verified"    => user["verified"]}
           |> Helpers.prune

    {:ok, %{conn: conn, client: client, user: user}}
  end
  defp normalize({:error, _} = error), do: error
end

@danschultzer
Copy link

In short, callback is using the default OAuth2 handler, and user_url is using the proper account method: https://vk.com/dev/account.getProfileInfo

Not sure what user_url_request_fields but it shouldn't be necessary.

@almays
Copy link
Author

almays commented Jan 13, 2018

I made some logging:

  defp normalize({:ok, %{conn: conn, client: client, user: user}}) do
    Logger.debug inspect(user)
    user = %{"uid"         => user["id"],

Here is the result.

%{"error" => %{"error_code" => 5, "error_msg" => "User authorization failed: no access_token passed.", "request_params" => [%{"key" => "oauth", "value" => "1"}, %{"key" => "method", "value" => "account.getProfileInfo"}]}}

@almays
Copy link
Author

almays commented Jan 13, 2018

users.get like in my attempt returns

%{"response" => []}

@danschultzer
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment