Created
January 13, 2018 01:17
-
-
Save almays/b7d2031201e1a80c1e7ec780b8721c68 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This should work: danschultzer/coherence_assent#2