-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from mmrobins/auth
Add authorization via Session ID
- Loading branch information
Showing
17 changed files
with
417 additions
and
77 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,9 +4,10 @@ elixir: | |
notifications: | ||
recipients: | ||
- [email protected] | ||
- [email protected] | ||
otp_release: | ||
- 20.0 | ||
env: | ||
- MIX_ENV=test | ||
script: | ||
- "mix do deps.get, compile" | ||
- "mix do deps.get, compile, test" |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
use Mix.Config | ||
|
||
config :forcex, :api, Forcex.Api.MockHttp |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
defmodule Forcex.Api do | ||
@moduledoc """ | ||
Behavior for requests to Salesforce API | ||
""" | ||
|
||
@type method :: :get | :put | :post | :patch | :delete | ||
@type response :: map | {number, any} | ||
|
||
@callback raw_request(method, String.t, map | String.t, list, list) :: response | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
defmodule Forcex.Api.Http do | ||
@moduledoc """ | ||
HTTP communication with Salesforce API | ||
""" | ||
|
||
@behaviour Forcex.Api | ||
require Logger | ||
use HTTPoison.Base | ||
|
||
@user_agent [{"User-agent", "forcex"}] | ||
@accept [{"Accept", "application/json"}] | ||
@accept_encoding [{"Accept-Encoding", "gzip,deflate"}] | ||
|
||
@type method :: :get | :put | :post | :patch | :delete | ||
@type response :: map | {number, any} | ||
|
||
def raw_request(method, url, body, headers, options) do | ||
response = method |> request!(url, body, headers, extra_options() ++ options) |> process_response | ||
Logger.debug("#{__ENV__.module}.#{elem(__ENV__.function, 0)} response=" <> inspect(response)) | ||
response | ||
end | ||
|
||
@spec extra_options :: list | ||
defp extra_options() do | ||
Application.get_env(:forcex, :request_options, []) | ||
end | ||
|
||
@spec process_response(HTTPoison.Response.t) :: response | ||
defp process_response(%HTTPoison.Response{body: body, headers: %{"Content-Encoding" => "gzip"} = headers} = resp) do | ||
%{resp | body: :zlib.gunzip(body), headers: Map.drop(headers, ["Content-Encoding"])} | ||
|> process_response | ||
end | ||
defp process_response(%HTTPoison.Response{body: body, headers: %{"Content-Encoding" => "deflate"} = headers} = resp) do | ||
zstream = :zlib.open | ||
:ok = :zlib.inflateInit(zstream, -15) | ||
uncompressed_data = zstream |> :zlib.inflate(body) |> Enum.join | ||
:zlib.inflateEnd(zstream) | ||
:zlib.close(zstream) | ||
%{resp | body: uncompressed_data, headers: Map.drop(headers, ["Content-Encoding"])} | ||
|> process_response | ||
end | ||
defp process_response(%HTTPoison.Response{body: body, headers: %{"Content-Type" => "application/json" <> _} = headers} = resp) do | ||
%{resp | body: Poison.decode!(body, keys: :atoms), headers: Map.drop(headers, ["Content-Type"])} | ||
|> process_response | ||
end | ||
defp process_response(%HTTPoison.Response{body: body, status_code: 200}), do: body | ||
defp process_response(%HTTPoison.Response{body: body, status_code: status}), do: {status, body} | ||
|
||
@spec process_request_headers(list({String.t, String.t})) :: list({String.t, String.t}) | ||
defp process_request_headers(headers), do: headers ++ @user_agent ++ @accept ++ @accept_encoding | ||
|
||
@spec process_headers(list({String.t, String.t})) :: map | ||
defp process_headers(headers), do: Map.new(headers) | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
defmodule Forcex.Auth do | ||
@moduledoc """ | ||
Auth behavior | ||
""" | ||
|
||
@callback login(config :: Map.t(), struct) :: Map.t() | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
defmodule Forcex.Auth.OAuth do | ||
@moduledoc """ | ||
Auth via OAuth | ||
""" | ||
require Logger | ||
@behaviour Forcex.Auth | ||
|
||
def login(conf, starting_struct) do | ||
login_payload = | ||
conf | ||
|> Map.put(:password, "#{conf.password}#{conf.security_token}") | ||
|> Map.put(:grant_type, "password") | ||
|
||
"/services/oauth2/token?#{URI.encode_query(login_payload)}" | ||
|> Forcex.post(starting_struct) | ||
|> handle_login_response | ||
end | ||
|
||
defp handle_login_response(%{ | ||
access_token: token, | ||
token_type: token_type, | ||
instance_url: endpoint | ||
}) do | ||
%{ | ||
authorization_header: authorization_header(token, token_type), | ||
endpoint: endpoint | ||
} | ||
end | ||
|
||
defp handle_login_response({status_code, error_message}) do | ||
Logger.warn( | ||
"Cannot log into SFDC API. Please ensure you have Forcex properly configured. Got error code #{ | ||
status_code | ||
} and message #{inspect(error_message)}" | ||
) | ||
|
||
%{} | ||
end | ||
|
||
@spec authorization_header(token :: String.t(), type :: String.t()) :: list | ||
defp authorization_header(nil, _), do: [] | ||
|
||
defp authorization_header(token, type) do | ||
[{"Authorization", type <> " " <> token}] | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
defmodule Forcex.Auth.SessionId do | ||
@moduledoc """ | ||
Auth via a session id | ||
""" | ||
|
||
require Logger | ||
@behaviour Forcex.Auth | ||
@api Application.get_env(:forcex, :api) || Forcex.Api.Http | ||
|
||
def login(conf, starting_struct) do | ||
schema = "http://www.w3.org/2001/XMLSchema" | ||
schema_instance = "http://www.w3.org/2001/XMLSchema-instance" | ||
env = "http://schemas.xmlsoap.org/soap/envelope/" | ||
|
||
body = """ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<env:Envelope xmlns:xsd="#{schema}" xmlns:xsi="#{schema_instance}" xmlns:env="#{env}"> | ||
<env:Body> | ||
<n1:login xmlns:n1="urn:partner.soap.sforce.com"> | ||
<n1:username>#{conf.username}</n1:username> | ||
<n1:password>#{conf.password}#{conf.security_token}</n1:password> | ||
</n1:login> | ||
</env:Body> | ||
</env:Envelope> | ||
""" | ||
|
||
headers = [ | ||
{"Content-Type", "text/xml; charset=UTF-8"}, | ||
{"SOAPAction", "login"} | ||
] | ||
|
||
url = "https://login.salesforce.com/services/Soap/u/#{starting_struct.api_version}" | ||
|
||
Logger.debug("api=#{@api}") | ||
@api.raw_request(:post, url, body, headers, []) | ||
|> handle_login_response | ||
end | ||
|
||
defp handle_login_response(body) do | ||
{:ok, | ||
{'{http://schemas.xmlsoap.org/soap/envelope/}Envelope', _, | ||
[ | ||
{'{http://schemas.xmlsoap.org/soap/envelope/}Body', _, | ||
[ | ||
{'{urn:partner.soap.sforce.com}loginResponse', _, | ||
[ | ||
{'{urn:partner.soap.sforce.com}result', _, login_parameters} | ||
]} | ||
]} | ||
]}, _} = :erlsom.simple_form(body) | ||
|
||
server_url = extract_from_parameters(login_parameters, :serverUrl) | ||
session_id = extract_from_parameters(login_parameters, :sessionId) | ||
host = server_url |> URI.parse() |> Map.get(:host) | ||
endpoint = "https://#{host}/" | ||
|
||
%{authorization_header: authorization_header(session_id), endpoint: endpoint} | ||
end | ||
|
||
defp extract_from_parameters(params, key) do | ||
compound_key = "{urn:partner.soap.sforce.com}#{key}" |> to_charlist | ||
{^compound_key, _, [value]} = :lists.keyfind(compound_key, 1, params) | ||
value |> to_string | ||
end | ||
|
||
@spec authorization_header(session_id :: String.t()) :: list | ||
def authorization_header(nil), do: [] | ||
|
||
def authorization_header(session_id) do | ||
[{"Authorization", "Bearer #{session_id}"}] | ||
end | ||
end |
Oops, something went wrong.