2015-11-18 04:34:55 +01:00
|
|
|
defmodule Ueberauth.Strategy.Twitter.OAuth do
|
|
|
|
@moduledoc """
|
|
|
|
OAuth1 for Twitter.
|
|
|
|
|
|
|
|
Add `consumer_key` and `consumer_secret` to your configuration:
|
|
|
|
|
|
|
|
config :ueberauth, Ueberauth.Strategy.Twitter.OAuth,
|
|
|
|
consumer_key: System.get_env("TWITTER_CONSUMER_KEY"),
|
|
|
|
consumer_secret: System.get_env("TWITTER_CONSUMER_SECRET"),
|
|
|
|
redirect_uri: System.get_env("TWITTER_REDIRECT_URI")
|
|
|
|
"""
|
|
|
|
|
2017-03-15 20:15:49 +01:00
|
|
|
alias Ueberauth.Strategy.Twitter.OAuth.Internal
|
|
|
|
|
2015-11-18 04:34:55 +01:00
|
|
|
@defaults [access_token: "/oauth/access_token",
|
|
|
|
authorize_url: "/oauth/authorize",
|
|
|
|
request_token: "/oauth/request_token",
|
|
|
|
site: "https://api.twitter.com"]
|
|
|
|
|
|
|
|
def access_token({token, token_secret}, verifier, opts \\ []) do
|
|
|
|
opts
|
2018-12-23 07:45:39 +01:00
|
|
|
|> client()
|
2015-11-18 04:34:55 +01:00
|
|
|
|> to_url(:access_token)
|
2017-03-15 20:15:49 +01:00
|
|
|
|> Internal.get([{"oauth_verifier", verifier}], consumer(client()), token, token_secret)
|
|
|
|
|> decode_response
|
2015-11-18 04:34:55 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def access_token!(access_token, verifier, opts \\ []) do
|
|
|
|
case access_token(access_token, verifier, opts) do
|
|
|
|
{:ok, token} -> token
|
|
|
|
{:error, error} -> raise error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def authorize_url!({token, _token_secret}, opts \\ []) do
|
|
|
|
opts
|
2018-12-23 07:45:39 +01:00
|
|
|
|> client()
|
2017-03-15 20:15:49 +01:00
|
|
|
|> to_url(:authorize_url, %{"oauth_token" => token})
|
2015-11-18 04:34:55 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def client(opts \\ []) do
|
|
|
|
config = Application.get_env(:ueberauth, __MODULE__)
|
|
|
|
|
|
|
|
@defaults
|
|
|
|
|> Keyword.merge(config)
|
|
|
|
|> Keyword.merge(opts)
|
|
|
|
|> Enum.into(%{})
|
|
|
|
end
|
|
|
|
|
|
|
|
def get(url, access_token), do: get(url, [], access_token)
|
2015-12-12 23:16:40 +01:00
|
|
|
def get(url, params, {token, token_secret}) do
|
2017-03-15 20:15:49 +01:00
|
|
|
client()
|
2015-11-18 04:34:55 +01:00
|
|
|
|> to_url(url)
|
2017-03-15 20:15:49 +01:00
|
|
|
|> Internal.get(params, consumer(client()), token, token_secret)
|
2015-11-18 04:34:55 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def request_token(params \\ [], opts \\ []) do
|
|
|
|
client = client(opts)
|
2017-03-15 20:15:49 +01:00
|
|
|
params = [{"oauth_callback", client.redirect_uri} | params]
|
2015-11-18 04:34:55 +01:00
|
|
|
|
|
|
|
client
|
|
|
|
|> to_url(:request_token)
|
2017-03-15 20:15:49 +01:00
|
|
|
|> Internal.get(params, consumer(client))
|
|
|
|
|> decode_response
|
2015-11-18 04:34:55 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def request_token!(params \\ [], opts \\ []) do
|
|
|
|
case request_token(params, opts) do
|
|
|
|
{:ok, token} -> token
|
|
|
|
{:error, error} -> raise error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp consumer(client), do: {client.consumer_key, client.consumer_secret, :hmac_sha1}
|
|
|
|
|
2017-03-15 20:15:49 +01:00
|
|
|
defp decode_response({:ok, %{status_code: 200, body: body, headers: _}}) do
|
|
|
|
params = Internal.params_decode(body)
|
|
|
|
token = Internal.token(params)
|
|
|
|
token_secret = Internal.token_secret(params)
|
2015-11-18 04:34:55 +01:00
|
|
|
|
|
|
|
{:ok, {token, token_secret}}
|
|
|
|
end
|
2017-03-15 20:15:49 +01:00
|
|
|
defp decode_response({:ok, %{status_code: status_code, body: _, headers: _}}) do
|
|
|
|
{:error, "#{status_code}"}
|
|
|
|
end
|
|
|
|
defp decode_response({:error, %{reason: reason}}) do
|
|
|
|
{:error, "#{reason}"}
|
|
|
|
end
|
|
|
|
defp decode_response(error) do
|
|
|
|
{:error, error}
|
2015-11-18 04:34:55 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
defp endpoint("/" <> _path = endpoint, client), do: client.site <> endpoint
|
|
|
|
defp endpoint(endpoint, _client), do: endpoint
|
|
|
|
|
|
|
|
defp to_url(client, endpoint, params \\ nil) do
|
|
|
|
endpoint =
|
|
|
|
client
|
|
|
|
|> Map.get(endpoint, endpoint)
|
|
|
|
|> endpoint(client)
|
2016-10-27 17:35:12 +02:00
|
|
|
|
2016-07-27 19:05:54 +02:00
|
|
|
endpoint =
|
|
|
|
if params do
|
|
|
|
endpoint <> "?" <> URI.encode_query(params)
|
|
|
|
else
|
|
|
|
endpoint
|
|
|
|
end
|
2015-11-18 04:34:55 +01:00
|
|
|
|
|
|
|
endpoint
|
|
|
|
end
|
|
|
|
end
|