Created
March 1, 2017 22:38
-
-
Save masnun/be5f58f734f4fd4b9076adec9e889ca1 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 Extorrent.Middleware.HTTPBasicAuth do | |
def call(env, next, %{username: username, password: password} = opts) do | |
env | |
|> add_authorization_header(username, password) | |
|> Tesla.run(next) | |
end | |
defp add_authorization_header(env, username, password) do | |
auth_hash = Base.encode64(username <> ":" <> password) | |
auth_string = "Basic #{auth_hash}" | |
headers = | |
Map.get(env, :headers) | |
|> Map.merge(%{"Authorization" => auth_string}) | |
Map.put(env, :headers, headers) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment