diff --git a/lib/ueberauth/strategy/ok.ex b/lib/ueberauth/strategy/ok.ex index 108566b..a0d252b 100644 --- a/lib/ueberauth/strategy/ok.ex +++ b/lib/ueberauth/strategy/ok.ex @@ -11,12 +11,14 @@ defmodule Ueberauth.Strategy.Ok do def handle_request!(conn) do scopes = conn.params["scope"] || option(conn, :default_scope) + opts = [scope: scopes] |> with_optional(:layout, conn) |> with_optional(:state, conn) |> Keyword.put(:response_type, "code") |> Keyword.put(:redirect_uri, callback_url(conn)) + redirect!(conn, Ueberauth.Strategy.Ok.OAuth.authorize_url!(opts)) end @@ -25,7 +27,9 @@ defmodule Ueberauth.Strategy.Ok do token = Ueberauth.Strategy.Ok.OAuth.get_token!([code: code], opts) if token.access_token == nil do - set_errors!(conn, [error(token.other_params["error"], token.other_params["error_description"])]) + set_errors!(conn, [ + error(token.other_params["error"], token.other_params["error_description"]) + ]) else fetch_user(conn, token) end @@ -35,7 +39,6 @@ defmodule Ueberauth.Strategy.Ok do Handles the callback from app with access_token. """ def handle_callback!(%Plug.Conn{params: %{"access_token" => access_token}} = conn) do - client = Ueberauth.Strategy.Ok.OAuth.client token = OAuth2.AccessToken.new(access_token) fetch_user(conn, token) end @@ -47,6 +50,7 @@ defmodule Ueberauth.Strategy.Ok do def info(conn) do user = conn.private.ok_user city = if user["location"]["city"], do: user["location"]["city"] <> ", " + %Info{ email: user["email"], first_name: user["first_name"], @@ -65,6 +69,7 @@ defmodule Ueberauth.Strategy.Ok do conn |> option(:uid_field) |> to_string + conn.private.ok_user[uid_field] end @@ -79,6 +84,7 @@ defmodule Ueberauth.Strategy.Ok do def credentials(conn) do token = conn.private.ok_token + %Credentials{ expires: !!token.expires_at, expires_at: token.expires_at, @@ -96,11 +102,15 @@ defmodule Ueberauth.Strategy.Ok do defp fetch_user(conn, token) do conn = put_private(conn, :ok_token, token) + case Ueberauth.Strategy.Ok.OAuth.get(conn, token) do {:ok, %OAuth2.Response{status_code: 401, body: _body}} -> set_errors!(conn, [error("token", "unauthorized")]) - {:ok, %OAuth2.Response{status_code: status_code, body: user}} when status_code in 200..399 -> + + {:ok, %OAuth2.Response{status_code: status_code, body: user}} + when status_code in 200..399 -> put_private(conn, :ok_user, user) + {:error, %OAuth2.Error{reason: reason}} -> set_errors!(conn, [error("OAuth2", reason)]) end diff --git a/lib/ueberauth/strategy/ok/oauth.ex b/lib/ueberauth/strategy/ok/oauth.ex index fb7ab94..c294d81 100644 --- a/lib/ueberauth/strategy/ok/oauth.ex +++ b/lib/ueberauth/strategy/ok/oauth.ex @@ -10,10 +10,12 @@ defmodule Ueberauth.Strategy.Ok.OAuth do def client(opts \\ []) do config = Application.get_env(:ueberauth, Ueberauth.Strategy.Ok.OAuth) + opts = @defaults |> Keyword.merge(config) |> Keyword.merge(opts) + OAuth2.Client.new(opts) end @@ -22,6 +24,7 @@ defmodule Ueberauth.Strategy.Ok.OAuth do |> client |> OAuth2.Client.authorize_url!(params) end + def authorize_url(client, params) do OAuth2.Strategy.AuthCode.authorize_url(client, params) end @@ -31,8 +34,10 @@ defmodule Ueberauth.Strategy.Ok.OAuth do opts |> client |> OAuth2.Client.get_token!(params) + client.token end + def get_token(client, params, headers) do client |> put_param("client_secret", client.client_secret) @@ -41,14 +46,15 @@ defmodule Ueberauth.Strategy.Ok.OAuth do end def get(conn, token) do - OAuth2.Client.get(client, "https://api.ok.ru/fb.do?#{user_query(conn, token)}") + OAuth2.Client.get(client(), "https://api.ok.ru/fb.do?#{user_query(conn, token)}") end - defp user_query(conn, token) do + defp user_query(_conn, token) do access_token = Map.fetch!(token, :access_token) config = Application.get_env(:ueberauth, Ueberauth.Strategy.Ok.OAuth) client_public = Keyword.get(config, :client_public) client_secret = Keyword.get(config, :client_secret) + URI.encode_query(%{ application_key: client_public, format: "json", diff --git a/mix.lock b/mix.lock index 0c75b57..f46e7b4 100644 --- a/mix.lock +++ b/mix.lock @@ -1,12 +1,14 @@ -%{"certifi": {:hex, :certifi, "1.0.0", "1c787a85b1855ba354f0b8920392c19aa1d06b0ee1362f9141279620a5be2039", [:rebar3], []}, - "earmark": {:hex, :earmark, "1.1.1", "433136b7f2e99cde88b745b3a0cfc3fbc81fe58b918a09b40fce7f00db4d8187", [:mix], []}, - "ex_doc": {:hex, :ex_doc, "0.15.0", "e73333785eef3488cf9144a6e847d3d647e67d02bd6fdac500687854dd5c599f", [:mix], [{:earmark, "~> 1.1", [hex: :earmark, optional: false]}]}, - "hackney": {:hex, :hackney, "1.7.1", "e238c52c5df3c3b16ce613d3a51c7220a784d734879b1e231c9babd433ac1cb4", [:rebar3], [{:certifi, "1.0.0", [hex: :certifi, optional: false]}, {:idna, "4.0.0", [hex: :idna, optional: false]}, {:metrics, "1.0.1", [hex: :metrics, optional: false]}, {:mimerl, "1.0.2", [hex: :mimerl, optional: false]}, {:ssl_verify_fun, "1.1.1", [hex: :ssl_verify_fun, optional: false]}]}, - "idna": {:hex, :idna, "4.0.0", "10aaa9f79d0b12cf0def53038547855b91144f1bfcc0ec73494f38bb7b9c4961", [:rebar3], []}, - "metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], []}, - "mime": {:hex, :mime, "1.1.0", "01c1d6f4083d8aa5c7b8c246ade95139620ef8effb009edde934e0ec3b28090a", [:mix], []}, - "mimerl": {:hex, :mimerl, "1.0.2", "993f9b0e084083405ed8252b99460c4f0563e41729ab42d9074fd5e52439be88", [:rebar3], []}, - "oauth2": {:hex, :oauth2, "0.8.3", "080ea0bf2f7060ddb66204ed756b8363d8c34f20fc5684183a16f19fc5d3ad97", [:mix], [{:hackney, "~> 1.6", [hex: :hackney, optional: false]}]}, - "plug": {:hex, :plug, "1.3.2", "8391d8ba2e2c187de069211110a882599e851f64550c556163b5130e1e2dbc1b", [:mix], [{:cowboy, "~> 1.0.1 or ~> 1.1", [hex: :cowboy, optional: true]}, {:mime, "~> 1.0", [hex: :mime, optional: false]}]}, - "ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.1", "28a4d65b7f59893bc2c7de786dec1e1555bd742d336043fe644ae956c3497fbe", [:make, :rebar], []}, - "ueberauth": {:hex, :ueberauth, "0.4.0", "bc72d5e5a7bdcbfcf28a756e34630816edabc926303bdce7e171f7ac7ffa4f91", [:mix], [{:plug, "~> 1.2", [hex: :plug, optional: false]}]}} +%{ + "certifi": {:hex, :certifi, "1.0.0", "1c787a85b1855ba354f0b8920392c19aa1d06b0ee1362f9141279620a5be2039", [:rebar3], [], "hexpm", "44a5aa4261490a7d7fa6909ab4bcf14bff928a4fef49e80fc1e7a8fdb7b45f79"}, + "earmark": {:hex, :earmark, "1.1.1", "433136b7f2e99cde88b745b3a0cfc3fbc81fe58b918a09b40fce7f00db4d8187", [:mix], [], "hexpm", "d446a8ffea9968237fcada883303ac6e04a00a1dc41039061645eea9e7af65a0"}, + "ex_doc": {:hex, :ex_doc, "0.15.0", "e73333785eef3488cf9144a6e847d3d647e67d02bd6fdac500687854dd5c599f", [:mix], [{:earmark, "~> 1.1", [hex: :earmark, repo: "hexpm", optional: false]}], "hexpm", "e5ea59f50ecdfe4cc755808450dafe35221d5a0f4a31c42e80a7188eca570e4c"}, + "hackney": {:hex, :hackney, "1.7.1", "e238c52c5df3c3b16ce613d3a51c7220a784d734879b1e231c9babd433ac1cb4", [:rebar3], [{:certifi, "1.0.0", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "4.0.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "1.0.1", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "1.0.2", [hex: :mimerl, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "1.1.1", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}], "hexpm", "ec8309cb6d42251513492ef683d212c614d78b20594e5f4d89a05d8411dd0dea"}, + "idna": {:hex, :idna, "4.0.0", "10aaa9f79d0b12cf0def53038547855b91144f1bfcc0ec73494f38bb7b9c4961", [:rebar3], [], "hexpm", "f1b699f7275728538da7b5e35679f9e0f41ad8e0a49896e6a27b61867ed344eb"}, + "metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm", "69b09adddc4f74a40716ae54d140f93beb0fb8978d8636eaded0c31b6f099f16"}, + "mime": {:hex, :mime, "1.1.0", "01c1d6f4083d8aa5c7b8c246ade95139620ef8effb009edde934e0ec3b28090a", [:mix], [], "hexpm", "33dd09e615daab5668c15cc3a33829892728fdbed910ab0c0a0edb06b45fc54d"}, + "mimerl": {:hex, :mimerl, "1.0.2", "993f9b0e084083405ed8252b99460c4f0563e41729ab42d9074fd5e52439be88", [:rebar3], [], "hexpm", "7a4c8e1115a2732a67d7624e28cf6c9f30c66711a9e92928e745c255887ba465"}, + "oauth2": {:hex, :oauth2, "0.8.3", "080ea0bf2f7060ddb66204ed756b8363d8c34f20fc5684183a16f19fc5d3ad97", [:mix], [{:hackney, "~> 1.6", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm", "a2ae2391d296fb15c0858a0fe6b5c24aec6ae006f4865ea5b3d8838b2de53f9e"}, + "plug": {:hex, :plug, "1.3.2", "8391d8ba2e2c187de069211110a882599e851f64550c556163b5130e1e2dbc1b", [:mix], [{:cowboy, "~> 1.0.1 or ~> 1.1", [hex: :cowboy, repo: "hexpm", optional: true]}, {:mime, "~> 1.0", [hex: :mime, repo: "hexpm", optional: false]}], "hexpm", "f5e96ab5685ae8a238eae2340d1d9763f2d316b0a9cb014d97d4165e8d7ec4e3"}, + "ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.1", "28a4d65b7f59893bc2c7de786dec1e1555bd742d336043fe644ae956c3497fbe", [:make, :rebar], [], "hexpm", "4f8805eb5c8a939cf2359367cb651a3180b27dfb48444846be2613d79355d65e"}, + "ueberauth": {:hex, :ueberauth, "0.4.0", "bc72d5e5a7bdcbfcf28a756e34630816edabc926303bdce7e171f7ac7ffa4f91", [:mix], [{:plug, "~> 1.2", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "d3bcb678a8fdcd0add619eacb3e45e51003f50aa434ea732746ea25c37f6c92b"}, +}